Skip to content

Commit f00fba5

Browse files
committed
Merge pull request #46 from BranchMetrics/project-restructure
Project restructure
2 parents 91cff91 + b89bb4a commit f00fba5

File tree

7 files changed

+275
-35
lines changed

7 files changed

+275
-35
lines changed

README.md

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,54 @@ branchUniversalObj.showShareSheet({
412412
});
413413
```
414414

415+
##### Share Sheet Callbacks (Android ONLY)
416+
417+
To implement the callback, you must add listeners to the following events:
418+
419+
###### onShareSheetLaunched
420+
421+
The event fires when the share sheet is presented.
422+
423+
```js
424+
branchUniversalObj.onShareSheetLaunched(function () {
425+
console.log('Share sheet launched');
426+
});
427+
```
428+
429+
###### onShareSheetDismissed
430+
431+
The event fires when the share sheet is dismissed.
432+
433+
```js
434+
branchUniversalObj.onShareSheetDismissed(function () {
435+
console.log('Share sheet dimissed');
436+
});
437+
```
438+
439+
###### onLinkShareResponse
440+
441+
The event returns a dictionary of the response data.
442+
443+
```js
444+
branchUniversalObj.onLinkShareResponse(function (res) {
445+
console.log('Share link response: ' + JSON.stringify(res));
446+
});
447+
```
448+
449+
###### onChannelSelected
450+
451+
The event fires when a channel is selected.
452+
453+
```js
454+
branchUniversalObj.onChannelSelected(function (res) {
455+
console.log('Channel selected: ' + JSON.stringify(res));
456+
});
457+
```
458+
459+
**Note:** Callbacks in iOS are ignored. There is no need to implement them as the events are handled by `UIActivityViewController`.
460+
461+
**Note:** Avoid passing `alias` in iOS. Adding an `alias` key in the `options` parameter will return a Non-Universal link which will not work in iOS 9.2.
462+
415463
### <a id="listOnSpotlight"></a>listOnSpotlight()
416464

417465
**Note: iOS only.** Used for Spotlight listing
@@ -452,11 +500,14 @@ Redeems a reward with the given amount/value.
452500

453501
**Parameters**
454502

455-
**value**: `int` - Amount to be redeemed.
503+
| KEY | TYPE | MEANING
504+
| -------- | -------- |------------------------
505+
| value | `int` | Amount to be redeemed.
506+
| bucket | `int` | Bucket where the amount will be redeemed. _optional_
456507

457508
##### Usage
458509
```js
459-
Branch.redeemRewards(100).then(function (res) {
510+
Branch.redeemRewards(100, "default").then(function (res) {
460511
// Success Callback
461512
console.log(res);
462513
}).catch(function (err) {

src/android/io/branch/BranchSDK.java

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ static class BranchLinkProperties extends io.branch.referral.util.LinkProperties
3131
// Private Method Properties
3232
private BranchUniversalObject branchObj;
3333
private CallbackContext callbackContext;
34+
private CallbackContext onShareLinkDialogLaunched;
35+
private CallbackContext onShareLinkDialogDismissed;
36+
private CallbackContext onLinkShareResponse;
37+
private CallbackContext onChannelSelected;
3438
private Activity activity;
3539
private Branch instance;
3640

@@ -43,6 +47,10 @@ public BranchSDK()
4347
this.instance = null;
4448
this.branchObj = null;
4549
this.callbackContext = null;
50+
this.onShareLinkDialogLaunched = null;
51+
this.onShareLinkDialogDismissed = null;
52+
this.onLinkShareResponse = null;
53+
this.onChannelSelected = null;
4654
}
4755

4856
/**
@@ -134,7 +142,11 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
134142
this.loadRewards();
135143
return true;
136144
} else if (action.equals("redeemRewards")) {
137-
this.redeemRewards(args.getInt(0));
145+
if (args.length() == 1) {
146+
this.redeemRewards(args.getInt(0));
147+
} else if (args.length() == 2) {
148+
this.redeemRewards(args.getInt(0), args.getString(1));
149+
}
138150
return true;
139151
} else if (action.equals("getCreditHistory")) {
140152
this.getCreditHistory();
@@ -151,6 +163,18 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
151163
} else if (action.equals("showShareSheet")) {
152164
this.showShareSheet(args.getJSONObject(0), args.getJSONObject(1));
153165
return true;
166+
} else if (action.equals("onShareLinkDialogLaunched")) {
167+
this.onShareLinkDialogLaunched = callbackContext;
168+
return true;
169+
} else if (action.equals("onShareLinkDialogDismissed")) {
170+
this.onShareLinkDialogDismissed = callbackContext;
171+
return true;
172+
} else if (action.equals("onLinkShareResponse")) {
173+
this.onLinkShareResponse = callbackContext;
174+
return true;
175+
} else if (action.equals("onChannelSelected")) {
176+
this.onChannelSelected = callbackContext;
177+
return true;
154178
}
155179
}
156180
}
@@ -200,7 +224,7 @@ private void logout()
200224
* @param count A {@link Integer} specifying the number of credits to attempt to redeem from
201225
* the bucket.
202226
*/
203-
private void redeemRewards(int value)
227+
private void redeemRewards(final int value)
204228
{
205229

206230
Log.d(LCAT, "start redeemRewards()");
@@ -209,6 +233,23 @@ private void redeemRewards(int value)
209233

210234
}
211235

236+
/**
237+
* <p>Redeems the specified number of credits from the "default" bucket, if there are sufficient
238+
* credits within it. If the number to redeem exceeds the number available in the bucket, all of
239+
* the available credits will be redeemed instead.</p>
240+
*
241+
* @param count A {@link Integer} specifying the number of credits to attempt to redeem from
242+
* the bucket.
243+
*/
244+
private void redeemRewards(int value, String bucket)
245+
{
246+
247+
Log.d(LCAT, "start redeemRewards()");
248+
249+
this.instance.redeemRewards(bucket, value, new LoadRewardsListener());
250+
251+
}
252+
212253
/**
213254
* <p>Retrieves rewards for the current session, with a callback to perform a predefined
214255
* action following successful report of state change. You'll then need to call getCredits
@@ -730,11 +771,29 @@ protected class ShowShareSheetListener implements Branch.BranchLinkShareListener
730771
@Override
731772
public void onShareLinkDialogLaunched() {
732773
Log.d(LCAT, "inside onShareLinkDialogLaunched");
774+
775+
if (onShareLinkDialogLaunched != null) {
776+
PluginResult result = new PluginResult(PluginResult.Status.OK);
777+
778+
result.setKeepCallback(true);
779+
780+
onShareLinkDialogLaunched.sendPluginResult(result);
781+
}
782+
733783
}
734784

735785
@Override
736786
public void onShareLinkDialogDismissed() {
737787
Log.d(LCAT, "inside onShareLinkDialogDismissed");
788+
789+
if (onShareLinkDialogDismissed != null) {
790+
PluginResult result = new PluginResult(PluginResult.Status.OK);
791+
792+
result.setKeepCallback(true);
793+
794+
onShareLinkDialogDismissed.sendPluginResult(result);
795+
}
796+
738797
}
739798

740799
@Override
@@ -771,6 +830,14 @@ public void onLinkShareResponse(String sharedLink, String sharedChannel, BranchE
771830

772831
Log.d(LCAT, response.toString());
773832

833+
if (onLinkShareResponse != null) {
834+
PluginResult result = new PluginResult(PluginResult.Status.OK, response);
835+
836+
result.setKeepCallback(true);
837+
838+
onLinkShareResponse.sendPluginResult(result);
839+
}
840+
774841
}
775842

776843
@Override
@@ -790,6 +857,14 @@ public void onChannelSelected(String channelName) {
790857

791858
Log.d(LCAT, response.toString());
792859

860+
if (onChannelSelected != null) {
861+
PluginResult result = new PluginResult(PluginResult.Status.OK, response);
862+
863+
result.setKeepCallback(true);
864+
865+
onChannelSelected.sendPluginResult(result);
866+
}
867+
793868
}
794869
}
795870

src/ios/BranchSDK.m

Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -280,34 +280,68 @@ - (void)loadRewards:(CDVInvokedUrlCommand*)command
280280
- (void)redeemRewards:(CDVInvokedUrlCommand*)command
281281
{
282282
NSLog(@"start redeemRewards");
283+
283284
NSInteger amount = ((NSNumber *)[command.arguments objectAtIndex:0]).integerValue;
284285
Branch *branch = [self getInstance];
285286

286-
[branch redeemRewards:amount callback:^(BOOL changed, NSError *error) {
287-
NSLog(@"inside redeemRewards block");
288-
CDVPluginResult* pluginResult = nil;
289-
if (!error) {
290-
NSNumber *changedValue = [NSNumber numberWithBool:changed];
291-
NSError *err;
292-
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:@{@"changed":changedValue}
293-
options:0
294-
error:&err];
295-
if (!jsonData) {
296-
NSLog(@"Parsing Error: %@", [err localizedDescription]);
297-
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[err localizedDescription]];
298-
} else {
299-
NSLog(@"Success");
300-
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
301-
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:jsonString];
287+
if ([command.arguments count] == 2) {
288+
289+
NSString *bucket = [command.arguments objectAtIndex:1];
290+
291+
[branch redeemRewards:(NSInteger)amount forBucket:(NSString *)bucket callback:^(BOOL changed, NSError *error) {
292+
NSLog(@"inside redeemRewards:forBucket block");
293+
CDVPluginResult* pluginResult = nil;
294+
if (!error) {
295+
NSNumber *changedValue = [NSNumber numberWithBool:changed];
296+
NSError *err;
297+
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:@{@"changed":changedValue}
298+
options:0
299+
error:&err];
300+
if (!jsonData) {
301+
NSLog(@"Parsing Error: %@", [err localizedDescription]);
302+
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[err localizedDescription]];
303+
} else {
304+
NSLog(@"Success");
305+
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
306+
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:jsonString];
307+
}
302308
}
303-
}
304-
else {
305-
NSLog(@"Redeem Rewards Error: %@", [error localizedDescription]);
306-
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[error localizedDescription]];
307-
}
308-
NSLog(@"returning data to js interface..");
309-
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
310-
}];
309+
else {
310+
NSLog(@"Redeem Rewards Error: %@", [error localizedDescription]);
311+
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[error localizedDescription]];
312+
}
313+
NSLog(@"returning data to js interface..");
314+
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
315+
}];
316+
} else {
317+
[branch redeemRewards:amount callback:^(BOOL changed, NSError *error) {
318+
NSLog(@"inside redeemRewards block");
319+
CDVPluginResult* pluginResult = nil;
320+
if (!error) {
321+
NSNumber *changedValue = [NSNumber numberWithBool:changed];
322+
NSError *err;
323+
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:@{@"changed":changedValue}
324+
options:0
325+
error:&err];
326+
if (!jsonData) {
327+
NSLog(@"Parsing Error: %@", [err localizedDescription]);
328+
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[err localizedDescription]];
329+
} else {
330+
NSLog(@"Success");
331+
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
332+
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:jsonString];
333+
}
334+
}
335+
else {
336+
NSLog(@"Redeem Rewards Error: %@", [error localizedDescription]);
337+
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[error localizedDescription]];
338+
}
339+
NSLog(@"returning data to js interface..");
340+
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
341+
}];
342+
}
343+
344+
311345
}
312346

313347
- (void)getCreditHistory:(CDVInvokedUrlCommand*)command

src/ios/dependencies/Branch.framework/Versions/A/Headers/BNCServerRequestQueue.h

100755100644
File mode changed.

testbed/init.sh

100755100644
File mode changed.

testbed/www/js/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,21 @@ function ShowShareSheet()
232232
};
233233

234234
branchUniversalObj.showShareSheet(properties, controlParams);
235+
236+
// Set listeners
237+
branchUniversalObj.onShareSheetLaunched(function () {
238+
console.log('Share sheet launched');
239+
});
240+
branchUniversalObj.onShareSheetDismissed(function () {
241+
console.log('Share sheet dimissed');
242+
});
243+
branchUniversalObj.onLinkShareResponse(function (res) {
244+
console.log('Share link response: ' + JSON.stringify(res));
245+
});
246+
branchUniversalObj.onChannelSelected(function (res) {
247+
console.log('Channel selected: ' + JSON.stringify(res));
248+
});
249+
235250
}
236251

237252
function ListOnSpotlight()

0 commit comments

Comments
 (0)