From d92b3dc0a65496f538bef8bc0dc101dd12b5eb01 Mon Sep 17 00:00:00 2001 From: Dominic Go <18517029+dominicstop@users.noreply.github.com> Date: Sat, 13 May 2023 13:27:32 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=AB=20Update:=20`setModalVisibilityByI?= =?UTF-8?q?D`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: * Update to use `RNIModalRequestable` for showing/hiding the modal. * Add `animated` param. to `RNIModalViewModule.setModalVisibilityByID`. --- .../RNIModalViewModule/RNIModalViewModule.m | 1 + .../RNIModalViewModule.swift | 34 +++++++------------ src/module/ModalViewModule.ts | 12 +++++-- src/native_modules/RNIModalViewModule.ts | 1 + 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/ios/src_library/React Native/RNIModalViewModule/RNIModalViewModule.m b/ios/src_library/React Native/RNIModalViewModule/RNIModalViewModule.m index c8f28137..e37b151d 100644 --- a/ios/src_library/React Native/RNIModalViewModule/RNIModalViewModule.m +++ b/ios/src_library/React Native/RNIModalViewModule/RNIModalViewModule.m @@ -16,6 +16,7 @@ @interface RCT_EXTERN_MODULE(RNIModalViewModule, RCTEventEmitter) RCT_EXTERN_METHOD(setModalVisibilityByID: (NSString)modalID visibility: (BOOL)visibility + animated: (BOOL)visibility // promise blocks ----------------------- resolve: (RCTPromiseResolveBlock *)resolve reject : (RCTPromiseRejectBlock *)reject); diff --git a/ios/src_library/React Native/RNIModalViewModule/RNIModalViewModule.swift b/ios/src_library/React Native/RNIModalViewModule/RNIModalViewModule.swift index 2e7631de..6b881846 100644 --- a/ios/src_library/React Native/RNIModalViewModule/RNIModalViewModule.swift +++ b/ios/src_library/React Native/RNIModalViewModule/RNIModalViewModule.swift @@ -55,14 +55,15 @@ class RNIModalViewModule: RCTEventEmitter { // ------------------------ @objc func setModalVisibilityByID( - _ modalID: NSString, + _ modalID: String, visibility: Bool, + animated: Bool, // promise blocks ------------------------ resolve: @escaping RCTPromiseResolveBlock, reject : @escaping RCTPromiseRejectBlock ) { DispatchQueue.main.async { - let listPresentedVC = RNIUtilities.getPresentedViewControllers(); + let modalInstances = RNIModalManagerShared.modalInstances; let debugData: Dictionary = [ "modalID": modalID, @@ -70,23 +71,19 @@ class RNIModalViewModule: RCTEventEmitter { ]; do { - guard listPresentedVC.count > 0 else { + guard modalInstances.count > 0 else { throw RNIModalError( code: .runtimeError, - message: "The list of presented view controllers is empty", + message: "The list of modalInstances is empty", debugData: debugData ); }; - - let listPresentedModalVC = - listPresentedVC.compactMap { $0 as? RNIModalViewController }; - - let targetModalVC = listPresentedModalVC.first { - guard let modalID = $0.modalID else { return false }; - return modalID == modalID; + + let targetModal = modalInstances.first { + $0.modalUserID == modalID || $0.modalNativeID == modalID }; - guard let targetModalView = targetModalVC?.modalViewRef else { + guard let targetModal = targetModal else { let errorMessage = "Unable to get the matching RNIModalView instance for" + " modalID: \(modalID)"; @@ -99,20 +96,13 @@ class RNIModalViewModule: RCTEventEmitter { }; let modalAction = visibility - ? targetModalView.presentModal - : targetModalView.dismissModal; + ? targetModal.requestModalToShow + : targetModal.requestModalToHide; - try modalAction { + try modalAction(animated, visibility) { // modal dismissed resolve([:]); }; - - #if DEBUG - print( - "Log - RNIModalViewModule.setModalVisibilityByID - Dismissing modal" - + " - target modalID: '\(targetModalView.modalID!)'" - ); - #endif } catch let error as RNIModalError { error.invokePromiseRejectBlock(reject); diff --git a/src/module/ModalViewModule.ts b/src/module/ModalViewModule.ts index bc3e4a87..34f8ee5c 100644 --- a/src/module/ModalViewModule.ts +++ b/src/module/ModalViewModule.ts @@ -1,8 +1,16 @@ import { RNIModalViewModule } from '../native_modules/RNIModalViewModule'; export class ModalViewModule { - static async setModalVisibilityByID(modalID: string, visibility: boolean) { - await RNIModalViewModule.setModalVisibilityByID(modalID, visibility); + static async setModalVisibilityByID( + modalID: string, + visibility: boolean, + animated = true + ) { + await RNIModalViewModule.setModalVisibilityByID( + modalID, + visibility, + animated + ); } static async dismissAllModals(animated = true) { diff --git a/src/native_modules/RNIModalViewModule.ts b/src/native_modules/RNIModalViewModule.ts index 1858140e..5f2db852 100644 --- a/src/native_modules/RNIModalViewModule.ts +++ b/src/native_modules/RNIModalViewModule.ts @@ -11,6 +11,7 @@ interface RNIModalViewModule { setModalVisibilityByID( modalID: string, visibility: boolean, + animated: boolean, ): Promise; // prettier-ignore