Skip to content

Commit

Permalink
🛠 Refactor: RNIModalManager.currentModalIndex
Browse files Browse the repository at this point in the history
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".
  • Loading branch information
dominicstop committed Apr 6, 2023
1 parent 4f27941 commit 839d01a
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 14 deletions.
7 changes: 6 additions & 1 deletion ios/src_library/React Native/RNIModal/RNIModal+Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
129 changes: 116 additions & 13 deletions ios/src_library/React Native/RNIModal/RNIModalManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,18 +155,15 @@ public class RNIModalManager {
// MARK: - Properties
// ------------------

private(set) public var currentModalIndex = -1;

private(set) public var modalInstanceDict =
RNIWeakDictionary<UUID, any RNIModal>();

private(set) public var windowToCurrentModalIndexMap:
Dictionary<String, Int> = [:];

// MARK: - Properties - Computed
// -----------------------------

public var isAnyModalPresented: Bool {
self.currentModalIndex >= 0;
};

public var modalInstances: [any RNIModal] {
self.modalInstanceDict.dict.compactMap {
$0.value.synthesizedRef;
Expand All @@ -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
Expand All @@ -203,36 +217,86 @@ 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);
};
};

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;
Expand All @@ -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;
Expand All @@ -252,22 +330,47 @@ extension RNIModalManager: RNIModalFocusNotifiable {
guard $0 !== sender,
$0.isModalPresented,
!$0.isModalInFocus,
$0.modalIndex >= self.currentModalIndex
$0.modalIndex >= nextModalIndex
else { return };

$0.onModalWillFocusNotification(sender: sender);
};
};

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;
Expand Down

0 comments on commit 839d01a

Please sign in to comment.