Skip to content

Commit

Permalink
🛠 Refactor: Replace dismissModalByID
Browse files Browse the repository at this point in the history
Related:
* `TODO:2023-05-12-14-11-25` - Refactor: Replace `RNIModalViewModule.dismissModalByID` w/ `setModalVisibilityByID`.
* `TODO:2023-05-12-14-40-46` - Refactor: Update  `RNIModalViewModule` functions to use promises.
  • Loading branch information
dominicstop committed May 12, 2023
1 parent 0c8788a commit ebbf886
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ @interface RCT_EXTERN_MODULE(RNIModalViewModule, RCTEventEmitter)
// MARK: - Standalone Functions
// ----------------------------

RCT_EXTERN_METHOD(dismissModalByID: (NSString)modalID
callback: (RCTResponseSenderBlock)callback
);
RCT_EXTERN_METHOD(setModalVisibilityByID: (NSString)modalID
// promise blocks -----------------------
resolve: (RCTPromiseResolveBlock *)resolve
reject : (RCTPromiseRejectBlock *)reject);

RCT_EXTERN_METHOD(dismissAllModals: (BOOL)animated
callback: (RCTResponseSenderBlock)callback
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,76 +54,70 @@ class RNIModalViewModule: RCTEventEmitter {
// MARK: - Module Functions
// ------------------------

// TODO: `TODO:2023-05-12-14-11-25`
// TODO: `TODO:2023-05-12-14-40-46`
@objc func dismissModalByID(
@objc func setModalVisibilityByID(
_ modalID: NSString,
callback: @escaping RCTResponseSenderBlock
// promise blocks ------------------------
resolve: @escaping RCTPromiseResolveBlock,
reject : @escaping RCTPromiseRejectBlock
) {
DispatchQueue.main.async {
let listPresentedVC = RNIUtilities.getPresentedViewControllers();

guard listPresentedVC.count > 0 else {
let errorMessage =
"The list of presented view controllers is empty"
+ " modalID: \(modalID)";

let _ = RNIModalError(
code: .runtimeError,
message: errorMessage,
debugData: [
"modalID": modalID
]
);

callback([false]);
return;
};

let listPresentedModalVC =
listPresentedVC.compactMap { $0 as? RNIModalViewController };

let targetModalVC = listPresentedModalVC.first {
guard let modalID = $0.modalID else { return false };
return modalID == modalID;
};

guard let targetModalView = targetModalVC?.modalViewRef else {
let errorMessage =
"Unable to get the matching RNIModalView instance for"
+ " modalID: \(modalID)";

let _ = RNIModalError(
code: .runtimeError,
message: errorMessage,
debugData: [
"modalID": modalID
]
);

callback([false]);
return;
};

do {
guard listPresentedVC.count > 0 else {
throw RNIModalError(
code: .runtimeError,
message: "The list of presented view controllers is empty",
debugData: [
"modalID": modalID
]
);
};

let listPresentedModalVC =
listPresentedVC.compactMap { $0 as? RNIModalViewController };

let targetModalVC = listPresentedModalVC.first {
guard let modalID = $0.modalID else { return false };
return modalID == modalID;
};

guard let targetModalView = targetModalVC?.modalViewRef else {
let errorMessage =
"Unable to get the matching RNIModalView instance for"
+ " modalID: \(modalID)";

throw RNIModalError(
code: .runtimeError,
message: errorMessage,
debugData: [
"modalID": modalID
]
);
};

try targetModalView.dismissModal {
// modal dismissed
callback([true]);
resolve([:]);
};

#if DEBUG
print(
"Log - RNIModalViewModule.dismissModalByID - Dismissing modal"
"Log - RNIModalViewModule.setModalVisibilityByID - Dismissing modal"
+ " - target modalID: '\(targetModalView.modalID!)'"
);
#endif

// } catch let error as RNIModalError {
// callback([false]);
} catch let error as RNIModalError {
error.invokePromiseRejectBlock(reject);

} catch {
let _ = RNIModalError(code: .unknownError);
callback([false]);
let errorWrapper = RNIModalError(
code: .unknownError,
error: error
);

errorWrapper.invokePromiseRejectBlock(reject);
};
};
};
Expand Down
18 changes: 2 additions & 16 deletions src/module/ModalViewModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,8 @@ import { RNIModalViewModule } from 'src/native_modules/RNIModalViewModule';
import * as Helpers from '../functions/helpers';

export class ModalViewModule {
static dismissModalByID(modalID = '') {
const promise = new Promise((resolve, reject) => {
try {
RNIModalViewModule.dismissModalByID(modalID, (success) => {
(success ? resolve : reject)();
});

// prettier-ignore
} catch (error) {
console.log('RNIModalViewModule, dismissModalByID error:');
console.log(error);
reject(error);
}
});

return Helpers.promiseWithTimeout(1000, promise);
static async setModalVisibilityByID(modalID: string) {
await RNIModalViewModule.setModalVisibilityByID(modalID);
}

static dismissAllModals(animated = true) {
Expand Down
5 changes: 2 additions & 3 deletions src/native_modules/RNIModalViewModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ interface RNIModalViewModule {
// --------------------

// prettier-ignore
dismissModalByID(
setModalVisibilityByID(
modalID: string,
callback: (success: boolean) => void
): void;
): Promise<void>;

// prettier-ignore
dismissAllModals(
Expand Down

0 comments on commit ebbf886

Please sign in to comment.