You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ag...@apache.org on 2013/03/21 04:08:39 UTC

[1/2] ios commit: CB-2674 Add prompt to Notification API for iOS

Updated Branches:
  refs/heads/master 411440d32 -> e9e67d0b9


CB-2674 Add prompt to Notification API for iOS

- adds native code corresponding to JS added in CB-2679


Project: http://git-wip-us.apache.org/repos/asf/cordova-ios/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-ios/commit/0b0b4317
Tree: http://git-wip-us.apache.org/repos/asf/cordova-ios/tree/0b0b4317
Diff: http://git-wip-us.apache.org/repos/asf/cordova-ios/diff/0b0b4317

Branch: refs/heads/master
Commit: 0b0b4317dcf03ac69429fe33ef8dc51b00605ec0
Parents: 411440d
Author: James Jong <wj...@gmail.com>
Authored: Mon Mar 18 13:45:28 2013 -0400
Committer: Andrew Grieve <ag...@chromium.org>
Committed: Wed Mar 20 22:56:37 2013 -0400

----------------------------------------------------------------------
 CordovaLib/Classes/CDVNotification.h |    1 +
 CordovaLib/Classes/CDVNotification.m |   65 +++++++++++++++++++++++++----
 2 files changed, 58 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-ios/blob/0b0b4317/CordovaLib/Classes/CDVNotification.h
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVNotification.h b/CordovaLib/Classes/CDVNotification.h
index 1eedb54..5b5b89f 100644
--- a/CordovaLib/Classes/CDVNotification.h
+++ b/CordovaLib/Classes/CDVNotification.h
@@ -26,6 +26,7 @@
 
 - (void)alert:(CDVInvokedUrlCommand*)command;
 - (void)confirm:(CDVInvokedUrlCommand*)command;
+- (void)prompt:(CDVInvokedUrlCommand*)command;
 - (void)vibrate:(CDVInvokedUrlCommand*)command;
 
 @end

http://git-wip-us.apache.org/repos/asf/cordova-ios/blob/0b0b4317/CordovaLib/Classes/CDVNotification.m
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVNotification.m b/CordovaLib/Classes/CDVNotification.m
index 5c2dfda..b74eea0 100644
--- a/CordovaLib/Classes/CDVNotification.m
+++ b/CordovaLib/Classes/CDVNotification.m
@@ -20,9 +20,21 @@
 #import "CDVNotification.h"
 #import "NSDictionary+Extensions.h"
 
+#define DIALOG_TYPE_ALERT @"alert"
+#define DIALOG_TYPE_PROMPT @"prompt"
+
 @implementation CDVNotification
 
-- (void)showDialogWithMessage:(NSString*)message title:(NSString*)title buttons:(NSArray*)buttons callbackId:(NSString*)callbackId
+/*
+ * showDialogWithMessage - Common method to instantiate the alert view for alert, confirm, and prompt notifications.
+ * Parameters:
+ *  message       The alert view message.
+ *  title         The alert view title.
+ *  buttons       The array of customized strings for the buttons.
+ *  callbackId    The commmand callback id.
+ *  dialogType    The type of alert view [alert | prompt].
+ */
+- (void)showDialogWithMessage:(NSString*)message title:(NSString*)title buttons:(NSArray*)buttons callbackId:(NSString*)callbackId dialogType:(NSString*)dialogType
 {
     CDVAlertView* alertView = [[CDVAlertView alloc]
         initWithTitle:title
@@ -39,6 +51,10 @@
         [alertView addButtonWithTitle:[buttons objectAtIndex:n]];
     }
 
+    if ([dialogType isEqualToString:DIALOG_TYPE_PROMPT]) {
+        alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
+    }
+
     [alertView show];
 }
 
@@ -59,7 +75,7 @@
         buttons = NSLocalizedString(@"OK", @"OK");
     }
 
-    [self showDialogWithMessage:message title:title buttons:@[buttons] callbackId:callbackId];
+    [self showDialogWithMessage:message title:title buttons:@[buttons] callbackId:callbackId dialogType:DIALOG_TYPE_ALERT];
 }
 
 - (void)confirm:(CDVInvokedUrlCommand*)command
@@ -79,18 +95,51 @@
         buttons = @[NSLocalizedString(@"OK", @"OK"), NSLocalizedString(@"Cancel", @"Cancel")];
     }
 
-    [self showDialogWithMessage:message title:title buttons:buttons callbackId:callbackId];
+    [self showDialogWithMessage:message title:title buttons:buttons callbackId:callbackId dialogType:DIALOG_TYPE_ALERT];
+}
+
+- (void)prompt:(CDVInvokedUrlCommand*)command
+{
+    NSString* callbackId = command.callbackId;
+    NSArray* arguments = command.arguments;
+    int argc = [arguments count];
+
+    NSString* message = argc > 0 ? [arguments objectAtIndex:0] : nil;
+    NSString* title = argc > 1 ? [arguments objectAtIndex:1] : nil;
+    NSArray* buttons = argc > 2 ? [arguments objectAtIndex:2] : nil;
+
+    if (!message) {
+        title = NSLocalizedString(@"Prompt message", @"Prompt message");
+    }
+    if (!title) {
+        title = NSLocalizedString(@"Prompt", @"Prompt");
+    }
+    if (!buttons) {
+        buttons = @[NSLocalizedString(@"OK", @"OK"), NSLocalizedString(@"Cancel", @"Cancel")];
+    }
+    [self showDialogWithMessage:message title:title buttons:buttons callbackId:callbackId dialogType:DIALOG_TYPE_PROMPT];
 }
 
 /**
- Callback invoked when an alert dialog's buttons are clicked.
- Passes the index + label back to JS
- */
+  * Callback invoked when an alert dialog's buttons are clicked.
+  */
 - (void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
 {
     CDVAlertView* cdvAlertView = (CDVAlertView*)alertView;
-    CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:++buttonIndex];
-
+    CDVPluginResult* result;
+
+    // Determine what gets returned to JS based on the alert view type.
+    if (alertView.alertViewStyle == UIAlertViewStyleDefault) {
+        // For alert and confirm, return button index as int back to JS.
+        result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:++buttonIndex];
+    } else {
+        // For prompt, return button index and input text back to JS.
+        NSString* value0 = [[alertView textFieldAtIndex:0] text];
+        NSMutableDictionary* info = [NSMutableDictionary dictionaryWithCapacity:3];
+        [info setValue:[NSNumber numberWithInt:++buttonIndex] forKey:@"buttonIndex"];
+        [info setValue:value0 ? value0:[NSNull null] forKey:@"input1"];
+        result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:info];
+    }
     [self.commandDelegate sendPluginResult:result callbackId:cdvAlertView.callbackId];
 }
 


[2/2] ios commit: Remove some unnecessary argument checks in CDVNotification

Posted by ag...@apache.org.
Remove some unnecessary argument checks in CDVNotification

The JS ensures that all the params are present, and that they are not
nil.


Project: http://git-wip-us.apache.org/repos/asf/cordova-ios/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-ios/commit/e9e67d0b
Tree: http://git-wip-us.apache.org/repos/asf/cordova-ios/tree/e9e67d0b
Diff: http://git-wip-us.apache.org/repos/asf/cordova-ios/diff/e9e67d0b

Branch: refs/heads/master
Commit: e9e67d0b976ca69b5374fd6bb67e052032fe66b0
Parents: 0b0b431
Author: Andrew Grieve <ag...@chromium.org>
Authored: Wed Mar 20 23:07:34 2013 -0400
Committer: Andrew Grieve <ag...@chromium.org>
Committed: Wed Mar 20 23:07:34 2013 -0400

----------------------------------------------------------------------
 CordovaLib/Classes/CDVNotification.m |   59 +++++++----------------------
 1 files changed, 14 insertions(+), 45 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-ios/blob/e9e67d0b/CordovaLib/Classes/CDVNotification.m
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVNotification.m b/CordovaLib/Classes/CDVNotification.m
index b74eea0..821cb9f 100644
--- a/CordovaLib/Classes/CDVNotification.m
+++ b/CordovaLib/Classes/CDVNotification.m
@@ -61,19 +61,9 @@
 - (void)alert:(CDVInvokedUrlCommand*)command
 {
     NSString* callbackId = command.callbackId;
-    NSArray* arguments = command.arguments;
-    int argc = [arguments count];
-
-    NSString* message = argc > 0 ? [arguments objectAtIndex:0] : nil;
-    NSString* title = argc > 1 ? [arguments objectAtIndex:1] : nil;
-    NSString* buttons = argc > 2 ? [arguments objectAtIndex:2] : nil;
-
-    if (!title) {
-        title = NSLocalizedString(@"Alert", @"Alert");
-    }
-    if (!buttons) {
-        buttons = NSLocalizedString(@"OK", @"OK");
-    }
+    NSString* message = [command argumentAtIndex:0];
+    NSString* title = [command argumentAtIndex:1];
+    NSString* buttons = [command argumentAtIndex:2];
 
     [self showDialogWithMessage:message title:title buttons:@[buttons] callbackId:callbackId dialogType:DIALOG_TYPE_ALERT];
 }
@@ -81,19 +71,9 @@
 - (void)confirm:(CDVInvokedUrlCommand*)command
 {
     NSString* callbackId = command.callbackId;
-    NSArray* arguments = command.arguments;
-    int argc = [arguments count];
-
-    NSString* message = argc > 0 ? [arguments objectAtIndex:0] : nil;
-    NSString* title = argc > 1 ? [arguments objectAtIndex:1] : nil;
-    NSArray* buttons = argc > 2 ? [arguments objectAtIndex:2] : nil;
-
-    if (!title) {
-        title = NSLocalizedString(@"Confirm", @"Confirm");
-    }
-    if (!buttons) {
-        buttons = @[NSLocalizedString(@"OK", @"OK"), NSLocalizedString(@"Cancel", @"Cancel")];
-    }
+    NSString* message = [command argumentAtIndex:0];
+    NSString* title = [command argumentAtIndex:1];
+    NSArray* buttons = [command argumentAtIndex:2];
 
     [self showDialogWithMessage:message title:title buttons:buttons callbackId:callbackId dialogType:DIALOG_TYPE_ALERT];
 }
@@ -101,22 +81,10 @@
 - (void)prompt:(CDVInvokedUrlCommand*)command
 {
     NSString* callbackId = command.callbackId;
-    NSArray* arguments = command.arguments;
-    int argc = [arguments count];
-
-    NSString* message = argc > 0 ? [arguments objectAtIndex:0] : nil;
-    NSString* title = argc > 1 ? [arguments objectAtIndex:1] : nil;
-    NSArray* buttons = argc > 2 ? [arguments objectAtIndex:2] : nil;
+    NSString* message = [command argumentAtIndex:0];
+    NSString* title = [command argumentAtIndex:1];
+    NSArray* buttons = [command argumentAtIndex:2];
 
-    if (!message) {
-        title = NSLocalizedString(@"Prompt message", @"Prompt message");
-    }
-    if (!title) {
-        title = NSLocalizedString(@"Prompt", @"Prompt");
-    }
-    if (!buttons) {
-        buttons = @[NSLocalizedString(@"OK", @"OK"), NSLocalizedString(@"Cancel", @"Cancel")];
-    }
     [self showDialogWithMessage:message title:title buttons:buttons callbackId:callbackId dialogType:DIALOG_TYPE_PROMPT];
 }
 
@@ -131,13 +99,14 @@
     // Determine what gets returned to JS based on the alert view type.
     if (alertView.alertViewStyle == UIAlertViewStyleDefault) {
         // For alert and confirm, return button index as int back to JS.
-        result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:++buttonIndex];
+        result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:buttonIndex + 1];
     } else {
         // For prompt, return button index and input text back to JS.
         NSString* value0 = [[alertView textFieldAtIndex:0] text];
-        NSMutableDictionary* info = [NSMutableDictionary dictionaryWithCapacity:3];
-        [info setValue:[NSNumber numberWithInt:++buttonIndex] forKey:@"buttonIndex"];
-        [info setValue:value0 ? value0:[NSNull null] forKey:@"input1"];
+        NSDictionary* info = @{
+            @"buttonIndex":@(buttonIndex + 1),
+            @"input1":(value0 ? value0 : [NSNull null])
+        };
         result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:info];
     }
     [self.commandDelegate sendPluginResult:result callbackId:cdvAlertView.callbackId];