forked from github/Toast-PhoneGap-Plugin
#59 Add specific duration of the toast
This commit is contained in:
parent
4fb9932c9b
commit
fe1aed17e3
@ -141,7 +141,7 @@ Toast.js is brought in automatically. There is no need to change or add anything
|
|||||||
### Showing a Toast
|
### Showing a Toast
|
||||||
You have two choices to make when showing a Toast: where to show it and for how long.
|
You have two choices to make when showing a Toast: where to show it and for how long.
|
||||||
* show(message, duration, position)
|
* show(message, duration, position)
|
||||||
* duration: 'short', 'long'
|
* duration: 'short', 'long', '3000', 900 (the latter are milliseconds)
|
||||||
* position: 'top', 'center', 'bottom'
|
* position: 'top', 'center', 'bottom'
|
||||||
|
|
||||||
You can also use any of these convenience methods:
|
You can also use any of these convenience methods:
|
||||||
@ -167,7 +167,7 @@ function showBottom() {
|
|||||||
window.plugins.toast.showWithOptions(
|
window.plugins.toast.showWithOptions(
|
||||||
{
|
{
|
||||||
message: "hey there",
|
message: "hey there",
|
||||||
duration: "short",
|
duration: "short", // which is 2000 ms. "long" is 4000. Or specify the nr of ms yourself.
|
||||||
position: "bottom",
|
position: "bottom",
|
||||||
addPixelsY: -40 // added a negative value to move it up a bit (default 0)
|
addPixelsY: -40 // added a negative value to move it up a bit (default 0)
|
||||||
},
|
},
|
||||||
@ -196,7 +196,7 @@ called again. You can distinguish between those events of course:
|
|||||||
window.plugins.toast.showWithOptions(
|
window.plugins.toast.showWithOptions(
|
||||||
{
|
{
|
||||||
message: "hey there",
|
message: "hey there",
|
||||||
duration: "short",
|
duration: 1500, // ms
|
||||||
position: "bottom",
|
position: "bottom",
|
||||||
addPixelsY: -40, // (optional) added a negative value to move it up a bit (default 0)
|
addPixelsY: -40, // (optional) added a negative value to move it up a bit (default 0)
|
||||||
data: {'foo':'bar'} // (optional) pass in a JSON object here (it will be sent back in the success callback below)
|
data: {'foo':'bar'} // (optional) pass in a JSON object here (it will be sent back in the success callback below)
|
||||||
@ -224,7 +224,7 @@ Note that on WP this object is currently ignored.
|
|||||||
```js
|
```js
|
||||||
window.plugins.toast.showWithOptions({
|
window.plugins.toast.showWithOptions({
|
||||||
message: "hey there",
|
message: "hey there",
|
||||||
duration: "short",
|
duration: "short", // 2000 ms
|
||||||
position: "bottom",
|
position: "bottom",
|
||||||
styling: {
|
styling: {
|
||||||
opacity: 0.75, // 0.0 (transparent) to 1.0 (opaque). Default 0.8
|
opacity: 0.75, // 0.0 (transparent) to 1.0 (opaque). Default 0.8
|
||||||
@ -254,6 +254,7 @@ The Android code was entirely created by me.
|
|||||||
For iOS most credits go to this excellent [Toast for iOS project by Charles Scalesse] (https://github.com/scalessec/Toast).
|
For iOS most credits go to this excellent [Toast for iOS project by Charles Scalesse] (https://github.com/scalessec/Toast).
|
||||||
|
|
||||||
## 6. CHANGELOG
|
## 6. CHANGELOG
|
||||||
|
- 2.5.0: By popular demand: Specify the duration of the Toast on iOS and Android. Pass in `short` (2000ms), `long` (4000ms), or any nr of milliseconds: `900`.
|
||||||
- 2.4.2: You can now also set the Toast `opacity` for iOS.
|
- 2.4.2: You can now also set the Toast `opacity` for iOS.
|
||||||
- 2.4.1: As an addition to 2.4.0, [Sino](https://github.com/SinoBoeckmann) added the option to change the text color!
|
- 2.4.1: As an addition to 2.4.0, [Sino](https://github.com/SinoBoeckmann) added the option to change the text color!
|
||||||
- 2.4.0: You can now style the Toast with a number of properties. See
|
- 2.4.0: You can now style the Toast with a number of properties. See
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "cordova-plugin-x-toast",
|
"name": "cordova-plugin-x-toast",
|
||||||
"version": "2.4.2",
|
"version": "2.5.0",
|
||||||
"description": "This plugin allows you to show a Toast. A Toast is a little non intrusive buttonless popup which automatically disappears.",
|
"description": "This plugin allows you to show a Toast. A Toast is a little non intrusive buttonless popup which automatically disappears.",
|
||||||
"cordova": {
|
"cordova": {
|
||||||
"id": "cordova-plugin-x-toast",
|
"id": "cordova-plugin-x-toast",
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
|
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
id="cordova-plugin-x-toast"
|
id="cordova-plugin-x-toast"
|
||||||
version="2.4.2">
|
version="2.5.0">
|
||||||
|
|
||||||
<name>Toast</name>
|
<name>Toast</name>
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ package nl.xservices.plugins;
|
|||||||
import android.graphics.Color;
|
import android.graphics.Color;
|
||||||
import android.graphics.drawable.GradientDrawable;
|
import android.graphics.drawable.GradientDrawable;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
|
import android.os.CountDownTimer;
|
||||||
import android.view.Gravity;
|
import android.view.Gravity;
|
||||||
import android.view.MotionEvent;
|
import android.view.MotionEvent;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
@ -16,16 +17,6 @@ import org.json.JSONArray;
|
|||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
/*
|
|
||||||
// TODO nice way for the Toast plugin to offer a longer delay than the default short and long options
|
|
||||||
// TODO also look at https://github.com/JohnPersano/Supertoasts
|
|
||||||
new CountDownTimer(6000, 1000) {
|
|
||||||
public void onTick(long millisUntilFinished) {toast.show();}
|
|
||||||
public void onFinish() {toast.show();}
|
|
||||||
}.start();
|
|
||||||
|
|
||||||
Also, check https://github.com/JohnPersano/SuperToasts
|
|
||||||
*/
|
|
||||||
public class Toast extends CordovaPlugin {
|
public class Toast extends CordovaPlugin {
|
||||||
|
|
||||||
private static final String ACTION_SHOW_EVENT = "show";
|
private static final String ACTION_SHOW_EVENT = "show";
|
||||||
@ -40,29 +31,24 @@ public class Toast extends CordovaPlugin {
|
|||||||
private android.widget.Toast mostRecentToast;
|
private android.widget.Toast mostRecentToast;
|
||||||
private ViewGroup viewGroup;
|
private ViewGroup viewGroup;
|
||||||
|
|
||||||
private static final boolean IS_AT_LEAST_LOLLIPOP = Build.VERSION.SDK_INT >= 21;
|
|
||||||
|
|
||||||
// note that webView.isPaused() is not Xwalk compatible, so tracking it poor-man style
|
// note that webView.isPaused() is not Xwalk compatible, so tracking it poor-man style
|
||||||
private boolean isPaused;
|
private boolean isPaused;
|
||||||
|
|
||||||
|
private static CountDownTimer _timer;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
|
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
|
||||||
if (ACTION_HIDE_EVENT.equals(action)) {
|
if (ACTION_HIDE_EVENT.equals(action)) {
|
||||||
if (mostRecentToast != null) {
|
hide();
|
||||||
mostRecentToast.cancel();
|
|
||||||
getViewGroup().setOnTouchListener(null);
|
|
||||||
}
|
|
||||||
callbackContext.success();
|
callbackContext.success();
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
} else if (ACTION_SHOW_EVENT.equals(action)) {
|
} else if (ACTION_SHOW_EVENT.equals(action)) {
|
||||||
|
|
||||||
if (this.isPaused) {
|
if (this.isPaused) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
final JSONObject options = args.getJSONObject(0);
|
final JSONObject options = args.getJSONObject(0);
|
||||||
|
|
||||||
final String message = options.getString("message");
|
final String message = options.getString("message");
|
||||||
final String duration = options.getString("duration");
|
final String duration = options.getString("duration");
|
||||||
final String position = options.getString("position");
|
final String position = options.getString("position");
|
||||||
@ -72,10 +58,20 @@ public class Toast extends CordovaPlugin {
|
|||||||
|
|
||||||
cordova.getActivity().runOnUiThread(new Runnable() {
|
cordova.getActivity().runOnUiThread(new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
|
int hideAfterMs;
|
||||||
|
if ("short".equalsIgnoreCase(duration)) {
|
||||||
|
hideAfterMs = 2000;
|
||||||
|
} else if ("long".equalsIgnoreCase(duration)) {
|
||||||
|
hideAfterMs = 4000;
|
||||||
|
} else {
|
||||||
|
// assuming a number of ms
|
||||||
|
hideAfterMs = Integer.parseInt(duration);
|
||||||
|
}
|
||||||
final android.widget.Toast toast = android.widget.Toast.makeText(
|
final android.widget.Toast toast = android.widget.Toast.makeText(
|
||||||
IS_AT_LEAST_LOLLIPOP ? cordova.getActivity().getWindow().getContext() : cordova.getActivity().getApplicationContext(),
|
Build.VERSION.SDK_INT >= 21 ? cordova.getActivity().getWindow().getContext() : cordova.getActivity().getApplicationContext(),
|
||||||
message,
|
message,
|
||||||
"short".equals(duration) ? android.widget.Toast.LENGTH_SHORT : android.widget.Toast.LENGTH_LONG);
|
android.widget.Toast.LENGTH_LONG // actually controlled by a timer further down
|
||||||
|
);
|
||||||
|
|
||||||
if ("top".equals(position)) {
|
if ("top".equals(position)) {
|
||||||
toast.setGravity(GRAVITY_TOP, 0, BASE_TOP_BOTTOM_OFFSET + addPixelsY);
|
toast.setGravity(GRAVITY_TOP, 0, BASE_TOP_BOTTOM_OFFSET + addPixelsY);
|
||||||
@ -117,67 +113,60 @@ public class Toast extends CordovaPlugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// On Android >= 5 you can no longer rely on the 'toast.getView().setOnTouchListener',
|
// On newer Android devices you can no longer rely on the 'toast.getView().setOnTouchListener',
|
||||||
// so created something funky that compares the Toast position to the tap coordinates.
|
// so created something funky that compares the Toast position to the tap coordinates.
|
||||||
if (IS_AT_LEAST_LOLLIPOP) {
|
getViewGroup().setOnTouchListener(new View.OnTouchListener() {
|
||||||
getViewGroup().setOnTouchListener(new View.OnTouchListener() {
|
@Override
|
||||||
@Override
|
public boolean onTouch(View view, MotionEvent motionEvent) {
|
||||||
public boolean onTouch(View view, MotionEvent motionEvent) {
|
if (motionEvent.getAction() != MotionEvent.ACTION_DOWN) {
|
||||||
if (motionEvent.getAction() != MotionEvent.ACTION_DOWN) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (mostRecentToast == null || !mostRecentToast.getView().isShown()) {
|
|
||||||
getViewGroup().setOnTouchListener(null);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
float w = mostRecentToast.getView().getWidth();
|
|
||||||
float startX = (view.getWidth() / 2) - (w / 2);
|
|
||||||
float endX = (view.getWidth() / 2) + (w / 2);
|
|
||||||
|
|
||||||
float startY;
|
|
||||||
float endY;
|
|
||||||
|
|
||||||
float g = mostRecentToast.getGravity();
|
|
||||||
float y = mostRecentToast.getYOffset();
|
|
||||||
float h = mostRecentToast.getView().getHeight();
|
|
||||||
|
|
||||||
if (g == GRAVITY_BOTTOM) {
|
|
||||||
startY = view.getHeight() - y - h;
|
|
||||||
endY = view.getHeight() - y;
|
|
||||||
} else if (g == GRAVITY_CENTER) {
|
|
||||||
startY = (view.getHeight() / 2) + y - (h / 2);
|
|
||||||
endY = (view.getHeight() / 2) + y + (h / 2);
|
|
||||||
} else {
|
|
||||||
// top
|
|
||||||
startY = y;
|
|
||||||
endY = y + h;
|
|
||||||
}
|
|
||||||
|
|
||||||
float tapX = motionEvent.getX();
|
|
||||||
float tapY = motionEvent.getY();
|
|
||||||
|
|
||||||
final boolean tapped = tapX >= startX && tapX <= endX &&
|
|
||||||
tapY >= startY && tapY <= endY;
|
|
||||||
|
|
||||||
if (tapped) {
|
|
||||||
getViewGroup().setOnTouchListener(null);
|
|
||||||
return returnTapEvent(message, data, callbackContext);
|
|
||||||
}
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
});
|
if (mostRecentToast == null || !mostRecentToast.getView().isShown()) {
|
||||||
} else {
|
getViewGroup().setOnTouchListener(null);
|
||||||
toast.getView().setOnTouchListener(new View.OnTouchListener() {
|
return false;
|
||||||
@Override
|
|
||||||
public boolean onTouch(View view, MotionEvent motionEvent) {
|
|
||||||
return motionEvent.getAction() == MotionEvent.ACTION_DOWN && returnTapEvent(message, data, callbackContext);
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
toast.show();
|
float w = mostRecentToast.getView().getWidth();
|
||||||
|
float startX = (view.getWidth() / 2) - (w / 2);
|
||||||
|
float endX = (view.getWidth() / 2) + (w / 2);
|
||||||
|
|
||||||
|
float startY;
|
||||||
|
float endY;
|
||||||
|
|
||||||
|
float g = mostRecentToast.getGravity();
|
||||||
|
float y = mostRecentToast.getYOffset();
|
||||||
|
float h = mostRecentToast.getView().getHeight();
|
||||||
|
|
||||||
|
if (g == GRAVITY_BOTTOM) {
|
||||||
|
startY = view.getHeight() - y - h;
|
||||||
|
endY = view.getHeight() - y;
|
||||||
|
} else if (g == GRAVITY_CENTER) {
|
||||||
|
startY = (view.getHeight() / 2) + y - (h / 2);
|
||||||
|
endY = (view.getHeight() / 2) + y + (h / 2);
|
||||||
|
} else {
|
||||||
|
// top
|
||||||
|
startY = y;
|
||||||
|
endY = y + h;
|
||||||
|
}
|
||||||
|
|
||||||
|
float tapX = motionEvent.getX();
|
||||||
|
float tapY = motionEvent.getY();
|
||||||
|
|
||||||
|
final boolean tapped = tapX >= startX && tapX <= endX &&
|
||||||
|
tapY >= startY && tapY <= endY;
|
||||||
|
|
||||||
|
return tapped && returnTapEvent(message, data, callbackContext);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// trigger show every 2500 ms for as long as the requested duration
|
||||||
|
_timer = new CountDownTimer(hideAfterMs, 2500) {
|
||||||
|
public void onTick(long millisUntilFinished) {toast.show();}
|
||||||
|
public void onFinish() {toast.cancel();}
|
||||||
|
}.start();
|
||||||
|
|
||||||
mostRecentToast = toast;
|
mostRecentToast = toast;
|
||||||
|
toast.show();
|
||||||
|
|
||||||
PluginResult pr = new PluginResult(PluginResult.Status.OK);
|
PluginResult pr = new PluginResult(PluginResult.Status.OK);
|
||||||
pr.setKeepCallback(true);
|
pr.setKeepCallback(true);
|
||||||
@ -192,6 +181,16 @@ public class Toast extends CordovaPlugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void hide() {
|
||||||
|
if (mostRecentToast != null) {
|
||||||
|
mostRecentToast.cancel();
|
||||||
|
getViewGroup().setOnTouchListener(null);
|
||||||
|
}
|
||||||
|
if (_timer != null) {
|
||||||
|
_timer.cancel();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private boolean returnTapEvent(String message, JSONObject data, CallbackContext callbackContext) {
|
private boolean returnTapEvent(String message, JSONObject data, CallbackContext callbackContext) {
|
||||||
final JSONObject json = new JSONObject();
|
final JSONObject json = new JSONObject();
|
||||||
try {
|
try {
|
||||||
@ -202,6 +201,7 @@ public class Toast extends CordovaPlugin {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
callbackContext.success(json);
|
callbackContext.success(json);
|
||||||
|
hide();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -215,10 +215,7 @@ public class Toast extends CordovaPlugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPause(boolean multitasking) {
|
public void onPause(boolean multitasking) {
|
||||||
if (mostRecentToast != null) {
|
hide();
|
||||||
mostRecentToast.cancel();
|
|
||||||
getViewGroup().setOnTouchListener(null);
|
|
||||||
}
|
|
||||||
this.isPaused = true;
|
this.isPaused = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,9 +5,7 @@
|
|||||||
@implementation Toast
|
@implementation Toast
|
||||||
|
|
||||||
- (void)show:(CDVInvokedUrlCommand*)command {
|
- (void)show:(CDVInvokedUrlCommand*)command {
|
||||||
|
|
||||||
NSDictionary* options = [command argumentAtIndex:0];
|
NSDictionary* options = [command argumentAtIndex:0];
|
||||||
|
|
||||||
NSString *message = options[@"message"];
|
NSString *message = options[@"message"];
|
||||||
NSString *duration = options[@"duration"];
|
NSString *duration = options[@"duration"];
|
||||||
NSString *position = options[@"position"];
|
NSString *position = options[@"position"];
|
||||||
@ -21,19 +19,17 @@
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
NSInteger durationInt;
|
NSTimeInterval durationMS;
|
||||||
if ([duration isEqual: @"short"]) {
|
if ([duration.lowercaseString isEqualToString: @"short"]) {
|
||||||
durationInt = 2;
|
durationMS = 2000;
|
||||||
} else if ([duration isEqual: @"long"]) {
|
} else if ([duration.lowercaseString isEqualToString: @"long"]) {
|
||||||
durationInt = 5;
|
durationMS = 4000;
|
||||||
} else {
|
} else {
|
||||||
CDVPluginResult * pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"invalid duration. valid options are 'short' and 'long'"];
|
durationMS = [duration intValue];
|
||||||
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[self.webView makeToast:message
|
[self.webView makeToast:message
|
||||||
duration:durationInt
|
duration:durationMS / 1000
|
||||||
position:position
|
position:position
|
||||||
addPixelsY:addPixelsY == nil ? 0 : [addPixelsY intValue]
|
addPixelsY:addPixelsY == nil ? 0 : [addPixelsY intValue]
|
||||||
data:data
|
data:data
|
||||||
|
@ -126,10 +126,10 @@ namespace WPCordovaClassLib.Cordova.Commands
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int hideDelay = 2800;
|
int hideDelay = 2500;
|
||||||
if ("long".Equals(duration))
|
if ("long".Equals(duration))
|
||||||
{
|
{
|
||||||
hideDelay = 5500;
|
hideDelay = 5000;
|
||||||
}
|
}
|
||||||
else if (!"short".Equals(duration))
|
else if (!"short".Equals(duration))
|
||||||
{
|
{
|
||||||
|
@ -16,7 +16,7 @@ Toast.prototype.optionsBuilder = function () {
|
|||||||
},
|
},
|
||||||
|
|
||||||
withDuration: function(d) {
|
withDuration: function(d) {
|
||||||
duration = d;
|
duration = d.toString();
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -43,6 +43,7 @@ Toast.prototype.optionsBuilder = function () {
|
|||||||
|
|
||||||
|
|
||||||
Toast.prototype.showWithOptions = function (options, successCallback, errorCallback) {
|
Toast.prototype.showWithOptions = function (options, successCallback, errorCallback) {
|
||||||
|
options.duration = (options.duration === undefined ? 'long' : options.duration.toString());
|
||||||
cordova.exec(successCallback, errorCallback, "Toast", "show", [options]);
|
cordova.exec(successCallback, errorCallback, "Toast", "show", [options]);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user