From 9b9365d9da035950a51c7dedb4aaa970db9d24b3 Mon Sep 17 00:00:00 2001 From: ElNinjaGaiden Date: Fri, 4 Mar 2016 09:24:55 -0600 Subject: [PATCH 1/4] Changes to send same parameters to success callback when the toast hides automatically - no tap --- src/android/nl/xservices/plugins/Toast.java | 13 ++++++++++++- src/ios/Toast+UIView.m | 9 +++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/android/nl/xservices/plugins/Toast.java b/src/android/nl/xservices/plugins/Toast.java index 1232c7a..dc73424 100644 --- a/src/android/nl/xservices/plugins/Toast.java +++ b/src/android/nl/xservices/plugins/Toast.java @@ -52,7 +52,18 @@ public class Toast extends CordovaPlugin { mostRecentToast.cancel(); getViewGroup().setOnTouchListener(null); } - callbackContext.success(); + final JSONObject hideOptions = args.getJSONObject(0); + final String hidemessage = hideOptions.getString("message"); + final JSONObject hideData = hideOptions.has("data") ? hideOptions.getJSONObject("data") : null; + final JSONObject json = new JSONObject(); + try { + json.put("event", "hide"); + json.put("message", hidemessage); + json.put("data", hideData); + } catch (JSONException e) { + e.printStackTrace(); + } + callbackContext.success(json); return true; } else if (ACTION_SHOW_EVENT.equals(action)) { diff --git a/src/ios/Toast+UIView.m b/src/ios/Toast+UIView.m index 8b33055..18c7289 100644 --- a/src/ios/Toast+UIView.m +++ b/src/ios/Toast+UIView.m @@ -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 { From 566f6f7392f024896af0a150dd48d28cdadb4ff4 Mon Sep 17 00:00:00 2001 From: ElNinjaGaiden Date: Fri, 4 Mar 2016 11:26:00 -0600 Subject: [PATCH 2/4] Changes to support hide notification with data on android devices -Since android toasts does not support any kind of listener or callback when the toast hides, the solutions is kind of a hack but it works. --- src/android/nl/xservices/plugins/Toast.java | 44 +++++++++++++-------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/src/android/nl/xservices/plugins/Toast.java b/src/android/nl/xservices/plugins/Toast.java index dc73424..dc151fd 100644 --- a/src/android/nl/xservices/plugins/Toast.java +++ b/src/android/nl/xservices/plugins/Toast.java @@ -45,25 +45,19 @@ 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; + @Override public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException { if (ACTION_HIDE_EVENT.equals(action)) { if (mostRecentToast != null) { mostRecentToast.cancel(); getViewGroup().setOnTouchListener(null); + returnTapEvent("hide", currentMessage, currentData, callbackContext); + mostRecentToast = null; } - final JSONObject hideOptions = args.getJSONObject(0); - final String hidemessage = hideOptions.getString("message"); - final JSONObject hideData = hideOptions.has("data") ? hideOptions.getJSONObject("data") : null; - final JSONObject json = new JSONObject(); - try { - json.put("event", "hide"); - json.put("message", hidemessage); - json.put("data", hideData); - } catch (JSONException e) { - e.printStackTrace(); - } - callbackContext.success(json); + callbackContext.success(); return true; } else if (ACTION_SHOW_EVENT.equals(action)) { @@ -81,6 +75,9 @@ public class Toast extends CordovaPlugin { final JSONObject data = options.has("data") ? options.getJSONObject("data") : null; final JSONObject styling = options.optJSONObject("styling"); + currentMessage = message; + currentData = data; + cordova.getActivity().runOnUiThread(new Runnable() { public void run() { final android.widget.Toast toast = android.widget.Toast.makeText( @@ -173,7 +170,7 @@ public class Toast extends CordovaPlugin { if (tapped) { getViewGroup().setOnTouchListener(null); - return returnTapEvent(message, data, callbackContext); + return returnTapEvent("touch", message, data, callbackContext); } return false; } @@ -182,12 +179,27 @@ public class Toast extends CordovaPlugin { toast.getView().setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { - return motionEvent.getAction() == MotionEvent.ACTION_DOWN && returnTapEvent(message, data, callbackContext); + return motionEvent.getAction() == MotionEvent.ACTION_DOWN && returnTapEvent("touch", message, data, callbackContext); } }); } + Thread thread = new Thread(){ + @Override + public void run() { + try { + Thread.sleep("short".equals(duration) ? 2000 : 3500); + if (mostRecentToast != null) { + returnTapEvent("hide", message, data, callbackContext); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + }; + toast.show(); + thread.start(); mostRecentToast = toast; PluginResult pr = new PluginResult(PluginResult.Status.OK); @@ -203,10 +215,10 @@ 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) { From 0b76cb88112ca77a89c508857decfb79fcec138c Mon Sep 17 00:00:00 2001 From: ElNinjaGaiden Date: Tue, 31 May 2016 11:25:10 -0600 Subject: [PATCH 3/4] Minor bug fix --- src/android/nl/xservices/plugins/Toast.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/android/nl/xservices/plugins/Toast.java b/src/android/nl/xservices/plugins/Toast.java index 201e857..a572481 100644 --- a/src/android/nl/xservices/plugins/Toast.java +++ b/src/android/nl/xservices/plugins/Toast.java @@ -193,7 +193,10 @@ public class Toast extends CordovaPlugin { // 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; From 37a73baa1fee76c2187b67172554472940b6311b Mon Sep 17 00:00:00 2001 From: ElNinjaGaiden Date: Tue, 31 May 2016 18:04:50 -0600 Subject: [PATCH 4/4] Readme update --- README.md | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index c7702a6..3dbedab 100644 --- a/README.md +++ b/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.