Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 0500545

Browse files
committed
refactor getNewStatusReport
1 parent eb44af2 commit 0500545

File tree

5 files changed

+170
-129
lines changed

5 files changed

+170
-129
lines changed

CodePush.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ failCallback:(void (^)(NSError *err))failCallback;
9191
@interface CodePushStatusReport : NSObject
9292

9393
+ (NSString *)getDeploymentKeyFromStatusReportIdentifier:(NSString *)statusReportIdentifier;
94+
+ (NSDictionary *)getFailedUpdateStatusReport:(NSDictionary *)lastFailedPackage;
95+
+ (NSDictionary *)getNewPackageStatusReport:(NSDictionary *)currentPackage;
96+
+ (NSDictionary *)getNewAppVersionStatusReport:(NSString *)appVersion;
9497
+ (NSString *)getPackageStatusReportIdentifier:(NSDictionary *)package;
9598
+ (NSString *)getPreviousStatusReportIdentifier;
9699
+ (NSString *)getVersionLabelFromStatusReportIdentifier:(NSString *)statusReportIdentifier;

CodePush.m

Lines changed: 8 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -493,11 +493,11 @@ - (void)savePendingUpdate:(NSString *)packageHash
493493
rejecter:(RCTPromiseRejectBlock)reject)
494494
{
495495
[CodePush removePendingUpdate];
496-
resolve([NSNull null]);
496+
resolve(nil);
497497
}
498498

499499
/*
500-
* This method is checks if a new status update exists (new version was installed,
500+
* This method is checks if a new status update exists (new version was installed,
501501
* or an update failed) and return its details (version label, status).
502502
*/
503503
RCT_EXPORT_METHOD(getNewStatusReport:(RCTPromiseResolveBlock)resolve
@@ -511,10 +511,7 @@ - (void)savePendingUpdate:(NSString *)packageHash
511511
if (failedUpdates) {
512512
NSDictionary *lastFailedPackage = [failedUpdates lastObject];
513513
if (lastFailedPackage) {
514-
resolve(@{
515-
@"package": lastFailedPackage,
516-
@"status": DeploymentFailed
517-
});
514+
resolve([CodePushStatusReport getFailedUpdateStatusReport:lastFailedPackage]);
518515
return;
519516
}
520517
}
@@ -523,69 +520,17 @@ - (void)savePendingUpdate:(NSString *)packageHash
523520
NSError *error;
524521
NSDictionary *currentPackage = [CodePushPackage getCurrentPackage:&error];
525522
if (!error && currentPackage) {
526-
NSString *currentPackageIdentifier = [CodePushStatusReport getPackageStatusReportIdentifier:currentPackage];
527-
NSString *previousStatusReportIdentifier = [CodePushStatusReport getPreviousStatusReportIdentifier];
528-
if (currentPackageIdentifier) {
529-
if (previousStatusReportIdentifier == nil) {
530-
[CodePushStatusReport recordDeploymentStatusReported:currentPackageIdentifier];
531-
resolve(@{
532-
@"package": currentPackage,
533-
@"status": DeploymentSucceeded
534-
});
535-
return;
536-
} else if (![previousStatusReportIdentifier isEqualToString:currentPackageIdentifier]) {
537-
[CodePushStatusReport recordDeploymentStatusReported:currentPackageIdentifier];
538-
if ([CodePushStatusReport isStatusReportIdentifierCodePushLabel:previousStatusReportIdentifier]) {
539-
NSString *previousDeploymentKey = [CodePushStatusReport getDeploymentKeyFromStatusReportIdentifier:previousStatusReportIdentifier];
540-
NSString *previousLabel = [CodePushStatusReport getVersionLabelFromStatusReportIdentifier:previousStatusReportIdentifier];
541-
resolve(@{
542-
@"package": currentPackage,
543-
@"status": DeploymentSucceeded,
544-
@"previousDeploymentKey": previousDeploymentKey,
545-
@"previousLabelOrAppVersion": previousLabel
546-
});
547-
} else {
548-
// Previous status report was with a binary app version.
549-
resolve(@{
550-
@"package": currentPackage,
551-
@"status": DeploymentSucceeded,
552-
@"previousLabelOrAppVersion": previousStatusReportIdentifier
553-
});
554-
}
555-
return;
556-
}
557-
}
523+
resolve([CodePushStatusReport getNewPackageStatusReport:currentPackage]);
524+
return;
558525
}
559526
} else if (isRunningBinaryVersion || [_bridge.bundleURL.scheme hasPrefix:@"http"]) {
560527
// Check if the current appVersion has been reported.
561528
NSString *appVersion = [[CodePushConfig current] appVersion];
562-
NSString *previousStatusReportIdentifier = [CodePushStatusReport getPreviousStatusReportIdentifier];
563-
if (previousStatusReportIdentifier == nil) {
564-
[CodePushStatusReport recordDeploymentStatusReported:appVersion];
565-
resolve(@{ @"appVersion": appVersion });
566-
return;
567-
} else if (![previousStatusReportIdentifier isEqualToString:appVersion]) {
568-
[CodePushStatusReport recordDeploymentStatusReported:appVersion];
569-
if ([CodePushStatusReport isStatusReportIdentifierCodePushLabel:previousStatusReportIdentifier]) {
570-
NSString *previousDeploymentKey = [CodePushStatusReport getDeploymentKeyFromStatusReportIdentifier:previousStatusReportIdentifier];
571-
NSString *previousLabel = [CodePushStatusReport getVersionLabelFromStatusReportIdentifier:previousStatusReportIdentifier];
572-
resolve(@{
573-
@"appVersion": appVersion,
574-
@"previousDeploymentKey": previousDeploymentKey,
575-
@"previousLabelOrAppVersion": previousLabel
576-
});
577-
} else {
578-
// Previous status report was with a binary app version.
579-
resolve(@{
580-
@"appVersion": appVersion,
581-
@"previousLabelOrAppVersion": previousStatusReportIdentifier
582-
});
583-
}
584-
return;
585-
}
529+
resolve([CodePushStatusReport getNewAppVersionStatusReport:appVersion]);
530+
return;
586531
}
587532

588-
resolve([NSNull null]);
533+
resolve(nil);
589534
}
590535

591536
/*

CodePushStatusReport.m

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#import "CodePush.h"
22

3-
static NSString *const LastDeploymentReportKey = @"CODE_PUSH_LAST_DEPLOYMENT_REPORT";
3+
static NSString *const DeploymentFailed = @"DeploymentFailed";
44
static NSString *const DeploymentKeyKey = @"deploymentKey";
5+
static NSString *const DeploymentSucceeded = @"DeploymentSucceeded";
56
static NSString *const LabelKey = @"label";
7+
static NSString *const LastDeploymentReportKey = @"CODE_PUSH_LAST_DEPLOYMENT_REPORT";
68

79
@implementation CodePushStatusReport
810

@@ -11,6 +13,78 @@ + (NSString *)getDeploymentKeyFromStatusReportIdentifier:(NSString *)statusRepor
1113
return [[statusReportIdentifier componentsSeparatedByString:@":"] firstObject];
1214
}
1315

16+
+ (NSDictionary *)getFailedUpdateStatusReport:(NSDictionary *)lastFailedPackage
17+
{
18+
return @{
19+
@"package": lastFailedPackage,
20+
@"status": DeploymentFailed
21+
};
22+
}
23+
24+
+ (NSDictionary *)getNewPackageStatusReport:(NSDictionary *)currentPackage
25+
{
26+
NSString *currentPackageIdentifier = [self getPackageStatusReportIdentifier:currentPackage];
27+
NSString *previousStatusReportIdentifier = [self getPreviousStatusReportIdentifier];
28+
if (currentPackageIdentifier) {
29+
if (previousStatusReportIdentifier == nil) {
30+
[self recordDeploymentStatusReported:currentPackageIdentifier];
31+
return @{
32+
@"package": currentPackage,
33+
@"status": DeploymentSucceeded
34+
};
35+
} else if (![previousStatusReportIdentifier isEqualToString:currentPackageIdentifier]) {
36+
[self recordDeploymentStatusReported:currentPackageIdentifier];
37+
if ([self isStatusReportIdentifierCodePushLabel:previousStatusReportIdentifier]) {
38+
NSString *previousDeploymentKey = [self getDeploymentKeyFromStatusReportIdentifier:previousStatusReportIdentifier];
39+
NSString *previousLabel = [self getVersionLabelFromStatusReportIdentifier:previousStatusReportIdentifier];
40+
return @{
41+
@"package": currentPackage,
42+
@"status": DeploymentSucceeded,
43+
@"previousDeploymentKey": previousDeploymentKey,
44+
@"previousLabelOrAppVersion": previousLabel
45+
};
46+
} else {
47+
// Previous status report was with a binary app version.
48+
return @{
49+
@"package": currentPackage,
50+
@"status": DeploymentSucceeded,
51+
@"previousLabelOrAppVersion": previousStatusReportIdentifier
52+
};
53+
}
54+
}
55+
}
56+
57+
return nil;
58+
}
59+
60+
+ (NSDictionary *)getNewAppVersionStatusReport:(NSString *)appVersion
61+
{
62+
NSString *previousStatusReportIdentifier = [self getPreviousStatusReportIdentifier];
63+
if (previousStatusReportIdentifier == nil) {
64+
[self recordDeploymentStatusReported:appVersion];
65+
return @{ @"appVersion": appVersion };
66+
} else if (![previousStatusReportIdentifier isEqualToString:appVersion]) {
67+
[self recordDeploymentStatusReported:appVersion];
68+
if ([self isStatusReportIdentifierCodePushLabel:previousStatusReportIdentifier]) {
69+
NSString *previousDeploymentKey = [self getDeploymentKeyFromStatusReportIdentifier:previousStatusReportIdentifier];
70+
NSString *previousLabel = [self getVersionLabelFromStatusReportIdentifier:previousStatusReportIdentifier];
71+
return @{
72+
@"appVersion": appVersion,
73+
@"previousDeploymentKey": previousDeploymentKey,
74+
@"previousLabelOrAppVersion": previousLabel
75+
};
76+
} else {
77+
// Previous status report was with a binary app version.
78+
return @{
79+
@"appVersion": appVersion,
80+
@"previousLabelOrAppVersion": previousStatusReportIdentifier
81+
};
82+
}
83+
}
84+
85+
return nil;
86+
}
87+
1488
+ (NSString *)getPackageStatusReportIdentifier:(NSDictionary *)package
1589
{
1690
// Because deploymentKeys can be dynamically switched, we use a

android/app/src/main/java/com/microsoft/codepush/react/CodePush.java

Lines changed: 12 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ public class CodePush {
5151
private final String ASSETS_BUNDLE_PREFIX = "assets://";
5252
private final String BINARY_MODIFIED_TIME_KEY = "binaryModifiedTime";
5353
private final String CODE_PUSH_PREFERENCES = "CodePush";
54-
private final String DEPLOYMENT_FAILED_STATUS = "DeploymentFailed";
55-
private final String DEPLOYMENT_SUCCEEDED_STATUS = "DeploymentSucceeded";
5654
private final String DOWNLOAD_PROGRESS_EVENT_NAME = "CodePushDownloadProgress";
5755
private final String FAILED_UPDATES_KEY = "CODE_PUSH_FAILED_UPDATES";
5856
private final String PACKAGE_HASH_KEY = "packageHash";
@@ -429,85 +427,34 @@ protected Void doInBackground(Object... params) {
429427
@ReactMethod
430428
public void getNewStatusReport(Promise promise) {
431429
if (needToReportRollback) {
432-
// Check if there was a rollback that was not yet reported
433430
needToReportRollback = false;
434431
JSONArray failedUpdates = getFailedUpdates();
435432
if (failedUpdates != null && failedUpdates.length() > 0) {
436433
try {
437434
JSONObject lastFailedPackageJSON = failedUpdates.getJSONObject(failedUpdates.length() - 1);
438435
WritableMap lastFailedPackage = CodePushUtils.convertJsonObjectToWriteable(lastFailedPackageJSON);
439-
WritableNativeMap reportMap = new WritableNativeMap();
440-
reportMap.putMap("package", lastFailedPackage);
441-
reportMap.putString("status", DEPLOYMENT_FAILED_STATUS);
442-
promise.resolve(reportMap);
443-
return;
436+
WritableMap failedStatusReport = codePushStatusReport.getFailedUpdateStatusReport(lastFailedPackage);
437+
if (failedStatusReport != null) {
438+
promise.resolve(failedStatusReport);
439+
return;
440+
}
444441
} catch (JSONException e) {
445442
throw new CodePushUnknownException("Unable to read failed updates information stored in SharedPreferences.", e);
446443
}
447444
}
448445
} else if (didUpdate) {
449-
// Check if the current CodePush package has been reported
450446
WritableMap currentPackage = codePushPackage.getCurrentPackage();
451447
if (currentPackage != null) {
452-
String currentPackageIdentifier = codePushStatusReport.getPackageStatusReportIdentifier(currentPackage);
453-
String previousStatusReportIdentifier = codePushStatusReport.getPreviousStatusReportIdentifier();
454-
if (currentPackageIdentifier != null) {
455-
if (previousStatusReportIdentifier == null) {
456-
codePushStatusReport.recordDeploymentStatusReported(currentPackageIdentifier);
457-
WritableNativeMap reportMap = new WritableNativeMap();
458-
reportMap.putMap("package", currentPackage);
459-
reportMap.putString("status", DEPLOYMENT_SUCCEEDED_STATUS);
460-
promise.resolve(reportMap);
461-
return;
462-
} else if (!previousStatusReportIdentifier.equals(currentPackageIdentifier)) {
463-
codePushStatusReport.recordDeploymentStatusReported(currentPackageIdentifier);
464-
if (codePushStatusReport.isStatusReportIdentifierCodePushLabel(previousStatusReportIdentifier)) {
465-
String previousDeploymentKey = codePushStatusReport.getDeploymentKeyFromStatusReportIdentifier(previousStatusReportIdentifier);
466-
String previousLabel = codePushStatusReport.getVersionLabelFromStatusReportIdentifier(previousStatusReportIdentifier);
467-
WritableNativeMap reportMap = new WritableNativeMap();
468-
reportMap.putMap("package", currentPackage);
469-
reportMap.putString("status", DEPLOYMENT_SUCCEEDED_STATUS);
470-
reportMap.putString("previousDeploymentKey", previousDeploymentKey);
471-
reportMap.putString("previousLabelOrAppVersion", previousLabel);
472-
promise.resolve(reportMap);
473-
} else {
474-
// Previous status report was with a binary app version.
475-
WritableNativeMap reportMap = new WritableNativeMap();
476-
reportMap.putMap("package", currentPackage);
477-
reportMap.putString("status", DEPLOYMENT_SUCCEEDED_STATUS);
478-
reportMap.putString("previousLabelOrAppVersion", previousStatusReportIdentifier);
479-
promise.resolve(reportMap);
480-
}
481-
return;
482-
}
448+
WritableMap newPackageStatusReport = codePushStatusReport.getNewPackageStatusReport(currentPackage);
449+
if (newPackageStatusReport != null) {
450+
promise.resolve(newPackageStatusReport);
451+
return;
483452
}
484453
}
485454
} else if (isRunningBinaryVersion) {
486-
// Check if the current appVersion has been reported.
487-
String previousStatusReportIdentifier = codePushStatusReport.getPreviousStatusReportIdentifier();
488-
if (previousStatusReportIdentifier == null) {
489-
codePushStatusReport.recordDeploymentStatusReported(appVersion);
490-
WritableNativeMap reportMap = new WritableNativeMap();
491-
reportMap.putString("appVersion", appVersion);
492-
promise.resolve(reportMap);
493-
return;
494-
} else if (!previousStatusReportIdentifier.equals(appVersion)) {
495-
codePushStatusReport.recordDeploymentStatusReported(appVersion);
496-
if (codePushStatusReport.isStatusReportIdentifierCodePushLabel(previousStatusReportIdentifier)) {
497-
String previousDeploymentKey = codePushStatusReport.getDeploymentKeyFromStatusReportIdentifier(previousStatusReportIdentifier);
498-
String previousLabel = codePushStatusReport.getVersionLabelFromStatusReportIdentifier(previousStatusReportIdentifier);
499-
WritableNativeMap reportMap = new WritableNativeMap();
500-
reportMap.putString("appVersion", appVersion);
501-
reportMap.putString("previousDeploymentKey", previousDeploymentKey);
502-
reportMap.putString("previousLabelOrAppVersion", previousLabel);
503-
promise.resolve(reportMap);
504-
} else {
505-
// Previous status report was with a binary app version.
506-
WritableNativeMap reportMap = new WritableNativeMap();
507-
reportMap.putString("appVersion", appVersion);
508-
reportMap.putString("previousLabelOrAppVersion", previousStatusReportIdentifier);
509-
promise.resolve(reportMap);
510-
}
455+
WritableMap newAppVersionStatusReport = codePushStatusReport.getNewAppVersionStatusReport(appVersion);
456+
if (newAppVersionStatusReport != null) {
457+
promise.resolve(newAppVersionStatusReport);
511458
return;
512459
}
513460
}

0 commit comments

Comments
 (0)