Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(android): make dialog color options take effect and update API calls #156

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 23 additions & 18 deletions src/dialogs/dialogs.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand Down Expand Up @@ -82,16 +86,24 @@ 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('android:id/alertTitle', null, null);
const textViewId = dlg.getContext().getResources().getIdentifier('alertTitle', 'id', packageName);
if (textViewId) {
const tv = <android.widget.TextView>dlg.findViewById(textViewId);
if (tv) {
tv.setTextColor(options.titleColor.android);
}
}
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 = <android.widget.TextView>dlg.findViewById(messageTextViewId);
if (messageTextView) {
Expand Down Expand Up @@ -128,27 +140,23 @@ 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] = <android.widget.Button>dlg.findViewById(id);
}
let buttons: android.widget.Button[] = [
dlg.getButton(BUTTON_POSITIVE),
dlg.getButton(BUTTON_NEGATIVE),
dlg.getButton(BUTTON_NEUTRAL)
];

buttons.forEach((button) => {
if (button) {
button.setTextColor((options.buttonInkColor || options.buttonTitleColor).android);
}
});
}
dlg.show();
return dlg;
}

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) {
Expand Down Expand Up @@ -201,9 +209,8 @@ function prepareAndCreateAlertDialog(builder: androidx.appcompat.app.AlertDialog

if (options.okButtonText) {
dlg.setButton(
android.content.DialogInterface.BUTTON_POSITIVE,
BUTTON_POSITIVE,
options.okButtonText,
null,
new android.content.DialogInterface.OnClickListener({
onClick: function (dialog: android.content.DialogInterface, id: number) {
onDone(true, dialog);
Expand All @@ -217,9 +224,8 @@ function prepareAndCreateAlertDialog(builder: androidx.appcompat.app.AlertDialog

if (options.cancelButtonText) {
dlg.setButton(
android.content.DialogInterface.BUTTON_NEGATIVE,
BUTTON_NEGATIVE,
options.cancelButtonText,
null,
new android.content.DialogInterface.OnClickListener({
onClick: function (dialog: android.content.DialogInterface, id: number) {
onDone(false, dialog);
Expand All @@ -238,9 +244,8 @@ function prepareAndCreateAlertDialog(builder: androidx.appcompat.app.AlertDialog

if (options.neutralButtonText) {
dlg.setButton(
android.content.DialogInterface.BUTTON_NEUTRAL,
BUTTON_NEUTRAL,
options.neutralButtonText,
null,
new android.content.DialogInterface.OnClickListener({
onClick: function (dialog: android.content.DialogInterface, id: number) {
onDone(undefined, dialog);
Expand Down