Skip to content

Commit

Permalink
💫 Update: setModalVisibilityByID
Browse files Browse the repository at this point in the history
Summary:
* Update to use `RNIModalRequestable` for showing/hiding the modal.
* Add `animated` param. to `RNIModalViewModule.setModalVisibilityByID`.
  • Loading branch information
dominicstop committed May 13, 2023
1 parent 7d3531e commit d92b3dc
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,38 +55,35 @@ 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<String, Any> = [
"modalID": modalID,
"visibility": visibility,
];

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)";
Expand All @@ -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);
Expand Down
12 changes: 10 additions & 2 deletions src/module/ModalViewModule.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
1 change: 1 addition & 0 deletions src/native_modules/RNIModalViewModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface RNIModalViewModule {
setModalVisibilityByID(
modalID: string,
visibility: boolean,
animated: boolean,
): Promise<void>;

// prettier-ignore
Expand Down

0 comments on commit d92b3dc

Please sign in to comment.