Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(android,ios): Added option to specify the input type of the prompt dialog + max lenght for prompt dialog (android only) #62

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 46 additions & 11 deletions src/android/Notification.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,13 @@ Licensed to the Apache Software Foundation (ASF) under one
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.text.InputType;
import android.text.InputFilter;
import android.text.method.PasswordTransformationMethod;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.view.Gravity;


/**
Expand Down Expand Up @@ -87,7 +92,7 @@ else if (action.equals("confirm")) {
return true;
}
else if (action.equals("prompt")) {
this.prompt(args.getString(0), args.getString(1), args.getJSONArray(2), args.getString(3), callbackContext);
this.prompt(args.getString(0), args.getString(1), args.getJSONArray(2), args.getString(3), args.getInt(4), args.getInt(5), callbackContext);
return true;
}
else if (action.equals("activityStart")) {
Expand Down Expand Up @@ -268,24 +273,35 @@ public void onCancel(DialogInterface dialog)
* @param title The title of the dialog
* @param buttonLabels A comma separated list of button labels (Up to 3 buttons)
* @param callbackContext The callback context.
* @param inputSyle Textbox input type: 1:text, 2:numeric + 3:Obscured/Password (default: 1).
*/
public synchronized void prompt(final String message, final String title, final JSONArray buttonLabels, final String defaultText, final CallbackContext callbackContext) {
public synchronized void prompt(final String message, final String title, final JSONArray buttonLabels, final String defaultText, final int inputSyle, final int maxLength, final CallbackContext callbackContext) {

final CordovaInterface cordova = this.cordova;

Runnable runnable = new Runnable() {
public void run() {
final EditText promptInput = new EditText(cordova.getActivity());
final EditText promptInput = new EditText(cordova.getActivity());
final LinearLayout promptInputLayout = new LinearLayout(cordova.getActivity());
promptInputLayout.setOrientation(LinearLayout.VERTICAL);
promptInputLayout.setGravity(Gravity.CENTER_HORIZONTAL);
promptInputLayout.setPadding(70, 0, 70, 0);

if (maxLength > 0) {
promptInput.setFilters(new InputFilter[] {new InputFilter.LengthFilter(maxLength)});
}
promptInput.setHint(defaultText);
AlertDialog.Builder dlg = createDialog(cordova); // new AlertDialog.Builder(cordova.getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
promptInputLayout.addView(promptInput);

AlertDialog.Builder dlg = createDialog(cordova);
dlg.setMessage(message);
dlg.setTitle(title);
dlg.setCancelable(true);
dlg.setView(promptInput);

dlg.setView(promptInputLayout);

final JSONObject result = new JSONObject();

// First button
if (buttonLabels.length() > 0) {
try {
Expand All @@ -295,7 +311,7 @@ public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
try {
result.put("buttonIndex",1);
result.put("input1", promptInput.getText().toString().trim().length()==0 ? defaultText : promptInput.getText());
result.put("input1", promptInput.getText().toString().trim().length()==0 ? defaultText : promptInput.getText());
} catch (JSONException e) { e.printStackTrace(); }
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, result));
}
Expand Down Expand Up @@ -347,6 +363,25 @@ public void onCancel(DialogInterface dialog){
}
});

switch (inputSyle) {
case 2:
promptInput.setInputType(InputType.TYPE_CLASS_NUMBER);
break;
case 4:
promptInput.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
promptInput.setTransformationMethod(PasswordTransformationMethod.getInstance());
break;
case 5:
promptInput.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
promptInput.setTransformationMethod(PasswordTransformationMethod.getInstance());
break;
case 1:
case 3:
default:
promptInput.setInputType(InputType.TYPE_CLASS_TEXT);
break;
}

changeTextDirection(dlg);
};
};
Expand Down
91 changes: 53 additions & 38 deletions src/ios/CDVNotification.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Licensed to the Apache Software Foundation (ASF) under one
*/

#import "CDVNotification.h"
#import <Cordova/NSDictionary+Extensions.h>
#import <Cordova/NSArray+Comparisons.h>

#define DIALOG_TYPE_ALERT @"alert"
#define DIALOG_TYPE_PROMPT @"prompt"
Expand All @@ -36,68 +38,68 @@ @implementation CDVNotification
* callbackId The commmand callback id.
* dialogType The type of alert view [alert | prompt].
*/
- (void)showDialogWithMessage:(NSString*)message title:(NSString*)title buttons:(NSArray*)buttons defaultText:(NSString*)defaultText callbackId:(NSString*)callbackId dialogType:(NSString*)dialogType
- (void)showDialogWithMessage:(NSString*)message title:(NSString*)title buttons:(NSArray*)buttons defaultText:(NSString*)defaultText inputStyle:(NSUInteger*)inputStyle callbackId:(NSString*)callbackId dialogType:(NSString*)dialogType
{

NSUInteger count = [buttons count];
#ifdef __IPHONE_8_0
if (NSClassFromString(@"UIAlertController")) {

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];

if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.3) {

CGRect alertFrame = [UIScreen mainScreen].applicationFrame;

if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
// swap the values for the app frame since it is now in landscape
CGFloat temp = alertFrame.size.width;
alertFrame.size.width = alertFrame.size.height;
alertFrame.size.height = temp;
}

alertController.view.frame = alertFrame;
}

for (int n = 0; n < count; n++) {

UIAlertAction* action = [UIAlertAction actionWithTitle:[buttons objectAtIndex:n] style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
{
CDVPluginResult* result;

if ([dialogType isEqualToString:DIALOG_TYPE_PROMPT]) {

NSString* value0 = [[alertController.textFields objectAtIndex:0] text];
NSDictionary* info = @{
@"buttonIndex":@(n + 1),
@"input1":(value0 ? value0 : [NSNull null])
};
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:info];

} else {

result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:(int)(n + 1)];

}

[self.commandDelegate sendPluginResult:result callbackId:callbackId];

}];
[alertController addAction:action];

}

if ([dialogType isEqualToString:DIALOG_TYPE_PROMPT]) {

[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.text = defaultText;
}];
}



[self.viewController presentViewController:alertController animated:YES completion:nil];

} else {
#endif
CDVAlertView* alertView = [[CDVAlertView alloc]
Expand All @@ -106,26 +108,38 @@ - (void)showDialogWithMessage:(NSString*)message title:(NSString*)title buttons:
delegate:self
cancelButtonTitle:nil
otherButtonTitles:nil];

alertView.callbackId = callbackId;


NSUInteger count = [buttons count];

for (int n = 0; n < count; n++) {
[alertView addButtonWithTitle:[buttons objectAtIndex:n]];
}

if ([dialogType isEqualToString:DIALOG_TYPE_PROMPT]) {

// The view style must be assigned before we access the textfield.
if(inputStyle == (uint*)4 || inputStyle == (uint*)5) {
alertView.alertViewStyle = UIAlertViewStyleSecureTextInput;
} else {
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField* textField = [alertView textFieldAtIndex:0];
textField.text = defaultText;
}

UITextField* textField = [alertView textFieldAtIndex:0];
if (inputStyle == (uint*)2 || inputStyle == (uint*)5) {
textField.keyboardType = UIKeyboardTypeNumberPad;
} else {
textField.keyboardType = UIKeyboardTypeAlphabet;
}

textField.text = defaultText;
}

[alertView show];
#ifdef __IPHONE_8_0
}
#endif

}

- (void)alert:(CDVInvokedUrlCommand*)command
Expand All @@ -135,7 +149,7 @@ - (void)alert:(CDVInvokedUrlCommand*)command
NSString* title = [command argumentAtIndex:1];
NSString* buttons = [command argumentAtIndex:2];

[self showDialogWithMessage:message title:title buttons:@[buttons] defaultText:nil callbackId:callbackId dialogType:DIALOG_TYPE_ALERT];
[self showDialogWithMessage:message title:title buttons:@[buttons] defaultText:nil inputStyle:(uint*)1 callbackId:callbackId dialogType:DIALOG_TYPE_ALERT];
}

- (void)confirm:(CDVInvokedUrlCommand*)command
Expand All @@ -145,7 +159,7 @@ - (void)confirm:(CDVInvokedUrlCommand*)command
NSString* title = [command argumentAtIndex:1];
NSArray* buttons = [command argumentAtIndex:2];

[self showDialogWithMessage:message title:title buttons:buttons defaultText:nil callbackId:callbackId dialogType:DIALOG_TYPE_ALERT];
[self showDialogWithMessage:message title:title buttons:buttons defaultText:nil inputStyle:(uint*)1 callbackId:callbackId dialogType:DIALOG_TYPE_ALERT];
}

- (void)prompt:(CDVInvokedUrlCommand*)command
Expand All @@ -155,13 +169,14 @@ - (void)prompt:(CDVInvokedUrlCommand*)command
NSString* title = [command argumentAtIndex:1];
NSArray* buttons = [command argumentAtIndex:2];
NSString* defaultText = [command argumentAtIndex:3];
NSUInteger* inputSyle = (uint*)[[command argumentAtIndex:4] intValue];

[self showDialogWithMessage:message title:title buttons:buttons defaultText:defaultText callbackId:callbackId dialogType:DIALOG_TYPE_PROMPT];
[self showDialogWithMessage:message title:title buttons:buttons defaultText:defaultText inputStyle:inputSyle callbackId:callbackId dialogType:DIALOG_TYPE_PROMPT];
}

/**
* Callback invoked when an alert dialog's buttons are clicked.
*/
/*
* Callback invoked when an alert dialog's buttons are clicked.
*/
- (void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
CDVAlertView* cdvAlertView = (CDVAlertView*)alertView;
Expand Down
17 changes: 10 additions & 7 deletions www/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* specific language governing permissions and limitations
* under the License.
*
*/
*/

var exec = require('cordova/exec');
var platform = require('cordova/platform');
Expand Down Expand Up @@ -63,9 +63,7 @@ module.exports = {
// Some platforms take an array of button label names.
// Other platforms take a comma separated list.
// For compatibility, we convert to the desired type based on the platform.
if (platform.id == "amazon-fireos" || platform.id == "android" || platform.id == "ios" ||
platform.id == "windowsphone" || platform.id == "firefoxos" || platform.id == "ubuntu" ||
platform.id == "windows8" || platform.id == "windows") {
if (platform.id == "amazon-fireos" || platform.id == "android" || platform.id == "ios" || platform.id == "windowsphone" || platform.id == "firefoxos" || platform.id == "ubuntu") {

if (typeof _buttonLabels === 'string') {
_buttonLabels = _buttonLabels.split(","); // not crazy about changing the var type here
Expand All @@ -79,6 +77,7 @@ module.exports = {
exec(resultCallback, null, "Notification", "confirm", [message, _title, _buttonLabels]);
},


/**
* Open a native prompt dialog, with a customizable title and button text.
* The following results are returned to the result callback:
Expand All @@ -90,13 +89,17 @@ module.exports = {
* @param {String} title Title of the dialog (default: "Prompt")
* @param {Array} buttonLabels Array of strings for the button labels (default: ["OK","Cancel"])
* @param {String} defaultText Textbox input value (default: empty string)
* @param {String} inputSyle Textbox input type: 1:text, 2:numeric + 3:Obscured/Password (default: 1)
* @param {String} maxLength Textbox max length: 0:unlimited, other:max length size
*/
prompt: function(message, resultCallback, title, buttonLabels, defaultText) {
prompt: function(message, resultCallback, title, buttonLabels, defaultText, inputSyle, maxLength) {
var _message = (message || "Prompt message");
var _title = (title || "Prompt");
var _buttonLabels = (buttonLabels || ["OK","Cancel"]);
var _defaultText = (defaultText || "");
exec(resultCallback, null, "Notification", "prompt", [_message, _title, _buttonLabels, _defaultText]);
var _inputSyle = (inputSyle || 1);
var _maxLength = (maxLength || 0);
exec(resultCallback, null, "Notification", "prompt", [_message, _title, _buttonLabels, _defaultText, _inputSyle, _maxLength]);
},

/**
Expand All @@ -109,4 +112,4 @@ module.exports = {
var defaultedCount = count || 1;
exec(null, null, "Notification", "beep", [ defaultedCount ]);
}
};
};