From bd24ecd2f986ae2e63c03fee43ae0af0986c02f8 Mon Sep 17 00:00:00 2001 From: Vladimir Enchev Date: Wed, 28 Oct 2015 09:57:39 +0200 Subject: [PATCH] code improved --- ui/dialogs/dialogs.ios.ts | 40 +++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/ui/dialogs/dialogs.ios.ts b/ui/dialogs/dialogs.ios.ts index 4881bd9fc..2c452c572 100644 --- a/ui/dialogs/dialogs.ios.ts +++ b/ui/dialogs/dialogs.ios.ts @@ -104,37 +104,36 @@ function getDialogResult(buttons: allertButtons, index: number) { return undefined; } -function addButtonsToAlertController(alertController: UIAlertController, options: dialogs.ConfirmOptions, - okCallback?: Function, cancelCallback?: Function, neutralCallback?: Function): void { +function addButtonsToAlertController(alertController: UIAlertController, options: dialogs.ConfirmOptions, callback?: Function): void { if (!options) { return; } if (types.isString(options.cancelButtonText)) { alertController.addAction(UIAlertAction.actionWithTitleStyleHandler(options.cancelButtonText, UIAlertActionStyle.UIAlertActionStyleDefault, (arg: UIAlertAction) => { - if (types.isFunction(cancelCallback)) { - cancelCallback(); - } + raiseCallback(callback, false); })); } if (types.isString(options.neutralButtonText)) { alertController.addAction(UIAlertAction.actionWithTitleStyleHandler(options.neutralButtonText, UIAlertActionStyle.UIAlertActionStyleDefault, (arg: UIAlertAction) => { - if (types.isFunction(cancelCallback)) { - neutralCallback(); - } + raiseCallback(callback, undefined); })); } if (types.isString(options.okButtonText)) { alertController.addAction(UIAlertAction.actionWithTitleStyleHandler(options.okButtonText, UIAlertActionStyle.UIAlertActionStyleDefault, (arg: UIAlertAction) => { - if (types.isFunction(okCallback)) { - okCallback(); - } + raiseCallback(callback, true); })); } } +function raiseCallback(callback, result) { + if (types.isFunction(callback)) { + callback(result); + } +} + export function alert(arg: any): Promise { return new Promise((resolve, reject) => { try { @@ -194,7 +193,7 @@ export function confirm(arg: any): Promise { } else { 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); } @@ -269,9 +268,7 @@ export function prompt(arg: any): Promise { textField = alertController.textFields.firstObject; addButtonsToAlertController(alertController, options, - () => { resolve({ result: true, text: textField.text }); }, - () => { resolve({ result: false, text: textField.text }) }, - () => { resolve({ result: undefined, text: textField.text }) }); + (r) => { resolve({ result: r, text: textField.text }); }); showUIAlertController(alertController); } @@ -355,9 +352,16 @@ export function login(arg: any): Promise { passwordTextField = alertController.textFields.lastObject; addButtonsToAlertController(alertController, options, - () => { resolve({ result: true, userName: userNameTextField.text, password: passwordTextField.text }); }, - () => { resolve({ result: false, userName: userNameTextField.text, password: passwordTextField.text }); }, - () => { resolve({ result: undefined, userName: userNameTextField.text, password: passwordTextField.text }); }); + (r) => { + + resolve({ + result: r, + userName: + userNameTextField.text, + password: passwordTextField.text + }); + + }); showUIAlertController(alertController); }