mirror of
https://github.com/EddyVerbruggen/Toast-PhoneGap-Plugin.git
synced 2026-07-31 00:02:14 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ecb47ab110 | ||
|
|
f8887dc45e | ||
|
|
273334664a | ||
|
|
6ee16e9e7d | ||
|
|
03320113b4 | ||
|
|
5634e1d84c | ||
|
|
2add9bef7b | ||
|
|
c252764372 | ||
|
|
ba7c60f324 | ||
|
|
eed523111b | ||
|
|
e0420e0aef | ||
|
|
8c10bcaec5 | ||
|
|
5d9aa4f187 | ||
|
|
50290d83de |
@@ -63,18 +63,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 +119,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.
|
||||
@@ -177,7 +174,7 @@ function hide() {
|
||||
}
|
||||
```
|
||||
|
||||
### WP8 quircks
|
||||
### 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.
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
{
|
||||
"name": "cordova-plugin-x-toast",
|
||||
"version": "2.2.3",
|
||||
"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.2.3">
|
||||
|
||||
<name>Toast</name>
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package nl.xservices.plugins;
|
||||
|
||||
import android.os.Build;
|
||||
import android.view.Gravity;
|
||||
|
||||
import org.apache.cordova.CallbackContext;
|
||||
import org.apache.cordova.CordovaPlugin;
|
||||
import org.json.JSONArray;
|
||||
@@ -24,6 +26,8 @@ public class Toast extends CordovaPlugin {
|
||||
|
||||
private android.widget.Toast mostRecentToast;
|
||||
|
||||
private static final boolean IS_AT_LEAST_ANDROID5 = Build.VERSION.SDK_INT >= 21;
|
||||
|
||||
// note that webView.isPaused() is not Xwalk compatible, so tracking it poor-man style
|
||||
private boolean isPaused;
|
||||
|
||||
@@ -52,10 +56,16 @@ public class Toast extends CordovaPlugin {
|
||||
cordova.getActivity().runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
android.widget.Toast toast = android.widget.Toast.makeText(
|
||||
webView.getContext(),
|
||||
IS_AT_LEAST_ANDROID5 ? cordova.getActivity().getWindow().getContext() : cordova.getActivity().getApplicationContext(),
|
||||
message,
|
||||
"short".equals(duration) ? android.widget.Toast.LENGTH_SHORT : android.widget.Toast.LENGTH_LONG);
|
||||
|
||||
// if we want to change the background color some day, we can use this
|
||||
// try {
|
||||
// final Method setTintMethod = Drawable.class.getMethod("setTint", int.class);
|
||||
// setTintMethod.invoke(toast.getView().getBackground(), Color.RED); // default is Color.DKGRAY
|
||||
// } catch (Exception ignore) {
|
||||
// }
|
||||
if ("top".equals(position)) {
|
||||
toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 20 + addPixelsY);
|
||||
} else if ("bottom".equals(position)) {
|
||||
@@ -92,4 +102,4 @@ public class Toast extends CordovaPlugin {
|
||||
public void onResume(boolean multitasking) {
|
||||
this.isPaused = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+29
-16
@@ -80,17 +80,17 @@ static UIView *prevToast = NULL;
|
||||
|
||||
- (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 {
|
||||
@@ -106,17 +106,19 @@ static UIView *prevToast = NULL;
|
||||
prevToast = toast;
|
||||
toast.center = [self centerPointForPosition:point withToast:toast withAddedPixelsY:addPixelsY];
|
||||
toast.alpha = 0.0;
|
||||
|
||||
|
||||
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 +129,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,7 +166,7 @@ static UIView *prevToast = NULL;
|
||||
- (void)handleToastTapped:(UITapGestureRecognizer *)recognizer {
|
||||
NSTimer *timer = (NSTimer *)objc_getAssociatedObject(self, &CSToastTimerKey);
|
||||
[timer invalidate];
|
||||
|
||||
|
||||
[self hideToast:recognizer.view];
|
||||
}
|
||||
|
||||
@@ -170,31 +180,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 +243,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 +257,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 {
|
||||
|
||||
Reference in New Issue
Block a user