Skip to content

Commit

Permalink
🛠 Refactor: Update dismissAllModals
Browse files Browse the repository at this point in the history
Related:
* `TODO:2023-05-12-14-40-46` - Refactor: Update `RNIModalViewModule` functions to use promises.
  • Loading branch information
dominicstop committed May 13, 2023
1 parent ab15df5 commit 5282bfb
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ @interface RCT_EXTERN_MODULE(RNIModalViewModule, RCTEventEmitter)
reject : (RCTPromiseRejectBlock *)reject);

RCT_EXTERN_METHOD(dismissAllModals: (BOOL)animated
callback: (RCTResponseSenderBlock)callback
);
// promise blocks ------------------------
resolve: (RCTPromiseResolveBlock *)resolve
reject : (RCTPromiseRejectBlock *)reject);

// MARK: - View-Related Functions
// ------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,34 @@ class RNIModalViewModule: RCTEventEmitter {
};
};

// TODO: `TODO:2023-05-12-14-40-46`
@objc func dismissAllModals(
_ animated: Bool,
callback: @escaping RCTResponseSenderBlock
// promise blocks ------------------------
resolve: @escaping RCTPromiseResolveBlock,
reject : @escaping RCTPromiseRejectBlock
) {
DispatchQueue.main.async {
let success: Void? = UIWindow.key?
.rootViewController?
.dismiss(animated: animated, completion: nil);
let windows = RNIUtilities.getWindows();
let rootViewControllers = windows.map { $0.rootViewController };

callback([success != nil]);
guard rootViewControllers.isEmpty else {
let error = RNIModalError(
code: .runtimeError,
message: "Could not get root view controllers"
);

error.invokePromiseRejectBlock(reject);
return;
};

rootViewControllers.enumerated().forEach {
let isLast = $0.offset == rootViewControllers.count - 1;

$0.element?.dismiss(animated: animated) {
guard isLast else { return };
resolve([:]);
};
};
};
};

Expand Down
19 changes: 2 additions & 17 deletions src/module/ModalViewModule.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,11 @@
import { RNIModalViewModule } from '../native_modules/RNIModalViewModule';
import * as Helpers from '../functions/helpers';

export class ModalViewModule {
static async setModalVisibilityByID(modalID: string) {
await RNIModalViewModule.setModalVisibilityByID(modalID);
}

static dismissAllModals(animated = true) {
const promise = new Promise((resolve, reject) => {
try {
RNIModalViewModule.dismissAllModals(animated, (success) => {
(success ? resolve : reject)();
});

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

return Helpers.promiseWithTimeout(1000, promise);
static async dismissAllModals(animated = true) {
await RNIModalViewModule.dismissAllModals(animated);
}
}
3 changes: 1 addition & 2 deletions src/native_modules/RNIModalViewModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ interface RNIModalViewModule {
// prettier-ignore
dismissAllModals(
animated: boolean,
callback: (success: boolean) => void
): void;
): Promise<void>;

// View-Related Functions
// ----------------------
Expand Down

0 comments on commit 5282bfb

Please sign in to comment.