Skip to content

Commit 9066166

Browse files
author
Vladimir Enchev
committed
dialogs improved with neutral button and ConfirmResult for confirm and prompt dialogs
1 parent 3e033da commit 9066166

File tree

3 files changed

+101
-33
lines changed

3 files changed

+101
-33
lines changed

ui/dialogs/dialogs.android.ts

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,39 @@ function createAlertDialog(options: dialogs.DialogOptions): android.app.AlertDia
1818
return alert;
1919
}
2020

21-
function addOkCancelButtonsToAlertDialog(alert: android.app.AlertDialog.Builder, options: dialogs.ConfirmOptions,
22-
okCallback: Function, cancelCallback?: Function): void {
23-
alert.setPositiveButton(options.okButtonName, new android.content.DialogInterface.OnClickListener({
24-
onClick: function (dialog: android.content.DialogInterface, id: number) {
25-
dialog.cancel();
26-
okCallback();
27-
}
28-
}));
21+
function addButtonsToAlertDialog(alert: android.app.AlertDialog.Builder, options: dialogs.ConfirmOptions,
22+
okCallback: Function, cancelCallback?: Function, neutralCallback?: Function): void {
2923

30-
alert.setNegativeButton(options.cancelButtonName, new android.content.DialogInterface.OnClickListener({
31-
onClick: function (dialog: android.content.DialogInterface, id: number) {
32-
dialog.cancel();
33-
if (cancelCallback) {
34-
cancelCallback();
24+
if (options.okButtonName) {
25+
alert.setPositiveButton(options.okButtonName, new android.content.DialogInterface.OnClickListener({
26+
onClick: function (dialog: android.content.DialogInterface, id: number) {
27+
dialog.cancel();
28+
okCallback();
3529
}
36-
}
37-
}));
30+
}));
31+
}
32+
33+
if (options.cancelButtonName) {
34+
alert.setNegativeButton(options.cancelButtonName, new android.content.DialogInterface.OnClickListener({
35+
onClick: function (dialog: android.content.DialogInterface, id: number) {
36+
dialog.cancel();
37+
if (cancelCallback) {
38+
cancelCallback();
39+
}
40+
}
41+
}));
42+
}
43+
44+
if (options.otherButtonName) {
45+
alert.setNeutralButton(options.otherButtonName, new android.content.DialogInterface.OnClickListener({
46+
onClick: function (dialog: android.content.DialogInterface, id: number) {
47+
dialog.cancel();
48+
if (neutralCallback) {
49+
neutralCallback();
50+
}
51+
}
52+
}));
53+
}
3854
}
3955

4056
export function alert(arg: any): promises.Promise<void> {
@@ -67,7 +83,7 @@ export function confirm(arg: any): promises.Promise<boolean> {
6783

6884
var alert = createAlertDialog(options);
6985

70-
addOkCancelButtonsToAlertDialog(alert, options, function () { d.resolve(true); }, function () { d.resolve(false); });
86+
addButtonsToAlertDialog(alert, options, function () { d.resolve(true); }, function () { d.resolve(false); }, function () { d.resolve(); });
7187

7288
alert.show();
7389

@@ -78,8 +94,8 @@ export function confirm(arg: any): promises.Promise<boolean> {
7894
return d.promise();
7995
}
8096

81-
export function prompt(arg: any): promises.Promise<string> {
82-
var d = promises.defer<string>();
97+
export function prompt(arg: any): promises.Promise<dialogs.PromptResult> {
98+
var d = promises.defer<dialogs.PromptResult>();
8399
try {
84100
var options = typeof arg === STRING ? { message: arg, title: ALERT, okButtonName: OK, cancelButtonName: CANCEL } : arg
85101

@@ -90,7 +106,11 @@ export function prompt(arg: any): promises.Promise<string> {
90106

91107
alert.setView(input);
92108

93-
addOkCancelButtonsToAlertDialog(alert, options, function () { d.resolve(input.getText().toString()); });
109+
var getText = function () { return input.getText().toString(); };
110+
111+
addButtonsToAlertDialog(alert, options, function () { d.resolve({ result: true, text: getText() }); },
112+
function () { d.resolve({ result: false, text: getText() }); },
113+
function () { d.resolve({ result: undefined, text: getText() }); });
94114

95115
alert.show();
96116

@@ -105,7 +125,7 @@ export class Dialog {
105125
private _dialog: android.app.AlertDialog;
106126
private _android: android.app.AlertDialog.Builder;
107127
private _title: string;
108-
private _view: view.View;
128+
//private _view: view.View;
109129

110130
constructor() {
111131
this._android = new android.app.AlertDialog.Builder(appmodule.android.foregroundActivity);
@@ -122,14 +142,14 @@ export class Dialog {
122142
this._title = value;
123143
this.android.setTitle(this._title);
124144
}
125-
145+
/*
126146
get view(): view.View {
127147
return this._view;
128148
}
129149
set view(value: view.View) {
130150
this._view = value;
131151
this.android.setView(this._view.android);
132-
}
152+
}*/
133153

134154
public show() {
135155
this._dialog = this.android.show();

ui/dialogs/dialogs.d.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@
7676
* Gets or sets the Cancel button name.
7777
*/
7878
cancelButtonName?: string;
79+
80+
/**
81+
* Gets or sets the Cancel button name.
82+
*/
83+
otherButtonName?: string;
7984
}
8085

8186
/**
@@ -88,6 +93,21 @@
8893
defaultText?: string;
8994
}
9095

96+
/**
97+
* Provides result data from the prompt dialog.
98+
*/
99+
interface PromptResult {
100+
/**
101+
* Gets or sets the prompt dialog boolean result.
102+
*/
103+
result: boolean;
104+
105+
/**
106+
* Gets or sets the text entered in the prompt dialog.
107+
*/
108+
text: string;
109+
}
110+
91111
export class Dialog {
92112
/**
93113
* Shows the dialog.

ui/dialogs/dialogs.ios.ts

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44
import promises = require("promises");
55
import dialogs = require("ui/dialogs");
6+
import view = require("ui/core/view");
67

78
var UIALERTVIEWDELEGATE = "UIAlertViewDelegate",
89
STRING = "string",
@@ -29,14 +30,30 @@ function createDelegate(callback) {
2930
return new delegateType;
3031
}
3132

33+
function addButtonsToAlertDialog(alert: UIKit.UIAlertView, options: dialogs.ConfirmOptions): void {
34+
if (options.okButtonName) {
35+
alert.addButtonWithTitle(options.okButtonName);
36+
}
37+
38+
if (options.cancelButtonName) {
39+
alert.addButtonWithTitle(options.cancelButtonName);
40+
}
41+
42+
if (options.otherButtonName) {
43+
alert.addButtonWithTitle(options.otherButtonName);
44+
}
45+
}
46+
3247
export function alert(arg: any): promises.Promise<void> {
3348
var d = promises.defer<void>();
3449
try {
3550
var options = typeof arg === STRING ? { message: arg, title: ALERT, buttonName: OK } : arg
3651

3752
var alert = createUIAlertView(options);
3853

39-
alert.addButtonWithTitle(options.buttonName);
54+
if (options.buttonName) {
55+
alert.addButtonWithTitle(options.buttonName);
56+
}
4057

4158
// Assign first to local variable, otherwise it will be garbage collected since delegate is weak reference.
4259
var delegate = createDelegate(function (view, index) {
@@ -62,12 +79,11 @@ export function confirm(arg: any): promises.Promise<boolean> {
6279

6380
var alert = createUIAlertView(options);
6481

65-
alert.addButtonWithTitle(options.okButtonName);
66-
alert.addButtonWithTitle(options.cancelButtonName);
82+
addButtonsToAlertDialog(alert, options);
6783

6884
// Assign first to local variable, otherwise it will be garbage collected since delegate is weak reference.
6985
var delegate = createDelegate(function (view, index) {
70-
d.resolve(index === 0);
86+
d.resolve(index === 2 ? undefined : index === 0);
7187
// Remove the local variable for the delegate.
7288
delegate = undefined;
7389
});
@@ -83,24 +99,22 @@ export function confirm(arg: any): promises.Promise<boolean> {
8399
return d.promise();
84100
}
85101

86-
export function prompt(arg: any): promises.Promise<string> {
87-
var d = promises.defer<string>();
102+
export function prompt(arg: any): promises.Promise<dialogs.PromptResult> {
103+
var d = promises.defer<dialogs.PromptResult>();
88104
try {
89105
var options = typeof arg === STRING ? { message: arg, title: ALERT, okButtonName: OK, cancelButtonName: CANCEL } : arg
90106

91107
var alert = createUIAlertView(options);
92108
alert.alertViewStyle = UIKit.UIAlertViewStyle.UIAlertViewStylePlainTextInput;
93-
alert.addButtonWithTitle(options.okButtonName);
94-
alert.addButtonWithTitle(options.cancelButtonName);
109+
110+
addButtonsToAlertDialog(alert, options);
95111

96112
var textField = alert.textFieldAtIndex(0);
97113
textField.text = options.defaultText ? options.defaultText : "";
98114

99115
// Assign first to local variable, otherwise it will be garbage collected since delegate is weak reference.
100116
var delegate = createDelegate(function (view, index) {
101-
if (index === 0) {
102-
d.resolve(textField.text);
103-
}
117+
d.resolve({ result: index === 2 ? undefined : index === 0, text: textField.text });
104118
// Remove the local variable for the delegate.
105119
delegate = undefined;
106120
});
@@ -118,6 +132,8 @@ export function prompt(arg: any): promises.Promise<string> {
118132

119133
export class Dialog {
120134
private _ios: UIKit.UIAlertView;
135+
//private _view: view.View;
136+
//private _nativeView: UIKit.UIView;
121137

122138
constructor() {
123139
this._ios = new UIKit.UIAlertView();
@@ -133,6 +149,18 @@ export class Dialog {
133149
set title(value: string) {
134150
this.ios.title = value;
135151
}
152+
/*
153+
get view(): view.View {
154+
return this._view;
155+
}
156+
set view(value: view.View) {
157+
this._view = value;
158+
this._nativeView = this._view.ios;
159+
this._nativeView.removeFromSuperview();
160+
161+
// Not working on iOS7!
162+
this.ios.addSubview(this._nativeView);
163+
}*/
136164

137165
public show() {
138166
this.ios.show();

0 commit comments

Comments
 (0)