Skip to content

Commit d1eb283

Browse files
committed
fix(dialogs): AlertDialog hide is now async to allow to wait for dialog to be hidden
Especially useful on iOS to prevent errors like “already transitioning”
1 parent aec1784 commit d1eb283

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

src/dialogs/dialogs.android.ts

+16-5
Original file line numberDiff line numberDiff line change
@@ -296,18 +296,29 @@ export function alert(arg: any): Promise<void> {
296296
export class AlertDialog {
297297
dialog: androidx.appcompat.app.AlertDialog;
298298
constructor(private options: any) {}
299-
show(resolve?) {
299+
onCloseListeners: any[] = [];
300+
onClosed() {
301+
this.onCloseListeners.forEach((l) => l());
302+
this.onCloseListeners = [];
303+
}
304+
show(onClosed?) {
300305
if (!this.dialog) {
301306
const alert = createAlertDialogBuilder(this.options);
302307
this.dialog = alert.create();
303-
this.dialog = prepareAndCreateAlertDialog(alert, this.options, resolve);
308+
this.dialog = prepareAndCreateAlertDialog(alert, this.options, () => {
309+
this.onClosed();
310+
onClosed?.();
311+
});
304312
showDialog(this.dialog, this.options);
305313
}
306314
}
307-
hide() {
315+
async hide() {
308316
if (this.dialog) {
309-
this.dialog.cancel();
310-
this.dialog = null;
317+
return new Promise((resolve) => {
318+
this.onCloseListeners.push(resolve);
319+
this.dialog.cancel();
320+
this.dialog = null;
321+
});
311322
}
312323
}
313324
}

src/dialogs/dialogs.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,5 @@ export function action(options: ActionOptions & MDCAlertControlerOptions): Promi
124124
export class AlertDialog {
125125
constructor(options: AlertOptions & MDCAlertControlerOptions);
126126
show(resolve?);
127-
hide();
127+
async hide();
128128
}

src/dialogs/dialogs.ios.ts

+13-10
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ function createAlertController(options: DialogOptions & MDCAlertControlerOptions
174174
options.view instanceof View
175175
? options.view
176176
: Builder.createViewFromEntry({
177-
moduleName: options.view as string
178-
});
177+
moduleName: options.view as string
178+
});
179179

180180
view.cssClasses.add(CSSUtils.MODAL_ROOT_VIEW_CSS_CLASS);
181181
const modalRootViewCssClasses = CSSUtils.getSystemCssClasses();
@@ -246,17 +246,20 @@ export class AlertDialog {
246246
alertController: MDCAlertController;
247247
presentingController: UIViewController;
248248
constructor(private options: any) {}
249-
show(resolve?) {
249+
250+
show(onClosed?) {
250251
if (!this.alertController) {
251-
this.alertController = createAlertController(this.options, resolve);
252+
this.alertController = createAlertController(this.options, onClosed);
252253
this.presentingController = showUIAlertController(this.alertController, this.options);
253254
}
254255
}
255-
hide() {
256+
async hide() {
256257
if (this.presentingController) {
257-
this.presentingController.dismissViewControllerAnimatedCompletion(true, null);
258-
this.presentingController = null;
259-
this.alertController = null;
258+
return new Promise<void>((resolve) => {
259+
this.presentingController.dismissViewControllerAnimatedCompletion(true, resolve);
260+
this.presentingController = null;
261+
this.alertController = null;
262+
});
260263
}
261264
}
262265
}
@@ -271,8 +274,8 @@ export function confirm(arg: any): Promise<boolean> {
271274
};
272275
const options = !isDialogOptions(arg)
273276
? Object.assign(defaultOptions, {
274-
message: arg + ''
275-
})
277+
message: arg + ''
278+
})
276279
: Object.assign(defaultOptions, arg);
277280
const alertController = createAlertController(options, resolve);
278281

0 commit comments

Comments
 (0)