Skip to content

Commit

Permalink
⭐️ Impl: RNIModalManager.computeModalIndex
Browse files Browse the repository at this point in the history
Summary:
* Impl. `RNIModalManager.computeModalIndex`
* Refactor: `RNIModal+Helpers` - Update to use `RNIModalManager.computeModalIndex`.
  • Loading branch information
dominicstop committed Apr 8, 2023
1 parent 80d315a commit 1559c54
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 16 deletions.
29 changes: 13 additions & 16 deletions ios/src_library/React Native/RNIModal/RNIModal+Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,23 @@ extension RNIModalState where Self: RNIModalPresentation {

/// Programmatically get the "modal index"
public var synthesizedModalIndex: Int {
let listPresentedVC =
RNIModalManager.getPresentedViewControllers(for: self.window);

var index = -1;
guard let window = self.window,
let modalVC = self.modalViewController
else { return -1 };

for vc in listPresentedVC {
if vc.presentingViewController != nil {
index += 1;
};

guard vc === self.modalViewController else { continue };
return index;
};

return -1;
return RNIModalManager.computeModalIndex(
forWindow: window,
forViewController: modalVC
);
};

public var synthesizedCurrentModalIndex: Int {
guard let window = self.window else { return -1 };
return RNIModalManagerShared.getCurrentModalIndex(for: window);
RNIModalManager.computeModalIndex(forWindow: self.window);
};

internal var synthesizedWindowMapData: RNIWindowMapData? {
guard let window = self.window else { return nil };
return RNIModalWindowMapShared.get(forWindow: window);
};
};

Expand Down
40 changes: 40 additions & 0 deletions ios/src_library/React Native/RNIModal/RNIModalManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,46 @@ public class RNIModalManager {
return Self.getPresentedViewControllers(for: window).last;
};

static func computeModalIndex(
forWindow window: UIWindow,
forViewController viewController: UIViewController? = nil
) -> Int {

let listPresentedVC =
RNIModalManager.getPresentedViewControllers(for: window);

var index = -1;

for vc in listPresentedVC {
if vc.presentingViewController != nil {
index += 1;
};

/// A - no `viewController` arg., keep counting until all items in
/// `listPresentedVC` have been exhausted.
guard viewController == nil else { continue };

/// B - `viewController` arg. specified, stop counting if found matching
/// instance of `viewController` in `listPresentedVC`.
guard viewController !== vc
else { break };
};

return index;
};

static func computeModalIndex(
forWindow window: UIWindow?,
forViewController viewController: UIViewController? = nil
) -> Int {
guard let window = window else { return -1 };

return Self.computeModalIndex(
forWindow: window,
forViewController: viewController
);
};

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

Expand Down

0 comments on commit 1559c54

Please sign in to comment.