Skip to content

Commit

Permalink
Improved loadRewards to be able to specify a specific bucket to load.
Browse files Browse the repository at this point in the history
Signed-off-by: Renemari Padillo <rene@ingenuity.ph>
  • Loading branch information
Renemari Padillo committed Jul 31, 2016
1 parent 895ff9e commit c035768
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 49 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -554,10 +554,14 @@ branchUniversalObj.listOnSpotlight().then(function (res) {

## Referral System Rewarding

### <a id="loadRewards"></a>loadRewards()
### <a id="loadRewards"></a>loadRewards([bucket])

Reward balances change randomly on the backend when certain actions are taken (defined by your rules), so you'll need to make an asynchronous call to retrieve the balance. Here is the syntax:

**Parameters**

**bucket**: `String` _[Optional]_ - Load rewards of a specific bucket. If no value provided it will use the `default` bucket.

##### Usage
```js
Branch.loadRewards().then(function (rewards) {
Expand Down
25 changes: 19 additions & 6 deletions src/android/io/branch/BranchSDK.java
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,12 @@ private void redeemRewards(int value, String bucket, CallbackContext callbackCon
* in the callback to update the credit totals in your UX.</p>
*
* @param callbackContext A callback to execute at the end of this method
* @param bucket Load reward of a specific bucket
*/
private void loadRewards(CallbackContext callbackContext)
private void loadRewards(CallbackContext callbackContext, String bucket)
{

this.instance.loadRewards(new LoadRewardsListener(callbackContext));
this.instance.loadRewards(new LoadRewardsListener(callbackContext, this.instance, bucket));

}

Expand Down Expand Up @@ -802,19 +803,29 @@ public void onRegisterViewFinished(boolean registered, BranchError error) {
protected class LoadRewardsListener implements Branch.BranchReferralStateChangedListener
{
private CallbackContext _callbackContext;
private Branch _instance;
private String _bucket;

// Constructor that takes in a required callbackContext object
public LoadRewardsListener(CallbackContext callbackContext) {
public LoadRewardsListener(CallbackContext callbackContext, Branch instance, String bucket) {
this._callbackContext = callbackContext;
this._instance = instance;
this._bucket = bucket;
}

// Listener that implements BranchReferralStateChangedListener for loadRewards
@Override
public void onStateChanged(boolean changed, BranchError error) {
if (error == null) {

int credits = instance.getCredits();

int credits = 0;

if (this._bucket.length()) {
credits = this._instance.getCreditsForBucket(this._bucket);
} else {
credits = this._instance.getCredits();
}

this._callbackContext.success(credits);

} else {
Expand Down Expand Up @@ -1127,7 +1138,9 @@ public void run() {
} else if (this.action.equals("logout")) {
logout(this.callbackContext);
} else if (this.action.equals("loadRewards")) {
loadRewards(this.callbackContext);
if (this.args.length() == 1) {
loadRewards(this.callbackContext, this.args.getString(0));
}
} else if (this.action.equals("redeemRewards")) {
if (this.args.length() == 1) {
redeemRewards(this.args.getInt(0), this.callbackContext);
Expand Down
14 changes: 13 additions & 1 deletion src/ios/BranchSDK.m
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,24 @@ - (void)logout:(CDVInvokedUrlCommand*)command
- (void)loadRewards:(CDVInvokedUrlCommand*)command
{
Branch *branch = [self getInstance];
NSString *bucket = @"";

if ([command.arguments count] == 1) {
bucket = [command.arguments objectAtIndex:0];
}

[branch loadRewardsWithCallback:^(BOOL changed, NSError *error) {

CDVPluginResult* pluginResult = nil;
if(!error) {
int credits = (int)[branch getCredits];
int credits = 0;

if ([bucket length]) {
credits = (int)[branch getCreditsForBucket:bucket];
} else {
credits = (int)[branch getCredits];
}

pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:credits];
}
else {
Expand Down
39 changes: 0 additions & 39 deletions testbed/npm-debug.log

This file was deleted.

8 changes: 6 additions & 2 deletions www/branch.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,13 @@ Branch.prototype.createBranchUniversalObject = function (options) {
*
* @return (Promise)
*/
Branch.prototype.loadRewards = function () {
Branch.prototype.loadRewards = function (bucket) {

return execute('loadRewards');
if ( ! bucket) {
bucket = '';
}

return execute('loadRewards', [bucket]);

};

Expand Down

0 comments on commit c035768

Please sign in to comment.