-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🛠 Refactor: Extract to
RNIModalUtilities
Summary: Extract helper function in `RNIModalManager` to `RNIModalUtilities`.
- Loading branch information
1 parent
9c2771d
commit 615e566
Showing
5 changed files
with
138 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
ios/src_library/React Native/RNIModal/RNIModalUtilities.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// | ||
// RNIModalUtilities.swift | ||
// react-native-ios-modal | ||
// | ||
// Created by Dominic Go on 5/1/23. | ||
// | ||
|
||
import Foundation | ||
|
||
public class RNIModalUtilities { | ||
|
||
static func getPresentedModals( | ||
forWindow window: UIWindow | ||
) -> [any RNIModal] { | ||
|
||
let vcItems = RNIUtilities.getPresentedViewControllers(for: window); | ||
|
||
return vcItems.compactMap { | ||
guard let modalVC = $0 as? RNIModalViewController else { return nil }; | ||
return modalVC.modalViewRef; | ||
}; | ||
}; | ||
|
||
static func computeModalIndex( | ||
forWindow window: UIWindow, | ||
forViewController viewController: UIViewController? = nil | ||
) -> Int { | ||
|
||
let listPresentedVC = | ||
RNIUtilities.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 | ||
); | ||
}; | ||
|
||
static func getPresentedModal( | ||
forPresentingViewController presentingVC: UIViewController, | ||
presentedViewController presentedVC: UIViewController? = nil | ||
) -> (any RNIModal)? { | ||
|
||
let presentedVC = presentedVC ?? presentingVC.presentedViewController; | ||
|
||
/// A - `presentedVC` is a `RNIModalViewController`. | ||
if let presentedModalVC = presentedVC as? RNIModalViewController { | ||
return presentedModalVC.modalViewRef; | ||
}; | ||
|
||
/// B - `presentingVC` is a `RNIModalViewController`. | ||
if let presentingModalVC = presentingVC as? RNIModalViewController, | ||
let presentingModal = presentingModalVC.modalViewRef, | ||
let presentedModalVC = presentingModal.modalVC, | ||
let presentedModal = presentedModalVC.modalViewRef { | ||
|
||
return presentedModal; | ||
}; | ||
|
||
/// C - `presentedVC` has a corresponding `RNIModalViewControllerWrapper` | ||
/// instance associated to it. | ||
if let presentedVC = presentedVC, | ||
let presentedModalWrapper = RNIModalViewControllerWrapperRegistry.get( | ||
forViewController: presentedVC | ||
) { | ||
|
||
return presentedModalWrapper; | ||
}; | ||
|
||
/// D - `presentingVC` has a `RNIModalViewControllerWrapper` instance | ||
/// associated to it. | ||
if let presentingModalWrapper = RNIModalViewControllerWrapperRegistry.get( | ||
forViewController: presentingVC | ||
), | ||
let presentedVC = presentingModalWrapper.modalViewController, | ||
let presentedModalWrapper = RNIModalViewControllerWrapperRegistry.get( | ||
forViewController: presentedVC | ||
) { | ||
|
||
return presentedModalWrapper; | ||
}; | ||
|
||
let topmostVC = RNIUtilities.getTopmostPresentedViewController( | ||
for: presentingVC.view.window | ||
); | ||
|
||
if let topmostModalVC = topmostVC as? RNIModalViewController, | ||
let topmostModal = topmostModalVC.modalViewRef { | ||
|
||
return topmostModal; | ||
}; | ||
|
||
return nil; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters