From 839d01aca579ed7d3774d4e35db8b2e100e55404 Mon Sep 17 00:00:00 2001 From: Dominic Go <18517029+dominicstop@users.noreply.github.com> Date: Fri, 7 Apr 2023 02:02:56 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A0=20Refactor:=20`RNIModalManager.cur?= =?UTF-8?q?rentModalIndex`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related: * `Note:2023-03-30-19-36-33` * `TODO:2023-04-07-01-44-05` - Refactor: Replace `RNIModalManager.currentModalIndex` with per window instance "modal index". --- .../RNIModal/RNIModal+Helpers.swift | 7 +- .../RNIModal/RNIModalManager.swift | 129 ++++++++++++++++-- 2 files changed, 122 insertions(+), 14 deletions(-) diff --git a/ios/src_library/React Native/RNIModal/RNIModal+Helpers.swift b/ios/src_library/React Native/RNIModal/RNIModal+Helpers.swift index d623adda..61c59afa 100644 --- a/ios/src_library/React Native/RNIModal/RNIModal+Helpers.swift +++ b/ios/src_library/React Native/RNIModal/RNIModal+Helpers.swift @@ -54,6 +54,11 @@ extension RNIModalState where Self: RNIModalPresentation { return -1; }; + + var synthesizedCurrentModalIndex: Int { + guard let window = self.window else { return -1 }; + return RNIModalManagerShared.getCurrentModalIndex(for: window); + }; }; extension RNIModalState where Self: RNIModal { @@ -62,7 +67,7 @@ extension RNIModalState where Self: RNIModal { return RNIModalData( modalNativeID: self.modalNativeID, modalIndex: self.modalIndex, - currentModalIndex: RNIModalManagerShared.currentModalIndex, + currentModalIndex: self.synthesizedCurrentModalIndex, isModalPresented: self.isModalPresented, isModalInFocus: self.isModalInFocus, synthesizedIsModalInFocus: self.synthesizedIsModalInFocus, diff --git a/ios/src_library/React Native/RNIModal/RNIModalManager.swift b/ios/src_library/React Native/RNIModal/RNIModalManager.swift index 614cf6ad..26d56323 100644 --- a/ios/src_library/React Native/RNIModal/RNIModalManager.swift +++ b/ios/src_library/React Native/RNIModal/RNIModalManager.swift @@ -155,18 +155,15 @@ public class RNIModalManager { // MARK: - Properties // ------------------ - private(set) public var currentModalIndex = -1; - private(set) public var modalInstanceDict = RNIWeakDictionary(); + private(set) public var windowToCurrentModalIndexMap: + Dictionary = [:]; + // MARK: - Properties - Computed // ----------------------------- - public var isAnyModalPresented: Bool { - self.currentModalIndex >= 0; - }; - public var modalInstances: [any RNIModal] { self.modalInstanceDict.dict.compactMap { $0.value.synthesizedRef; @@ -192,6 +189,23 @@ public class RNIModalManager { modal.modalFocusDelegate = self; self.modalInstanceDict[modal.synthesizedUUID] = modal; }; + + func setCurrentModalIndex(for window: UIWindow, index: Int){ + self.windowToCurrentModalIndexMap[window.synthesizedStringID] = index; + }; + + func getCurrentModalIndex(for window: UIWindow) -> Int { + guard let currentModalIndex = + self.windowToCurrentModalIndexMap[window.synthesizedStringID] + else { + // No corresponding "modal index" for window yet, so initialize + // with value + self.setCurrentModalIndex(for: window, index: -1); + return -1; + }; + + return currentModalIndex; + }; }; // MARK: RNIModalFocusNotifiable @@ -203,19 +217,46 @@ public class RNIModalManager { extension RNIModalManager: RNIModalFocusNotifiable { public func onModalWillFocusNotification(sender: any RNIModal) { - let nextModalIndex = self.currentModalIndex + 1; - self.currentModalIndex = nextModalIndex; + guard let senderWindow = sender.window else { + #if DEBUG + print( + "Error - RNIModalManager.onModalWillFocusNotification" + + " - arg sender.modalNativeID: \(sender.modalNativeID)" + + " - Unable to send event because sender.window is nil" + ); + #endif + return; + }; + + let currentModalIndex = self.getCurrentModalIndex(for: senderWindow); + + let prevModalIndex = currentModalIndex; + let nextModalIndex = prevModalIndex + 1; + + #if DEBUG + print( + "Test - RNIModalManager.onModalWillFocusNotification" + + " - arg sender.modalNativeID: \(sender.modalNativeID)" + + " - prevModalIndex: \(prevModalIndex)" + + " - nextModalIndex: \(nextModalIndex)" + + " - sender.modalIndexPrev: \(sender.modalIndexPrev!)" + + " - sender.modalIndex: \(sender.modalIndex!)" + + "\n\n" + ); + #endif sender.modalIndexPrev = sender.modalIndex; sender.modalIndex = nextModalIndex; + self.setCurrentModalIndex(for: senderWindow, index: nextModalIndex); + sender.onModalWillFocusNotification(sender: sender); self.modalInstances.forEach { guard $0 !== sender, $0.isModalPresented, $0.isModalInFocus, - $0.modalIndex == self.currentModalIndex - 1 + $0.modalIndex == prevModalIndex else { return }; $0.onModalWillBlurNotification(sender: sender); @@ -223,16 +264,39 @@ extension RNIModalManager: RNIModalFocusNotifiable { }; public func onModalDidFocusNotification(sender: any RNIModal) { + guard let senderWindow = sender.window else { + #if DEBUG + print( + "Error - RNIModalManager.onModalDidFocusNotification" + + " - arg sender.modalNativeID: \(sender.modalNativeID)" + + " - Unable to send event because sender.window is nil" + ); + #endif + return; + }; + + let currentModalIndex = self.getCurrentModalIndex(for: senderWindow); + sender.isModalInFocus = true; sender.isModalPresented = true; + #if DEBUG + print( + "Test - RNIModalManager.onModalDidFocusNotification" + + " - arg sender.modalNativeID: \(sender.modalNativeID)" + + " - sender.modalIndexPrev: \(sender.modalIndexPrev!)" + + " - sender.modalIndex: \(sender.modalIndex!)" + + "\n\n" + ); + #endif + sender.onModalDidFocusNotification(sender: sender); self.modalInstances.forEach { guard $0 !== sender, $0.isModalPresented, $0.isModalInFocus, - $0.modalIndex == self.currentModalIndex - 1 + $0.modalIndex == currentModalIndex - 1 else { return }; $0.isModalInFocus = false; @@ -241,7 +305,21 @@ extension RNIModalManager: RNIModalFocusNotifiable { }; public func onModalWillBlurNotification(sender: any RNIModal) { - self.currentModalIndex -= 1; + guard let senderWindow = sender.window else { + #if DEBUG + print( + "Error - RNIModalManager.onModalWillBlurNotification" + + " - arg sender.modalNativeID: \(sender.modalNativeID)" + + " - Unable to send event because sender.window is nil" + ); + #endif + return; + }; + + let currentModalIndex = self.getCurrentModalIndex(for: senderWindow); + + let nextModalIndex = currentModalIndex - 1; + self.setCurrentModalIndex(for: senderWindow, index: nextModalIndex); sender.modalIndexPrev = sender.modalIndex; sender.modalIndex = -1; @@ -252,7 +330,7 @@ extension RNIModalManager: RNIModalFocusNotifiable { guard $0 !== sender, $0.isModalPresented, !$0.isModalInFocus, - $0.modalIndex >= self.currentModalIndex + $0.modalIndex >= nextModalIndex else { return }; $0.onModalWillFocusNotification(sender: sender); @@ -260,14 +338,39 @@ extension RNIModalManager: RNIModalFocusNotifiable { }; public func onModalDidBlurNotification(sender: any RNIModal) { + guard let senderWindow = sender.window else { + #if DEBUG + print( + "Error - RNIModalManager.onModalDidBlurNotification" + + " - arg sender.modalNativeID: \(sender.modalNativeID)" + + " - Unable to send event because sender.window is nil" + ); + #endif + return; + }; + + let currentModalIndex = self.getCurrentModalIndex(for: senderWindow); + sender.isModalInFocus = false; sender.isModalPresented = false; + #if DEBUG + print( + "Test - RNIModalManager.onModalDidBlurNotification" + + " - arg sender.modalNativeID: \(sender.modalNativeID)" + + " - sender.modalIndexPrev: \(sender.modalIndexPrev!)" + + " - sender.modalIndex: \(sender.modalIndex!)" + + "\n\n" + ); + #endif + + sender.onModalDidBlurNotification(sender: sender); + self.modalInstances.forEach { guard $0 !== sender, $0.isModalPresented, !$0.isModalInFocus, - $0.modalIndex >= self.currentModalIndex + $0.modalIndex >= currentModalIndex else { return }; $0.isModalInFocus = true;