Skip to content

Commit

Permalink
🛠 Refactor: Use RNIPresentedViewControllerCache
Browse files Browse the repository at this point in the history
Related:
* `TODO:2023-04-22-00-01-50` - Optimization - Add flag for caching the view-controller tree.
  • Loading branch information
dominicstop committed May 1, 2023
1 parent a143d43 commit 5ca1503
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 18 deletions.
14 changes: 10 additions & 4 deletions ios/src_library/React Native/RNIModal/RNIModal+Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 };
Expand All @@ -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 };
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -122,5 +125,8 @@ extension RNIModalState where Self: RNIModal {

synthesizedWindowID: self.window?.synthesizedStringID
);

purgeCache();
return modalData;
};
};
37 changes: 27 additions & 10 deletions ios/src_library/React Native/RNIModal/RNIModalManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -196,6 +198,8 @@ extension RNIModalManager: RNIModalPresentationNotifiable {
if let modalToBlur = presentedModalList.secondToLast {
windowData.nextModalToBlur = modalToBlur;
};

purgeCache();
};

public func notifyOnModalDidShow(sender: any RNIModal) {
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -277,6 +283,7 @@ extension RNIModalManager: RNIModalPresentationNotifiable {

// Reset
windowData.nextModalToBlur = nil;
purgeCache();
};

public func notifyOnModalWillHide(sender: any RNIModal) {
Expand Down Expand Up @@ -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);

Expand All @@ -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(
Expand All @@ -355,6 +367,7 @@ extension RNIModalManager: RNIModalPresentationNotifiable {
modalToFocus.onModalWillFocusNotification(sender: sender);

windowData.nextModalToFocus = modalToFocus;
purgeCache();
};

public func notifyOnModalDidHide(sender: any RNIModal) {
Expand Down Expand Up @@ -394,6 +407,9 @@ extension RNIModalManager: RNIModalPresentationNotifiable {
};
#endif

let purgeCache =
RNIPresentedVCListCache.beginCaching(forWindow: senderWindow);

windowData.apply(forBlurredModal: sender);

#if DEBUG
Expand Down Expand Up @@ -427,5 +443,6 @@ extension RNIModalManager: RNIModalPresentationNotifiable {

// reset
windowData.nextModalToFocus = nil;
purgeCache();
};
};
5 changes: 3 additions & 2 deletions ios/src_library/React Native/RNIModal/RNIModalUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand All @@ -27,7 +28,7 @@ public class RNIModalUtilities {
) -> Int {

let listPresentedVC =
RNIUtilities.getPresentedViewControllers(for: window);
RNIPresentedVCListCache.getPresentedViewControllers(forWindow: window);

var index = -1;

Expand Down
4 changes: 2 additions & 2 deletions ios/src_library/React Native/RNIModalView/RNIModalView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5ca1503

Please sign in to comment.