From 5ca15030079d5b77c706890e9d6a5b706372cfe4 Mon Sep 17 00:00:00 2001 From: Dominic Go <18517029+dominicstop@users.noreply.github.com> Date: Mon, 1 May 2023 17:57:20 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A0=20Refactor:=20Use=20`RNIPresentedV?= =?UTF-8?q?iewControllerCache`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related: * `TODO:2023-04-22-00-01-50` - Optimization - Add flag for caching the view-controller tree. --- .../RNIModal/RNIModal+Helpers.swift | 14 +++++-- .../RNIModal/RNIModalManager.swift | 37 ++++++++++++++----- .../RNIModal/RNIModalUtilities.swift | 5 ++- .../RNIModalView/RNIModalView.swift | 4 +- 4 files changed, 42 insertions(+), 18 deletions(-) diff --git a/ios/src_library/React Native/RNIModal/RNIModal+Helpers.swift b/ios/src_library/React Native/RNIModal/RNIModal+Helpers.swift index bf5c591e..f655a108 100644 --- a/ios/src_library/React Native/RNIModal/RNIModal+Helpers.swift +++ b/ios/src_library/React Native/RNIModal/RNIModal+Helpers.swift @@ -23,7 +23,7 @@ extension RNIModalState where Self: RNIModalPresentation { /// Programmatically check if this instance is presented public var computedIsModalPresented: Bool { let listPresentedVC = - RNIUtilities.getPresentedViewControllers(for: self.window); + RNIPresentedVCListCache.getPresentedViewControllers(forWindow: window); return listPresentedVC.contains { $0 === self.modalViewController; @@ -33,7 +33,7 @@ extension RNIModalState where Self: RNIModalPresentation { /// Programmatically check if this instance is in focus public var computedIsModalInFocus: Bool { let listPresentedVC = - RNIUtilities.getPresentedViewControllers(for: self.window); + RNIPresentedVCListCache.getPresentedViewControllers(forWindow: window); guard let topmostVC = listPresentedVC.last else { return self.synthesizedIsModalInFocus }; @@ -48,7 +48,7 @@ extension RNIModalState where Self: RNIModalPresentation { /// public var computedViewControllerIndex: Int { let listPresentedVC = - RNIUtilities.getPresentedViewControllers(for: self.window); + RNIPresentedVCListCache.getPresentedViewControllers(forWindow: window); for (index, vc) in listPresentedVC.enumerated() { guard vc === self.modalViewController else { continue }; @@ -87,7 +87,10 @@ extension RNIModalState where Self: RNIModalPresentation { extension RNIModalState where Self: RNIModal { public var synthesizedModalData: RNIModalData { - return RNIModalData( + let purgeCache = + RNIPresentedVCListCache.beginCaching(forWindow: self.window); + + let modalData = RNIModalData( modalNativeID: self.modalNativeID, modalIndex: self.modalIndex, @@ -122,5 +125,8 @@ extension RNIModalState where Self: RNIModal { synthesizedWindowID: self.window?.synthesizedStringID ); + + purgeCache(); + return modalData; }; }; diff --git a/ios/src_library/React Native/RNIModal/RNIModalManager.swift b/ios/src_library/React Native/RNIModal/RNIModalManager.swift index 10ccaedd..27d594ba 100644 --- a/ios/src_library/React Native/RNIModal/RNIModalManager.swift +++ b/ios/src_library/React Native/RNIModal/RNIModalManager.swift @@ -135,13 +135,15 @@ extension RNIModalManager: RNIModalPresentationNotifiable { return; }; + let purgeCache = + RNIPresentedVCListCache.beginCaching(forWindow: senderWindow); + /// `Note:2023-04-10-20-47-52` /// * The sender will already be in `presentedModalList` despite it being /// not fully presented yet. /// - let presentedModalList = RNIModalUtilities.getPresentedModals( - forWindow: senderWindow - ); + let presentedModalList = + RNIModalUtilities.getPresentedModals(forWindow: senderWindow); #if DEBUG if windowData.nextModalToFocus != nil { @@ -196,6 +198,8 @@ extension RNIModalManager: RNIModalPresentationNotifiable { if let modalToBlur = presentedModalList.secondToLast { windowData.nextModalToBlur = modalToBlur; }; + + purgeCache(); }; public func notifyOnModalDidShow(sender: any RNIModal) { @@ -236,9 +240,11 @@ extension RNIModalManager: RNIModalPresentationNotifiable { }; #endif - let presentedModalList = RNIModalUtilities.getPresentedModals( - forWindow: senderWindow - ); + let purgeCache = + RNIPresentedVCListCache.beginCaching(forWindow: senderWindow); + + let presentedModalList = + RNIModalUtilities.getPresentedModals(forWindow: senderWindow); windowData.apply(forFocusedModal: sender); @@ -277,6 +283,7 @@ extension RNIModalManager: RNIModalPresentationNotifiable { // Reset windowData.nextModalToBlur = nil; + purgeCache(); }; public func notifyOnModalWillHide(sender: any RNIModal) { @@ -317,9 +324,11 @@ extension RNIModalManager: RNIModalPresentationNotifiable { }; #endif - let presentedModalList = RNIModalUtilities.getPresentedModals( - forWindow: senderWindow - ); + let purgeCache = + RNIPresentedVCListCache.beginCaching(forWindow: senderWindow); + + let presentedModalList = + RNIModalUtilities.getPresentedModals(forWindow: senderWindow); windowData.set(nextModalToBlur: sender); @@ -339,7 +348,10 @@ extension RNIModalManager: RNIModalPresentationNotifiable { sender.onModalWillBlurNotification(sender: sender); - guard let modalToFocus = presentedModalList.secondToLast else { return }; + guard let modalToFocus = presentedModalList.secondToLast else { + purgeCache(); + return; + }; #if DEBUG print( @@ -355,6 +367,7 @@ extension RNIModalManager: RNIModalPresentationNotifiable { modalToFocus.onModalWillFocusNotification(sender: sender); windowData.nextModalToFocus = modalToFocus; + purgeCache(); }; public func notifyOnModalDidHide(sender: any RNIModal) { @@ -394,6 +407,9 @@ extension RNIModalManager: RNIModalPresentationNotifiable { }; #endif + let purgeCache = + RNIPresentedVCListCache.beginCaching(forWindow: senderWindow); + windowData.apply(forBlurredModal: sender); #if DEBUG @@ -427,5 +443,6 @@ extension RNIModalManager: RNIModalPresentationNotifiable { // reset windowData.nextModalToFocus = nil; + purgeCache(); }; }; diff --git a/ios/src_library/React Native/RNIModal/RNIModalUtilities.swift b/ios/src_library/React Native/RNIModal/RNIModalUtilities.swift index f642442b..8391e784 100644 --- a/ios/src_library/React Native/RNIModal/RNIModalUtilities.swift +++ b/ios/src_library/React Native/RNIModal/RNIModalUtilities.swift @@ -13,7 +13,8 @@ public class RNIModalUtilities { forWindow window: UIWindow ) -> [any RNIModal] { - let vcItems = RNIUtilities.getPresentedViewControllers(for: window); + let vcItems = + RNIPresentedVCListCache.getPresentedViewControllers(forWindow: window); return vcItems.compactMap { guard let modalVC = $0 as? RNIModalViewController else { return nil }; @@ -27,7 +28,7 @@ public class RNIModalUtilities { ) -> Int { let listPresentedVC = - RNIUtilities.getPresentedViewControllers(for: window); + RNIPresentedVCListCache.getPresentedViewControllers(forWindow: window); var index = -1; diff --git a/ios/src_library/React Native/RNIModalView/RNIModalView.swift b/ios/src_library/React Native/RNIModalView/RNIModalView.swift index ac1e7530..644726fe 100644 --- a/ios/src_library/React Native/RNIModalView/RNIModalView.swift +++ b/ios/src_library/React Native/RNIModalView/RNIModalView.swift @@ -648,7 +648,7 @@ public class RNIModalView: /// helper func to hide/show the other modals that are below level private func setIsHiddenForViewBelowLevel(_ level: Int, isHidden: Bool){ let presentedVCList = - RNIUtilities.getPresentedViewControllers(for: self.window); + RNIPresentedVCListCache.getPresentedViewControllers(forWindow: window); for (index, vc) in presentedVCList.enumerated() { if index < level { @@ -732,7 +732,7 @@ public class RNIModalView: }; let presentedViewControllers = - RNIUtilities.getPresentedViewControllers(for: self.window); + RNIPresentedVCListCache.getPresentedViewControllers(forWindow: window); guard let topMostPresentedVC = presentedViewControllers.last else { #if DEBUG