Skip to content

Commit

Permalink
feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
geof90 committed Jan 27, 2016
1 parent bea2b9c commit 35539d1
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 141 deletions.
11 changes: 11 additions & 0 deletions CodePush.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@ failCallback:(void (^)(NSError *err))failCallback;

@end

@interface CodePushStatusReport : NSObject

+ (NSString *)getDeploymentKeyFromStatusReportIdentifier:(NSString *)statusReportIdentifier;
+ (NSString *)getPackageStatusReportIdentifier:(NSDictionary *)package;
+ (NSString *)getPreviousStatusReportIdentifier;
+ (NSString *)getVersionLabelFromStatusReportIdentifier:(NSString *)statusReportIdentifier;
+ (BOOL)isStatusReportIdentifierCodePushLabel:(NSString *)statusReportIdentifier;
+ (void)recordDeploymentStatusReported:(NSString *)appVersionOrPackageIdentifier;

@end

typedef NS_ENUM(NSInteger, CodePushInstallMode) {
CodePushInstallModeImmediate,
CodePushInstallModeOnNextRestart,
Expand Down
86 changes: 18 additions & 68 deletions CodePush.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ @implementation CodePush {

// These keys represent the names we use to store data in NSUserDefaults
static NSString *const FailedUpdatesKey = @"CODE_PUSH_FAILED_UPDATES";
static NSString *const LastDeploymentReportKey = @"CODE_PUSH_LAST_DEPLOYMENT_REPORT";
static NSString *const PendingUpdateKey = @"CODE_PUSH_PENDING_UPDATE";

// These keys are already "namespaced" by the PendingUpdateKey, so
Expand All @@ -33,8 +32,6 @@ @implementation CodePush {

// These keys are used to inspect/augment the metadata
// that is associated with an update's package.
static NSString *const DeploymentKeyKey = @"deploymentKey";
static NSString *const LabelKey = @"label";
static NSString *const PackageHashKey = @"packageHash";
static NSString *const PackageIsPendingKey = @"isPending";

Expand Down Expand Up @@ -161,36 +158,6 @@ - (void)dealloc
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (NSString *)getDeploymentKeyFromStatusReportIdentifier:(NSString *)statusReportIdentifier
{
return [[statusReportIdentifier componentsSeparatedByString:@":"] firstObject];
}

- (NSString *)getPackageStatusReportIdentifier:(NSDictionary *)package
{
// Because deploymentKeys can be dynamically switched, we use a
// combination of the deploymentKey and label as the packageIdentifier.
NSString *deploymentKey = [package objectForKey:DeploymentKeyKey];
NSString *label = [package objectForKey:LabelKey];
if (deploymentKey && label) {
return [[deploymentKey stringByAppendingString:@":"] stringByAppendingString:label];
} else {
return nil;
}
}

- (NSString *)getPreviousStatusReportIdentifier
{
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
NSString *sentStatusReportIdentifier = [preferences objectForKey:LastDeploymentReportKey];
return sentStatusReportIdentifier;
}

- (NSString *)getVersionLabelFromStatusReportIdentifier:(NSString *)statusReportIdentifier
{
return [[statusReportIdentifier componentsSeparatedByString:@":"] lastObject];
}

- (instancetype)init
{
self = [super init];
Expand Down Expand Up @@ -277,11 +244,6 @@ - (BOOL)isPendingUpdate:(NSString*)packageHash
return updateIsPending;
}

- (BOOL)isStatusReportIdentifierCodePushLabel:(NSString *)statusReportIdentifier
{
return statusReportIdentifier != nil && [statusReportIdentifier containsString:@":"];
}

/*
* This method updates the React Native bridge's bundle URL
* to point at the latest CodePush update, and then restarts
Expand All @@ -304,13 +266,6 @@ - (void)loadBundle
});
}

- (void)recordDeploymentStatusReported:(NSString *)appVersionOrPackageIdentifier
{
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
[preferences setValue:appVersionOrPackageIdentifier forKey:LastDeploymentReportKey];
[preferences synchronize];
}

/*
* This method is used when an update has failed installation
* and the app needs to be rolled back to the previous bundle.
Expand Down Expand Up @@ -556,38 +511,33 @@ - (void)savePendingUpdate:(NSString *)packageHash
if (failedUpdates) {
NSDictionary *lastFailedPackage = [failedUpdates lastObject];
if (lastFailedPackage) {
NSString *lastFailedPackageIdentifier = [self getPackageStatusReportIdentifier:lastFailedPackage];
NSString *previousStatusReportIdentifier = [self getPreviousStatusReportIdentifier];
if (lastFailedPackageIdentifier && (previousStatusReportIdentifier == nil || ![previousStatusReportIdentifier isEqualToString:lastFailedPackageIdentifier])) {
[self recordDeploymentStatusReported:lastFailedPackageIdentifier];
resolve(@{
@"package": lastFailedPackage,
@"status": DeploymentFailed
});
return;
}
resolve(@{
@"package": lastFailedPackage,
@"status": DeploymentFailed
});
return;
}
}
} else if (_isFirstRunAfterUpdate) {
// Check if the current CodePush package has been reported
NSError *error;
NSDictionary *currentPackage = [CodePushPackage getCurrentPackage:&error];
if (!error && currentPackage) {
NSString *currentPackageIdentifier = [self getPackageStatusReportIdentifier:currentPackage];
NSString *previousStatusReportIdentifier = [self getPreviousStatusReportIdentifier];
NSString *currentPackageIdentifier = [CodePushStatusReport getPackageStatusReportIdentifier:currentPackage];
NSString *previousStatusReportIdentifier = [CodePushStatusReport getPreviousStatusReportIdentifier];
if (currentPackageIdentifier) {
if (previousStatusReportIdentifier == nil) {
[self recordDeploymentStatusReported:currentPackageIdentifier];
[CodePushStatusReport recordDeploymentStatusReported:currentPackageIdentifier];
resolve(@{
@"package": currentPackage,
@"status": DeploymentSucceeded
});
return;
} else if (![previousStatusReportIdentifier isEqualToString:currentPackageIdentifier]) {
[self recordDeploymentStatusReported:currentPackageIdentifier];
if ([self isStatusReportIdentifierCodePushLabel:previousStatusReportIdentifier]) {
NSString *previousDeploymentKey = [self getDeploymentKeyFromStatusReportIdentifier:previousStatusReportIdentifier];
NSString *previousLabel = [self getVersionLabelFromStatusReportIdentifier:previousStatusReportIdentifier];
[CodePushStatusReport recordDeploymentStatusReported:currentPackageIdentifier];
if ([CodePushStatusReport isStatusReportIdentifierCodePushLabel:previousStatusReportIdentifier]) {
NSString *previousDeploymentKey = [CodePushStatusReport getDeploymentKeyFromStatusReportIdentifier:previousStatusReportIdentifier];
NSString *previousLabel = [CodePushStatusReport getVersionLabelFromStatusReportIdentifier:previousStatusReportIdentifier];
resolve(@{
@"package": currentPackage,
@"status": DeploymentSucceeded,
Expand All @@ -609,16 +559,16 @@ - (void)savePendingUpdate:(NSString *)packageHash
} else if (isRunningBinaryVersion || [_bridge.bundleURL.scheme hasPrefix:@"http"]) {
// Check if the current appVersion has been reported.
NSString *appVersion = [[CodePushConfig current] appVersion];
NSString *previousStatusReportIdentifier = [self getPreviousStatusReportIdentifier];
NSString *previousStatusReportIdentifier = [CodePushStatusReport getPreviousStatusReportIdentifier];
if (previousStatusReportIdentifier == nil) {
[self recordDeploymentStatusReported:appVersion];
[CodePushStatusReport recordDeploymentStatusReported:appVersion];
resolve(@{ @"appVersion": appVersion });
return;
} else if (![previousStatusReportIdentifier isEqualToString:appVersion]) {
[self recordDeploymentStatusReported:appVersion];
if ([self isStatusReportIdentifierCodePushLabel:previousStatusReportIdentifier]) {
NSString *previousDeploymentKey = [self getDeploymentKeyFromStatusReportIdentifier:previousStatusReportIdentifier];
NSString *previousLabel = [self getVersionLabelFromStatusReportIdentifier:previousStatusReportIdentifier];
[CodePushStatusReport recordDeploymentStatusReported:appVersion];
if ([CodePushStatusReport isStatusReportIdentifierCodePushLabel:previousStatusReportIdentifier]) {
NSString *previousDeploymentKey = [CodePushStatusReport getDeploymentKeyFromStatusReportIdentifier:previousStatusReportIdentifier];
NSString *previousLabel = [CodePushStatusReport getVersionLabelFromStatusReportIdentifier:previousStatusReportIdentifier];
resolve(@{
@"appVersion": appVersion,
@"previousDeploymentKey": previousDeploymentKey,
Expand Down
4 changes: 4 additions & 0 deletions CodePush.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/* Begin PBXBuildFile section */
13BE3DEE1AC21097009241FE /* CodePush.m in Sources */ = {isa = PBXBuildFile; fileRef = 13BE3DED1AC21097009241FE /* CodePush.m */; };
1B23B9141BF9267B000BB2F0 /* RCTConvert+CodePushInstallMode.m in Sources */ = {isa = PBXBuildFile; fileRef = 1B23B9131BF9267B000BB2F0 /* RCTConvert+CodePushInstallMode.m */; };
5421FE311C58AD5A00986A55 /* CodePushStatusReport.m in Sources */ = {isa = PBXBuildFile; fileRef = 5421FE301C58AD5A00986A55 /* CodePushStatusReport.m */; };
54A0026C1C0E2880004C3CEC /* aescrypt.c in Sources */ = {isa = PBXBuildFile; fileRef = 54A0024C1C0E2880004C3CEC /* aescrypt.c */; };
54A0026D1C0E2880004C3CEC /* aeskey.c in Sources */ = {isa = PBXBuildFile; fileRef = 54A0024D1C0E2880004C3CEC /* aeskey.c */; };
54A0026E1C0E2880004C3CEC /* aestab.c in Sources */ = {isa = PBXBuildFile; fileRef = 54A0024F1C0E2880004C3CEC /* aestab.c */; };
Expand Down Expand Up @@ -45,6 +46,7 @@
13BE3DEC1AC21097009241FE /* CodePush.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CodePush.h; sourceTree = "<group>"; };
13BE3DED1AC21097009241FE /* CodePush.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CodePush.m; sourceTree = "<group>"; };
1B23B9131BF9267B000BB2F0 /* RCTConvert+CodePushInstallMode.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "RCTConvert+CodePushInstallMode.m"; sourceTree = "<group>"; };
5421FE301C58AD5A00986A55 /* CodePushStatusReport.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CodePushStatusReport.m; sourceTree = "<group>"; };
54A0024A1C0E2880004C3CEC /* aes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = aes.h; sourceTree = "<group>"; };
54A0024B1C0E2880004C3CEC /* aes_via_ace.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = aes_via_ace.h; sourceTree = "<group>"; };
54A0024C1C0E2880004C3CEC /* aescrypt.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = aescrypt.c; sourceTree = "<group>"; };
Expand Down Expand Up @@ -165,6 +167,7 @@
1B23B9131BF9267B000BB2F0 /* RCTConvert+CodePushInstallMode.m */,
54FFEDDF1BF550630061DD23 /* CodePushDownloadHandler.m */,
810D4E6C1B96935000B397E9 /* CodePushPackage.m */,
5421FE301C58AD5A00986A55 /* CodePushStatusReport.m */,
81D51F391B6181C2000DA084 /* CodePushConfig.m */,
13BE3DEC1AC21097009241FE /* CodePush.h */,
13BE3DED1AC21097009241FE /* CodePush.m */,
Expand Down Expand Up @@ -239,6 +242,7 @@
54FFEDE01BF550630061DD23 /* CodePushDownloadHandler.m in Sources */,
54A002711C0E2880004C3CEC /* hmac.c in Sources */,
54A002721C0E2880004C3CEC /* prng.c in Sources */,
5421FE311C58AD5A00986A55 /* CodePushStatusReport.m in Sources */,
54A002731C0E2880004C3CEC /* pwd2key.c in Sources */,
54A002751C0E2880004C3CEC /* ioapi.c in Sources */,
54A002771C0E2880004C3CEC /* unzip.c in Sources */,
Expand Down
51 changes: 51 additions & 0 deletions CodePushStatusReport.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#import "CodePush.h"

static NSString *const LastDeploymentReportKey = @"CODE_PUSH_LAST_DEPLOYMENT_REPORT";
static NSString *const DeploymentKeyKey = @"deploymentKey";
static NSString *const LabelKey = @"label";

@implementation CodePushStatusReport

+ (NSString *)getDeploymentKeyFromStatusReportIdentifier:(NSString *)statusReportIdentifier
{
return [[statusReportIdentifier componentsSeparatedByString:@":"] firstObject];
}

+ (NSString *)getPackageStatusReportIdentifier:(NSDictionary *)package
{
// Because deploymentKeys can be dynamically switched, we use a
// combination of the deploymentKey and label as the packageIdentifier.
NSString *deploymentKey = [package objectForKey:DeploymentKeyKey];
NSString *label = [package objectForKey:LabelKey];
if (deploymentKey && label) {
return [[deploymentKey stringByAppendingString:@":"] stringByAppendingString:label];
} else {
return nil;
}
}

+ (NSString *)getPreviousStatusReportIdentifier
{
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
NSString *sentStatusReportIdentifier = [preferences objectForKey:LastDeploymentReportKey];
return sentStatusReportIdentifier;
}

+ (NSString *)getVersionLabelFromStatusReportIdentifier:(NSString *)statusReportIdentifier
{
return [[statusReportIdentifier componentsSeparatedByString:@":"] lastObject];
}

+ (BOOL)isStatusReportIdentifierCodePushLabel:(NSString *)statusReportIdentifier
{
return statusReportIdentifier != nil && [statusReportIdentifier containsString:@":"];
}

+ (void)recordDeploymentStatusReported:(NSString *)appVersionOrPackageIdentifier
{
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
[preferences setValue:appVersionOrPackageIdentifier forKey:LastDeploymentReportKey];
[preferences synchronize];
}

@end
Loading

0 comments on commit 35539d1

Please sign in to comment.