Skip to content

Commit bc812fd

Browse files
committed
chore(dialogs): only access native class when necessary
1 parent 5f32427 commit bc812fd

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

src/dialogs/dialogs.android.ts

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@ function isString(value): value is string {
2525
return typeof value === 'string';
2626
}
2727

28-
const DialogInterface = android.content.DialogInterface;
29-
const MaterialAlertDialogBuilder = com.google.android.material.dialog.MaterialAlertDialogBuilder;
28+
let DialogInterface: typeof android.content.DialogInterface;
29+
let MaterialAlertDialogBuilder: typeof com.google.android.material.dialog.MaterialAlertDialogBuilder;
3030

3131
function createAlertDialogBuilder(options?: DialogOptions & MDCAlertControlerOptions) {
3232
const activity = Application.android.foregroundActivity || (Application.android.startActivity as globalAndroid.app.Activity);
33+
if (!MaterialAlertDialogBuilder) {
34+
MaterialAlertDialogBuilder = com.google.android.material.dialog.MaterialAlertDialogBuilder;
35+
}
3336
const builder = new MaterialAlertDialogBuilder(activity);
3437
builder.setTitle(options && isString(options.title) ? options.title : null);
3538
builder.setMessage(options && isString(options.message) ? options.message : null);
@@ -38,6 +41,9 @@ function createAlertDialogBuilder(options?: DialogOptions & MDCAlertControlerOpt
3841
}
3942
if (options && options.cancelable === false) {
4043
builder.setCancelable(false);
44+
if (!DialogInterface) {
45+
DialogInterface = android.content.DialogInterface;
46+
}
4147
builder.setOnKeyListener(
4248
new DialogInterface.OnKeyListener({
4349
onKey(dialog, keyCode, event) {
@@ -55,8 +61,8 @@ function createAlertDialogBuilder(options?: DialogOptions & MDCAlertControlerOpt
5561
options.view instanceof View
5662
? options.view
5763
: Builder.createViewFromEntry({
58-
moduleName: options.view as string
59-
});
64+
moduleName: options.view as string
65+
});
6066

6167
view.cssClasses.add(CSSUtils.MODAL_ROOT_VIEW_CSS_CLASS);
6268
const modalRootViewCssClasses = CSSUtils.getSystemCssClasses();
@@ -165,7 +171,9 @@ function prepareAndCreateAlertDialog(
165171
}
166172
callback && callback(result);
167173
};
168-
174+
if (!DialogInterface) {
175+
DialogInterface = android.content.DialogInterface;
176+
}
169177
builder.setOnDismissListener(
170178
new DialogInterface.OnDismissListener({
171179
onDismiss() {
@@ -197,6 +205,9 @@ function prepareAndCreateAlertDialog(
197205
}
198206

199207
if (options.okButtonText) {
208+
if (!DialogInterface) {
209+
DialogInterface = android.content.DialogInterface;
210+
}
200211
dlg.setButton(
201212
DialogInterface.BUTTON_POSITIVE,
202213
options.okButtonText,
@@ -213,6 +224,9 @@ function prepareAndCreateAlertDialog(
213224
}
214225

215226
if (options.cancelButtonText) {
227+
if (!DialogInterface) {
228+
DialogInterface = android.content.DialogInterface;
229+
}
216230
dlg.setButton(
217231
DialogInterface.BUTTON_NEGATIVE,
218232
options.cancelButtonText,
@@ -234,6 +248,9 @@ function prepareAndCreateAlertDialog(
234248
}
235249

236250
if (options.neutralButtonText) {
251+
if (!DialogInterface) {
252+
DialogInterface = android.content.DialogInterface;
253+
}
237254
dlg.setButton(
238255
DialogInterface.BUTTON_NEUTRAL,
239256
options.neutralButtonText,
@@ -306,8 +323,8 @@ export function confirm(arg: any): Promise<boolean> {
306323
defaultOptions,
307324
!isDialogOptions(arg)
308325
? {
309-
message: arg + ''
310-
}
326+
message: arg + ''
327+
}
311328
: arg
312329
);
313330
const alert = createAlertDialogBuilder(options);
@@ -522,6 +539,9 @@ export function action(arg: any): Promise<string> {
522539
return new Promise<string>((resolve, reject) => {
523540
try {
524541
const activity = Application.android.foregroundActivity || (Application.android.startActivity as globalAndroid.app.Activity);
542+
if (!MaterialAlertDialogBuilder) {
543+
MaterialAlertDialogBuilder = com.google.android.material.dialog.MaterialAlertDialogBuilder;
544+
}
525545
const alert = new MaterialAlertDialogBuilder(activity);
526546
const message = options && isString(options.message) ? options.message : '';
527547
const title = options && isString(options.title) ? options.title : '';
@@ -539,6 +559,9 @@ export function action(arg: any): Promise<string> {
539559
}
540560

541561
if (options.actions) {
562+
if (!DialogInterface) {
563+
DialogInterface = android.content.DialogInterface;
564+
}
542565
alert.setItems(
543566
options.actions,
544567
new DialogInterface.OnClickListener({

0 commit comments

Comments
 (0)