From 0ac7808e4b2ff088ca54e1ab3ff637528177a6cd Mon Sep 17 00:00:00 2001 From: Alexander-Bliznyuk Date: Sat, 11 Jul 2020 20:20:34 +0300 Subject: [PATCH 1/2] fix(android): make dialog color options take effect and update API calls --- src/dialogs/dialogs.android.ts | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/dialogs/dialogs.android.ts b/src/dialogs/dialogs.android.ts index 0502b412c..0c89c46a5 100644 --- a/src/dialogs/dialogs.android.ts +++ b/src/dialogs/dialogs.android.ts @@ -82,8 +82,12 @@ function createAlertDialogBuilder(options?: DialogOptions & MDCAlertControlerOpt } function showDialog(dlg: androidx.appcompat.app.AlertDialog, options: DialogOptions & MDCAlertControlerOptions, resolve?: Function) { + dlg.show(); + + const packageName = dlg.getContext().getPackageName(); + if (options.titleColor) { - const textViewId = dlg.getContext().getResources().getIdentifier('android:id/alertTitle', null, null); + const textViewId = dlg.getContext().getResources().getIdentifier('alertTitle', 'id', packageName); if (textViewId) { const tv = dlg.findViewById(textViewId); if (tv) { @@ -91,7 +95,7 @@ function showDialog(dlg: androidx.appcompat.app.AlertDialog, options: DialogOpti } } if (options.messageColor) { - const messageTextViewId = dlg.getContext().getResources().getIdentifier('android:id/message', null, null); + const messageTextViewId = dlg.getContext().getResources().getIdentifier('message', 'id', packageName); if (messageTextViewId) { const messageTextView = dlg.findViewById(messageTextViewId); if (messageTextView) { @@ -128,14 +132,11 @@ function showDialog(dlg: androidx.appcompat.app.AlertDialog, options: DialogOpti // let { color, backgroundColor } = getButtonColors(); if (options.buttonInkColor || options.buttonTitleColor) { - let buttons: android.widget.Button[] = []; - for (let i = 0; i < 3; i++) { - let id = dlg - .getContext() - .getResources() - .getIdentifier('android:id/button' + i, null, null); - buttons[i] = dlg.findViewById(id); - } + let buttons: android.widget.Button[] = [ + dlg.getButton(android.content.DialogInterface.BUTTON_POSITIVE), + dlg.getButton(android.content.DialogInterface.BUTTON_NEGATIVE), + dlg.getButton(android.content.DialogInterface.BUTTON_NEUTRAL) + ]; buttons.forEach((button) => { if (button) { @@ -143,7 +144,6 @@ function showDialog(dlg: androidx.appcompat.app.AlertDialog, options: DialogOpti } }); } - dlg.show(); return dlg; } @@ -203,7 +203,6 @@ function prepareAndCreateAlertDialog(builder: androidx.appcompat.app.AlertDialog dlg.setButton( android.content.DialogInterface.BUTTON_POSITIVE, options.okButtonText, - null, new android.content.DialogInterface.OnClickListener({ onClick: function (dialog: android.content.DialogInterface, id: number) { onDone(true, dialog); @@ -219,7 +218,6 @@ function prepareAndCreateAlertDialog(builder: androidx.appcompat.app.AlertDialog dlg.setButton( android.content.DialogInterface.BUTTON_NEGATIVE, options.cancelButtonText, - null, new android.content.DialogInterface.OnClickListener({ onClick: function (dialog: android.content.DialogInterface, id: number) { onDone(false, dialog); @@ -240,7 +238,6 @@ function prepareAndCreateAlertDialog(builder: androidx.appcompat.app.AlertDialog dlg.setButton( android.content.DialogInterface.BUTTON_NEUTRAL, options.neutralButtonText, - null, new android.content.DialogInterface.OnClickListener({ onClick: function (dialog: android.content.DialogInterface, id: number) { onDone(undefined, dialog); From 9f66242298c47f474785068b48624f8fe5aa99ef Mon Sep 17 00:00:00 2001 From: abliznyuk Date: Mon, 10 Aug 2020 07:11:32 +0300 Subject: [PATCH 2/2] chore: refactoring --- src/dialogs/dialogs.android.ts | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/dialogs/dialogs.android.ts b/src/dialogs/dialogs.android.ts index 0c89c46a5..6e896b9fe 100644 --- a/src/dialogs/dialogs.android.ts +++ b/src/dialogs/dialogs.android.ts @@ -35,6 +35,10 @@ declare module '@nativescript/core/ui/core/view/view' { } } +const BUTTON_POSITIVE: String = android.content.DialogInterface.BUTTON_POSITIVE; +const BUTTON_NEGATIVE: String = android.content.DialogInterface.BUTTON_NEGATIVE; +const BUTTON_NEUTRAL: String = android.content.DialogInterface.BUTTON_NEUTRAL; + function isString(value): value is string { return typeof value === 'string'; } @@ -82,10 +86,14 @@ function createAlertDialogBuilder(options?: DialogOptions & MDCAlertControlerOpt } function showDialog(dlg: androidx.appcompat.app.AlertDialog, options: DialogOptions & MDCAlertControlerOptions, resolve?: Function) { + /** + * dialog should be shown before its subviews configuration, + * because some of them don't exist until dialog has been brought into view + */ dlg.show(); const packageName = dlg.getContext().getPackageName(); - + if (options.titleColor) { const textViewId = dlg.getContext().getResources().getIdentifier('alertTitle', 'id', packageName); if (textViewId) { @@ -133,9 +141,9 @@ function showDialog(dlg: androidx.appcompat.app.AlertDialog, options: DialogOpti if (options.buttonInkColor || options.buttonTitleColor) { let buttons: android.widget.Button[] = [ - dlg.getButton(android.content.DialogInterface.BUTTON_POSITIVE), - dlg.getButton(android.content.DialogInterface.BUTTON_NEGATIVE), - dlg.getButton(android.content.DialogInterface.BUTTON_NEUTRAL) + dlg.getButton(BUTTON_POSITIVE), + dlg.getButton(BUTTON_NEGATIVE), + dlg.getButton(BUTTON_NEUTRAL) ]; buttons.forEach((button) => { @@ -148,7 +156,7 @@ function showDialog(dlg: androidx.appcompat.app.AlertDialog, options: DialogOpti } function prepareAndCreateAlertDialog(builder: androidx.appcompat.app.AlertDialog.Builder, options: ConfirmOptions & MDCAlertControlerOptions, callback?: Function, validationArgs?: (r) => any) { - + // onDismiss will always be called. Prevent calling callback multiple times let onDoneCalled = false; const onDone = function (result: boolean, dialog?: android.content.DialogInterface) { @@ -201,7 +209,7 @@ function prepareAndCreateAlertDialog(builder: androidx.appcompat.app.AlertDialog if (options.okButtonText) { dlg.setButton( - android.content.DialogInterface.BUTTON_POSITIVE, + BUTTON_POSITIVE, options.okButtonText, new android.content.DialogInterface.OnClickListener({ onClick: function (dialog: android.content.DialogInterface, id: number) { @@ -216,7 +224,7 @@ function prepareAndCreateAlertDialog(builder: androidx.appcompat.app.AlertDialog if (options.cancelButtonText) { dlg.setButton( - android.content.DialogInterface.BUTTON_NEGATIVE, + BUTTON_NEGATIVE, options.cancelButtonText, new android.content.DialogInterface.OnClickListener({ onClick: function (dialog: android.content.DialogInterface, id: number) { @@ -236,7 +244,7 @@ function prepareAndCreateAlertDialog(builder: androidx.appcompat.app.AlertDialog if (options.neutralButtonText) { dlg.setButton( - android.content.DialogInterface.BUTTON_NEUTRAL, + BUTTON_NEUTRAL, options.neutralButtonText, new android.content.DialogInterface.OnClickListener({ onClick: function (dialog: android.content.DialogInterface, id: number) {