Skip to content

Commit

Permalink
🛠 Refactor: ModalView
Browse files Browse the repository at this point in the history
Related:
* TODO:2023-03-04-06-34-28 - Library Native Cleanup.
* TODO:2023-03-24-14-25-52 - Remove `RNIModalViewFocusDelegate`-related logic.
* TODO:2023-03-24-09-58-50 - Refactor `RNIModalView` to use `RNIModalManager`.
* TODO:2023-03-05-00-32-43 - Fix: Edge Case - Modal Focus/Blur Bug.

* Delete `RNIModalViewDelegate` + usage.
* Remove `RNIModalViewPresentDelegate`.
* Remove `RNIModalViewFocusDelegate` + usage.
* Replace `RNIModalViewPresentDelegate`.
* Refactor `RNIModalView` focus/blur logic to use `RNIModalViewManager`.
* Remove `ModalView` "Patch - Note:2023-03-04-02-58-31".
  • Loading branch information
dominicstop committed Mar 27, 2023
1 parent af3c090 commit 95f9279
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 155 deletions.
86 changes: 35 additions & 51 deletions ios/src_library/React Native/RNIModalView/RNIModalView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ class RNIModalView: UIView, RNIModalFocusNotifying, RNIModalIdentity,

weak var bridge: RCTBridge?;

/// TODO:2023-03-24-14-25-52 - Remove `RNIModalViewFocusDelegate`-related logic
weak var delegate: RNIModalViewPresentDelegate?;

private var modalVC: RNIModalViewController?;

private var touchHandler: RCTTouchHandler!;
Expand Down Expand Up @@ -545,9 +542,6 @@ class RNIModalView: UIView, RNIModalFocusNotifying, RNIModalIdentity,
// Reset swipe gesture before it was temporarily disabled
self.enableSwipeGesture();

/// TODO:2023-03-24-14-25-52 - Remove `RNIModalViewFocusDelegate`-related logic
self.delegate?.onPresentModalView(modalView: self);

self.modalFocusDelegate.onModalDidFocusNotification(sender: self);

self.onModalShow?(
Expand Down Expand Up @@ -615,9 +609,6 @@ class RNIModalView: UIView, RNIModalFocusNotifying, RNIModalIdentity,
self.enableSwipeGesture(false);

presentedVC.dismiss(animated: true){
/// TODO:2023-03-24-14-25-52 - Remove `RNIModalViewFocusDelegate`-related logic
self.delegate?.onDismissModalView(modalView: self);

self.onModalDismiss?(
self.synthesizedBaseEventData.synthesizedDictionary
);
Expand Down Expand Up @@ -693,10 +684,7 @@ extension RNIModalView: UIAdaptivePresentationControllerDelegate {
self.modalFocusDelegate.onModalDidBlurNotification(sender: self);

self.modalLevel = -1;

/// TODO:2023-03-24-14-25-52 - Remove `RNIModalViewFocusDelegate`-related logic
self.delegate?.onDismissModalView(modalView: self);


self.onModalDidDismiss?(
self.synthesizedBaseEventData.synthesizedDictionary
);
Expand Down Expand Up @@ -727,38 +715,8 @@ extension RNIModalView: UIAdaptivePresentationControllerDelegate {
};
};

// MARK: Extension: RNIModalViewFocusDelegate
// ------------------------------------------

/// TODO:2023-03-24-14-25-52 - Remove `RNIModalViewFocusDelegate`-related logic
extension RNIModalView: RNIModalViewFocusDelegate {

func onModalChangeFocus(modalLevel: Int, modalNativeID: String, isInFocus: Bool) {
guard
/// defer if the receiver of the event is the same as the sender
/// i.e defer if this instance of `RNIModalView` was the one who broadcasted the event
self.modalNativeID != modalNativeID,
/// defer if the modal is not currently presented or if the modalLevel is -1
self.synthesizedIsModalPresented && self.modalLevel > 0 else { return };

if isInFocus && !self.synthesizedIsModalInFocus {
/// a new `RNIModalView` instance is in focus and this modal was prev. in focus so
/// this modal shoud be now 'blurred'
self.onModalBlur?(
self.createModalNativeEventDict()
);

} else if !isInFocus && !self.synthesizedIsModalInFocus {
/// a `RNIModalView` instance has lost focus, so the prev modal shoul be focused
/// defer if the receiver's modalLevel isn't 1 value below the sender's modalLevel
guard self.modalLevel + 1 >= modalLevel else { return };

self.onModalFocus?(
self.createModalNativeEventDict()
);
};
};
};
// MARK: Extension: RNIModalRequestable
// ------------------------------------

extension RNIModalView: RNIModalRequestable {

Expand All @@ -774,25 +732,51 @@ extension RNIModalView: RNIModalRequestable {
};


// MARK: Extension: RNIModalFocusNotifiable
// ----------------------------------------

extension RNIModalView: RNIModalFocusNotifiable {

func onModalWillFocusNotification(sender modal: RNIModal) {
/// `TODO:2023-03-24-09-58-50` - Refactor `RNIModalView` to use `RNIModalManager`.
/// No-op - TBA
};

func onModalDidFocusNotification(sender modal: RNIModal) {
/// `TODO:2023-03-24-09-58-50` - Refactor `RNIModalView` to use `RNIModalManager`.
/// No-op - TBA

let eventData = RNIOnModalFocusEventData(
modalData: self.synthesizedBaseEventData,
senderInfo: modal.synthesizedModalData,
isInitial: modal === self
);

print(
"\nRNIModalFocusNotifiable - onModalDidFocusNotification - ",
"eventData: \(eventData.synthesizedDictionary)\n"
);

self.onModalFocus?(
eventData.synthesizedDictionary
);
};

func onModalWillBlurNotification(sender modal: RNIModal) {
/// `TODO:2023-03-24-09-58-50` - Refactor `RNIModalView` to use `RNIModalManager`.
/// No-op - TBA
};

func onModalDidBlurNotification(sender modal: RNIModal) {
/// `TODO:2023-03-24-09-58-50` - Refactor `RNIModalView` to use `RNIModalManager`.
/// No-op - TBA
let eventData = RNIOnModalFocusEventData(
modalData: self.synthesizedBaseEventData,
senderInfo: modal.synthesizedModalData,
isInitial: modal === self
);

print(
"\nRNIModalFocusNotifiable - onModalDidBlurNotification - ",
"eventData: \(eventData.synthesizedDictionary)\n"
);

self.onModalBlur?(
eventData.synthesizedDictionary
);
};
};

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,14 @@ class RNIModalViewManager: RCTViewManager {

// TODO: See `TODO:2023-03-04-15-33-15` - Refactor: Relocate
// `delegatesFocus`
var delegatesFocus = MulticastDelegate<RNIModalViewFocusDelegate>();
var delegatesFocus = MulticastDelegate<AnyObject>();

// TODO: See TODO:2023-03-04-15-38-02 - Refactor: Relocate
// `currentModalLevel`
private var currentModalLevel = -1;

override func view() -> UIView! {
let view = RNIModalView(bridge: self.bridge);
view.delegate = self;
self.delegatesFocus.add(view);

return view;
Expand All @@ -61,45 +60,3 @@ class RNIModalViewManager: RCTViewManager {
];
};
};

// ---------------------------------
// MARK: RNIModalViewPresentDelegate
// ---------------------------------

/// TODO:2023-03-24-14-25-52 - Remove `RNIModalViewFocusDelegate`-related logic
extension RNIModalViewManager: RNIModalViewPresentDelegate {

func onPresentModalView(modalView: RNIModalView) {
let modalLevel = modalView.modalLevel;
let modalNativeID = modalView.modalNativeID!;

self.currentModalLevel = modalLevel;
self.presentedModalRefs.setObject(modalView, forKey: modalNativeID as NSString);

// notify delegates that a new modal is in focus
self.delegatesFocus.invoke {
$0.onModalChangeFocus(
modalLevel: modalLevel,
modalNativeID: modalNativeID,
isInFocus : true
);
};
};

func onDismissModalView(modalView: RNIModalView) {
let modalLevel = modalView.modalLevelPrev;
let modalID = modalView.modalNativeID!

self.currentModalLevel = modalLevel;
self.presentedModalRefs.removeObject(forKey: modalID as NSString);

// notify delegates that a new modal is lost focus
self.delegatesFocus.invoke {
$0.onModalChangeFocus(
modalLevel: modalLevel,
modalNativeID: modalID,
isInFocus : false
);
};
};
};
27 changes: 0 additions & 27 deletions src/components/ModalView/ModalView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -341,30 +341,6 @@ export class ModalView extends
);

this.setStateIsModalVisible(true);

// TODO: Patch - Note:2023-03-04-02-58-31
// * Temp impl. to make focus/blur context to work
//
// * Modal focus/blur native event does not fire on initial
// focus/blur - they only fire in response to another modal
// stealing focus.
//
// * As such, when a modal becomes visible, the focus event
// does not fire; conversely, the blur event also does not
// fire when the modal becomes hidden.
//
// * Fix/Solution: Update focus/blur event to fire on initial
// blur or focus, but there should be a `isInitialFocus`,
// and `isInitialBlur` in the event params as a means to
// distinguish if this is the first time it's going to
// focus/blur.
//
// * I.e. a means of distinguishing whether the focus/blur
// was due to a `setVisibility` request, or due to
// another modal stealing focus.
this.setState({
isModalInFocus: true,
});
};

private _handleOnModalDismiss: OnModalDismissEvent = (event) => {
Expand All @@ -385,9 +361,6 @@ export class ModalView extends
// reset state values from props
enableSwipeGesture: props.enableSwipeGesture,
isModalInPresentation: props.isModalInPresentation,

// TODO: Patch - See Note:2023-03-04-02-58-31
isModalInFocus: false,
});
};

Expand Down

0 comments on commit 95f9279

Please sign in to comment.