forked from github/Toast-PhoneGap-Plugin
Merge pull request #79 from ElNinjaGaiden/master
Changes to send same parameters to success callback when the toast hides automatically
This commit is contained in:
commit
ce1dfa79cc
36
README.md
36
README.md
@ -18,9 +18,9 @@ If you like this plugin and want to say thanks please send a PR or donation. Bot
|
||||
1. [Description](#1-description)
|
||||
2. [Screenshots](#2-screenshots)
|
||||
3. [Installation](#3-installation)
|
||||
3. [Automatically (CLI / Plugman)](#automatically-cli--plugman)
|
||||
3. [Manually](#manually)
|
||||
3. [PhoneGap Build](#phonegap-build)
|
||||
3. [Automatically (CLI / Plugman)](#automatically-cli--plugman)
|
||||
3. [Manually](#manually)
|
||||
3. [PhoneGap Build](#phonegap-build)
|
||||
4. [Usage](#4-usage)
|
||||
4. [Styling](#styling)
|
||||
5. [Credits](#5-credits)
|
||||
@ -185,6 +185,24 @@ function hide() {
|
||||
}
|
||||
```
|
||||
|
||||
When the toast gets hidden, your success callback will be called (in case you have defined one) with the `event` property equals to `hide` (more details about the callback in the next section).
|
||||
```js
|
||||
|
||||
window.plugins.toast.showWithOptions({
|
||||
message: 'My message',
|
||||
// More config here...
|
||||
},
|
||||
//Success callback
|
||||
function(args) {
|
||||
console.log(args.event);
|
||||
//This will print 'hide'
|
||||
},
|
||||
function(error) {
|
||||
console.error('toast error: ', error);
|
||||
}
|
||||
);
|
||||
```
|
||||
|
||||
### Receiving a callback when a Toast is tapped
|
||||
On iOS and Android the success handler of your `show` function will be notified (again) when the toast was tapped.
|
||||
|
||||
@ -203,17 +221,21 @@ called again. You can distinguish between those events of course:
|
||||
// implement the success callback
|
||||
function(result) {
|
||||
if (result && result.event) {
|
||||
console.log("The toast was tapped");
|
||||
console.log("Event: " + result.event); // will be defined, with a value of "touch" when it was tapped by the user
|
||||
console.log("The toast was tapped or got hidden, see the value of result.event");
|
||||
console.log("Event: " + result.event); // "touch" when the toast was touched by the user or "hide" when the toast geot hidden
|
||||
console.log("Message: " + result.message); // will be equal to the message you passed in
|
||||
console.log("data.foo: " + result.data.foo); // .. retrieve passed in data here
|
||||
} else {
|
||||
console.log("The toast has been shown");
|
||||
|
||||
if (result.event === 'hide') {
|
||||
console.log("The toast has been shown");
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
```
|
||||
|
||||
The success callback is useful when your toast is binded to a notification id in your backend and you have to mark it as `read` when the toast is done, or to update the notifications counter for iOS. The usage of this will be defined by your application logic. Use the `result.data` object to support your specific logic.
|
||||
|
||||
### Styling
|
||||
Since version 2.4.0 you can pass an optional `styling` object to the plugin.
|
||||
The defaults make sure the Toast looks the same as when you would not pass in the `styling` object at all.
|
||||
|
@ -40,11 +40,14 @@ public class Toast extends CordovaPlugin {
|
||||
// note that webView.isPaused() is not Xwalk compatible, so tracking it poor-man style
|
||||
private boolean isPaused;
|
||||
|
||||
private String currentMessage;
|
||||
private JSONObject currentData;
|
||||
private static CountDownTimer _timer;
|
||||
|
||||
@Override
|
||||
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
|
||||
if (ACTION_HIDE_EVENT.equals(action)) {
|
||||
returnTapEvent("hide", currentMessage, currentData, callbackContext);
|
||||
hide();
|
||||
callbackContext.success();
|
||||
return true;
|
||||
@ -69,6 +72,9 @@ public class Toast extends CordovaPlugin {
|
||||
final JSONObject data = options.has("data") ? options.getJSONObject("data") : null;
|
||||
final JSONObject styling = options.optJSONObject("styling");
|
||||
|
||||
currentMessage = msg;
|
||||
currentData = data;
|
||||
|
||||
cordova.getActivity().runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
int hideAfterMs;
|
||||
@ -173,22 +179,24 @@ public class Toast extends CordovaPlugin {
|
||||
final boolean tapped = tapX >= startX && tapX <= endX &&
|
||||
tapY >= startY && tapY <= endY;
|
||||
|
||||
return tapped && returnTapEvent(msg, data, callbackContext);
|
||||
return tapped && returnTapEvent("touch", msg, data, callbackContext);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
toast.getView().setOnTouchListener(new View.OnTouchListener() {
|
||||
@Override
|
||||
public boolean onTouch(View view, MotionEvent motionEvent) {
|
||||
return motionEvent.getAction() == MotionEvent.ACTION_DOWN && returnTapEvent(msg, data, callbackContext);
|
||||
return motionEvent.getAction() == MotionEvent.ACTION_DOWN && returnTapEvent("touch", msg, 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();}
|
||||
public void onFinish() {
|
||||
returnTapEvent("hide", msg, data, callbackContext);
|
||||
toast.cancel();
|
||||
}
|
||||
}.start();
|
||||
|
||||
mostRecentToast = toast;
|
||||
@ -207,6 +215,7 @@ public class Toast extends CordovaPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void hide() {
|
||||
if (mostRecentToast != null) {
|
||||
mostRecentToast.cancel();
|
||||
@ -217,17 +226,16 @@ public class Toast extends CordovaPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean returnTapEvent(String message, JSONObject data, CallbackContext callbackContext) {
|
||||
private boolean returnTapEvent(String eventName, String message, JSONObject data, CallbackContext callbackContext) {
|
||||
final JSONObject json = new JSONObject();
|
||||
try {
|
||||
json.put("event", "touch");
|
||||
json.put("event", eventName);
|
||||
json.put("message", message);
|
||||
json.put("data", data);
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
callbackContext.success(json);
|
||||
hide();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -185,6 +185,15 @@ static id styling;
|
||||
|
||||
- (void)toastTimerDidFinish:(NSTimer *)timer {
|
||||
[self hideToast:(UIView *)timer.userInfo];
|
||||
|
||||
// also send an event back to JS
|
||||
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:msg, @"message", @"hide", @"event", nil];
|
||||
if (data != nil) {
|
||||
[dict setObject:data forKey:@"data"];
|
||||
}
|
||||
|
||||
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dict];
|
||||
[commandDelegate sendPluginResult:pluginResult callbackId:callbackId];
|
||||
}
|
||||
|
||||
- (void)handleToastTapped:(UITapGestureRecognizer *)recognizer {
|
||||
|
Loading…
Reference in New Issue
Block a user