Adding isPending

This commit is contained in:
Jonathan Carter
2015-12-16 20:49:27 -08:00
parent a84104c35d
commit 4694db558c
3 changed files with 214 additions and 153 deletions

View File

@@ -13,9 +13,9 @@
RCT_EXPORT_MODULE()
static NSTimer *_timer;
static BOOL usingTestFolder = NO;
// These keys represent the names we use to store data in NSUserDdefaults
static NSString *const FailedUpdatesKey = @"CODE_PUSH_FAILED_UPDATES";
static NSString *const PendingUpdateKey = @"CODE_PUSH_PENDING_UPDATE";
@@ -24,6 +24,11 @@ static NSString *const PendingUpdateKey = @"CODE_PUSH_PENDING_UPDATE";
static NSString *const PendingUpdateHashKey = @"hash";
static NSString *const PendingUpdateIsLoadingKey = @"isLoading";
// These keys are used to inspect/augment the metada
// that is associated with an update's package.
static NSString *const PackageHashKey = @"packageHash";
static NSString *const PackageIsPendingKey = @"isPending";
@synthesize bridge = _bridge;
// Public Obj-C API (see header for method comments)
@@ -141,6 +146,25 @@ static NSString *const PendingUpdateIsLoadingKey = @"isLoading";
return (failedUpdates != nil && [failedUpdates containsObject:packageHash]);
}
/*
* This method checks to see whether a specific package hash
* represents a downloaded and installed update, that hasn't
* been applied yet via an app restart.
*/
- (BOOL)isPendingUpdate:(NSString*)packageHash
{
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
NSDictionary *pendingUpdate = [preferences objectForKey:PendingUpdateKey];
// If there is a pending update, whose hash is equal to the one
// specified, and its "state" isn't loading, then we consider it "pending".
BOOL updateIsPending = pendingUpdate &&
[pendingUpdate[PendingUpdateIsLoadingKey] boolValue] == NO &&
[pendingUpdate[PendingUpdateHashKey] isEqualToString:packageHash];
return updateIsPending;
}
/*
* This method updates the React Native bridge's bundle URL
* to point at the latest CodePush update, and then restarts
@@ -259,7 +283,7 @@ RCT_EXPORT_METHOD(downloadUpdate:(NSDictionary*)updatePackage
// The download completed
doneCallback:^{
NSError *err;
NSDictionary *newPackage = [CodePushPackage getPackage:updatePackage[@"packageHash"] error:&err];
NSDictionary *newPackage = [CodePushPackage getPackage:updatePackage[PackageHashKey] error:&err];
if (err) {
return reject(err);
@@ -293,12 +317,18 @@ RCT_EXPORT_METHOD(getCurrentPackage:(RCTPromiseResolveBlock)resolve
{
dispatch_async(dispatch_get_main_queue(), ^{
NSError *error;
NSDictionary *package = [CodePushPackage getCurrentPackage:&error];
NSMutableDictionary *package = [[CodePushPackage getCurrentPackage:&error] mutableCopy];
if (error) {
reject(error);
} else {
resolve(package);
}
// Add the "isPending" virtual property to the package at this point, so that
// the script-side doesn't need to immediately call back into native to populate it.
BOOL isPendingUpdate = [self isPendingUpdate:[package objectForKey:PackageHashKey]];
[package setObject:@(isPendingUpdate) forKey:PackageIsPendingKey];
resolve(package);
});
}
@@ -318,7 +348,7 @@ RCT_EXPORT_METHOD(installUpdate:(NSDictionary*)updatePackage
if (error) {
reject(error);
} else {
[self savePendingUpdate:updatePackage[@"packageHash"]
[self savePendingUpdate:updatePackage[PackageHashKey]
isLoading:NO];
if (installMode == CodePushInstallModeImmediate) {
@@ -393,4 +423,4 @@ RCT_EXPORT_METHOD(setUsingTestFolder:(BOOL)shouldUseTestFolder)
usingTestFolder = shouldUseTestFolder;
}
@end
@end