diff --git a/README.md b/README.md index 8b4c652..4aa5fe5 100644 --- a/README.md +++ b/README.md @@ -186,7 +186,8 @@ called again. You can distinguish between those events of course: message: "hey there", duration: "short", position: "bottom", - addPixelsY: -40 // added a negative value to move it up a bit (default 0) + 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) { @@ -194,6 +195,7 @@ called again. You can distinguish between those events of course: 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"); } diff --git a/package.json b/package.json index 8d68891..5a5d7b0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cordova-plugin-x-toast", - "version": "2.3.0", + "version": "2.3.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 d0e99d3..a1fff4b 100755 --- a/plugin.xml +++ b/plugin.xml @@ -2,7 +2,7 @@ + version="2.3.1"> Toast diff --git a/src/android/nl/xservices/plugins/Toast.java b/src/android/nl/xservices/plugins/Toast.java index 6e2a560..ec5cdae 100644 --- a/src/android/nl/xservices/plugins/Toast.java +++ b/src/android/nl/xservices/plugins/Toast.java @@ -55,6 +55,7 @@ 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; cordova.getActivity().runOnUiThread(new Runnable() { public void run() { @@ -88,6 +89,7 @@ public class Toast extends CordovaPlugin { try { json.put("event", "touch"); json.put("message", message); + json.put("data", data); } catch (JSONException e) { e.printStackTrace(); } diff --git a/src/ios/Toast+UIView.h b/src/ios/Toast+UIView.h index 6ba8928..346f853 100644 --- a/src/ios/Toast+UIView.h +++ b/src/ios/Toast+UIView.h @@ -6,7 +6,7 @@ // 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 commandDelegate:(id )commandDelegate callbackId:(NSString *)callbackId; +- (void)makeToast:(NSString *)message duration:(NSTimeInterval)duration position:(id)position addPixelsY:(int)addPixelsY data:(NSDictionary*)data commandDelegate:(id )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; diff --git a/src/ios/Toast+UIView.m b/src/ios/Toast+UIView.m index bf165b2..83b32d6 100644 --- a/src/ios/Toast+UIView.m +++ b/src/ios/Toast+UIView.m @@ -52,6 +52,7 @@ static UIView *prevToast = NULL; static id commandDelegate; static id callbackId; static id msg; +static id data; @interface UIView (ToastPrivate) @@ -66,6 +67,7 @@ static id msg; @implementation UIView (Toast) + #pragma mark - Toast Methods - (void)makeToast:(NSString *)message { @@ -77,10 +79,11 @@ static id msg; [self showToast:toast duration:duration position:position]; } -- (void)makeToast:(NSString *)message duration:(NSTimeInterval)duration position:(id)position addPixelsY:(int)addPixelsY commandDelegate:(id )_commandDelegate callbackId:(NSString *)_callbackId { +- (void)makeToast:(NSString *)message duration:(NSTimeInterval)duration position:(id)position addPixelsY:(int)addPixelsY data:(NSDictionary*)_data commandDelegate:(id )_commandDelegate callbackId:(NSString *)_callbackId { commandDelegate = _commandDelegate; callbackId = _callbackId; msg = message; + data = _data; UIView *toast = [self viewForMessage:message title:nil image:nil]; [self showToast:toast duration:duration position:position addedPixelsY:addPixelsY]; } @@ -178,7 +181,11 @@ static id msg; [self hideToast:recognizer.view]; // also send an event back to JS - NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:msg, @"message", @"touch", @"event", nil]; + 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]; } diff --git a/src/ios/Toast.m b/src/ios/Toast.m index 43f6b8b..54e59b4 100644 --- a/src/ios/Toast.m +++ b/src/ios/Toast.m @@ -11,6 +11,7 @@ NSString *message = [options objectForKey:@"message"]; NSString *duration = [options objectForKey:@"duration"]; NSString *position = [options objectForKey:@"position"]; + NSDictionary *data = [options objectForKey:@"data"]; NSNumber *addPixelsY = [options objectForKey:@"addPixelsY"]; if (![position isEqual: @"top"] && ![position isEqual: @"center"] && ![position isEqual: @"bottom"]) { @@ -34,6 +35,7 @@ duration:durationInt position:position addPixelsY:addPixelsY == nil ? 0 : [addPixelsY intValue] + data:data commandDelegate:self.commandDelegate callbackId:command.callbackId];