From fe1aed17e36408e4e6a03346802f6e33e468da06 Mon Sep 17 00:00:00 2001 From: EddyVerbruggen Date: Thu, 17 Mar 2016 21:23:19 +0100 Subject: [PATCH 1/7] #59 Add specific duration of the toast --- README.md | 9 +- package.json | 2 +- plugin.xml | 2 +- src/android/nl/xservices/plugins/Toast.java | 161 ++++++++++---------- src/ios/Toast.m | 28 ++-- src/wp8/Toast.cs | 4 +- www/Toast.js | 3 +- 7 files changed, 102 insertions(+), 107 deletions(-) diff --git a/README.md b/README.md index 99639ab..c7fbe36 100644 --- a/README.md +++ b/README.md @@ -141,7 +141,7 @@ Toast.js is brought in automatically. There is no need to change or add anything ### Showing a Toast You have two choices to make when showing a Toast: where to show it and for how long. * show(message, duration, position) -* duration: 'short', 'long' +* duration: 'short', 'long', '3000', 900 (the latter are milliseconds) * position: 'top', 'center', 'bottom' You can also use any of these convenience methods: @@ -167,7 +167,7 @@ function showBottom() { window.plugins.toast.showWithOptions( { message: "hey there", - duration: "short", + duration: "short", // which is 2000 ms. "long" is 4000. Or specify the nr of ms yourself. position: "bottom", 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( { message: "hey there", - duration: "short", + duration: 1500, // ms position: "bottom", 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) @@ -224,7 +224,7 @@ Note that on WP this object is currently ignored. ```js window.plugins.toast.showWithOptions({ message: "hey there", - duration: "short", + duration: "short", // 2000 ms position: "bottom", styling: { 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). ## 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.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 diff --git a/package.json b/package.json index 1bcaea1..43a53df 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "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.", "cordova": { "id": "cordova-plugin-x-toast", diff --git a/plugin.xml b/plugin.xml index d79a2d3..1134fd0 100755 --- a/plugin.xml +++ b/plugin.xml @@ -2,7 +2,7 @@ + version="2.5.0"> Toast diff --git a/src/android/nl/xservices/plugins/Toast.java b/src/android/nl/xservices/plugins/Toast.java index 1232c7a..3b43b98 100644 --- a/src/android/nl/xservices/plugins/Toast.java +++ b/src/android/nl/xservices/plugins/Toast.java @@ -3,6 +3,7 @@ package nl.xservices.plugins; import android.graphics.Color; import android.graphics.drawable.GradientDrawable; import android.os.Build; +import android.os.CountDownTimer; import android.view.Gravity; import android.view.MotionEvent; import android.view.View; @@ -16,16 +17,6 @@ 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 - // 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 { private static final String ACTION_SHOW_EVENT = "show"; @@ -40,29 +31,24 @@ public class Toast extends CordovaPlugin { private android.widget.Toast mostRecentToast; 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 private boolean isPaused; + private static CountDownTimer _timer; + @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); - } + hide(); callbackContext.success(); return true; } else if (ACTION_SHOW_EVENT.equals(action)) { - if (this.isPaused) { return true; } final JSONObject options = args.getJSONObject(0); - final String message = options.getString("message"); final String duration = options.getString("duration"); final String position = options.getString("position"); @@ -72,10 +58,20 @@ public class Toast extends CordovaPlugin { cordova.getActivity().runOnUiThread(new Runnable() { 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( - IS_AT_LEAST_LOLLIPOP ? cordova.getActivity().getWindow().getContext() : cordova.getActivity().getApplicationContext(), + Build.VERSION.SDK_INT >= 21 ? cordova.getActivity().getWindow().getContext() : cordova.getActivity().getApplicationContext(), 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)) { toast.setGravity(GRAVITY_TOP, 0, BASE_TOP_BOTTOM_OFFSET + addPixelsY); @@ -104,11 +100,11 @@ public class Toast extends CordovaPlugin { shape.setAlpha((int)(opacity * 255)); // 0-255, where 0 is an invisible background shape.setColor(Color.parseColor(backgroundColor)); toast.getView().setBackground(shape); - + final TextView toastTextView; toastTextView = (TextView) toast.getView().findViewById(android.R.id.message); toastTextView.setTextColor(Color.parseColor(textColor)); - + toast.getView().setPadding(horizontalPadding, verticalPadding, horizontalPadding, verticalPadding); // this gives the toast a very subtle shadow on newer devices @@ -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. - if (IS_AT_LEAST_LOLLIPOP) { - getViewGroup().setOnTouchListener(new View.OnTouchListener() { - @Override - public boolean onTouch(View view, MotionEvent motionEvent) { - 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); - } + getViewGroup().setOnTouchListener(new View.OnTouchListener() { + @Override + public boolean onTouch(View view, MotionEvent motionEvent) { + if (motionEvent.getAction() != MotionEvent.ACTION_DOWN) { return false; } - }); - } else { - toast.getView().setOnTouchListener(new View.OnTouchListener() { - @Override - public boolean onTouch(View view, MotionEvent motionEvent) { - return motionEvent.getAction() == MotionEvent.ACTION_DOWN && returnTapEvent(message, data, callbackContext); + if (mostRecentToast == null || !mostRecentToast.getView().isShown()) { + getViewGroup().setOnTouchListener(null); + return false; } - }); - } - 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; + toast.show(); PluginResult pr = new PluginResult(PluginResult.Status.OK); 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) { final JSONObject json = new JSONObject(); try { @@ -202,6 +201,7 @@ public class Toast extends CordovaPlugin { e.printStackTrace(); } callbackContext.success(json); + hide(); return true; } @@ -215,10 +215,7 @@ public class Toast extends CordovaPlugin { @Override public void onPause(boolean multitasking) { - if (mostRecentToast != null) { - mostRecentToast.cancel(); - getViewGroup().setOnTouchListener(null); - } + hide(); this.isPaused = true; } @@ -226,4 +223,4 @@ public class Toast extends CordovaPlugin { public void onResume(boolean multitasking) { this.isPaused = false; } -} +} \ No newline at end of file diff --git a/src/ios/Toast.m b/src/ios/Toast.m index bd8e4e9..16cd9d1 100644 --- a/src/ios/Toast.m +++ b/src/ios/Toast.m @@ -5,42 +5,38 @@ @implementation Toast - (void)show:(CDVInvokedUrlCommand*)command { - NSDictionary* options = [command argumentAtIndex:0]; - NSString *message = options[@"message"]; NSString *duration = options[@"duration"]; NSString *position = options[@"position"]; NSDictionary *data = options[@"data"]; NSNumber *addPixelsY = options[@"addPixelsY"]; NSDictionary *styling = options[@"styling"]; - + 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'"]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; return; } - - NSInteger durationInt; - if ([duration isEqual: @"short"]) { - durationInt = 2; - } else if ([duration isEqual: @"long"]) { - durationInt = 5; + + NSTimeInterval durationMS; + if ([duration.lowercaseString isEqualToString: @"short"]) { + durationMS = 2000; + } else if ([duration.lowercaseString isEqualToString: @"long"]) { + durationMS = 4000; } else { - CDVPluginResult * pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"invalid duration. valid options are 'short' and 'long'"]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - return; + durationMS = [duration intValue]; } - + [self.webView makeToast:message - duration:durationInt + duration:durationMS / 1000 position:position addPixelsY:addPixelsY == nil ? 0 : [addPixelsY intValue] data:data styling:styling commandDelegate:self.commandDelegate callbackId:command.callbackId]; - + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; pluginResult.keepCallback = [NSNumber numberWithBool:YES]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; @@ -48,7 +44,7 @@ - (void)hide:(CDVInvokedUrlCommand*)command { [self.webView hideToast]; - + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } diff --git a/src/wp8/Toast.cs b/src/wp8/Toast.cs index 63f9afe..8c8a0c4 100644 --- a/src/wp8/Toast.cs +++ b/src/wp8/Toast.cs @@ -126,10 +126,10 @@ namespace WPCordovaClassLib.Cordova.Commands return; } - int hideDelay = 2800; + int hideDelay = 2500; if ("long".Equals(duration)) { - hideDelay = 5500; + hideDelay = 5000; } else if (!"short".Equals(duration)) { diff --git a/www/Toast.js b/www/Toast.js index 7782cc0..5481e53 100755 --- a/www/Toast.js +++ b/www/Toast.js @@ -16,7 +16,7 @@ Toast.prototype.optionsBuilder = function () { }, withDuration: function(d) { - duration = d; + duration = d.toString(); return this; }, @@ -43,6 +43,7 @@ Toast.prototype.optionsBuilder = function () { Toast.prototype.showWithOptions = function (options, successCallback, errorCallback) { + options.duration = (options.duration === undefined ? 'long' : options.duration.toString()); cordova.exec(successCallback, errorCallback, "Toast", "show", [options]); }; From 9ed9f018a7dd0c29c4d26e84ccf8f4ce3f9d468a Mon Sep 17 00:00:00 2001 From: EddyVerbruggen Date: Thu, 17 Mar 2016 21:30:17 +0100 Subject: [PATCH 2/7] #71 ios toast get error while message is integer --- www/Toast.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/www/Toast.js b/www/Toast.js index 5481e53..ca95231 100755 --- a/www/Toast.js +++ b/www/Toast.js @@ -11,7 +11,7 @@ Toast.prototype.optionsBuilder = function () { return { withMessage: function(m) { - message = m; + message = m.toString(); return this; }, @@ -44,6 +44,7 @@ Toast.prototype.optionsBuilder = function () { Toast.prototype.showWithOptions = function (options, successCallback, errorCallback) { options.duration = (options.duration === undefined ? 'long' : options.duration.toString()); + options.message = options.message.toString(); cordova.exec(successCallback, errorCallback, "Toast", "show", [options]); }; From dc6c5371252008a8a0312c0dab98b1f87589f78c Mon Sep 17 00:00:00 2001 From: EddyVerbruggen Date: Thu, 28 Apr 2016 19:52:33 +0200 Subject: [PATCH 3/7] #86 Is there anyway to get a significantly larger font on IOS and Android? --- LICENSE | 19 ++++++++++++++++ README.md | 25 ++------------------- package.json | 2 +- plugin.xml | 2 +- src/android/nl/xservices/plugins/Toast.java | 4 ++++ src/ios/Toast+UIView.m | 10 ++++++--- www/Toast.js | 4 ++-- 7 files changed, 36 insertions(+), 30 deletions(-) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..28ba826 --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +[The MIT License (MIT)](http://www.opensource.org/licenses/mit-license.html) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.md b/README.md index c7fbe36..c7702a6 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,6 @@ If you like this plugin and want to say thanks please send a PR or donation. Bot 4. [Styling](#styling) 5. [Credits](#5-credits) 6. [Changelog](#6-changelog) -7. [License](#7-license) ## 1. Description @@ -230,6 +229,7 @@ Note that on WP this object is currently ignored. opacity: 0.75, // 0.0 (transparent) to 1.0 (opaque). Default 0.8 backgroundColor: '#FF0000', // make sure you use #RRGGBB. Default #333333 textColor: '#FFFF00', // Ditto. Default #FFFFFF + textSize: 20.5, // Default is approx. 13. cornerRadius: 16, // minimum is 0 (square). iOS default 20, Android default 100 horizontalPadding: 20, // iOS default 16, Android default 50 verticalPadding: 16 // iOS default 12, Android default 30 @@ -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). ## 6. CHANGELOG +- 2.5.1: You can now specify the `textSize` used in the font for iOS and Android. - 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.1: As an addition to 2.4.0, [Sino](https://github.com/SinoBoeckmann) added the option to change the text color! @@ -263,25 +264,3 @@ For iOS most credits go to this excellent [Toast for iOS project by Charles Scal - 2.0.1: iOS messages are hidden when another one is shown. [Thanks Richie Min!](https://github.com/EddyVerbruggen/Toast-PhoneGap-Plugin/pull/13) - 2.0: WP8 support - 1.0: initial version supporting Android and iOS - -## 7. License - -[The MIT License (MIT)](http://www.opensource.org/licenses/mit-license.html) - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. diff --git a/package.json b/package.json index 43a53df..7713e3e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cordova-plugin-x-toast", - "version": "2.5.0", + "version": "2.5.1", "description": "This plugin allows you to show a Toast. A Toast is a little non intrusive buttonless popup which automatically disappears.", "cordova": { "id": "cordova-plugin-x-toast", diff --git a/plugin.xml b/plugin.xml index 1134fd0..d4945f0 100755 --- a/plugin.xml +++ b/plugin.xml @@ -2,7 +2,7 @@ + version="2.5.1"> Toast diff --git a/src/android/nl/xservices/plugins/Toast.java b/src/android/nl/xservices/plugins/Toast.java index 3b43b98..12f7542 100644 --- a/src/android/nl/xservices/plugins/Toast.java +++ b/src/android/nl/xservices/plugins/Toast.java @@ -90,6 +90,7 @@ public class Toast extends CordovaPlugin { // the defaults mimic the default toast as close as possible final String backgroundColor = styling.optString("backgroundColor", "#333333"); final String textColor = styling.optString("textColor", "#ffffff"); + final Double textSize = styling.optDouble("textSize", -1); final double opacity = styling.optDouble("opacity", 0.8); final int cornerRadius = styling.optInt("cornerRadius", 100); final int horizontalPadding = styling.optInt("horizontalPadding", 50); @@ -104,6 +105,9 @@ public class Toast extends CordovaPlugin { final TextView toastTextView; toastTextView = (TextView) toast.getView().findViewById(android.R.id.message); toastTextView.setTextColor(Color.parseColor(textColor)); + if (textSize > -1) { + toastTextView.setTextSize(textSize.floatValue()); + } toast.getView().setPadding(horizontalPadding, verticalPadding, horizontalPadding, verticalPadding); diff --git a/src/ios/Toast+UIView.m b/src/ios/Toast+UIView.m index 8b33055..5ba14a7 100644 --- a/src/ios/Toast+UIView.m +++ b/src/ios/Toast+UIView.m @@ -323,10 +323,14 @@ static id styling; UIColor *theColor = backgroundColor == nil ? [UIColor blackColor] : [self colorFromHexString:backgroundColor]; NSNumber * horizontalPadding = styling[@"horizontalPadding"]; - NSNumber * verticalPadding = styling[@"verticalPadding"]; CGFloat theHorizontalPadding = horizontalPadding == nil ? CSToastHorizontalPadding : [horizontalPadding floatValue]; + + NSNumber * verticalPadding = styling[@"verticalPadding"]; CGFloat theVerticalPadding = verticalPadding == nil ? CSToastVerticalPadding : [verticalPadding floatValue]; + NSNumber * textSize = styling[@"textSize"]; + CGFloat theTextSize = textSize == nil ? CSToastFontSize : [textSize floatValue]; + wrapperView.backgroundColor = theColor; if(image != nil) { @@ -352,7 +356,7 @@ static id styling; titleLabel = [[UILabel alloc] init]; titleLabel.numberOfLines = CSToastMaxTitleLines; - titleLabel.font = [UIFont boldSystemFontOfSize:CSToastFontSize]; + titleLabel.font = [UIFont boldSystemFontOfSize:theTextSize]; titleLabel.textAlignment = NSTextAlignmentLeft; titleLabel.lineBreakMode = NSLineBreakByWordWrapping; titleLabel.textColor = theTitleLabelTextColor; @@ -372,7 +376,7 @@ static id styling; messageLabel = [[UILabel alloc] init]; messageLabel.numberOfLines = CSToastMaxMessageLines; - messageLabel.font = [UIFont systemFontOfSize:CSToastFontSize]; + messageLabel.font = [UIFont systemFontOfSize:theTextSize]; messageLabel.lineBreakMode = NSLineBreakByWordWrapping; messageLabel.textColor = theMessageLabelTextColor; messageLabel.backgroundColor = [UIColor clearColor]; diff --git a/www/Toast.js b/www/Toast.js index ca95231..585b3e7 100755 --- a/www/Toast.js +++ b/www/Toast.js @@ -36,9 +36,9 @@ Toast.prototype.optionsBuilder = function () { duration: duration, position: position, addPixelsY: addPixelsY - } + }; } - } + }; }; From 0aeb2c4f959dcdff95f8a2b6549d0dc9f65be091 Mon Sep 17 00:00:00 2001 From: EddyVerbruggen Date: Fri, 29 Apr 2016 21:11:24 +0200 Subject: [PATCH 4/7] #86 Is there anyway to get a significantly larger font on IOS and Android? - center align on iOS --- src/ios/Toast+UIView.m | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ios/Toast+UIView.m b/src/ios/Toast+UIView.m index 5ba14a7..2a1b02c 100644 --- a/src/ios/Toast+UIView.m +++ b/src/ios/Toast+UIView.m @@ -357,7 +357,7 @@ static id styling; titleLabel = [[UILabel alloc] init]; titleLabel.numberOfLines = CSToastMaxTitleLines; titleLabel.font = [UIFont boldSystemFontOfSize:theTextSize]; - titleLabel.textAlignment = NSTextAlignmentLeft; + titleLabel.textAlignment = NSTextAlignmentCenter; titleLabel.lineBreakMode = NSLineBreakByWordWrapping; titleLabel.textColor = theTitleLabelTextColor; titleLabel.backgroundColor = [UIColor clearColor]; @@ -378,6 +378,7 @@ static id styling; messageLabel.numberOfLines = CSToastMaxMessageLines; messageLabel.font = [UIFont systemFontOfSize:theTextSize]; messageLabel.lineBreakMode = NSLineBreakByWordWrapping; + messageLabel.textAlignment = NSTextAlignmentCenter; messageLabel.textColor = theMessageLabelTextColor; messageLabel.backgroundColor = [UIColor clearColor]; messageLabel.alpha = 1.0; From 55f9b41d87e9051e403d57f8660f89eafae69eb6 Mon Sep 17 00:00:00 2001 From: EddyVerbruggen Date: Fri, 20 May 2016 21:27:23 +0200 Subject: [PATCH 5/7] #86 Is there anyway to get a significantly larger font on IOS and Android? - center align on Android as well --- src/android/nl/xservices/plugins/Toast.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/android/nl/xservices/plugins/Toast.java b/src/android/nl/xservices/plugins/Toast.java index 12f7542..5bf837d 100644 --- a/src/android/nl/xservices/plugins/Toast.java +++ b/src/android/nl/xservices/plugins/Toast.java @@ -4,6 +4,10 @@ import android.graphics.Color; import android.graphics.drawable.GradientDrawable; import android.os.Build; import android.os.CountDownTimer; +import android.text.Layout; +import android.text.Spannable; +import android.text.SpannableString; +import android.text.style.AlignmentSpan; import android.view.Gravity; import android.view.MotionEvent; import android.view.View; @@ -49,7 +53,14 @@ public class Toast extends CordovaPlugin { } final JSONObject options = args.getJSONObject(0); - final String message = options.getString("message"); + final String msg = options.getString("message"); + final Spannable message = new SpannableString(msg); + message.setSpan( + new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER), + 0, + msg.length() - 1, + Spannable.SPAN_INCLUSIVE_INCLUSIVE); + final String duration = options.getString("duration"); final String position = options.getString("position"); final int addPixelsY = options.has("addPixelsY") ? options.getInt("addPixelsY") : 0; @@ -159,7 +170,7 @@ public class Toast extends CordovaPlugin { final boolean tapped = tapX >= startX && tapX <= endX && tapY >= startY && tapY <= endY; - return tapped && returnTapEvent(message, data, callbackContext); + return tapped && returnTapEvent(msg, data, callbackContext); } }); From 11bcf9b14db61f710ae0486bbad7b601c1baf6d9 Mon Sep 17 00:00:00 2001 From: EddyVerbruggen Date: Sat, 21 May 2016 16:00:00 +0100 Subject: [PATCH 6/7] #86 Is there anyway to get a significantly larger font on IOS and Android? - center align on Android as well --- src/android/nl/xservices/plugins/Toast.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/android/nl/xservices/plugins/Toast.java b/src/android/nl/xservices/plugins/Toast.java index 12f7542..5bf837d 100644 --- a/src/android/nl/xservices/plugins/Toast.java +++ b/src/android/nl/xservices/plugins/Toast.java @@ -4,6 +4,10 @@ import android.graphics.Color; import android.graphics.drawable.GradientDrawable; import android.os.Build; import android.os.CountDownTimer; +import android.text.Layout; +import android.text.Spannable; +import android.text.SpannableString; +import android.text.style.AlignmentSpan; import android.view.Gravity; import android.view.MotionEvent; import android.view.View; @@ -49,7 +53,14 @@ public class Toast extends CordovaPlugin { } final JSONObject options = args.getJSONObject(0); - final String message = options.getString("message"); + final String msg = options.getString("message"); + final Spannable message = new SpannableString(msg); + message.setSpan( + new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER), + 0, + msg.length() - 1, + Spannable.SPAN_INCLUSIVE_INCLUSIVE); + final String duration = options.getString("duration"); final String position = options.getString("position"); final int addPixelsY = options.has("addPixelsY") ? options.getInt("addPixelsY") : 0; @@ -159,7 +170,7 @@ public class Toast extends CordovaPlugin { final boolean tapped = tapX >= startX && tapX <= endX && tapY >= startY && tapY <= endY; - return tapped && returnTapEvent(message, data, callbackContext); + return tapped && returnTapEvent(msg, data, callbackContext); } }); From b5adb0da912a04e90a2c899a00f214a9917e0683 Mon Sep 17 00:00:00 2001 From: EddyVerbruggen Date: Mon, 23 May 2016 09:19:38 +0200 Subject: [PATCH 7/7] #80 Receiving a callback when a Toast is tapped not trigger in Android --- src/android/nl/xservices/plugins/Toast.java | 97 ++++++++++++--------- 1 file changed, 54 insertions(+), 43 deletions(-) diff --git a/src/android/nl/xservices/plugins/Toast.java b/src/android/nl/xservices/plugins/Toast.java index 5bf837d..5855882 100644 --- a/src/android/nl/xservices/plugins/Toast.java +++ b/src/android/nl/xservices/plugins/Toast.java @@ -35,6 +35,8 @@ public class Toast extends CordovaPlugin { private android.widget.Toast mostRecentToast; 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 private boolean isPaused; @@ -79,7 +81,7 @@ public class Toast extends CordovaPlugin { hideAfterMs = Integer.parseInt(duration); } final android.widget.Toast toast = android.widget.Toast.makeText( - Build.VERSION.SDK_INT >= 21 ? cordova.getActivity().getWindow().getContext() : cordova.getActivity().getApplicationContext(), + IS_AT_LEAST_LOLLIPOP ? cordova.getActivity().getWindow().getContext() : cordova.getActivity().getApplicationContext(), message, android.widget.Toast.LENGTH_LONG // actually controlled by a timer further down ); @@ -128,51 +130,60 @@ public class Toast extends CordovaPlugin { } } - // On newer Android devices you can no longer rely on the 'toast.getView().setOnTouchListener', + // On Android >= 5 you can no longer rely on the 'toast.getView().setOnTouchListener', // so created something funky that compares the Toast position to the tap coordinates. - getViewGroup().setOnTouchListener(new View.OnTouchListener() { - @Override - public boolean onTouch(View view, MotionEvent motionEvent) { - if (motionEvent.getAction() != MotionEvent.ACTION_DOWN) { - return false; + if (IS_AT_LEAST_LOLLIPOP) { + getViewGroup().setOnTouchListener(new View.OnTouchListener() { + @Override + public boolean onTouch(View view, MotionEvent motionEvent) { + 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; + + return tapped && returnTapEvent(msg, data, callbackContext); } - if (mostRecentToast == null || !mostRecentToast.getView().isShown()) { - getViewGroup().setOnTouchListener(null); - return false; + }); + } 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); } - - 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(msg, data, callbackContext); - } - }); + }); + } // trigger show every 2500 ms for as long as the requested duration _timer = new CountDownTimer(hideAfterMs, 2500) {