From 9e4fac4a86b1e5f76b23f6d7f4d9115a9ac7f1a2 Mon Sep 17 00:00:00 2001 From: Dominic Go <18517029+dominicstop@users.noreply.github.com> Date: Fri, 7 Apr 2023 06:10:46 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A0=20Refactor:=20Replace=20`RNIModal.?= =?UTF-8?q?isModalPresented`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related: * `TODO:2023-03-31-18-33-34` - Unify/streamline/consolidate logic for invoking modal focus/blur. * `TODO:2023-04-07-06-10-22` Refactor - Replace `RNIModalState.isModalPresented` w/ `RNIModalPresentationState`. Summary: * Replace `RNIModalState.isModalPresented` w/ `modalState`. * Remove `RNIModalView.isModalPresented`. * Impl. `RNIModalView.modalState`. * Impl. `RNIModalPresentationState.isPresented`. * Replace all usage of `RNIModalState.isModalPresented` w/ `RNIModalPresentationStateMachine.isPresented`. --- .../RNIModal/RNIModal+Helpers.swift | 2 +- .../React Native/RNIModal/RNIModal.swift | 2 +- .../RNIModal/RNIModalManager.swift | 17 ++++++++-------- .../RNIModal/RNIModalPresentationState.swift | 4 ++++ .../RNIModalView/RNIModalView.swift | 20 +++++++++---------- 5 files changed, 24 insertions(+), 21 deletions(-) diff --git a/ios/src_library/React Native/RNIModal/RNIModal+Helpers.swift b/ios/src_library/React Native/RNIModal/RNIModal+Helpers.swift index b5e51fee..676d7529 100644 --- a/ios/src_library/React Native/RNIModal/RNIModal+Helpers.swift +++ b/ios/src_library/React Native/RNIModal/RNIModal+Helpers.swift @@ -85,7 +85,7 @@ extension RNIModalState where Self: RNIModal { modalNativeID: self.modalNativeID, modalIndex: self.modalIndex, currentModalIndex: self.synthesizedCurrentModalIndex, - isModalPresented: self.isModalPresented, + isModalPresented: self.modalState.isPresented, isModalInFocus: self.isModalInFocus, synthesizedIsModalInFocus: self.synthesizedIsModalInFocus, synthesizedIsModalPresented: self.synthesizedIsModalPresented, diff --git a/ios/src_library/React Native/RNIModal/RNIModal.swift b/ios/src_library/React Native/RNIModal/RNIModal.swift index d9df7695..c262c149 100644 --- a/ios/src_library/React Native/RNIModal/RNIModal.swift +++ b/ios/src_library/React Native/RNIModal/RNIModal.swift @@ -33,7 +33,7 @@ public protocol RNIModalState: AnyObject { var modalIndex: Int! { set get }; - var isModalPresented: Bool { set get }; + var modalState: RNIModalPresentationStateMachine { set get }; var isModalInFocus: Bool { set get }; diff --git a/ios/src_library/React Native/RNIModal/RNIModalManager.swift b/ios/src_library/React Native/RNIModal/RNIModalManager.swift index 7aca8d54..b9f4016e 100644 --- a/ios/src_library/React Native/RNIModal/RNIModalManager.swift +++ b/ios/src_library/React Native/RNIModal/RNIModalManager.swift @@ -185,7 +185,7 @@ public class RNIModalManager { public var presentedModals: [any RNIModal] { self.modalInstances.compactMap { - $0.isModalPresented ? $0 : nil; + $0.modalState.isPresented ? $0 : nil; }; }; @@ -196,7 +196,6 @@ public class RNIModalManager { modal.modalIndex = -1; modal.modalIndexPrev = -1; - modal.isModalPresented = false; modal.isModalInFocus = false; modal.modalFocusDelegate = self; @@ -263,11 +262,12 @@ extension RNIModalManager: RNIModalFocusNotifiable { self.setCurrentModalIndex(for: senderWindow, index: nextModalIndex); + sender.modalState.set(state: .PRESENTING_UNKNOWN); sender.onModalWillFocusNotification(sender: sender); self.modalInstances.forEach { guard $0 !== sender, - $0.isModalPresented, + $0.modalState.isPresented, $0.isModalInFocus, $0.modalIndex == prevModalIndex else { return }; @@ -291,7 +291,7 @@ extension RNIModalManager: RNIModalFocusNotifiable { let currentModalIndex = self.getCurrentModalIndex(for: senderWindow); sender.isModalInFocus = true; - sender.isModalPresented = true; + sender.modalState.set(state: .PRESENTED); #if DEBUG print( @@ -307,7 +307,7 @@ extension RNIModalManager: RNIModalFocusNotifiable { self.modalInstances.forEach { guard $0 !== sender, - $0.isModalPresented, + $0.modalState.isPresented, $0.isModalInFocus, $0.modalIndex == currentModalIndex - 1 else { return }; @@ -337,11 +337,12 @@ extension RNIModalManager: RNIModalFocusNotifiable { sender.modalIndexPrev = sender.modalIndex; sender.modalIndex = -1; + sender.modalState.set(state: .DISMISSING_UNKNOWN); sender.onModalWillBlurNotification(sender: sender); self.modalInstances.forEach { guard $0 !== sender, - $0.isModalPresented, + $0.modalState.isPresented, !$0.isModalInFocus, $0.modalIndex >= nextModalIndex else { return }; @@ -365,7 +366,7 @@ extension RNIModalManager: RNIModalFocusNotifiable { let currentModalIndex = self.getCurrentModalIndex(for: senderWindow); sender.isModalInFocus = false; - sender.isModalPresented = false; + sender.modalState.set(state: .DISMISSED); #if DEBUG print( @@ -381,7 +382,7 @@ extension RNIModalManager: RNIModalFocusNotifiable { self.modalInstances.forEach { guard $0 !== sender, - $0.isModalPresented, + $0.modalState.isPresented, !$0.isModalInFocus, $0.modalIndex >= currentModalIndex else { return }; diff --git a/ios/src_library/React Native/RNIModal/RNIModalPresentationState.swift b/ios/src_library/React Native/RNIModal/RNIModalPresentationState.swift index bff65c55..bf9e3204 100644 --- a/ios/src_library/React Native/RNIModal/RNIModalPresentationState.swift +++ b/ios/src_library/React Native/RNIModal/RNIModalPresentationState.swift @@ -104,6 +104,10 @@ public struct RNIModalPresentationStateMachine { // MARK: - Computed Properties // --------------------------- + var isPresented: Bool { + self.state == .PRESENTED; + }; + var wasDismissViaGestureCancelled: Bool { self.statePrev.isDismissViaGestureCancelling }; diff --git a/ios/src_library/React Native/RNIModalView/RNIModalView.swift b/ios/src_library/React Native/RNIModalView/RNIModalView.swift index a128e641..43fa1f78 100644 --- a/ios/src_library/React Native/RNIModalView/RNIModalView.swift +++ b/ios/src_library/React Native/RNIModalView/RNIModalView.swift @@ -30,16 +30,6 @@ class RNIModalView: UIView, RNIIdentifiable, RNIModalFocusNotifying, var modalContentWrapper: RNIWrapperView?; var modalVC: RNIModalViewController?; - lazy var modalState = RNIModalPresentationStateMachine( - onDismissWillCancel: { [weak self] in - // no-op - TBA - }, - onDismissDidCancel: { [weak self] in - // no-op - TBA - } - ); - - // MARK: - Properties - RNIModalFocusNotifying // ------------------------------------------- @@ -54,7 +44,15 @@ class RNIModalView: UIView, RNIIdentifiable, RNIModalFocusNotifying, // MARK: - Properties - RNIModalState // ---------------------------------- - var isModalPresented: Bool = false; + lazy var modalState = RNIModalPresentationStateMachine( + onDismissWillCancel: { [weak self] in + // no-op - TBA + }, + onDismissDidCancel: { [weak self] in + // no-op - TBA + } + ); + var isModalInFocus: Bool = false; // MARK: - Properties - RNIModalPresentation