refactor getNewStatusReport

This commit is contained in:
Geoffrey Goh
2016-01-27 16:13:00 -08:00
parent eb44af2497
commit 0500545235
5 changed files with 170 additions and 129 deletions

View File

@@ -91,6 +91,9 @@ failCallback:(void (^)(NSError *err))failCallback;
@interface CodePushStatusReport : NSObject
+ (NSString *)getDeploymentKeyFromStatusReportIdentifier:(NSString *)statusReportIdentifier;
+ (NSDictionary *)getFailedUpdateStatusReport:(NSDictionary *)lastFailedPackage;
+ (NSDictionary *)getNewPackageStatusReport:(NSDictionary *)currentPackage;
+ (NSDictionary *)getNewAppVersionStatusReport:(NSString *)appVersion;
+ (NSString *)getPackageStatusReportIdentifier:(NSDictionary *)package;
+ (NSString *)getPreviousStatusReportIdentifier;
+ (NSString *)getVersionLabelFromStatusReportIdentifier:(NSString *)statusReportIdentifier;

View File

@@ -493,11 +493,11 @@ RCT_EXPORT_METHOD(notifyApplicationReady:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
[CodePush removePendingUpdate];
resolve([NSNull null]);
resolve(nil);
}
/*
* This method is checks if a new status update exists (new version was installed,
* This method is checks if a new status update exists (new version was installed,
* or an update failed) and return its details (version label, status).
*/
RCT_EXPORT_METHOD(getNewStatusReport:(RCTPromiseResolveBlock)resolve
@@ -511,10 +511,7 @@ RCT_EXPORT_METHOD(getNewStatusReport:(RCTPromiseResolveBlock)resolve
if (failedUpdates) {
NSDictionary *lastFailedPackage = [failedUpdates lastObject];
if (lastFailedPackage) {
resolve(@{
@"package": lastFailedPackage,
@"status": DeploymentFailed
});
resolve([CodePushStatusReport getFailedUpdateStatusReport:lastFailedPackage]);
return;
}
}
@@ -523,69 +520,17 @@ RCT_EXPORT_METHOD(getNewStatusReport:(RCTPromiseResolveBlock)resolve
NSError *error;
NSDictionary *currentPackage = [CodePushPackage getCurrentPackage:&error];
if (!error && currentPackage) {
NSString *currentPackageIdentifier = [CodePushStatusReport getPackageStatusReportIdentifier:currentPackage];
NSString *previousStatusReportIdentifier = [CodePushStatusReport getPreviousStatusReportIdentifier];
if (currentPackageIdentifier) {
if (previousStatusReportIdentifier == nil) {
[CodePushStatusReport recordDeploymentStatusReported:currentPackageIdentifier];
resolve(@{
@"package": currentPackage,
@"status": DeploymentSucceeded
});
return;
} else if (![previousStatusReportIdentifier isEqualToString:currentPackageIdentifier]) {
[CodePushStatusReport recordDeploymentStatusReported:currentPackageIdentifier];
if ([CodePushStatusReport isStatusReportIdentifierCodePushLabel:previousStatusReportIdentifier]) {
NSString *previousDeploymentKey = [CodePushStatusReport getDeploymentKeyFromStatusReportIdentifier:previousStatusReportIdentifier];
NSString *previousLabel = [CodePushStatusReport getVersionLabelFromStatusReportIdentifier:previousStatusReportIdentifier];
resolve(@{
@"package": currentPackage,
@"status": DeploymentSucceeded,
@"previousDeploymentKey": previousDeploymentKey,
@"previousLabelOrAppVersion": previousLabel
});
} else {
// Previous status report was with a binary app version.
resolve(@{
@"package": currentPackage,
@"status": DeploymentSucceeded,
@"previousLabelOrAppVersion": previousStatusReportIdentifier
});
}
return;
}
}
resolve([CodePushStatusReport getNewPackageStatusReport:currentPackage]);
return;
}
} else if (isRunningBinaryVersion || [_bridge.bundleURL.scheme hasPrefix:@"http"]) {
// Check if the current appVersion has been reported.
NSString *appVersion = [[CodePushConfig current] appVersion];
NSString *previousStatusReportIdentifier = [CodePushStatusReport getPreviousStatusReportIdentifier];
if (previousStatusReportIdentifier == nil) {
[CodePushStatusReport recordDeploymentStatusReported:appVersion];
resolve(@{ @"appVersion": appVersion });
return;
} else if (![previousStatusReportIdentifier isEqualToString:appVersion]) {
[CodePushStatusReport recordDeploymentStatusReported:appVersion];
if ([CodePushStatusReport isStatusReportIdentifierCodePushLabel:previousStatusReportIdentifier]) {
NSString *previousDeploymentKey = [CodePushStatusReport getDeploymentKeyFromStatusReportIdentifier:previousStatusReportIdentifier];
NSString *previousLabel = [CodePushStatusReport getVersionLabelFromStatusReportIdentifier:previousStatusReportIdentifier];
resolve(@{
@"appVersion": appVersion,
@"previousDeploymentKey": previousDeploymentKey,
@"previousLabelOrAppVersion": previousLabel
});
} else {
// Previous status report was with a binary app version.
resolve(@{
@"appVersion": appVersion,
@"previousLabelOrAppVersion": previousStatusReportIdentifier
});
}
return;
}
resolve([CodePushStatusReport getNewAppVersionStatusReport:appVersion]);
return;
}
resolve([NSNull null]);
resolve(nil);
}
/*

View File

@@ -1,8 +1,10 @@
#import "CodePush.h"
static NSString *const LastDeploymentReportKey = @"CODE_PUSH_LAST_DEPLOYMENT_REPORT";
static NSString *const DeploymentFailed = @"DeploymentFailed";
static NSString *const DeploymentKeyKey = @"deploymentKey";
static NSString *const DeploymentSucceeded = @"DeploymentSucceeded";
static NSString *const LabelKey = @"label";
static NSString *const LastDeploymentReportKey = @"CODE_PUSH_LAST_DEPLOYMENT_REPORT";
@implementation CodePushStatusReport
@@ -11,6 +13,78 @@ static NSString *const LabelKey = @"label";
return [[statusReportIdentifier componentsSeparatedByString:@":"] firstObject];
}
+ (NSDictionary *)getFailedUpdateStatusReport:(NSDictionary *)lastFailedPackage
{
return @{
@"package": lastFailedPackage,
@"status": DeploymentFailed
};
}
+ (NSDictionary *)getNewPackageStatusReport:(NSDictionary *)currentPackage
{
NSString *currentPackageIdentifier = [self getPackageStatusReportIdentifier:currentPackage];
NSString *previousStatusReportIdentifier = [self getPreviousStatusReportIdentifier];
if (currentPackageIdentifier) {
if (previousStatusReportIdentifier == nil) {
[self recordDeploymentStatusReported:currentPackageIdentifier];
return @{
@"package": currentPackage,
@"status": DeploymentSucceeded
};
} else if (![previousStatusReportIdentifier isEqualToString:currentPackageIdentifier]) {
[self recordDeploymentStatusReported:currentPackageIdentifier];
if ([self isStatusReportIdentifierCodePushLabel:previousStatusReportIdentifier]) {
NSString *previousDeploymentKey = [self getDeploymentKeyFromStatusReportIdentifier:previousStatusReportIdentifier];
NSString *previousLabel = [self getVersionLabelFromStatusReportIdentifier:previousStatusReportIdentifier];
return @{
@"package": currentPackage,
@"status": DeploymentSucceeded,
@"previousDeploymentKey": previousDeploymentKey,
@"previousLabelOrAppVersion": previousLabel
};
} else {
// Previous status report was with a binary app version.
return @{
@"package": currentPackage,
@"status": DeploymentSucceeded,
@"previousLabelOrAppVersion": previousStatusReportIdentifier
};
}
}
}
return nil;
}
+ (NSDictionary *)getNewAppVersionStatusReport:(NSString *)appVersion
{
NSString *previousStatusReportIdentifier = [self getPreviousStatusReportIdentifier];
if (previousStatusReportIdentifier == nil) {
[self recordDeploymentStatusReported:appVersion];
return @{ @"appVersion": appVersion };
} else if (![previousStatusReportIdentifier isEqualToString:appVersion]) {
[self recordDeploymentStatusReported:appVersion];
if ([self isStatusReportIdentifierCodePushLabel:previousStatusReportIdentifier]) {
NSString *previousDeploymentKey = [self getDeploymentKeyFromStatusReportIdentifier:previousStatusReportIdentifier];
NSString *previousLabel = [self getVersionLabelFromStatusReportIdentifier:previousStatusReportIdentifier];
return @{
@"appVersion": appVersion,
@"previousDeploymentKey": previousDeploymentKey,
@"previousLabelOrAppVersion": previousLabel
};
} else {
// Previous status report was with a binary app version.
return @{
@"appVersion": appVersion,
@"previousLabelOrAppVersion": previousStatusReportIdentifier
};
}
}
return nil;
}
+ (NSString *)getPackageStatusReportIdentifier:(NSDictionary *)package
{
// Because deploymentKeys can be dynamically switched, we use a

View File

@@ -51,8 +51,6 @@ public class CodePush {
private final String ASSETS_BUNDLE_PREFIX = "assets://";
private final String BINARY_MODIFIED_TIME_KEY = "binaryModifiedTime";
private final String CODE_PUSH_PREFERENCES = "CodePush";
private final String DEPLOYMENT_FAILED_STATUS = "DeploymentFailed";
private final String DEPLOYMENT_SUCCEEDED_STATUS = "DeploymentSucceeded";
private final String DOWNLOAD_PROGRESS_EVENT_NAME = "CodePushDownloadProgress";
private final String FAILED_UPDATES_KEY = "CODE_PUSH_FAILED_UPDATES";
private final String PACKAGE_HASH_KEY = "packageHash";
@@ -429,85 +427,34 @@ public class CodePush {
@ReactMethod
public void getNewStatusReport(Promise promise) {
if (needToReportRollback) {
// Check if there was a rollback that was not yet reported
needToReportRollback = false;
JSONArray failedUpdates = getFailedUpdates();
if (failedUpdates != null && failedUpdates.length() > 0) {
try {
JSONObject lastFailedPackageJSON = failedUpdates.getJSONObject(failedUpdates.length() - 1);
WritableMap lastFailedPackage = CodePushUtils.convertJsonObjectToWriteable(lastFailedPackageJSON);
WritableNativeMap reportMap = new WritableNativeMap();
reportMap.putMap("package", lastFailedPackage);
reportMap.putString("status", DEPLOYMENT_FAILED_STATUS);
promise.resolve(reportMap);
return;
WritableMap failedStatusReport = codePushStatusReport.getFailedUpdateStatusReport(lastFailedPackage);
if (failedStatusReport != null) {
promise.resolve(failedStatusReport);
return;
}
} catch (JSONException e) {
throw new CodePushUnknownException("Unable to read failed updates information stored in SharedPreferences.", e);
}
}
} else if (didUpdate) {
// Check if the current CodePush package has been reported
WritableMap currentPackage = codePushPackage.getCurrentPackage();
if (currentPackage != null) {
String currentPackageIdentifier = codePushStatusReport.getPackageStatusReportIdentifier(currentPackage);
String previousStatusReportIdentifier = codePushStatusReport.getPreviousStatusReportIdentifier();
if (currentPackageIdentifier != null) {
if (previousStatusReportIdentifier == null) {
codePushStatusReport.recordDeploymentStatusReported(currentPackageIdentifier);
WritableNativeMap reportMap = new WritableNativeMap();
reportMap.putMap("package", currentPackage);
reportMap.putString("status", DEPLOYMENT_SUCCEEDED_STATUS);
promise.resolve(reportMap);
return;
} else if (!previousStatusReportIdentifier.equals(currentPackageIdentifier)) {
codePushStatusReport.recordDeploymentStatusReported(currentPackageIdentifier);
if (codePushStatusReport.isStatusReportIdentifierCodePushLabel(previousStatusReportIdentifier)) {
String previousDeploymentKey = codePushStatusReport.getDeploymentKeyFromStatusReportIdentifier(previousStatusReportIdentifier);
String previousLabel = codePushStatusReport.getVersionLabelFromStatusReportIdentifier(previousStatusReportIdentifier);
WritableNativeMap reportMap = new WritableNativeMap();
reportMap.putMap("package", currentPackage);
reportMap.putString("status", DEPLOYMENT_SUCCEEDED_STATUS);
reportMap.putString("previousDeploymentKey", previousDeploymentKey);
reportMap.putString("previousLabelOrAppVersion", previousLabel);
promise.resolve(reportMap);
} else {
// Previous status report was with a binary app version.
WritableNativeMap reportMap = new WritableNativeMap();
reportMap.putMap("package", currentPackage);
reportMap.putString("status", DEPLOYMENT_SUCCEEDED_STATUS);
reportMap.putString("previousLabelOrAppVersion", previousStatusReportIdentifier);
promise.resolve(reportMap);
}
return;
}
WritableMap newPackageStatusReport = codePushStatusReport.getNewPackageStatusReport(currentPackage);
if (newPackageStatusReport != null) {
promise.resolve(newPackageStatusReport);
return;
}
}
} else if (isRunningBinaryVersion) {
// Check if the current appVersion has been reported.
String previousStatusReportIdentifier = codePushStatusReport.getPreviousStatusReportIdentifier();
if (previousStatusReportIdentifier == null) {
codePushStatusReport.recordDeploymentStatusReported(appVersion);
WritableNativeMap reportMap = new WritableNativeMap();
reportMap.putString("appVersion", appVersion);
promise.resolve(reportMap);
return;
} else if (!previousStatusReportIdentifier.equals(appVersion)) {
codePushStatusReport.recordDeploymentStatusReported(appVersion);
if (codePushStatusReport.isStatusReportIdentifierCodePushLabel(previousStatusReportIdentifier)) {
String previousDeploymentKey = codePushStatusReport.getDeploymentKeyFromStatusReportIdentifier(previousStatusReportIdentifier);
String previousLabel = codePushStatusReport.getVersionLabelFromStatusReportIdentifier(previousStatusReportIdentifier);
WritableNativeMap reportMap = new WritableNativeMap();
reportMap.putString("appVersion", appVersion);
reportMap.putString("previousDeploymentKey", previousDeploymentKey);
reportMap.putString("previousLabelOrAppVersion", previousLabel);
promise.resolve(reportMap);
} else {
// Previous status report was with a binary app version.
WritableNativeMap reportMap = new WritableNativeMap();
reportMap.putString("appVersion", appVersion);
reportMap.putString("previousLabelOrAppVersion", previousStatusReportIdentifier);
promise.resolve(reportMap);
}
WritableMap newAppVersionStatusReport = codePushStatusReport.getNewAppVersionStatusReport(appVersion);
if (newAppVersionStatusReport != null) {
promise.resolve(newAppVersionStatusReport);
return;
}
}

View File

@@ -4,12 +4,15 @@ import android.content.Context;
import android.content.SharedPreferences;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableNativeMap;
public class CodePushStatusReport {
private Context applicationContext;
private final String CODE_PUSH_PREFERENCES;
private final String DEPLOYMENT_FAILED_STATUS = "DeploymentFailed";
private final String DEPLOYMENT_KEY_KEY = "deploymentKey";
private final String DEPLOYMENT_SUCCEEDED_STATUS = "DeploymentSucceeded";
private final String LABEL_KEY = "label";
private final String LAST_DEPLOYMENT_REPORT_KEY = "CODE_PUSH_LAST_DEPLOYMENT_REPORT";
@@ -27,6 +30,75 @@ public class CodePushStatusReport {
}
}
public WritableMap getFailedUpdateStatusReport(WritableMap lastFailedPackage) {
WritableNativeMap reportMap = new WritableNativeMap();
reportMap.putMap("package", lastFailedPackage);
reportMap.putString("status", DEPLOYMENT_FAILED_STATUS);
return reportMap;
}
public WritableMap getNewPackageStatusReport(WritableMap currentPackage) {
String currentPackageIdentifier = this.getPackageStatusReportIdentifier(currentPackage);
String previousStatusReportIdentifier = this.getPreviousStatusReportIdentifier();
if (currentPackageIdentifier != null) {
if (previousStatusReportIdentifier == null) {
this.recordDeploymentStatusReported(currentPackageIdentifier);
WritableNativeMap reportMap = new WritableNativeMap();
reportMap.putMap("package", currentPackage);
reportMap.putString("status", DEPLOYMENT_SUCCEEDED_STATUS);
return reportMap;
} else if (!previousStatusReportIdentifier.equals(currentPackageIdentifier)) {
this.recordDeploymentStatusReported(currentPackageIdentifier);
if (this.isStatusReportIdentifierCodePushLabel(previousStatusReportIdentifier)) {
String previousDeploymentKey = this.getDeploymentKeyFromStatusReportIdentifier(previousStatusReportIdentifier);
String previousLabel = this.getVersionLabelFromStatusReportIdentifier(previousStatusReportIdentifier);
WritableNativeMap reportMap = new WritableNativeMap();
reportMap.putMap("package", currentPackage);
reportMap.putString("status", DEPLOYMENT_SUCCEEDED_STATUS);
reportMap.putString("previousDeploymentKey", previousDeploymentKey);
reportMap.putString("previousLabelOrAppVersion", previousLabel);
return reportMap;
} else {
// Previous status report was with a binary app version.
WritableNativeMap reportMap = new WritableNativeMap();
reportMap.putMap("package", currentPackage);
reportMap.putString("status", DEPLOYMENT_SUCCEEDED_STATUS);
reportMap.putString("previousLabelOrAppVersion", previousStatusReportIdentifier);
return reportMap;
}
}
}
return null;
}
public WritableMap getNewAppVersionStatusReport(String appVersion) {
String previousStatusReportIdentifier = this.getPreviousStatusReportIdentifier();
if (previousStatusReportIdentifier == null) {
this.recordDeploymentStatusReported(appVersion);
WritableNativeMap reportMap = new WritableNativeMap();
reportMap.putString("appVersion", appVersion);
return reportMap;
} else if (!previousStatusReportIdentifier.equals(appVersion)) {
this.recordDeploymentStatusReported(appVersion);
WritableNativeMap reportMap = new WritableNativeMap();
if (this.isStatusReportIdentifierCodePushLabel(previousStatusReportIdentifier)) {
String previousDeploymentKey = this.getDeploymentKeyFromStatusReportIdentifier(previousStatusReportIdentifier);
String previousLabel = this.getVersionLabelFromStatusReportIdentifier(previousStatusReportIdentifier);
reportMap.putString("appVersion", appVersion);
reportMap.putString("previousDeploymentKey", previousDeploymentKey);
reportMap.putString("previousLabelOrAppVersion", previousLabel);
} else {
// Previous status report was with a binary app version.
reportMap.putString("appVersion", appVersion);
reportMap.putString("previousLabelOrAppVersion", previousStatusReportIdentifier);
}
return reportMap;
}
return null;
}
public String getPackageStatusReportIdentifier(WritableMap updatePackage) {
// Because deploymentKeys can be dynamically switched, we use a
// combination of the deploymentKey and label as the packageIdentifier.