mirror of
https://github.com/EddyVerbruggen/Toast-PhoneGap-Plugin.git
synced 2026-07-31 00:02:14 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a2d253bdde | ||
|
|
ffe056db75 | ||
|
|
5e5251bbd2 | ||
|
|
37c14a6d2d | ||
|
|
369ab363b9 | ||
|
|
6ba778dd22 | ||
|
|
e8ade312d9 | ||
|
|
f6479c8846 | ||
|
|
658ee711db | ||
|
|
5f4a0f5636 | ||
|
|
c7a329a7ac | ||
|
|
577b1704d9 | ||
|
|
ac0746b9b9 | ||
|
|
7a08e6afd6 | ||
|
|
23d35404b5 | ||
|
|
a5cdda3ea2 | ||
|
|
c5b6060973 | ||
|
|
cabeea18ed | ||
|
|
335f731da4 | ||
|
|
ecb47ab110 | ||
|
|
f8887dc45e | ||
|
|
273334664a | ||
|
|
6ee16e9e7d | ||
|
|
6e13de8982 | ||
|
|
03320113b4 | ||
|
|
5634e1d84c | ||
|
|
2add9bef7b | ||
|
|
c252764372 | ||
|
|
ba7c60f324 | ||
|
|
eed523111b | ||
|
|
e0420e0aef | ||
|
|
8c10bcaec5 | ||
|
|
5d9aa4f187 | ||
|
|
50290d83de |
@@ -19,6 +19,7 @@ for Android, iOS and WP8, by [Eddy Verbruggen](http://www.x-services.nl/phonegap
|
||||
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)
|
||||
@@ -48,10 +49,18 @@ iOS
|
||||
|
||||

|
||||
|
||||
A few styling options
|
||||
|
||||

|
||||
|
||||

|
||||
|
||||
|
||||
Android
|
||||
|
||||

|
||||
|
||||
|
||||
Windows Phone 8
|
||||
|
||||

|
||||
@@ -63,18 +72,19 @@ Toast is compatible with [Cordova Plugman](https://github.com/apache/cordova-plu
|
||||
|
||||
Using the Cordova CLI and the [Cordova Plugin Registry](http://plugins.cordova.io)
|
||||
```
|
||||
$ cordova plugin add nl.x-services.plugins.toast
|
||||
$ cordova plugin add cordova-plugin-x-toast
|
||||
$ cordova prepare
|
||||
```
|
||||
|
||||
Or using the phonegap CLI
|
||||
```
|
||||
$ phonegap local plugin add nl.x-services.plugins.toast
|
||||
$ phonegap local plugin add cordova-plugin-x-toast
|
||||
```
|
||||
|
||||
Toast.js is brought in automatically. There is no need to change or add anything in your html.
|
||||
|
||||
### Manually
|
||||
You'd better use the CLI, but here goes:
|
||||
|
||||
1\. Add the following xml to your `config.xml` in the root directory of your `www` folder:
|
||||
```xml
|
||||
@@ -118,11 +128,7 @@ Toast works with PhoneGap build too, but only with PhoneGap 3.0 and up.
|
||||
|
||||
Just add the following xml to your `config.xml` to always use the latest version of this plugin:
|
||||
```xml
|
||||
<gap:plugin name="nl.x-services.plugins.toast" source="plugins.cordova.io" />
|
||||
```
|
||||
or to use a specific version:
|
||||
```xml
|
||||
<gap:plugin name="nl.x-services.plugins.toast" source="plugins.cordova.io" version="2.0.6" />
|
||||
<gap:plugin name="cordova-plugin-x-toast" source="npm" />
|
||||
```
|
||||
|
||||
Toast.js is brought in automatically. There is no need to change or add anything in your html.
|
||||
@@ -154,7 +160,7 @@ You can copy-paste these lines of code for a quick test:
|
||||
Since 2.1.0 you can add pixels to move the toast up or down.
|
||||
Note that `showWithOptions` can be used instead of the functions above, but it's not useful unless you want to pass `addPixelsY`.
|
||||
```js
|
||||
function showTop() {
|
||||
function showBottom() {
|
||||
window.plugins.toast.showWithOptions(
|
||||
{
|
||||
message: "hey there",
|
||||
@@ -177,7 +183,61 @@ function hide() {
|
||||
}
|
||||
```
|
||||
|
||||
### WP8 quircks
|
||||
### 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.
|
||||
|
||||
So the first time the success handler fires is when the toast is shown, and in case the user taps the toast it will be
|
||||
called again. You can distinguish between those events of course:
|
||||
|
||||
```js
|
||||
window.plugins.toast.showWithOptions(
|
||||
{
|
||||
message: "hey there",
|
||||
duration: "short",
|
||||
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)
|
||||
},
|
||||
// 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("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");
|
||||
}
|
||||
}
|
||||
);
|
||||
```
|
||||
|
||||
### 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.
|
||||
|
||||
Note that on WP this object is currently ignored.
|
||||
|
||||
```js
|
||||
window.plugins.toast.showWithOptions({
|
||||
message: "hey there",
|
||||
duration: "short",
|
||||
position: "bottom",
|
||||
styling: {
|
||||
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
|
||||
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
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
Tip: if you need to pass different values for iOS and Android you can use fi. the device plugin
|
||||
to determine the platform and pass `opacity: isAndroid() ? 0.7 : 0.9`.
|
||||
|
||||
### WP8 quirks
|
||||
The WP8 implementation needs a little more work, but it's perfectly useable when you keep this in mind:
|
||||
* You can't show two Toasts simultaneously.
|
||||
* Wait until the first Toast is hidden before the second is shown, otherwise the second one will be hidden quickly.
|
||||
@@ -191,11 +251,13 @@ 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.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
|
||||
- 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.3.2: The click event introduced with 2.3.0 did not work with Android 5+.
|
||||
- 2.3.0: The plugin will now report back to JS if Toasts were tapped by the user.
|
||||
- 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
|
||||
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
{
|
||||
"name": "cordova-plugin-x-toast",
|
||||
"version": "2.4.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",
|
||||
"platforms": [
|
||||
"ios",
|
||||
"android",
|
||||
"wp8",
|
||||
"blackberry10"
|
||||
]
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/EddyVerbruggen/Toast-PhoneGap-Plugin.git"
|
||||
},
|
||||
"keywords": [
|
||||
"Toast",
|
||||
"Notification",
|
||||
"Message",
|
||||
"Alert",
|
||||
"ecosystem:cordova",
|
||||
"cordova-ios",
|
||||
"cordova-android",
|
||||
"cordova-wp8",
|
||||
"cordova-blackberry10"
|
||||
],
|
||||
"engines": [
|
||||
{
|
||||
"name": "cordova",
|
||||
"version": ">=3.0.0"
|
||||
}
|
||||
],
|
||||
"author": "Eddy Verbruggen <eddyverbruggen@gmail.com> (https://github.com/EddyVerbruggen)",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/EddyVerbruggen/Toast-PhoneGap-Plugin/issues"
|
||||
},
|
||||
"homepage": "https://github.com/EddyVerbruggen/Toast-PhoneGap-Plugin#readme"
|
||||
}
|
||||
+2
-2
@@ -1,8 +1,8 @@
|
||||
<?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="nl.x-services.plugins.toast"
|
||||
version="2.1.0">
|
||||
id="cordova-plugin-x-toast"
|
||||
version="2.4.1">
|
||||
|
||||
<name>Toast</name>
|
||||
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 16 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 22 KiB |
@@ -1,8 +1,17 @@
|
||||
package nl.xservices.plugins;
|
||||
|
||||
import android.graphics.Color;
|
||||
import android.graphics.drawable.GradientDrawable;
|
||||
import android.os.Build;
|
||||
import android.view.Gravity;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.apache.cordova.CallbackContext;
|
||||
import org.apache.cordova.CordovaPlugin;
|
||||
import org.apache.cordova.PluginResult;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
@@ -22,7 +31,16 @@ public class Toast extends CordovaPlugin {
|
||||
private static final String ACTION_SHOW_EVENT = "show";
|
||||
private static final String ACTION_HIDE_EVENT = "hide";
|
||||
|
||||
private static final int GRAVITY_TOP = Gravity.TOP|Gravity.CENTER_HORIZONTAL;
|
||||
private static final int GRAVITY_CENTER = Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL;
|
||||
private static final int GRAVITY_BOTTOM = Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL;
|
||||
|
||||
private static final int BASE_TOP_BOTTOM_OFFSET = 20;
|
||||
|
||||
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;
|
||||
@@ -32,6 +50,7 @@ public class Toast extends CordovaPlugin {
|
||||
if (ACTION_HIDE_EVENT.equals(action)) {
|
||||
if (mostRecentToast != null) {
|
||||
mostRecentToast.cancel();
|
||||
getViewGroup().setOnTouchListener(null);
|
||||
}
|
||||
callbackContext.success();
|
||||
return true;
|
||||
@@ -48,28 +67,121 @@ public class Toast extends CordovaPlugin {
|
||||
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");
|
||||
|
||||
cordova.getActivity().runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
android.widget.Toast toast = android.widget.Toast.makeText(
|
||||
webView.getContext(),
|
||||
final android.widget.Toast toast = android.widget.Toast.makeText(
|
||||
IS_AT_LEAST_LOLLIPOP ? cordova.getActivity().getWindow().getContext() : cordova.getActivity().getApplicationContext(),
|
||||
message,
|
||||
"short".equals(duration) ? android.widget.Toast.LENGTH_SHORT : android.widget.Toast.LENGTH_LONG);
|
||||
|
||||
if ("top".equals(position)) {
|
||||
toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 20 + addPixelsY);
|
||||
toast.setGravity(GRAVITY_TOP, 0, BASE_TOP_BOTTOM_OFFSET + addPixelsY);
|
||||
} else if ("bottom".equals(position)) {
|
||||
toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 20 - addPixelsY);
|
||||
toast.setGravity(GRAVITY_BOTTOM, 0, BASE_TOP_BOTTOM_OFFSET - addPixelsY);
|
||||
} else if ("center".equals(position)) {
|
||||
toast.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL, 0, addPixelsY);
|
||||
toast.setGravity(GRAVITY_CENTER, 0, addPixelsY);
|
||||
} else {
|
||||
callbackContext.error("invalid position. valid options are 'top', 'center' and 'bottom'");
|
||||
return;
|
||||
}
|
||||
|
||||
// if one of the custom layout options have been passed in, draw our own shape
|
||||
if (styling != null && Build.VERSION.SDK_INT >= 16) {
|
||||
|
||||
// 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 opacity = styling.optDouble("opacity", 0.8);
|
||||
final int cornerRadius = styling.optInt("cornerRadius", 100);
|
||||
final int horizontalPadding = styling.optInt("horizontalPadding", 50);
|
||||
final int verticalPadding = styling.optInt("verticalPadding", 30);
|
||||
|
||||
GradientDrawable shape = new GradientDrawable();
|
||||
shape.setCornerRadius(cornerRadius);
|
||||
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
|
||||
if (Build.VERSION.SDK_INT >= 21) {
|
||||
toast.getView().setElevation(6);
|
||||
}
|
||||
}
|
||||
|
||||
// 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.
|
||||
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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
toast.show();
|
||||
mostRecentToast = toast;
|
||||
callbackContext.success();
|
||||
|
||||
PluginResult pr = new PluginResult(PluginResult.Status.OK);
|
||||
pr.setKeepCallback(true);
|
||||
callbackContext.sendPluginResult(pr);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -80,10 +192,32 @@ public class Toast extends CordovaPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean returnTapEvent(String message, JSONObject data, CallbackContext callbackContext) {
|
||||
final JSONObject json = new JSONObject();
|
||||
try {
|
||||
json.put("event", "touch");
|
||||
json.put("message", message);
|
||||
json.put("data", data);
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
callbackContext.success(json);
|
||||
return true;
|
||||
}
|
||||
|
||||
// lazy init and caching
|
||||
private ViewGroup getViewGroup() {
|
||||
if (viewGroup == null) {
|
||||
viewGroup = (ViewGroup) ((ViewGroup) cordova.getActivity().findViewById(android.R.id.content)).getChildAt(0);
|
||||
}
|
||||
return viewGroup;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause(boolean multitasking) {
|
||||
if (mostRecentToast != null) {
|
||||
mostRecentToast.cancel();
|
||||
getViewGroup().setOnTouchListener(null);
|
||||
}
|
||||
this.isPaused = true;
|
||||
}
|
||||
@@ -92,4 +226,4 @@ public class Toast extends CordovaPlugin {
|
||||
public void onResume(boolean multitasking) {
|
||||
this.isPaused = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <Cordova/CDV.h>
|
||||
|
||||
@interface UIView (Toast)
|
||||
|
||||
// each makeToast method creates a view and displays it as toast
|
||||
- (void)makeToast:(NSString *)message;
|
||||
- (void)makeToast:(NSString *)message duration:(NSTimeInterval)interval position:(id)position;
|
||||
- (void)makeToast:(NSString *)message duration:(NSTimeInterval)duration position:(id)position addPixelsY:(int)addPixelsY;
|
||||
- (void)makeToast:(NSString *)message duration:(NSTimeInterval)duration position:(id)position addPixelsY:(int)addPixelsY data:(NSDictionary*)data styling:(NSDictionary*)styling commandDelegate:(id <CDVCommandDelegate>)commandDelegate callbackId:(NSString *)callbackId;
|
||||
- (void)makeToast:(NSString *)message duration:(NSTimeInterval)interval position:(id)position image:(UIImage *)image;
|
||||
- (void)makeToast:(NSString *)message duration:(NSTimeInterval)interval position:(id)position title:(NSString *)title;
|
||||
- (void)makeToast:(NSString *)message duration:(NSTimeInterval)interval position:(id)position title:(NSString *)title image:(UIImage *)image;
|
||||
|
||||
+104
-33
@@ -48,6 +48,13 @@ static const NSString * CSToastActivityViewKey = @"CSToastActivityViewKey";
|
||||
|
||||
static UIView *prevToast = NULL;
|
||||
|
||||
// doesn't matter these are static
|
||||
static id commandDelegate;
|
||||
static id callbackId;
|
||||
static id msg;
|
||||
static id data;
|
||||
static id styling;
|
||||
|
||||
@interface UIView (ToastPrivate)
|
||||
|
||||
- (void)hideToast:(UIView *)toast;
|
||||
@@ -56,12 +63,12 @@ static UIView *prevToast = NULL;
|
||||
- (CGPoint)centerPointForPosition:(id)position withToast:(UIView *)toast withAddedPixelsY:(int) addPixelsY;
|
||||
- (UIView *)viewForMessage:(NSString *)message title:(NSString *)title image:(UIImage *)image;
|
||||
- (CGSize)sizeForString:(NSString *)string font:(UIFont *)font constrainedToSize:(CGSize)constrainedSize lineBreakMode:(NSLineBreakMode)lineBreakMode;
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@implementation UIView (Toast)
|
||||
|
||||
|
||||
#pragma mark - Toast Methods
|
||||
|
||||
- (void)makeToast:(NSString *)message {
|
||||
@@ -73,24 +80,37 @@ static UIView *prevToast = NULL;
|
||||
[self showToast:toast duration:duration position:position];
|
||||
}
|
||||
|
||||
- (void)makeToast:(NSString *)message duration:(NSTimeInterval)duration position:(id)position addPixelsY:(int)addPixelsY {
|
||||
UIView *toast = [self viewForMessage:message title:nil image:nil];
|
||||
[self showToast:toast duration:duration position:position addedPixelsY:addPixelsY];
|
||||
- (void)makeToast:(NSString *)message
|
||||
duration:(NSTimeInterval)duration
|
||||
position:(id)position addPixelsY:(int)addPixelsY
|
||||
data:(NSDictionary*)_data
|
||||
styling:(NSDictionary*)_styling
|
||||
commandDelegate:(id <CDVCommandDelegate>)_commandDelegate
|
||||
callbackId:(NSString *)_callbackId {
|
||||
|
||||
commandDelegate = _commandDelegate;
|
||||
callbackId = _callbackId;
|
||||
msg = message;
|
||||
data = _data;
|
||||
styling = _styling;
|
||||
|
||||
UIView *toast = [self viewForMessage:message title:nil image:nil];
|
||||
[self showToast:toast duration:duration position:position addedPixelsY:addPixelsY];
|
||||
}
|
||||
|
||||
- (void)makeToast:(NSString *)message duration:(NSTimeInterval)duration position:(id)position title:(NSString *)title {
|
||||
UIView *toast = [self viewForMessage:message title:title image:nil];
|
||||
[self showToast:toast duration:duration position:position];
|
||||
[self showToast:toast duration:duration position:position];
|
||||
}
|
||||
|
||||
- (void)makeToast:(NSString *)message duration:(NSTimeInterval)duration position:(id)position image:(UIImage *)image {
|
||||
UIView *toast = [self viewForMessage:message title:nil image:image];
|
||||
[self showToast:toast duration:duration position:position];
|
||||
[self showToast:toast duration:duration position:position];
|
||||
}
|
||||
|
||||
- (void)makeToast:(NSString *)message duration:(NSTimeInterval)duration position:(id)position title:(NSString *)title image:(UIImage *)image {
|
||||
UIView *toast = [self viewForMessage:message title:title image:image];
|
||||
[self showToast:toast duration:duration position:position];
|
||||
[self showToast:toast duration:duration position:position];
|
||||
}
|
||||
|
||||
- (void)showToast:(UIView *)toast {
|
||||
@@ -101,22 +121,25 @@ static UIView *prevToast = NULL;
|
||||
[self showToast:toast duration:CSToastDefaultDuration position:CSToastDefaultPosition addedPixelsY:0];
|
||||
}
|
||||
|
||||
- (void)showToast:(UIView *)toast duration:(NSTimeInterval)duration position:(id)point addedPixelsY:(int) addPixelsY {
|
||||
- (void)showToast:(UIView *)toast duration:(NSTimeInterval)duration position:(id)point addedPixelsY:(int) addPixelsY {
|
||||
[self hideToast];
|
||||
prevToast = toast;
|
||||
toast.center = [self centerPointForPosition:point withToast:toast withAddedPixelsY:addPixelsY];
|
||||
toast.alpha = 0.0;
|
||||
|
||||
|
||||
// note that we changed this to be always true
|
||||
if (CSToastHidesOnTap) {
|
||||
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:toast action:@selector(handleToastTapped:)];
|
||||
[toast addGestureRecognizer:recognizer];
|
||||
toast.userInteractionEnabled = YES;
|
||||
toast.exclusiveTouch = YES;
|
||||
}
|
||||
|
||||
|
||||
// make sure that if InAppBrowser is active, we're still showing Toasts on top of it
|
||||
[self.superview.superview addSubview:toast];
|
||||
|
||||
UIViewController *vc = [self getTopMostViewController];
|
||||
UIView *v = [vc view];
|
||||
[v addSubview:toast];
|
||||
|
||||
[UIView animateWithDuration:CSToastFadeDuration
|
||||
delay:0.0
|
||||
options:(UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionAllowUserInteraction)
|
||||
@@ -127,7 +150,15 @@ static UIView *prevToast = NULL;
|
||||
// associate the timer with the toast view
|
||||
objc_setAssociatedObject (toast, &CSToastTimerKey, timer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
||||
}];
|
||||
|
||||
|
||||
}
|
||||
|
||||
- (UIViewController*) getTopMostViewController {
|
||||
UIViewController *presentingViewController = [[[UIApplication sharedApplication] delegate] window].rootViewController;
|
||||
while (presentingViewController.presentedViewController != nil) {
|
||||
presentingViewController = presentingViewController.presentedViewController;
|
||||
}
|
||||
return presentingViewController;
|
||||
}
|
||||
|
||||
- (void)hideToast {
|
||||
@@ -156,8 +187,17 @@ static UIView *prevToast = NULL;
|
||||
- (void)handleToastTapped:(UITapGestureRecognizer *)recognizer {
|
||||
NSTimer *timer = (NSTimer *)objc_getAssociatedObject(self, &CSToastTimerKey);
|
||||
[timer invalidate];
|
||||
|
||||
|
||||
[self hideToast:recognizer.view];
|
||||
|
||||
// also send an event back to JS
|
||||
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:msg, @"message", @"touch", @"event", nil];
|
||||
if (data != nil) {
|
||||
[dict setObject:data forKey:@"data"];
|
||||
}
|
||||
|
||||
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dict];
|
||||
[commandDelegate sendPluginResult:pluginResult callbackId:callbackId];
|
||||
}
|
||||
|
||||
#pragma mark - Toast Activity Methods
|
||||
@@ -170,31 +210,31 @@ static UIView *prevToast = NULL;
|
||||
// sanity
|
||||
UIView *existingActivityView = (UIView *)objc_getAssociatedObject(self, &CSToastActivityViewKey);
|
||||
if (existingActivityView != nil) return;
|
||||
|
||||
|
||||
UIView *activityView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CSToastActivityWidth, CSToastActivityHeight)];
|
||||
activityView.center = [self centerPointForPosition:position withToast:activityView withAddedPixelsY:0];
|
||||
activityView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:CSToastOpacity];
|
||||
activityView.alpha = 0.0;
|
||||
activityView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin);
|
||||
activityView.layer.cornerRadius = CSToastCornerRadius;
|
||||
|
||||
|
||||
if (CSToastDisplayShadow) {
|
||||
activityView.layer.shadowColor = [UIColor blackColor].CGColor;
|
||||
activityView.layer.shadowOpacity = CSToastShadowOpacity;
|
||||
activityView.layer.shadowRadius = CSToastShadowRadius;
|
||||
activityView.layer.shadowOffset = CSToastShadowOffset;
|
||||
}
|
||||
|
||||
|
||||
UIActivityIndicatorView *activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
|
||||
activityIndicatorView.center = CGPointMake(activityView.bounds.size.width / 2, activityView.bounds.size.height / 2);
|
||||
[activityView addSubview:activityIndicatorView];
|
||||
[activityIndicatorView startAnimating];
|
||||
|
||||
|
||||
// associate the activity view with self
|
||||
objc_setAssociatedObject (self, &CSToastActivityViewKey, activityView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
||||
|
||||
|
||||
[self addSubview:activityView];
|
||||
|
||||
|
||||
[UIView animateWithDuration:CSToastFadeDuration
|
||||
delay:0.0
|
||||
options:UIViewAnimationOptionCurveEaseOut
|
||||
@@ -233,7 +273,7 @@ static UIView *prevToast = NULL;
|
||||
} else if ([point isKindOfClass:[NSValue class]]) {
|
||||
return [point CGPointValue];
|
||||
}
|
||||
|
||||
|
||||
NSLog(@"Warning: Invalid position for toast.");
|
||||
return [self centerPointForPosition:CSToastDefaultPosition withToast:toast withAddedPixelsY:addPixelsY];
|
||||
}
|
||||
@@ -247,7 +287,10 @@ static UIView *prevToast = NULL;
|
||||
return CGSizeMake(ceilf(boundingRect.size.width), ceilf(boundingRect.size.height));
|
||||
}
|
||||
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
||||
return [string sizeWithFont:font constrainedToSize:constrainedSize lineBreakMode:lineBreakMode];
|
||||
#pragma clang diagnostic pop
|
||||
}
|
||||
|
||||
- (UIView *)viewForMessage:(NSString *)message title:(NSString *)title image:(UIImage *)image {
|
||||
@@ -262,7 +305,9 @@ static UIView *prevToast = NULL;
|
||||
// create the parent view
|
||||
UIView *wrapperView = [[UIView alloc] init];
|
||||
wrapperView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin);
|
||||
wrapperView.layer.cornerRadius = CSToastCornerRadius;
|
||||
|
||||
NSNumber * cornerRadius = styling[@"cornerRadius"];
|
||||
wrapperView.layer.cornerRadius = cornerRadius == nil ? CSToastCornerRadius : [cornerRadius floatValue];
|
||||
|
||||
if (CSToastDisplayShadow) {
|
||||
wrapperView.layer.shadowColor = [UIColor blackColor].CGColor;
|
||||
@@ -271,12 +316,23 @@ static UIView *prevToast = NULL;
|
||||
wrapperView.layer.shadowOffset = CSToastShadowOffset;
|
||||
}
|
||||
|
||||
wrapperView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:CSToastOpacity];
|
||||
NSString * backgroundColor = styling[@"backgroundColor"];
|
||||
UIColor *theColor = backgroundColor == nil ? [UIColor blackColor] : [self colorFromHexString:backgroundColor];
|
||||
|
||||
NSNumber * opacity = styling[@"opacity"];
|
||||
CGFloat theOpacity = opacity == nil ? CSToastOpacity : [opacity floatValue];
|
||||
|
||||
NSNumber * horizontalPadding = styling[@"horizontalPadding"];
|
||||
NSNumber * verticalPadding = styling[@"verticalPadding"];
|
||||
CGFloat theHorizontalPadding = horizontalPadding == nil ? CSToastHorizontalPadding : [horizontalPadding floatValue];
|
||||
CGFloat theVerticalPadding = verticalPadding == nil ? CSToastVerticalPadding : [verticalPadding floatValue];
|
||||
|
||||
wrapperView.backgroundColor = [theColor colorWithAlphaComponent:theOpacity];
|
||||
|
||||
if(image != nil) {
|
||||
imageView = [[UIImageView alloc] initWithImage:image];
|
||||
imageView.contentMode = UIViewContentModeScaleAspectFit;
|
||||
imageView.frame = CGRectMake(CSToastHorizontalPadding, CSToastVerticalPadding, CSToastImageViewWidth, CSToastImageViewHeight);
|
||||
imageView.frame = CGRectMake(theHorizontalPadding, theVerticalPadding, CSToastImageViewWidth, CSToastImageViewHeight);
|
||||
}
|
||||
|
||||
CGFloat imageWidth, imageHeight, imageLeft;
|
||||
@@ -285,18 +341,21 @@ static UIView *prevToast = NULL;
|
||||
if(imageView != nil) {
|
||||
imageWidth = imageView.bounds.size.width;
|
||||
imageHeight = imageView.bounds.size.height;
|
||||
imageLeft = CSToastHorizontalPadding;
|
||||
imageLeft = theHorizontalPadding;
|
||||
} else {
|
||||
imageWidth = imageHeight = imageLeft = 0.0;
|
||||
}
|
||||
|
||||
if (title != nil) {
|
||||
NSString * titleLabelTextColor = styling[@"textColor"];
|
||||
UIColor *theTitleLabelTextColor = titleLabelTextColor == nil ? [UIColor whiteColor] : [self colorFromHexString:titleLabelTextColor];
|
||||
|
||||
titleLabel = [[UILabel alloc] init];
|
||||
titleLabel.numberOfLines = CSToastMaxTitleLines;
|
||||
titleLabel.font = [UIFont boldSystemFontOfSize:CSToastFontSize];
|
||||
titleLabel.textAlignment = NSTextAlignmentLeft;
|
||||
titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
|
||||
titleLabel.textColor = [UIColor whiteColor];
|
||||
titleLabel.textColor = theTitleLabelTextColor;
|
||||
titleLabel.backgroundColor = [UIColor clearColor];
|
||||
titleLabel.alpha = 1.0;
|
||||
titleLabel.text = title;
|
||||
@@ -308,11 +367,14 @@ static UIView *prevToast = NULL;
|
||||
}
|
||||
|
||||
if (message != nil) {
|
||||
NSString * messageLabelTextColor = styling[@"textColor"];
|
||||
UIColor *theMessageLabelTextColor = messageLabelTextColor == nil ? [UIColor whiteColor] : [self colorFromHexString:messageLabelTextColor];
|
||||
|
||||
messageLabel = [[UILabel alloc] init];
|
||||
messageLabel.numberOfLines = CSToastMaxMessageLines;
|
||||
messageLabel.font = [UIFont systemFontOfSize:CSToastFontSize];
|
||||
messageLabel.lineBreakMode = NSLineBreakByWordWrapping;
|
||||
messageLabel.textColor = [UIColor whiteColor];
|
||||
messageLabel.textColor = theMessageLabelTextColor;
|
||||
messageLabel.backgroundColor = [UIColor clearColor];
|
||||
messageLabel.alpha = 1.0;
|
||||
messageLabel.text = message;
|
||||
@@ -329,8 +391,8 @@ static UIView *prevToast = NULL;
|
||||
if(titleLabel != nil) {
|
||||
titleWidth = titleLabel.bounds.size.width;
|
||||
titleHeight = titleLabel.bounds.size.height;
|
||||
titleTop = CSToastVerticalPadding;
|
||||
titleLeft = imageLeft + imageWidth + CSToastHorizontalPadding;
|
||||
titleTop = theVerticalPadding;
|
||||
titleLeft = imageLeft + imageWidth + theHorizontalPadding;
|
||||
} else {
|
||||
titleWidth = titleHeight = titleTop = titleLeft = 0.0;
|
||||
}
|
||||
@@ -341,8 +403,8 @@ static UIView *prevToast = NULL;
|
||||
if(messageLabel != nil) {
|
||||
messageWidth = messageLabel.bounds.size.width;
|
||||
messageHeight = messageLabel.bounds.size.height;
|
||||
messageLeft = imageLeft + imageWidth + CSToastHorizontalPadding;
|
||||
messageTop = titleTop + titleHeight + CSToastVerticalPadding;
|
||||
messageLeft = imageLeft + imageWidth + theHorizontalPadding;
|
||||
messageTop = titleTop + titleHeight + theVerticalPadding;
|
||||
} else {
|
||||
messageWidth = messageHeight = messageLeft = messageTop = 0.0;
|
||||
}
|
||||
@@ -351,8 +413,8 @@ static UIView *prevToast = NULL;
|
||||
CGFloat longerLeft = MAX(titleLeft, messageLeft);
|
||||
|
||||
// wrapper width uses the longerWidth or the image width, whatever is larger. same logic applies to the wrapper height
|
||||
CGFloat wrapperWidth = MAX((imageWidth + (CSToastHorizontalPadding * 2)), (longerLeft + longerWidth + CSToastHorizontalPadding));
|
||||
CGFloat wrapperHeight = MAX((messageTop + messageHeight + CSToastVerticalPadding), (imageHeight + (CSToastVerticalPadding * 2)));
|
||||
CGFloat wrapperWidth = MAX((imageWidth + (theHorizontalPadding * 2)), (longerLeft + longerWidth + theHorizontalPadding));
|
||||
CGFloat wrapperHeight = MAX((messageTop + messageHeight + theVerticalPadding), (imageHeight + (theVerticalPadding * 2)));
|
||||
|
||||
wrapperView.frame = CGRectMake(0.0, 0.0, wrapperWidth, wrapperHeight);
|
||||
|
||||
@@ -373,4 +435,13 @@ static UIView *prevToast = NULL;
|
||||
return wrapperView;
|
||||
}
|
||||
|
||||
// Assumes input like "#00FF00" (#RRGGBB)
|
||||
- (UIColor*) colorFromHexString:(NSString*) hexString {
|
||||
unsigned rgbValue = 0;
|
||||
NSScanner *scanner = [NSScanner scannerWithString:hexString];
|
||||
[scanner setScanLocation:1]; // bypass '#' character
|
||||
[scanner scanHexInt:&rgbValue];
|
||||
return [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16) / 255.0 green:((rgbValue & 0xFF00) >> 8) / 255.0 blue:(rgbValue & 0xFF) / 255.0 alpha:1.0];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
+16
-6
@@ -6,12 +6,14 @@
|
||||
|
||||
- (void)show:(CDVInvokedUrlCommand*)command {
|
||||
|
||||
NSDictionary* options = [command.arguments objectAtIndex:0];
|
||||
NSDictionary* options = [command argumentAtIndex:0];
|
||||
|
||||
NSString *message = [options objectForKey:@"message"];
|
||||
NSString *duration = [options objectForKey:@"duration"];
|
||||
NSString *position = [options objectForKey:@"position"];
|
||||
NSNumber *addPixelsY = [options objectForKey:@"addPixelsY"];
|
||||
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'"];
|
||||
@@ -30,9 +32,17 @@
|
||||
return;
|
||||
}
|
||||
|
||||
[self.webView makeToast:message duration:durationInt position:position addPixelsY:addPixelsY == nil ? 0 : [addPixelsY intValue]];
|
||||
[self.webView makeToast:message
|
||||
duration:durationInt
|
||||
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];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user