forked from github/Toast-PhoneGap-Plugin
commit
be56435c26
@ -2,7 +2,7 @@
|
||||
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
id="nl.x-services.plugins.toast"
|
||||
version="2.0.4">
|
||||
version="2.0.5">
|
||||
|
||||
<name>Toast</name>
|
||||
|
||||
|
@ -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();
|
||||
|
@ -70,7 +70,7 @@ static UIView *prevToast = NULL;
|
||||
|
||||
- (void)makeToast:(NSString *)message duration:(NSTimeInterval)duration position:(id)position {
|
||||
UIView *toast = [self viewForMessage:message title:nil image:nil];
|
||||
[self showToast:toast duration:duration position:position];
|
||||
[self showToast:toast duration:duration position:position];
|
||||
}
|
||||
|
||||
- (void)makeToast:(NSString *)message duration:(NSTimeInterval)duration position:(id)position title:(NSString *)title {
|
||||
@ -106,8 +106,9 @@ static UIView *prevToast = NULL;
|
||||
toast.userInteractionEnabled = YES;
|
||||
toast.exclusiveTouch = YES;
|
||||
}
|
||||
|
||||
[self addSubview:toast];
|
||||
|
||||
// make sure that if InAppBrowser is active, we're still showing Toasts on top of it
|
||||
[self.superview.superview addSubview:toast];
|
||||
|
||||
[UIView animateWithDuration:CSToastFadeDuration
|
||||
delay:0.0
|
||||
|
@ -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'"];
|
||||
|
@ -1,3 +1,5 @@
|
||||
using System;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
@ -26,13 +28,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 = JSON.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(() =>
|
||||
{
|
||||
|
47
www/Toast.js
47
www/Toast.js
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user