Skip to content

Commit

Permalink
🛠 Refactor: Extract to RNIModalUtilities
Browse files Browse the repository at this point in the history
Summary: Extract helper function in `RNIModalManager` to `RNIModalUtilities`.
  • Loading branch information
dominicstop committed May 1, 2023
1 parent 9c2771d commit 615e566
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ extension UIViewController {
_ presentedVC: UIViewController? = nil
) -> (any RNIModal)? {

let presentedModal = RNIModalManager.getPresentedModal(
let presentedModal = RNIModalUtilities.getPresentedModal(
forPresentingViewController: self,
presentedViewController: presentedVC
);
Expand Down
4 changes: 2 additions & 2 deletions ios/src_library/React Native/RNIModal/RNIModal+Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ extension RNIModalState where Self: RNIModalPresentation {
let modalVC = self.modalViewController
else { return -1 };

return RNIModalManager.computeModalIndex(
return RNIModalUtilities.computeModalIndex(
forWindow: window,
forViewController: modalVC
);
Expand All @@ -75,7 +75,7 @@ extension RNIModalState where Self: RNIModalPresentation {
};

public var computedCurrentModalIndex: Int {
RNIModalManager.computeModalIndex(forWindow: self.window);
RNIModalUtilities.computeModalIndex(forWindow: self.window);
};

public var synthesizedIsModalInFocus: Bool {
Expand Down
126 changes: 11 additions & 115 deletions ios/src_library/React Native/RNIModal/RNIModalManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,118 +21,6 @@ public class RNIModalManager {

public static let sharedInstance = RNIModalManager();

// MARK: - Static Functions
// ------------------------

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;
};

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

Expand Down Expand Up @@ -251,7 +139,9 @@ extension RNIModalManager: RNIModalPresentationNotifiable {
/// * The sender will already be in `presentedModalList` despite it being
/// not fully presented yet.
///
let presentedModalList = Self.getPresentedModals(forWindow: senderWindow);
let presentedModalList = RNIModalUtilities.getPresentedModals(
forWindow: senderWindow
);

#if DEBUG
if windowData.nextModalToFocus != nil {
Expand Down Expand Up @@ -346,7 +236,10 @@ extension RNIModalManager: RNIModalPresentationNotifiable {
};
#endif

let presentedModalList = Self.getPresentedModals(forWindow: senderWindow);
let presentedModalList = RNIModalUtilities.getPresentedModals(
forWindow: senderWindow
);

windowData.apply(forFocusedModal: sender);

#if DEBUG
Expand Down Expand Up @@ -424,7 +317,10 @@ extension RNIModalManager: RNIModalPresentationNotifiable {
};
#endif

let presentedModalList = Self.getPresentedModals(forWindow: senderWindow);
let presentedModalList = RNIModalUtilities.getPresentedModals(
forWindow: senderWindow
);

windowData.set(nextModalToBlur: sender);

#if DEBUG
Expand Down
120 changes: 120 additions & 0 deletions ios/src_library/React Native/RNIModal/RNIModalUtilities.swift
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;
};
};
8 changes: 4 additions & 4 deletions ios/src_library/React Native/RNIModal/RNIModalWindowMap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ internal class RNIWindowMapData {
self.nextModalToFocus = modalToFocus;

let modalIndexCurrent =
RNIModalManager.computeModalIndex(forWindow: modalToFocus.window);
RNIModalUtilities.computeModalIndex(forWindow: modalToFocus.window);

let modalIndexPrev = self.modalIndexCurrent;

Expand All @@ -52,7 +52,7 @@ internal class RNIWindowMapData {
self.nextModalToFocus = nil;

let modalIndexCurrent =
RNIModalManager.computeModalIndex(forWindow: focusedModal.window);
RNIModalUtilities.computeModalIndex(forWindow: focusedModal.window);

let modalIndexPrev = self.modalIndexCurrent;

Expand All @@ -68,7 +68,7 @@ internal class RNIWindowMapData {
self.nextModalToBlur = modalToBlur;

let modalIndexCurrent =
RNIModalManager.computeModalIndex(forWindow: modalToBlur.window);
RNIModalUtilities.computeModalIndex(forWindow: modalToBlur.window);

let modalIndexPrev = self.modalIndexCurrent;

Expand All @@ -81,7 +81,7 @@ internal class RNIWindowMapData {
self.nextModalToBlur = nil;

let modalIndexCurrent =
RNIModalManager.computeModalIndex(forWindow: blurredModal.window);
RNIModalUtilities.computeModalIndex(forWindow: blurredModal.window);

let modalIndexPrev = self.modalIndexCurrent;

Expand Down

0 comments on commit 615e566

Please sign in to comment.