Skip to content

Commit

Permalink
💫 Update: UIViewController+Swizzling
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicstop committed Apr 27, 2023
1 parent 8d3891d commit b4b1723
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions ios/src_library/Extensions/UIViewController+Swizzling.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation


fileprivate class RNIModalWrapperMap {
class RNIModalWrapperMap {
static let instanceMap = NSMapTable<
UIViewController,
RNIModalViewControllerWrapper
Expand Down Expand Up @@ -52,23 +52,36 @@ extension UIViewController {
fileprivate func registerIfNeeded(
viewControllerToPresent: UIViewController
) -> RNIModalViewControllerWrapper? {

/// If `viewControllerToPresent` is being presented by a
/// `RNIModalViewController` instance, then we don't need to wrap the
/// current instance inside a `RNIModalViewControllerWrapper` since it
/// will already notify `RNIModalManager` of modal-related events...
///
guard !(self is RNIModalViewController) else { return nil };

let modalWrapper: RNIModalViewControllerWrapper = {
guard let modalWrapper = RNIModalWrapperMap.get(
forViewController: viewControllerToPresent
) else {
let newModalWrapper = RNIModalViewControllerWrapper();

RNIModalWrapperMap.set(
forViewController: self,
newModalWrapper
);

/// A - Wrapper already exists for `viewControllerToPresent`,
/// return matching instance.
///
if let modalWrapper =
RNIModalWrapperMap.get(forViewController: viewControllerToPresent) {

return newModalWrapper;
return modalWrapper;
};

return modalWrapper;
// B - Wrapper does not exists for `viewControllerToPresent`,
// so create new instance.
//
let newModalWrapper = RNIModalViewControllerWrapper();

RNIModalWrapperMap.set(
forViewController: self,
newModalWrapper
);

return newModalWrapper;
}();

modalWrapper.presentingViewController = self;
Expand All @@ -77,7 +90,7 @@ extension UIViewController {
return modalWrapper;
};

func getPresentedModal(
fileprivate func getPresentedModal(
viewControllerToPresent: UIViewController? = nil
) -> (any RNIModal)? {
if let presentedModalVC = viewControllerToPresent as? RNIModalViewController {
Expand Down

0 comments on commit b4b1723

Please sign in to comment.