Skip to content

Commit b7badb9

Browse files
committed
fix(android): fix error with multiple dialogs showing and the release of the custom view
1 parent 5e52691 commit b7badb9

File tree

1 file changed

+25
-40
lines changed

1 file changed

+25
-40
lines changed

src/dialogs/dialogs.android.ts

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,20 @@ function isString(value): value is string {
4141

4242
function createAlertDialogBuilder(options?: DialogOptions & MDCAlertControlerOptions): androidx.appcompat.app.AlertDialog.Builder {
4343
const activity = androidApp.foregroundActivity || (androidApp.startActivity as globalAndroid.app.Activity);
44-
const alert = new androidx.appcompat.app.AlertDialog.Builder(activity);
45-
alert.setTitle(options && isString(options.title) ? options.title : null);
46-
alert.setMessage(options && isString(options.message) ? options.message : null);
44+
const builder = new androidx.appcompat.app.AlertDialog.Builder(activity);
45+
builder.setTitle(options && isString(options.title) ? options.title : null);
46+
builder.setMessage(options && isString(options.message) ? options.message : null);
4747
if (options.titleIcon) {
48-
alert.setIcon(options.titleIcon.android);
48+
builder.setIcon(options.titleIcon.android);
4949
}
5050
if (options && options.cancelable === false) {
51-
alert.setCancelable(false);
51+
builder.setCancelable(false);
5252
}
5353
if (options.titleIcon) {
54-
alert.setIcon(options.titleIcon.android);
54+
builder.setIcon(options.titleIcon.android);
5555
}
5656
if (options.customTitleView) {
57-
alert.setCustomTitle(options.customTitleView.nativeViewProtected);
57+
builder.setCustomTitle(options.customTitleView.nativeViewProtected);
5858
}
5959
if (options.view) {
6060
const view =
@@ -68,32 +68,20 @@ function createAlertDialogBuilder(options?: DialogOptions & MDCAlertControlerOpt
6868
const modalRootViewCssClasses = getSystemCssClasses();
6969
modalRootViewCssClasses.forEach((c) => view.cssClasses.add(c));
7070

71-
(activity as any)._currentModalCustomView = view;
71+
(builder as any)._currentModalCustomView = view;
7272
view._setupAsRootView(activity);
7373
view._isAddedToNativeVisualTree = true;
7474
view.callLoaded();
7575

7676
// seems necessary to add a frame or the view wont correctly size itself
7777
const frame = new android.widget.FrameLayout(activity);
7878
frame.addView(view.nativeViewProtected);
79-
alert.setView(frame);
79+
builder.setView(frame);
8080
}
81-
return alert;
81+
return builder;
8282
}
8383

8484
function showDialog(dlg: androidx.appcompat.app.AlertDialog, options: DialogOptions & MDCAlertControlerOptions, resolve?: Function) {
85-
const activity = androidApp.foregroundActivity || (androidApp.startActivity as globalAndroid.app.Activity);
86-
if ((activity as any)._currentModalCustomView) {
87-
const view = (activity as any)._currentModalCustomView as View;
88-
const context = options.context || {};
89-
context.closeCallback = function (...originalArgs) {
90-
dlg.dismiss();
91-
if (resolve) {
92-
resolve.apply(this, originalArgs);
93-
}
94-
};
95-
view.bindingContext = fromObject(context);
96-
}
9785
if (options.titleColor) {
9886
const textViewId = dlg.getContext().getResources().getIdentifier('android:id/alertTitle', null, null);
9987
if (textViewId) {
@@ -185,12 +173,12 @@ function prepareAndCreateAlertDialog(builder: androidx.appcompat.app.AlertDialog
185173
new android.content.DialogInterface.OnDismissListener({
186174
onDismiss: function () {
187175
onDone(false);
188-
if ((activity as any)._currentModalCustomView) {
189-
const view = (activity as any)._currentModalCustomView;
176+
if ((builder as any)._currentModalCustomView) {
177+
const view = (builder as any)._currentModalCustomView;
190178
view.callUnloaded();
191179
view._tearDownUI(true);
192180
view._isAddedToNativeVisualTree = false;
193-
(activity as any)._currentModalCustomView = null;
181+
(builder as any)._currentModalCustomView = null;
194182
}
195183
},
196184
})
@@ -199,6 +187,17 @@ function prepareAndCreateAlertDialog(builder: androidx.appcompat.app.AlertDialog
199187
if (!options) {
200188
return dlg;
201189
}
190+
if ((builder as any)._currentModalCustomView) {
191+
const view = (builder as any)._currentModalCustomView as View;
192+
const context = options.context || {};
193+
context.closeCallback = function (...originalArgs) {
194+
dlg.dismiss();
195+
if (callback) {
196+
callback.apply(this, originalArgs);
197+
}
198+
};
199+
view.bindingContext = fromObject(context);
200+
}
202201

203202
if (options.okButtonText) {
204203
dlg.setButton(
@@ -287,22 +286,8 @@ export class AlertDialog {
287286
show() {
288287
if (!this.dialog) {
289288
const alert = createAlertDialogBuilder(this.options);
290-
291-
const activity = androidApp.foregroundActivity || (androidApp.startActivity as globalAndroid.app.Activity);
292-
alert.setOnDismissListener(
293-
new android.content.DialogInterface.OnDismissListener({
294-
onDismiss: function () {
295-
if ((activity as any)._currentModalCustomView) {
296-
const view = (activity as any)._currentModalCustomView;
297-
view.callUnloaded();
298-
view._tearDownUI(true);
299-
view._isAddedToNativeVisualTree = false;
300-
(activity as any)._currentModalCustomView = null;
301-
}
302-
},
303-
})
304-
);
305289
this.dialog = alert.create();
290+
this.dialog = prepareAndCreateAlertDialog(alert, this.options, null);
306291
showDialog(this.dialog, this.options);
307292
}
308293
}

0 commit comments

Comments
 (0)