code improved

This commit is contained in:
Vladimir Enchev
2015-10-28 09:57:39 +02:00
parent c55939286e
commit bd24ecd2f9

View File

@ -104,37 +104,36 @@ function getDialogResult(buttons: allertButtons, index: number) {
return undefined; return undefined;
} }
function addButtonsToAlertController(alertController: UIAlertController, options: dialogs.ConfirmOptions, function addButtonsToAlertController(alertController: UIAlertController, options: dialogs.ConfirmOptions, callback?: Function): void {
okCallback?: Function, cancelCallback?: Function, neutralCallback?: Function): void {
if (!options) { if (!options) {
return; return;
} }
if (types.isString(options.cancelButtonText)) { if (types.isString(options.cancelButtonText)) {
alertController.addAction(UIAlertAction.actionWithTitleStyleHandler(options.cancelButtonText, UIAlertActionStyle.UIAlertActionStyleDefault, (arg: UIAlertAction) => { alertController.addAction(UIAlertAction.actionWithTitleStyleHandler(options.cancelButtonText, UIAlertActionStyle.UIAlertActionStyleDefault, (arg: UIAlertAction) => {
if (types.isFunction(cancelCallback)) { raiseCallback(callback, false);
cancelCallback();
}
})); }));
} }
if (types.isString(options.neutralButtonText)) { if (types.isString(options.neutralButtonText)) {
alertController.addAction(UIAlertAction.actionWithTitleStyleHandler(options.neutralButtonText, UIAlertActionStyle.UIAlertActionStyleDefault, (arg: UIAlertAction) => { alertController.addAction(UIAlertAction.actionWithTitleStyleHandler(options.neutralButtonText, UIAlertActionStyle.UIAlertActionStyleDefault, (arg: UIAlertAction) => {
if (types.isFunction(cancelCallback)) { raiseCallback(callback, undefined);
neutralCallback();
}
})); }));
} }
if (types.isString(options.okButtonText)) { if (types.isString(options.okButtonText)) {
alertController.addAction(UIAlertAction.actionWithTitleStyleHandler(options.okButtonText, UIAlertActionStyle.UIAlertActionStyleDefault, (arg: UIAlertAction) => { alertController.addAction(UIAlertAction.actionWithTitleStyleHandler(options.okButtonText, UIAlertActionStyle.UIAlertActionStyleDefault, (arg: UIAlertAction) => {
if (types.isFunction(okCallback)) { raiseCallback(callback, true);
okCallback();
}
})); }));
} }
} }
function raiseCallback(callback, result) {
if (types.isFunction(callback)) {
callback(result);
}
}
export function alert(arg: any): Promise<void> { export function alert(arg: any): Promise<void> {
return new Promise<void>((resolve, reject) => { return new Promise<void>((resolve, reject) => {
try { try {
@ -194,7 +193,7 @@ export function confirm(arg: any): Promise<boolean> {
} else { } else {
var alertController = UIAlertController.alertControllerWithTitleMessagePreferredStyle(options.title, options.message, UIAlertControllerStyle.UIAlertControllerStyleAlert); var alertController = UIAlertController.alertControllerWithTitleMessagePreferredStyle(options.title, options.message, UIAlertControllerStyle.UIAlertControllerStyleAlert);
addButtonsToAlertController(alertController, options, () => { resolve(true); }, () => { resolve(false) }, () => { resolve(undefined) }); addButtonsToAlertController(alertController, options, (r) => { resolve(r); });
showUIAlertController(alertController); showUIAlertController(alertController);
} }
@ -269,9 +268,7 @@ export function prompt(arg: any): Promise<dialogs.PromptResult> {
textField = alertController.textFields.firstObject; textField = alertController.textFields.firstObject;
addButtonsToAlertController(alertController, options, addButtonsToAlertController(alertController, options,
() => { resolve({ result: true, text: textField.text }); }, (r) => { resolve({ result: r, text: textField.text }); });
() => { resolve({ result: false, text: textField.text }) },
() => { resolve({ result: undefined, text: textField.text }) });
showUIAlertController(alertController); showUIAlertController(alertController);
} }
@ -355,9 +352,16 @@ export function login(arg: any): Promise<dialogs.LoginResult> {
passwordTextField = alertController.textFields.lastObject; passwordTextField = alertController.textFields.lastObject;
addButtonsToAlertController(alertController, options, addButtonsToAlertController(alertController, options,
() => { resolve({ result: true, userName: userNameTextField.text, password: passwordTextField.text }); }, (r) => {
() => { resolve({ result: false, userName: userNameTextField.text, password: passwordTextField.text }); },
() => { resolve({ result: undefined, userName: userNameTextField.text, password: passwordTextField.text }); }); resolve({
result: r,
userName:
userNameTextField.text,
password: passwordTextField.text
});
});
showUIAlertController(alertController); showUIAlertController(alertController);
} }