#4 Upgrading the JS API to prepare for custom options (no additional options yet, this is a change to make that possible in the future)

This commit is contained in:
eddyverbruggen@gmail.com 2015-04-22 21:51:38 +02:00
parent 75819ee95e
commit 6d4c052a12
4 changed files with 93 additions and 21 deletions

View File

@ -5,6 +5,7 @@ import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/*
// TODO nice way for the Toast plugin to offer a longer delay than the default short and long options
@ -33,13 +34,18 @@ public class Toast extends CordovaPlugin {
return true;
}
final String message = args.getString(0);
final String duration = args.getString(1);
final String position = args.getString(2);
final JSONObject options = args.getJSONObject(0);
final String message = options.getString("message");
final String duration = options.getString("duration");
final String position = options.getString("position");
cordova.getActivity().runOnUiThread(new Runnable() {
public void run() {
android.widget.Toast toast = android.widget.Toast.makeText(webView.getContext(), message, 0);
android.widget.Toast toast = android.widget.Toast.makeText(
webView.getContext(),
message,
"short".equals(duration) ? android.widget.Toast.LENGTH_SHORT : android.widget.Toast.LENGTH_LONG);
if ("top".equals(position)) {
toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 20);
@ -52,15 +58,6 @@ public class Toast extends CordovaPlugin {
return;
}
if ("short".equals(duration)) {
toast.setDuration(android.widget.Toast.LENGTH_SHORT);
} else if ("long".equals(duration)) {
toast.setDuration(android.widget.Toast.LENGTH_LONG);
} else {
callbackContext.error("invalid duration. valid options are 'short' and 'long'");
return;
}
toast.show();
mostRecentToast = toast;
callbackContext.success();

View File

@ -6,9 +6,11 @@
- (void)show:(CDVInvokedUrlCommand*)command {
NSString *message = [command.arguments objectAtIndex:0];
NSString *duration = [command.arguments objectAtIndex:1];
NSString *position = [command.arguments objectAtIndex:2];
NSDictionary* options = [command.arguments objectAtIndex:0];
NSString *message = [options objectForKey:@"message"];
NSString *duration = [options objectForKey:@"duration"];
NSString *position = [options objectForKey:@"position"];
if (![position isEqual: @"top"] && ![position isEqual: @"center"] && ![position isEqual: @"bottom"]) {
CDVPluginResult * pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"invalid position. valid options are 'top', 'center' and 'bottom'"];

View File

@ -1,3 +1,4 @@
using System.Runtime.Serialization;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
@ -26,13 +27,40 @@ namespace WPCordovaClassLib.Cordova.Commands
}
}
[DataContract]
public class ToastOptions
{
[DataMember(IsRequired = true, Name = "message")]
public string message { get; set; }
[DataMember(IsRequired = true, Name = "duration")]
public string duration { get; set; }
[DataMember(IsRequired = true, Name = "position")]
public string position { get; set; }
}
public void show(string options)
{
ToastOptions toastOptions;
string[] args = JSON.JsonHelper.Deserialize<string[]>(options);
var message = args[0];
var duration = args[1];
var position = args[2];
string aliasCurrentCommandCallbackId = args[3];
String jsonOptions = args[0];
try
{
toastOptions = JsonHelper.Deserialize<ToastOptions>(jsonOptions);
}
catch (Exception)
{
DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
return;
}
var message = toastOptions.message;
var duration = toastOptions.duration;
var position = toastOptions.position;
string aliasCurrentCommandCallbackId = args[1];
Deployment.Current.Dispatcher.BeginInvoke(() =>
{

View File

@ -1,8 +1,53 @@
function Toast() {
}
Toast.prototype.optionsBuilder = function () {
// defaults
var message = null;
var duration = "short";
var position = "center";
return {
withMessage: function(m) {
message = m;
return this;
},
withDuration: function(d) {
duration = d;
return this;
},
withPosition: function(p) {
position = p;
return this;
},
build: function() {
return {
message: message,
duration: duration,
position: position
}
}
}
};
Toast.prototype.showWithOptions = function (options, successCallback, errorCallback) {
cordova.exec(successCallback, errorCallback, "Toast", "show", [options]);
};
Toast.prototype.show = function (message, duration, position, successCallback, errorCallback) {
cordova.exec(successCallback, errorCallback, "Toast", "show", [message, duration, position]);
this.showWithOptions(
this.optionsBuilder()
.withMessage(message)
.withDuration(duration)
.withPosition(position)
.build(),
successCallback,
errorCallback);
};
Toast.prototype.showShortTop = function (message, successCallback, errorCallback) {