#20 Click event

This commit is contained in:
EddyVerbruggen 2015-12-08 10:18:20 +01:00
parent ecb47ab110
commit 335f731da4
7 changed files with 83 additions and 9 deletions

View File

@ -151,7 +151,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",
@ -174,6 +174,33 @@ function hide() {
}
```
### 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 // added a negative value to move it up a bit (default 0)
},
// 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
} else {
console.log("The toast has been shown");
}
}
);
```
### 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.
@ -188,6 +215,8 @@ 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.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

View File

@ -1,6 +1,6 @@
{
"name": "cordova-plugin-x-toast",
"version": "2.2.3",
"version": "2.3.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",

View File

@ -2,7 +2,7 @@
<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.2.3">
version="2.3.0">
<name>Toast</name>

View File

@ -2,9 +2,12 @@ package nl.xservices.plugins;
import android.os.Build;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
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;
@ -77,9 +80,31 @@ public class Toast extends CordovaPlugin {
return;
}
toast.getView().setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
JSONObject json = new JSONObject();
try {
json.put("event", "touch");
json.put("message", message);
} catch (JSONException e) {
e.printStackTrace();
}
callbackContext.success(json);
return true;
} else {
return false;
}
}
});
toast.show();
mostRecentToast = toast;
callbackContext.success();
PluginResult pr = new PluginResult(PluginResult.Status.OK);
pr.setKeepCallback(true);
callbackContext.sendPluginResult(pr);
}
});

View File

@ -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 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;

View File

@ -48,6 +48,11 @@ static const NSString * CSToastActivityViewKey = @"CSToastActivityViewKey";
static UIView *prevToast = NULL;
// doesn't matter these are static
static id commandDelegate;
static id callbackId;
static id msg;
@interface UIView (ToastPrivate)
- (void)hideToast:(UIView *)toast;
@ -56,7 +61,6 @@ 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
@ -73,7 +77,10 @@ static UIView *prevToast = NULL;
[self showToast:toast duration:duration position: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 commandDelegate:(id <CDVCommandDelegate>)_commandDelegate callbackId:(NSString *)_callbackId {
commandDelegate = _commandDelegate;
callbackId = _callbackId;
msg = message;
UIView *toast = [self viewForMessage:message title:nil image:nil];
[self showToast:toast duration:duration position:position addedPixelsY:addPixelsY];
}
@ -101,12 +108,13 @@ 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];
@ -168,6 +176,11 @@ static UIView *prevToast = NULL;
[timer invalidate];
[self hideToast:recognizer.view];
// also send an event back to JS
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:msg, @"message", @"touch", @"event", nil];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dict];
[commandDelegate sendPluginResult:pluginResult callbackId:callbackId];
}
#pragma mark - Toast Activity Methods

View File

@ -30,9 +30,15 @@
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]
commandDelegate:self.commandDelegate
callbackId:command.callbackId];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
pluginResult.keepCallback = [NSNumber numberWithBool:YES];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}