Skip to content

Commit

Permalink
chore: merged with master
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanneff committed Jan 9, 2017
2 parents 892bd36 + c9c601d commit f5a9c35
Show file tree
Hide file tree
Showing 22 changed files with 690 additions and 205 deletions.
1 change: 0 additions & 1 deletion DEVELOPING.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ cordova plugin rm branch-cordova-sdk;
cordova plugin add ../;
cordova build ios;
open -a Xcode platforms/ios/Branch\ Testing.xcworkspace;

```

> Validate all features on both `iOS` and `Android` on `device` only (no `simulator` or `TestFlight`)
Expand Down
300 changes: 159 additions & 141 deletions README.md

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ SOFTWARE.
<header-file src="src/ios/dependencies/Fabric/Fabric.h" />

<header-file src="src/ios/dependencies/Branch-SDK/BNCCallbacks.h" />
<header-file src="src/ios/dependencies/Branch-SDK/BNCCommerceEvent.h" />
<source-file src="src/ios/dependencies/Branch-SDK/BNCCommerceEvent.m" />
<header-file src="src/ios/dependencies/Branch-SDK/BNCConfig.h" />
<source-file src="src/ios/dependencies/Branch-SDK/BNCConfig.m" />
<header-file src="src/ios/dependencies/Branch-SDK/BNCContentDiscoveryManager.h" />
Expand Down
23 changes: 12 additions & 11 deletions src/ios/BranchSDK.m
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ - (void)initSession:(CDVInvokedUrlCommand*)command

if (!error) {
if (params != nil && [params count] > 0 && isFromBranchLink) {

NSError *err;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params options:0 error:&err];

Expand Down Expand Up @@ -136,7 +136,7 @@ - (void)initSession:(CDVInvokedUrlCommand*)command
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"BSDKPostUnhandledURL" object:self.deepLinkUrl]];
}
self.deepLinkUrl = nil;

if (command != nil) {
[self.commandDelegate sendPluginResult: pluginResult callbackId: command.callbackId];
}
Expand All @@ -147,7 +147,7 @@ - (void)setMixpanelToken:(CDVInvokedUrlCommand*)command
{

[[Branch getInstance] setRequestMetadataKey:@"$mixpanel_distinct_id" value:[command.arguments objectAtIndex:0]];

}

- (void)setDebug:(CDVInvokedUrlCommand*)command
Expand Down Expand Up @@ -208,10 +208,10 @@ - (void)setIdentity:(CDVInvokedUrlCommand*)command
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:params];
}
else {

pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[error localizedDescription]];
}

[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
Expand All @@ -227,13 +227,14 @@ - (void)userCompletedAction:(CDVInvokedUrlCommand*)command
{
NSString *name;
NSDictionary *state;

// if a state dictionary is passed as an argument
if ([command.arguments count] == 2) {
name = [command.arguments objectAtIndex:0];
state = [command.arguments objectAtIndex:1];
}
else {
name = (NSString *)command.arguments;
name = [command.arguments objectAtIndex:0];
}

Branch *branch = [self getInstance];
Expand All @@ -245,7 +246,7 @@ - (void)userCompletedAction:(CDVInvokedUrlCommand*)command
[branch userCompletedAction:name];
}

// TODO: need to resolve according to result of userCompletedAction, but no callback version of the method is exposed.
// TODO: iOS Branch.userCompletedAction needs a callback for success or failure
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: @"Success"];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
Expand Down Expand Up @@ -417,7 +418,7 @@ - (void)createBranchUniversalObject:(CDVInvokedUrlCommand*)command
- (void)registerView:(CDVInvokedUrlCommand*)command
{
int branchUniversalObjectId = [[command.arguments objectAtIndex:0] intValue];

NSMutableDictionary *branchUniversalObjDict = [self.branchUniversalObjArray objectAtIndex:branchUniversalObjectId];
BranchUniversalObject *branchUniversalObj = [branchUniversalObjDict objectForKey:@"branchUniversalObj"];

Expand Down Expand Up @@ -532,7 +533,7 @@ - (void)showShareSheet:(CDVInvokedUrlCommand*)command
completion:^(NSString *activityType, BOOL completed) {

int listenerCallbackId = [[command.arguments objectAtIndex:0] intValue];

if (completed) {
NSLog(@"Share link complete");
[branchUniversalObj getShortUrlWithLinkProperties:linkProperties andCallback:^(NSString *url, NSError *error) {
Expand All @@ -542,7 +543,7 @@ - (void)showShareSheet:(CDVInvokedUrlCommand*)command
}
}];
}

CDVPluginResult *shareDialogDismissed = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];

NSMutableDictionary *branchUniversalObjDict = [self.branchUniversalObjArray objectAtIndex:listenerCallbackId];
Expand Down Expand Up @@ -615,7 +616,7 @@ - (void)postUnhandledURL:(NSNotification *)notification {
// We create a JSON string result, because we're unable to handle the url. We will include the url in the return string.
NSError *error;
NSString *urlString;

// if ([notification.object respondsToSelector:@selector(absoluteString:)]) {
SEL selector = NSSelectorFromString(@"absoluteString:");
if ([notification.object respondsToSelector:selector]) {
Expand Down
72 changes: 72 additions & 0 deletions src/ios/dependencies/Branch-SDK/BNCCommerceEvent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//
// BNCCommerceEvent.h
// BranchSDK-iOS
//
// Created by Edward Smith on 12/14/16.
// Copyright (c) 2016 Branch Metrics. All rights reserved.
//


#import <Foundation/Foundation.h>
#import "BNCServerRequest.h"


#pragma mark BNCProductCategory

typedef NSString*const BNCProductCategory;

extern BNCProductCategory BNCProductCategoryAnimalSupplies;
extern BNCProductCategory BNCProductCategoryApparel;
extern BNCProductCategory BNCProductCategoryArtsEntertainment;
extern BNCProductCategory BNCProductCategoryBabyToddler;
extern BNCProductCategory BNCProductCategoryBusinessIndustrial;
extern BNCProductCategory BNCProductCategoryCamerasOptics;
extern BNCProductCategory BNCProductCategoryElectronics;
extern BNCProductCategory BNCProductCategoryFoodBeverageTobacco;
extern BNCProductCategory BNCProductCategoryFurniture;
extern BNCProductCategory BNCProductCategoryHardware;
extern BNCProductCategory BNCProductCategoryHealthBeauty;
extern BNCProductCategory BNCProductCategoryHomeGarden;
extern BNCProductCategory BNCProductCategoryLuggageBags;
extern BNCProductCategory BNCProductCategoryMature;
extern BNCProductCategory BNCProductCategoryMedia;
extern BNCProductCategory BNCProductCategoryOfficeSupplies;
extern BNCProductCategory BNCProductCategoryReligious;
extern BNCProductCategory BNCProductCategorySoftware;
extern BNCProductCategory BNCProductCategorySportingGoods;
extern BNCProductCategory BNCProductCategoryToysGames;
extern BNCProductCategory BNCProductCategoryVehiclesParts;

#pragma mark - BNCProduct

@interface BNCProduct : NSObject
@property (nonatomic, strong) NSString *sku;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSDecimalNumber *price;
@property (nonatomic, strong) NSNumber *quantity;
@property (nonatomic, strong) NSString *brand;
@property (nonatomic, strong) BNCProductCategory category;
@property (nonatomic, strong) NSString *variant;
@end

#pragma mark - BNCCommerceEvent

@interface BNCCommerceEvent : NSObject
@property (nonatomic, strong) NSDecimalNumber *revenue;
@property (nonatomic, strong) NSString *currency;
@property (nonatomic, strong) NSString *transactionID;
@property (nonatomic, strong) NSDecimalNumber *shipping;
@property (nonatomic, strong) NSDecimalNumber *tax;
@property (nonatomic, strong) NSString *coupon;
@property (nonatomic, strong) NSString *affiliation;
@property (nonatomic, strong) NSArray<BNCProduct*> *products;
@end


@interface BranchCommerceEventRequest : BNCServerRequest <NSCoding>

- (instancetype) initWithCommerceEvent:(BNCCommerceEvent*)commerceEvent
metadata:(NSDictionary*)dictionary
completion:(void (^)(NSDictionary* response, NSError* error))callBack;

@end
203 changes: 203 additions & 0 deletions src/ios/dependencies/Branch-SDK/BNCCommerceEvent.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
//
// BNCCommerceEvent.h
// BranchSDK-iOS
//
// Created by Edward Smith on 12/14/16.
// Copyright (c) 2016 Branch Metrics. All rights reserved.
//


#import "BNCCommerceEvent.h"
#import "BranchConstants.h"


#pragma mark BNCProductCategory

BNCProductCategory BNCProductCategoryAnimalSupplies = @"Animals & Pet Supplies";
BNCProductCategory BNCProductCategoryApparel = @"Apparel & Accessories";
BNCProductCategory BNCProductCategoryArtsEntertainment = @"Arts & Entertainment";
BNCProductCategory BNCProductCategoryBabyToddler = @"Baby & Toddler";
BNCProductCategory BNCProductCategoryBusinessIndustrial = @"Business & Industrial";
BNCProductCategory BNCProductCategoryCamerasOptics = @"Cameras & Optics";
BNCProductCategory BNCProductCategoryElectronics = @"Electronics";
BNCProductCategory BNCProductCategoryFoodBeverageTobacco = @"Food, Beverages & Tobacco";
BNCProductCategory BNCProductCategoryFurniture = @"Furniture";
BNCProductCategory BNCProductCategoryHardware = @"Hardware";
BNCProductCategory BNCProductCategoryHealthBeauty = @"Health & Beauty";
BNCProductCategory BNCProductCategoryHomeGarden = @"Home & Garden";
BNCProductCategory BNCProductCategoryLuggageBags = @"Luggage & Bags";
BNCProductCategory BNCProductCategoryMature = @"Mature";
BNCProductCategory BNCProductCategoryMedia = @"Media";
BNCProductCategory BNCProductCategoryOfficeSupplies = @"Office Supplies";
BNCProductCategory BNCProductCategoryReligious = @"Religious & Ceremonial";
BNCProductCategory BNCProductCategorySoftware = @"Software";
BNCProductCategory BNCProductCategorySportingGoods = @"Sporting Goods";
BNCProductCategory BNCProductCategoryToysGames = @"Toys & Games";
BNCProductCategory BNCProductCategoryVehiclesParts = @"Vehicles & Parts";

#pragma mark - BNCProduct

@implementation BNCProduct

- (NSMutableDictionary*) dictionary {
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];

#define assign(x) \
do { if (self.x) { dictionary[@#x] = self.x; } } while (0)

assign(sku);
assign(name);
assign(price);
assign(quantity);
assign(brand);
assign(category);
assign(variant);

#undef assign

return dictionary;
}

- (NSString*) description {
return [NSString stringWithFormat:
@"Name: %@ Sku: %@ Price: %@ Quantity: %@ Brand: %@ Category: %@ Variant: %@",
self.name,
self.sku,
self.price,
self.quantity,
self.brand,
self.category,
self.variant];
}

@end

#pragma mark - BNCCommerceEvent

@implementation BNCCommerceEvent : NSObject

- (NSDictionary*) dictionary {
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];

#define assign(x) \
do { if (self.x) { dictionary[@#x] = self.x; } } while (0)

assign(revenue);
assign(currency);
assign(transactionID);
assign(shipping);
assign(tax);
assign(coupon);
assign(affiliation);

NSMutableArray *products = [NSMutableArray arrayWithCapacity:self.products.count];
for (BNCProduct *product in self.products) {
NSDictionary * d = [product dictionary];
if (d) [products addObject:d];
}
dictionary[@"products"] = products;

#undef assign

return dictionary;
}

- (NSString*) description {
return [NSString stringWithFormat:
@"Revenue: %@ Currency: %@ TxID: %@ Shipping: %@ Tax: %@ Coupon: %@ Affl: %@ Products: %lu",
self.revenue,
self.currency,
self.transactionID,
self.shipping,
self.tax,
self.coupon,
self.affiliation,
(unsigned long) self.products.count];
}

@end


#pragma mark - BranchCommerceEventRequest


@interface BranchCommerceEventRequest ()
@property (strong) NSDictionary *commerceDictionary;
@property (strong) NSDictionary *metadata;
@property (copy) void (^completion)(NSDictionary* response, NSError* error);
@end


@implementation BranchCommerceEventRequest

- (instancetype) initWithCommerceEvent:(BNCCommerceEvent*)commerceEvent
metadata:(NSDictionary*)metadata
completion:(void (^)(NSDictionary* response, NSError* error))completion {
self = [super init];
if (!self) return self;

if ([commerceEvent.revenue isEqualToNumber:[NSDecimalNumber numberWithDouble:0.0]]) {
NSLog(@"[Branch] Warning: Sending a commerce event with zero value!!");
}

self.commerceDictionary = [commerceEvent dictionary];
self.metadata = metadata;
self.completion = completion;
return self;
}

- (void)makeRequest:(BNCServerInterface *)serverInterface
key:(NSString *)key callback:(BNCServerCallback)callback {

BNCPreferenceHelper *preferenceHelper = [BNCPreferenceHelper preferenceHelper];

NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[BRANCH_REQUEST_KEY_ACTION] = @"purchase";
params[BRANCH_REQUEST_KEY_DEVICE_FINGERPRINT_ID] = preferenceHelper.deviceFingerprintID;
params[BRANCH_REQUEST_KEY_BRANCH_IDENTITY] = preferenceHelper.identityID;
params[BRANCH_REQUEST_KEY_SESSION_ID] = preferenceHelper.sessionID;

if (self.metadata)
params[@"metadata"] = self.metadata;
if (self.commerceDictionary)
params[@"commerce_data"] = self.commerceDictionary;

NSString *URL = [preferenceHelper getAPIURL:BRANCH_REQUEST_ENDPOINT_USER_COMPLETED_ACTION];
[serverInterface postRequest:params
url:URL
key:key
callback:callback];
}

- (void)processResponse:(BNCServerResponse*)response
error:(NSError*)error {

NSDictionary *dictionary =
([response.data isKindOfClass:[NSDictionary class]])
? (NSDictionary*) response.data
: nil;

if (self.completion)
self.completion(dictionary, error);
}


#pragma mark BranchCommerceEventRequest NSCoding


- (instancetype)initWithCoder:(NSCoder *)decoder {
self = [super initWithCoder:decoder];
if (!self) return self;

self.commerceDictionary = [decoder decodeObjectForKey:@"commerceDictionary"];
self.metadata = [decoder decodeObjectForKey:@"metaData"];
return self;
}

- (void)encodeWithCoder:(NSCoder *)coder {
[super encodeWithCoder:coder];
[coder encodeObject:self.commerceDictionary forKey:@"commerceDictionary"];
[coder encodeObject:self.metadata forKey:@"metadata"];
}

@end
Loading

0 comments on commit f5a9c35

Please sign in to comment.