Skip to content

Commit b4e7a8f

Browse files
author
Vladimir Enchev
committed
dialogs refactored
1 parent 1fdb2e9 commit b4e7a8f

File tree

3 files changed

+49
-76
lines changed

3 files changed

+49
-76
lines changed

ui/dialogs/dialogs.android.ts

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,27 @@ var STRING = "string",
1111
OK = "OK",
1212
CANCEL = "Cancel";
1313

14-
function createAlertDialog(options: dialogs.DialogOptions): android.app.AlertDialog.Builder {
14+
function createAlertDialog(message: string, options: dialogs.DialogOptions): android.app.AlertDialog.Builder {
1515
var alert = new android.app.AlertDialog.Builder(appmodule.android.foregroundActivity);
16-
alert.setTitle(options.title);
17-
alert.setMessage(options.message);
16+
alert.setTitle(options && options.title ? options.title : "");
17+
alert.setMessage(message);
1818
return alert;
1919
}
2020

2121
function addButtonsToAlertDialog(alert: android.app.AlertDialog.Builder, options: dialogs.ConfirmOptions,
2222
okCallback: Function, cancelCallback?: Function, neutralCallback?: Function): void {
2323

24-
if (options.okButtonName) {
25-
alert.setPositiveButton(options.okButtonName, new android.content.DialogInterface.OnClickListener({
24+
if (options.okButtonText) {
25+
alert.setPositiveButton(options.okButtonText, new android.content.DialogInterface.OnClickListener({
2626
onClick: function (dialog: android.content.DialogInterface, id: number) {
2727
dialog.cancel();
2828
okCallback();
2929
}
3030
}));
3131
}
3232

33-
if (options.cancelButtonName) {
34-
alert.setNegativeButton(options.cancelButtonName, new android.content.DialogInterface.OnClickListener({
33+
if (options.cancelButtonText) {
34+
alert.setNegativeButton(options.cancelButtonText, new android.content.DialogInterface.OnClickListener({
3535
onClick: function (dialog: android.content.DialogInterface, id: number) {
3636
dialog.cancel();
3737
if (cancelCallback) {
@@ -41,8 +41,8 @@ function addButtonsToAlertDialog(alert: android.app.AlertDialog.Builder, options
4141
}));
4242
}
4343

44-
if (options.otherButtonName) {
45-
alert.setNeutralButton(options.otherButtonName, new android.content.DialogInterface.OnClickListener({
44+
if (options.otherButtonText) {
45+
alert.setNeutralButton(options.otherButtonText, new android.content.DialogInterface.OnClickListener({
4646
onClick: function (dialog: android.content.DialogInterface, id: number) {
4747
dialog.cancel();
4848
if (neutralCallback) {
@@ -53,14 +53,12 @@ function addButtonsToAlertDialog(alert: android.app.AlertDialog.Builder, options
5353
}
5454
}
5555

56-
export function alert(arg: any): promises.Promise<void> {
56+
export function alert(message: string, options = { title: ALERT, buttonText: OK }): promises.Promise<void> {
5757
var d = promises.defer<void>();
5858
try {
59-
var options = typeof arg === STRING ? { message: arg, title: ALERT, buttonName: OK } : arg
59+
var alert = createAlertDialog(message, options);
6060

61-
var alert = createAlertDialog(options);
62-
63-
alert.setPositiveButton(options.buttonName, new android.content.DialogInterface.OnClickListener({
61+
alert.setPositiveButton(options.buttonText, new android.content.DialogInterface.OnClickListener({
6462
onClick: function (dialog: android.content.DialogInterface, id: number) {
6563
dialog.cancel();
6664
d.resolve();
@@ -76,12 +74,10 @@ export function alert(arg: any): promises.Promise<void> {
7674
return d.promise();
7775
}
7876

79-
export function confirm(arg: any): promises.Promise<boolean> {
77+
export function confirm(message: string, options = { title: ALERT, okButtonText: OK, cancelButtonText: CANCEL }): promises.Promise<boolean> {
8078
var d = promises.defer<boolean>();
8179
try {
82-
var options = typeof arg === STRING ? { message: arg, title: ALERT, okButtonName: OK, cancelButtonName: CANCEL } : arg
83-
84-
var alert = createAlertDialog(options);
80+
var alert = createAlertDialog(message, options);
8581

8682
addButtonsToAlertDialog(alert, options, function () { d.resolve(true); }, function () { d.resolve(false); }, function () { d.resolve(); });
8783

@@ -94,12 +90,10 @@ export function confirm(arg: any): promises.Promise<boolean> {
9490
return d.promise();
9591
}
9692

97-
export function prompt(arg: any): promises.Promise<dialogs.PromptResult> {
93+
export function prompt(message: string, options = { title: ALERT, okButtonText: OK, cancelButtonText: CANCEL, defaultText: "" }): promises.Promise<dialogs.PromptResult> {
9894
var d = promises.defer<dialogs.PromptResult>();
9995
try {
100-
var options = typeof arg === STRING ? { message: arg, title: ALERT, okButtonName: OK, cancelButtonName: CANCEL } : arg
101-
102-
var alert = createAlertDialog(options);
96+
var alert = createAlertDialog(message, options);
10397

10498
var input = new android.widget.EditText(appmodule.android.context);
10599
input.setText(options.defaultText ? options.defaultText : "");

ui/dialogs/dialogs.d.ts

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,48 +5,28 @@
55
/**
66
* The alert() method displays an alert box with a specified message.
77
* @param message Specifies the text to display in the alert box.
8+
* @param options Specifies the options for the alert box. Optional.
89
*/
9-
function alert(message: string): promises.Promise<void>;
10-
11-
/**
12-
* The alert() method displays an alert box with a specified options.
13-
* @param options Specifies the options for the alert box.
14-
*/
15-
function alert(options: AlertOptions): promises.Promise<void>;
10+
function alert(message: string, options?: AlertOptions): promises.Promise<void>;
1611

1712
/**
1813
* The confirm() method displays a dialog box with a specified message.
1914
* @param message Specifies the text to display in the confirm box.
15+
* @param options Specifies the options for the confirm box. Optional.
2016
*/
21-
function confirm(message: string): promises.Promise<boolean>;
22-
23-
/**
24-
* The confirm() method displays a dialog box with a specified message.
25-
* @param options Specifies the options for the confirm box.
26-
*/
27-
function confirm(options: ConfirmOptions): promises.Promise<boolean>;
17+
function confirm(message: string, options?: ConfirmOptions): promises.Promise<boolean>;
2818

2919
/**
3020
* The prompt() method displays a dialog box that prompts the visitor for input.
3121
* @param message The text to display in the dialog box.
22+
* @param options The options for the dialog box. Optional.
3223
*/
33-
function prompt(message: string): promises.Promise<string>;
34-
35-
/**
36-
* The prompt() method displays a dialog box that prompts the visitor for input.
37-
* @param options The options for the dialog box.
38-
*/
39-
function prompt(options: PromptOptions): promises.Promise<string>;
24+
function prompt(message: string, options?: PromptOptions): promises.Promise<string>;
4025

4126
/**
4227
* Provides options for the dialog.
4328
*/
4429
interface DialogOptions {
45-
/**
46-
* Gets or sets the alert message.
47-
*/
48-
message: string;
49-
5030
/**
5131
* Gets or sets the alert title.
5232
*/
@@ -60,7 +40,7 @@
6040
/**
6141
* Gets or sets the button name.
6242
*/
63-
buttonName?: string;
43+
buttonText?: string;
6444
}
6545

6646
/**
@@ -70,17 +50,17 @@
7050
/**
7151
* Gets or sets the OK button name.
7252
*/
73-
okButtonName?: string;
53+
okButtonText?: string;
7454

7555
/**
7656
* Gets or sets the Cancel button name.
7757
*/
78-
cancelButtonName?: string;
58+
cancelButtonText?: string;
7959

8060
/**
8161
* Gets or sets the Cancel button name.
8262
*/
83-
otherButtonName?: string;
63+
otherButtonText?: string;
8464
}
8565

8666
/**

ui/dialogs/dialogs.ios.ts

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,22 @@ import view = require("ui/core/view");
77

88
var UIALERTVIEWDELEGATE = "UIAlertViewDelegate",
99
STRING = "string",
10+
PROMPT = "Prompt",
11+
CONFIRM = "Confirm",
1012
ALERT = "Alert",
1113
OK = "OK",
1214
CANCEL = "Cancel";
1315

14-
function createUIAlertView(options: dialogs.DialogOptions): UIKit.UIAlertView {
16+
function createUIAlertView(message: string, options: dialogs.DialogOptions): UIKit.UIAlertView {
1517
var alert = new UIKit.UIAlertView();
16-
alert.title = options.title;
17-
alert.message = options.message;
18+
alert.title = options && options.title ? options.title : "";
19+
alert.message = message;
1820
return alert;
1921
}
2022

2123
function createDelegate(callback) {
2224
var delegateType = Foundation.NSObject.extends({}, {}).implements({
23-
protocol: UIALERTVIEWDELEGATE,
25+
protocol: "UIAlertViewDelegate",
2426
implementation: {
2527
alertViewClickedButtonAtIndex: function (view, index) {
2628
callback(view, index);
@@ -31,28 +33,29 @@ function createDelegate(callback) {
3133
}
3234

3335
function addButtonsToAlertDialog(alert: UIKit.UIAlertView, options: dialogs.ConfirmOptions): void {
34-
if (options.okButtonName) {
35-
alert.addButtonWithTitle(options.okButtonName);
36+
if (!options)
37+
return;
38+
39+
if (options.okButtonText) {
40+
alert.addButtonWithTitle(options.okButtonText);
3641
}
3742

38-
if (options.cancelButtonName) {
39-
alert.addButtonWithTitle(options.cancelButtonName);
43+
if (options.cancelButtonText) {
44+
alert.addButtonWithTitle(options.cancelButtonText);
4045
}
4146

42-
if (options.otherButtonName) {
43-
alert.addButtonWithTitle(options.otherButtonName);
47+
if (options.otherButtonText) {
48+
alert.addButtonWithTitle(options.otherButtonText);
4449
}
4550
}
4651

47-
export function alert(arg: any): promises.Promise<void> {
52+
export function alert(message: string, options = { title: ALERT, buttonText: OK }): promises.Promise<void> {
4853
var d = promises.defer<void>();
4954
try {
50-
var options = typeof arg === STRING ? { message: arg, title: ALERT, buttonName: OK } : arg
51-
52-
var alert = createUIAlertView(options);
55+
var alert = createUIAlertView(message, options);
5356

54-
if (options.buttonName) {
55-
alert.addButtonWithTitle(options.buttonName);
57+
if (options.buttonText) {
58+
alert.addButtonWithTitle(options.buttonText);
5659
}
5760

5861
// Assign first to local variable, otherwise it will be garbage collected since delegate is weak reference.
@@ -72,12 +75,10 @@ export function alert(arg: any): promises.Promise<void> {
7275
return d.promise();
7376
}
7477

75-
export function confirm(arg: any): promises.Promise<boolean> {
78+
export function confirm(message: string, options = { title: CONFIRM, okButtonText: OK, cancelButtonText: CANCEL }): promises.Promise<boolean> {
7679
var d = promises.defer<boolean>();
7780
try {
78-
var options = typeof arg === STRING ? { message: arg, title: ALERT, okButtonName: OK, cancelButtonName: CANCEL } : arg
79-
80-
var alert = createUIAlertView(options);
81+
var alert = createUIAlertView(message, options);
8182

8283
addButtonsToAlertDialog(alert, options);
8384

@@ -99,12 +100,10 @@ export function confirm(arg: any): promises.Promise<boolean> {
99100
return d.promise();
100101
}
101102

102-
export function prompt(arg: any): promises.Promise<dialogs.PromptResult> {
103+
export function prompt(message: string, options = { title: PROMPT, okButtonText: OK, cancelButtonText: CANCEL, defaultText: "" }): promises.Promise<dialogs.PromptResult> {
103104
var d = promises.defer<dialogs.PromptResult>();
104105
try {
105-
var options = typeof arg === STRING ? { message: arg, title: ALERT, okButtonName: OK, cancelButtonName: CANCEL } : arg
106-
107-
var alert = createUIAlertView(options);
106+
var alert = createUIAlertView(message, options);
108107
alert.alertViewStyle = UIKit.UIAlertViewStyle.UIAlertViewStylePlainTextInput;
109108

110109
addButtonsToAlertDialog(alert, options);

0 commit comments

Comments
 (0)