Skip to content

Commit

Permalink
⭐️ Impl: Add RNIModalPresentationNotifiable
Browse files Browse the repository at this point in the history
Summary:
* Impl: Add `RNIModalPresentationNotifiable`.
* Replace `RNIModalFocusNotifying` w/ `RNIModalPresentationNotifying`.
* Update `RNIModalManager` - Replace `RNIModalFocusNotifiable` conformance w/ `RNIModalPresentationNotifiable`.
* Update `RNIModalView` - Replace `RNIModalPresentationNotifying` conformance w/ `RNIModalPresentationNotifying`.
  • Loading branch information
dominicstop committed Apr 10, 2023
1 parent 1ba88e5 commit fa04652
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 41 deletions.
10 changes: 5 additions & 5 deletions ios/src_library/React Native/RNIModal/RNIModal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public typealias RNIModal =
& RNIModalState
& RNIModalRequestable
& RNIModalFocusNotifiable
& RNIModalFocusNotifying
& RNIModalPresentationNotifying
& RNIModalPresentation;

/// Contains modal-related properties for keeping track of the state of the
Expand Down Expand Up @@ -78,17 +78,17 @@ public protocol RNIModalFocusNotifiable: AnyObject {
};

/// Specifies that the "adoptee/delegate" that conforms to this protocol must
/// notify a delegate of modal "focus/blur"-related events
/// notify a delegate of modal presentation-related events
///
public protocol RNIModalFocusNotifying: AnyObject {
public protocol RNIModalPresentationNotifying: AnyObject {

/// Notify the "shared modal manager" if the current modal instance is going
/// to be shown or hidden.
///
/// That focus notification will then be relayed to the other modal instances.
///
var modalFocusDelegate: RNIModalFocusNotifiable! { get set };

var modalPresentationNotificationDelegate:
RNIModalPresentationNotifiable! { get set };
};

/// Properties related to modal presentation.
Expand Down
52 changes: 26 additions & 26 deletions ios/src_library/React Native/RNIModal/RNIModalManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ public class RNIModalManager {

modal.isModalInFocus = false;

modal.modalFocusDelegate = self;
modal.modalPresentationNotificationDelegate = self;
self.modalInstanceDict[modal.synthesizedUUID] = modal;
};

Expand All @@ -293,19 +293,19 @@ public class RNIModalManager {
};
};

// MARK: RNIModalFocusNotifiable
// -----------------------------
// MARK: RNIModalPresentationNotifiable
// ------------------------------------

/// The modal instances will notify the manager when they're about to show/hide
/// a modal.
///
extension RNIModalManager: RNIModalFocusNotifiable {
extension RNIModalManager: RNIModalPresentationNotifiable {

public func onModalWillFocusNotification(sender: any RNIModal) {
public func notifyOnModalWillShow(sender: any RNIModal) {
guard let senderWindow = sender.window else {
#if DEBUG
print(
"Error - RNIModalManager.onModalWillFocusNotification"
"Error - RNIModalManager.notifyOnModalWillShow"
+ " - arg sender.modalNativeID: \(sender.modalNativeID)"
+ " - Unable to send event because sender.window is nil"
);
Expand All @@ -319,7 +319,7 @@ extension RNIModalManager: RNIModalFocusNotifiable {
#if DEBUG
let nextModalToFocus = windowData.nextModalToFocus!;
print(
"Error - RNIModalManager.onModalWillFocusNotification"
"Error - RNIModalManager.notifyOnModalWillShow"
+ " - arg sender.modalNativeID: \(sender.modalNativeID)"
+ " - nextModalToFocus.modalNativeID: \(nextModalToFocus.modalNativeID)"
+ " - possible multiple invokation"
Expand All @@ -332,7 +332,7 @@ extension RNIModalManager: RNIModalFocusNotifiable {
#if DEBUG
if windowData.nextModalToFocus != nil {
print(
"Warning - RNIModalManager.onModalWillFocusNotification"
"Warning - RNIModalManager.notifyOnModalWillShow"
+ " - arg sender.modalNativeID: \(sender.modalNativeID)"
+ " - nextModalToFocus is not nil"
+ " - a different modal is about to be focused"
Expand All @@ -345,7 +345,7 @@ extension RNIModalManager: RNIModalFocusNotifiable {

#if DEBUG
print(
"Log - RNIModalManager.onModalWillFocusNotification"
"Log - RNIModalManager.notifyOnModalWillShow"
+ " - arg sender.modalNativeID: \(sender.modalNativeID)"
+ " - prevModalIndex: \(windowData.modalIndexPrev)"
+ " - windowData.modalIndexNext: \(windowData.modalIndexNext_)"
Expand All @@ -367,11 +367,11 @@ extension RNIModalManager: RNIModalFocusNotifiable {
};
};

public func onModalDidFocusNotification(sender: any RNIModal) {
public func notifyOnModalDidShow(sender: any RNIModal) {
guard let senderWindow = sender.window else {
#if DEBUG
print(
"Error - RNIModalManager.onModalDidFocusNotification"
"Error - RNIModalManager.notifyOnModalDidShow"
+ " - arg sender.modalNativeID: \(sender.modalNativeID)"
+ " - Unable to send event because sender.window is nil"
);
Expand All @@ -384,10 +384,10 @@ extension RNIModalManager: RNIModalFocusNotifiable {
guard let nextModalToFocus = windowData.nextModalToFocus else {
#if DEBUG
print(
"Error - RNIModalManager.onModalDidFocusNotification"
"Error - RNIModalManager.notifyOnModalDidShow"
+ " - arg sender.modalNativeID: \(sender.modalNativeID)"
+ " - nextModalToFocus: nil"
+ " - possible onModalWillFocusNotification not invoked for sender"
+ " - possible notifyOnModalWillShow not invoked for sender"
);
#endif
return;
Expand All @@ -396,7 +396,7 @@ extension RNIModalManager: RNIModalFocusNotifiable {
#if DEBUG
if nextModalToFocus !== sender {
print(
"Warning - RNIModalManager.onModalDidFocusNotification"
"Warning - RNIModalManager.notifyOnModalDidShow"
+ " - arg sender.modalNativeID: \(sender.modalNativeID)"
+ " - nextModalToFocus.modalNativeID: \(nextModalToFocus.modalNativeID)"
+ " - nextModalToFocus !== sender"
Expand All @@ -410,7 +410,7 @@ extension RNIModalManager: RNIModalFocusNotifiable {

#if DEBUG
print(
"Log - RNIModalManager.onModalDidFocusNotification"
"Log - RNIModalManager.notifyOnModalDidShow"
+ " - arg sender.modalNativeID: \(sender.modalNativeID)"
+ " - sender.modalIndexPrev: \(sender.modalIndexPrev!)"
+ " - sender.modalIndex: \(sender.modalIndex!)"
Expand All @@ -432,11 +432,11 @@ extension RNIModalManager: RNIModalFocusNotifiable {
};
};

public func onModalWillBlurNotification(sender: any RNIModal) {
public func notifyOnModalWillHide(sender: any RNIModal) {
guard let senderWindow = sender.window else {
#if DEBUG
print(
"Error - RNIModalManager.onModalWillBlurNotification"
"Error - RNIModalManager.notifyOnModalWillHide"
+ " - arg sender.modalNativeID: \(sender.modalNativeID)"
+ " - Unable to send event because sender.window is nil"
);
Expand All @@ -450,7 +450,7 @@ extension RNIModalManager: RNIModalFocusNotifiable {
#if DEBUG
let nextModalToBlur = windowData.nextModalToBlur!;
print(
"Error - RNIModalManager.onModalWillBlurNotification"
"Error - RNIModalManager.notifyOnModalWillHide"
+ " - arg sender.modalNativeID: \(sender.modalNativeID)"
+ " - nextModalToBlur.modalNativeID: \(nextModalToBlur.modalNativeID)"
+ " - possible multiple invokation"
Expand All @@ -463,7 +463,7 @@ extension RNIModalManager: RNIModalFocusNotifiable {
#if DEBUG
if windowData.nextModalToBlur != nil {
print(
"Warning - RNIModalManager.onModalWillBlurNotification"
"Warning - RNIModalManager.notifyOnModalWillHide"
+ " - arg sender.modalNativeID: \(sender.modalNativeID)"
+ " - nextModalToBlur is not nil"
+ " - a different modal is about to blur"
Expand All @@ -476,7 +476,7 @@ extension RNIModalManager: RNIModalFocusNotifiable {

#if DEBUG
print(
"Log - RNIModalManager.onModalWillBlurNotification"
"Log - RNIModalManager.notifyOnModalWillHide"
+ " - arg sender.modalNativeID: \(sender.modalNativeID)"
+ " - prevModalIndex: \(windowData.modalIndexPrev)"
+ " - windowData.modalIndexNext: \(windowData.modalIndexNext_)"
Expand All @@ -497,11 +497,11 @@ extension RNIModalManager: RNIModalFocusNotifiable {
};
};

public func onModalDidBlurNotification(sender: any RNIModal) {
public func notifyOnModalDidHide(sender: any RNIModal) {
guard let senderWindow = sender.window else {
#if DEBUG
print(
"Error - RNIModalManager.onModalDidBlurNotification"
"Error - RNIModalManager.notifyOnModalDidHide"
+ " - arg sender.modalNativeID: \(sender.modalNativeID)"
+ " - Unable to send event because sender.window is nil"
);
Expand All @@ -514,10 +514,10 @@ extension RNIModalManager: RNIModalFocusNotifiable {
guard let nextModalToBlur = windowData.nextModalToBlur else {
#if DEBUG
print(
"Error - RNIModalManager.onModalDidBlurNotification"
"Error - RNIModalManager.notifyOnModalDidHide"
+ " - arg sender.modalNativeID: \(sender.modalNativeID)"
+ " - nextModalToBlur: nil"
+ " - possible onModalWillBlurNotification not invoked for sender"
+ " - possible notifyOnModalWillHide not invoked for sender"
);
#endif
return;
Expand All @@ -526,7 +526,7 @@ extension RNIModalManager: RNIModalFocusNotifiable {
#if DEBUG
if nextModalToBlur !== sender {
print(
"Warning - RNIModalManager.onModalDidBlurNotification"
"Warning - RNIModalManager.notifyOnModalDidHide"
+ " - arg sender.modalNativeID: \(sender.modalNativeID)"
+ " - nextModalToBlur.modalNativeID: \(nextModalToBlur.modalNativeID)"
+ " - nextModalToBlur !== sender"
Expand All @@ -540,7 +540,7 @@ extension RNIModalManager: RNIModalFocusNotifiable {

#if DEBUG
print(
"Log - RNIModalManager.onModalDidBlurNotification"
"Log - RNIModalManager.notifyOnModalDidHide"
+ " - arg sender.modalNativeID: \(sender.modalNativeID)"
+ " - sender.modalIndexPrev: \(sender.modalIndexPrev!)"
+ " - sender.modalIndex: \(sender.modalIndex!)"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// RNIModalPresentationNotifiable.swift
// react-native-ios-modal
//
// Created by Dominic Go on 4/10/23.
//

import Foundation

public protocol RNIModalPresentationNotifiable: AnyObject {

func notifyOnModalWillShow(sender: any RNIModal);

func notifyOnModalDidShow(sender: any RNIModal);

func notifyOnModalWillHide(sender: any RNIModal);

func notifyOnModalDidHide(sender: any RNIModal);
};
27 changes: 17 additions & 10 deletions ios/src_library/React Native/RNIModalView/RNIModalView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

import Foundation

public class RNIModalView: UIView, RNIIdentifiable, RNIModalFocusNotifying,
RNIModalState, RNIModalPresentation {
public class RNIModalView: UIView, RNIIdentifiable,
RNIModalPresentationNotifying, RNIModalState,
RNIModalPresentation {

public typealias CompletionHandler = (_ isSuccess: Bool, _ error: RNIModalViewError?) -> Void;

Expand All @@ -30,10 +31,11 @@ public class RNIModalView: UIView, RNIIdentifiable, RNIModalFocusNotifying,
var modalContentWrapper: RNIWrapperView?;
public var modalVC: RNIModalViewController?;

// MARK: - Properties - RNIModalFocusNotifying
// -------------------------------------------
// MARK: - Properties - RNIModalPresentationNotifying
// --------------------------------------------------

public weak var modalFocusDelegate: RNIModalFocusNotifiable!;
public weak var modalPresentationNotificationDelegate:
RNIModalPresentationNotifiable!;

// MARK: - Properties - RNIModalIdentity
// -------------------------------------
Expand Down Expand Up @@ -673,7 +675,8 @@ extension RNIModalView: UIAdaptivePresentationControllerDelegate {
};

public func presentationControllerDidDismiss(_ presentationController: UIPresentationController) {
self.modalFocusDelegate.onModalDidBlurNotification(sender: self);
self.modalPresentationNotificationDelegate
.notifyOnModalDidHide(sender: self);

#if DEBUG
print(
Expand Down Expand Up @@ -734,14 +737,16 @@ extension RNIModalView: RNIViewControllerLifeCycleNotifiable {
guard sender.isBeingPresented else { return };
self.modalState.set(state: .PRESENTING_UNKNOWN);

self.modalFocusDelegate.onModalWillFocusNotification(sender: self);
self.modalPresentationNotificationDelegate
.notifyOnModalWillShow(sender: self);
};

public func viewDidAppear(sender: UIViewController, animated: Bool) {
guard sender.isBeingPresented else { return };
self.modalState.set(state: .PRESENTED_UNKNOWN);

self.modalFocusDelegate.onModalDidFocusNotification(sender: self);
self.modalPresentationNotificationDelegate
.notifyOnModalDidShow(sender: self);

if !self.modalState.wasDismissViaGestureCancelled {
self.onModalShow?(
Expand All @@ -754,7 +759,8 @@ extension RNIModalView: RNIViewControllerLifeCycleNotifiable {
guard sender.isBeingDismissed else { return };
self.modalState.set(state: .DISMISSING_UNKNOWN);

self.modalFocusDelegate.onModalWillBlurNotification(sender: self);
self.modalPresentationNotificationDelegate
.notifyOnModalWillHide(sender: self);

if self.modalState.state.isDismissingViaGesture {
self.onModalWillDismiss?(
Expand All @@ -767,7 +773,8 @@ extension RNIModalView: RNIViewControllerLifeCycleNotifiable {
guard sender.isBeingDismissed else { return };
self.modalState.set(state: .DISMISSED);

self.modalFocusDelegate.onModalDidBlurNotification(sender: self);
self.modalPresentationNotificationDelegate
.notifyOnModalDidHide(sender: self);

if self.modalState.statePrev.isDismissingViaGesture {
self.onModalDidDismiss?(
Expand Down

0 comments on commit fa04652

Please sign in to comment.