mirror of
https://github.com/EddyVerbruggen/Toast-PhoneGap-Plugin.git
synced 2026-07-31 00:02:14 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
440ea5bded | ||
|
|
8869b81749 | ||
|
|
e4e18f143a | ||
|
|
9757e2c04c | ||
|
|
791beb0bc4 | ||
|
|
fde8da464f | ||
|
|
dbf27e63a4 | ||
|
|
3235b9c8e3 | ||
|
|
ed2398455a | ||
|
|
2a0ecbc2dd | ||
|
|
6351512cf1 | ||
|
|
206b7e014e | ||
|
|
a429424107 | ||
|
|
4324168d2a | ||
|
|
75287f6517 | ||
|
|
04b4cf7894 | ||
|
|
d5356d7562 | ||
|
|
8d4a3d646c | ||
|
|
d81936a08a | ||
|
|
f3f608ee1c | ||
|
|
e3fb477789 | ||
|
|
ce1dfa79cc | ||
|
|
7b903b7f72 | ||
|
|
093b523dc6 | ||
|
|
37a73baa1f | ||
|
|
0b76cb8811 | ||
|
|
b3e3d8b858 | ||
|
|
b5adb0da91 | ||
|
|
52e4072ce6 | ||
|
|
11bcf9b14d | ||
|
|
55f9b41d87 | ||
|
|
0aeb2c4f95 | ||
|
|
dc6c537125 | ||
|
|
566f6f7392 | ||
|
|
9b9365d9da |
@@ -0,0 +1,4 @@
|
||||
.idea/
|
||||
screenshots/
|
||||
test/
|
||||
issue_template.md
|
||||
@@ -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.
|
||||
@@ -1,6 +1,6 @@
|
||||
# PhoneGap Toast plugin
|
||||
|
||||
for Android, iOS and WP8, by [Eddy Verbruggen](http://www.x-services.nl/phonegap-toast-plugin/796)
|
||||
for Android, iOS, WP8, Windows and BlackBerry by [Eddy Verbruggen](http://www.x-services.nl/phonegap-toast-plugin/796)
|
||||
|
||||
[](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=eddyverbruggen%40gmail%2ecom&lc=US&item_name=cordova%2dplugin%2dtoast¤cy_code=USD&bn=PP%2dDonationsBF%3abtn_donate_SM%2egif%3aNonHosted)
|
||||
If you like this plugin and want to say thanks please send a PR or donation. Both are equally appreciated!
|
||||
@@ -18,14 +18,13 @@ 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)
|
||||
6. [Changelog](#6-changelog)
|
||||
7. [License](#7-license)
|
||||
|
||||
## 1. Description
|
||||
|
||||
@@ -33,18 +32,15 @@ This plugin allows you to show a native Toast (a little text popup) on iOS, Andr
|
||||
It's great for showing a non intrusive native notification which is guaranteed always in the viewport of the browser.
|
||||
* You can choose where to show the Toast: at the top, center or bottom of the screen.
|
||||
* You can choose two durations: short (approx. 2 seconds), or long (approx. 5 seconds), after which the Toast automatically disappears.
|
||||
* Compatible with [Cordova Plugman](https://github.com/apache/cordova-plugman).
|
||||
* Officially supported by [PhoneGap Build](https://build.phonegap.com/plugins).
|
||||
* Minimum iOS version is [6](https://github.com/EddyVerbruggen/Toast-PhoneGap-Plugin/issues/7).
|
||||
|
||||
Example usages:
|
||||
* "There were validation errors"
|
||||
* "Account created successfully"
|
||||
* "The record was deleted"
|
||||
* "Login successful"
|
||||
* "The battery is almost dead"
|
||||
* "You are now logged out"
|
||||
* "Connection failure, please try again later"
|
||||
* "Created Order #00287
|
||||
|
||||
## 2. Screenshots
|
||||
|
||||
@@ -186,6 +182,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.
|
||||
|
||||
@@ -204,17 +218,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.
|
||||
@@ -230,6 +248,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 +273,11 @@ 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.7.0: [Android P compatibility.](https://github.com/EddyVerbruggen/Toast-PhoneGap-Plugin/issues/116)
|
||||
- 2.6.2: [iOS view hierarchy fix.](https://github.com/EddyVerbruggen/Toast-PhoneGap-Plugin/pull/112)
|
||||
- 2.6.0: Windows support!
|
||||
- 2.5.2: Multi-line wrapping Toasts are now center aligned.
|
||||
- 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 +287,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.
|
||||
|
||||
+3
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "cordova-plugin-x-toast",
|
||||
"version": "2.5.0",
|
||||
"version": "2.7.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",
|
||||
@@ -8,6 +8,7 @@
|
||||
"ios",
|
||||
"android",
|
||||
"wp8",
|
||||
"windows",
|
||||
"blackberry10"
|
||||
]
|
||||
},
|
||||
@@ -24,6 +25,7 @@
|
||||
"cordova-ios",
|
||||
"cordova-android",
|
||||
"cordova-wp8",
|
||||
"cordova-windows",
|
||||
"cordova-blackberry10"
|
||||
],
|
||||
"engines": [
|
||||
|
||||
+20
-14
@@ -1,8 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
id="cordova-plugin-x-toast"
|
||||
version="2.5.0">
|
||||
version="2.7.1">
|
||||
|
||||
<name>Toast</name>
|
||||
|
||||
@@ -29,9 +28,6 @@
|
||||
<clobbers target="window.plugins.toast" />
|
||||
</js-module>
|
||||
|
||||
<js-module src="test/tests.js" name="tests">
|
||||
</js-module>
|
||||
|
||||
<!-- ios -->
|
||||
<platform name="ios">
|
||||
|
||||
@@ -72,13 +68,23 @@
|
||||
<source-file src="src/wp8/Toast.cs" />
|
||||
</platform>
|
||||
|
||||
<platform name="blackberry10">
|
||||
<source-file src="src/blackberry10/index.js" target-dir="Toast"/>
|
||||
<lib-file src="src/blackberry10/native/device/libToast.so" arch="device"/>
|
||||
<lib-file src="src/blackberry10/native/simulator/libToast.so" arch="simulator"/>
|
||||
<config-file target="www/config.xml" parent="/widget">
|
||||
<feature name="Toast" value="Toast" />
|
||||
</config-file>
|
||||
</platform>
|
||||
|
||||
<platform name="blackberry10">
|
||||
<source-file src="src/blackberry10/index.js" target-dir="Toast"/>
|
||||
<lib-file src="src/blackberry10/native/device/libToast.so" arch="device"/>
|
||||
<lib-file src="src/blackberry10/native/simulator/libToast.so" arch="simulator"/>
|
||||
<config-file target="www/config.xml" parent="/widget">
|
||||
<feature name="Toast" value="Toast" />
|
||||
</config-file>
|
||||
</platform>
|
||||
|
||||
<!-- windows -->
|
||||
<platform name="windows">
|
||||
<js-module src="src/windows/toastProxy.js" name="ToastProxy">
|
||||
<merges target="" />
|
||||
</js-module>
|
||||
<config-file target="config.xml" parent="/*">
|
||||
<feature name="Toast" value="Toast" />
|
||||
</config-file>
|
||||
</platform>
|
||||
|
||||
</plugin>
|
||||
|
||||
@@ -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;
|
||||
@@ -31,14 +35,20 @@ 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;
|
||||
private static final boolean IS_AT_LEAST_PIE = Build.VERSION.SDK_INT >= 28;
|
||||
|
||||
// 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;
|
||||
@@ -49,13 +59,23 @@ 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;
|
||||
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;
|
||||
@@ -68,7 +88,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
|
||||
);
|
||||
@@ -90,6 +110,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 +125,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);
|
||||
|
||||
@@ -113,56 +137,73 @@ 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("touch", 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("touch", 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(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();}
|
||||
public void onTick(long millisUntilFinished) {
|
||||
// see https://github.com/EddyVerbruggen/Toast-PhoneGap-Plugin/issues/116
|
||||
// and https://github.com/EddyVerbruggen/Toast-PhoneGap-Plugin/issues/120
|
||||
// if (!IS_AT_LEAST_PIE) {
|
||||
// toast.show();
|
||||
// }
|
||||
}
|
||||
public void onFinish() {
|
||||
returnTapEvent("hide", msg, data, callbackContext);
|
||||
toast.cancel();
|
||||
}
|
||||
}.start();
|
||||
|
||||
mostRecentToast = toast;
|
||||
@@ -181,6 +222,7 @@ public class Toast extends CordovaPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void hide() {
|
||||
if (mostRecentToast != null) {
|
||||
mostRecentToast.cancel();
|
||||
@@ -191,17 +233,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;
|
||||
}
|
||||
|
||||
@@ -223,4 +264,4 @@ public class Toast extends CordovaPlugin {
|
||||
public void onResume(boolean multitasking) {
|
||||
this.isPaused = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+19
-5
@@ -157,7 +157,7 @@ static id styling;
|
||||
}
|
||||
|
||||
- (UIViewController*) getTopMostViewController {
|
||||
UIViewController *presentingViewController = [[[UIApplication sharedApplication] delegate] window].rootViewController;
|
||||
UIViewController *presentingViewController = [[UIApplication sharedApplication] keyWindow].rootViewController;
|
||||
while (presentingViewController.presentedViewController != nil) {
|
||||
presentingViewController = presentingViewController.presentedViewController;
|
||||
}
|
||||
@@ -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 {
|
||||
@@ -323,10 +332,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,8 +365,8 @@ static id styling;
|
||||
|
||||
titleLabel = [[UILabel alloc] init];
|
||||
titleLabel.numberOfLines = CSToastMaxTitleLines;
|
||||
titleLabel.font = [UIFont boldSystemFontOfSize:CSToastFontSize];
|
||||
titleLabel.textAlignment = NSTextAlignmentLeft;
|
||||
titleLabel.font = [UIFont boldSystemFontOfSize:theTextSize];
|
||||
titleLabel.textAlignment = NSTextAlignmentCenter;
|
||||
titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
|
||||
titleLabel.textColor = theTitleLabelTextColor;
|
||||
titleLabel.backgroundColor = [UIColor clearColor];
|
||||
@@ -372,8 +385,9 @@ static id styling;
|
||||
|
||||
messageLabel = [[UILabel alloc] init];
|
||||
messageLabel.numberOfLines = CSToastMaxMessageLines;
|
||||
messageLabel.font = [UIFont systemFontOfSize:CSToastFontSize];
|
||||
messageLabel.font = [UIFont systemFontOfSize:theTextSize];
|
||||
messageLabel.lineBreakMode = NSLineBreakByWordWrapping;
|
||||
messageLabel.textAlignment = NSTextAlignmentCenter;
|
||||
messageLabel.textColor = theMessageLabelTextColor;
|
||||
messageLabel.backgroundColor = [UIColor clearColor];
|
||||
messageLabel.alpha = 1.0;
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* @author Piotr Smolarski <ph0ndragxdev@gmail.com>
|
||||
*/
|
||||
var toastProxy = {
|
||||
|
||||
lastDisplayedNotification: null,
|
||||
|
||||
show: function(successCallback, errorCallback, options) {
|
||||
var notifications = Windows.UI.Notifications;
|
||||
|
||||
var template = notifications.ToastTemplateType.ToastText01;
|
||||
var toastXml = notifications.ToastNotificationManager.getTemplateContent(template);
|
||||
|
||||
var toastTextElements = toastXml.getElementsByTagName("text");
|
||||
toastTextElements[0].appendChild(toastXml.createTextNode(options[0].message));
|
||||
|
||||
var toastNode = toastXml.selectSingleNode("/toast");
|
||||
toastNode.setAttribute("duration", options[0].duration);
|
||||
|
||||
var toast = new notifications.ToastNotification(toastXml);
|
||||
|
||||
toast.onactivated = function (event) {
|
||||
toastProxy.lastDisplayedNotification = null;
|
||||
successCallback({
|
||||
event: "touch",
|
||||
message: options[0].message,
|
||||
data: options[0].data
|
||||
});
|
||||
};
|
||||
|
||||
toast.ondismissed = function (event) {
|
||||
toastProxy.lastDisplayedNotification = null;
|
||||
successCallback({
|
||||
event: "hide",
|
||||
message: options[0].message,
|
||||
data: options[0].data
|
||||
});
|
||||
};
|
||||
|
||||
toast.onfailed = function(err) {
|
||||
toastProxy.lastDisplayedNotification = null;
|
||||
errorCallback(err);
|
||||
};
|
||||
|
||||
notifications.ToastNotificationManager.createToastNotifier().show(toast);
|
||||
},
|
||||
|
||||
hide: function() {
|
||||
if (this.lastDisplayedNotification !== null) {
|
||||
notifications.ToastNotificationManager.createToastNotifier().hide(toast);
|
||||
this.lastDisplayedNotification = null;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
cordova.commandProxy.add("Toast", toastProxy);
|
||||
+2
-2
@@ -36,9 +36,9 @@ Toast.prototype.optionsBuilder = function () {
|
||||
duration: duration,
|
||||
position: position,
|
||||
addPixelsY: addPixelsY
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user