Skip to content

Commit

Permalink
hacky fix for issue nordnet#85 nordnet#85
Browse files Browse the repository at this point in the history
  • Loading branch information
Bill Luoma committed Mar 24, 2016
1 parent 62dc201 commit eeb7c02
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 7 deletions.
54 changes: 51 additions & 3 deletions src/ios/HCPPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ -(void)pluginInitialize {

// install www folder if it is needed
if ([self isWWwFolderNeedsToBeInstalled]) {
[self installWwwFolder];
//[self installWwwFolder]; //BL
[self installWwwFolderSync]; //blocks on startup. BL
return;
}

Expand Down Expand Up @@ -108,6 +109,51 @@ - (void)installWwwFolder {
[HCPAssetsFolderHelper installWwwFolderToExternalStorageFolder:_filesStructure.wwwFolder];
}

//this blocks. for startup in wkwebview, BL
- (void)installWwwFolderSync {

NSLog(@"%s in", __PRETTY_FUNCTION__);

_isPluginReadyForWork = NO;

// reset www folder installed flag
if (_pluginInternalPrefs.isWwwFolderInstalled) {
_pluginInternalPrefs.wwwFolderInstalled = NO;
[_pluginInternalPrefs saveToUserDefaults];
}

HCPAssetsFolderHelper *helperInstance = [HCPAssetsFolderHelper sharedInstance];
NSError *error = [helperInstance installWwwFolderToExternalStorageFolderSync:_filesStructure.wwwFolder];

if (error) {
NSLog(@"%s error installing www to external storage: %@", __PRETTY_FUNCTION__, error);
}
else {

// update stored config with new application build version
_pluginInternalPrefs.appBuildVersion = [NSBundle applicationBuildVersion];
_pluginInternalPrefs.wwwFolderInstalled = YES;
[_pluginInternalPrefs saveToUserDefaults];

[self resetIndexPageToExternalStorage];

// fetch update
[self loadApplicationConfig];

if (_pluginXmlConfig.isUpdatesAutoDownloadAllowed &&
![HCPUpdateLoader sharedInstance].isDownloadInProgress &&
![HCPUpdateInstaller sharedInstance].isInstallationInProgress) {
[self _fetchUpdate:nil];
}
}

// allow work
_isPluginReadyForWork = YES;

NSLog(@"%s out", __PRETTY_FUNCTION__);
}


/**
* Load application config from file system
*/
Expand Down Expand Up @@ -199,6 +245,7 @@ - (BOOL)_installUpdate:(NSString *)callbackID {
NSString *newVersion = _pluginInternalPrefs.readyForInstallationReleaseVersionName;
NSString *currentVersion = _pluginInternalPrefs.currentReleaseVersionName;

NSLog(@"%s newVersion: %@, currentVersion: %@", __PRETTY_FUNCTION__, newVersion, currentVersion);
NSError *error = nil;
[[HCPUpdateInstaller sharedInstance] installVersion:newVersion currentVersion:currentVersion error:&error];
if (error) {
Expand Down Expand Up @@ -264,6 +311,7 @@ - (void)resetIndexPageToExternalStorage {

// rewrite starting page www folder path: should load from external storage
if ([self.viewController isKindOfClass:[CDVViewController class]]) {
NSLog(@"%s _filesStructure.wwwFolder.absoluteString: %@", __PRETTY_FUNCTION__, _filesStructure.wwwFolder.absoluteString);
((CDVViewController *)self.viewController).wwwFolderName = _filesStructure.wwwFolder.absoluteString;
} else {
NSLog(@"HotCodePushError: Can't make starting page to be from external storage. Main controller should be of type CDVViewController.");
Expand Down Expand Up @@ -572,11 +620,11 @@ - (void)onUpdateInstallationErrorEvent:(NSNotification *)notification {
* @param notification captured notification with the event details
*/
- (void)onUpdateInstalledEvent:(NSNotification *)notification {
_appConfig = notification.userInfo[kHCPEventUserInfoApplicationConfigKey];
_appConfig = notification.userInfo[kHCPEventUserInfoApplicationConfigKey]; //this is just the current config passed by HCPInstallationWorker. BL

_pluginInternalPrefs.readyForInstallationReleaseVersionName = @"";
_pluginInternalPrefs.previousReleaseVersionName = _pluginInternalPrefs.currentReleaseVersionName;
_pluginInternalPrefs.currentReleaseVersionName = _appConfig.contentConfig.releaseVersion;
_pluginInternalPrefs.currentReleaseVersionName = _appConfig.contentConfig.releaseVersion; //current and previous now the same. problem? BL
[_pluginInternalPrefs saveToUserDefaults];

_filesStructure = [[HCPFilesStructure alloc] initWithReleaseVersion:_pluginInternalPrefs.currentReleaseVersionName];
Expand Down
35 changes: 32 additions & 3 deletions src/ios/Updater/HCPInstallationWorker.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#import "NSError+HCPExtension.h"
#import "NSData+HCPMD5.h"
#import "HCPEvents.h"
#import "HCPPluginInternalPreferences.h"

@interface HCPInstallationWorker() {
HCPFilesStructure *_newReleaseFS;
Expand Down Expand Up @@ -41,6 +42,8 @@ - (instancetype)initWithNewVersion:(NSString *)newVersion currentVersion:(NSStri
if (self) {
_newReleaseFS = [[HCPFilesStructure alloc] initWithReleaseVersion:newVersion];
_currentReleaseFS = [[HCPFilesStructure alloc] initWithReleaseVersion:currentVersion];
NSLog(@"%s new: %@", __PRETTY_FUNCTION__, _newReleaseFS.wwwFolder.path);
NSLog(@"%s current: %@", __PRETTY_FUNCTION__, _currentReleaseFS.wwwFolder.path);
}

return self;
Expand Down Expand Up @@ -85,8 +88,12 @@ - (void)dispatchEventWithError:(NSError *)error {
* Send event that update was successfully installed
*/
- (void)dispatchSuccessEvent {
//NSNotification *notification = [HCPEvents notificationWithName:kHCPUpdateIsInstalledEvent //BL
// applicationConfig:_newConfig //BL
// taskId:self.workerId]; //BL

NSNotification *notification = [HCPEvents notificationWithName:kHCPUpdateIsInstalledEvent
applicationConfig:_newConfig
applicationConfig:_oldConfig //always load from same folder. BL
taskId:self.workerId];


Expand Down Expand Up @@ -259,19 +266,40 @@ - (BOOL)moveDownloadedFilesToWwwFolder:(NSError **)error {
}
}

//blow away current. BL
if (![_fileManager removeItemAtURL:_currentReleaseFS.wwwFolder error:error]) {
errorMsg = [NSString stringWithFormat:@"Failed to remove current www folder: %@. Installation failed: %@",
_currentReleaseFS.wwwFolder.path, [(*error) underlyingErrorLocalizedDesription]];
}

// copy new files into current www folder. BL
if (![_fileManager copyItemAtURL:_newReleaseFS.wwwFolder toURL:_currentReleaseFS.wwwFolder error:error]) {
errorMsg = [NSString stringWithFormat:@"Failed to copy new files %@ into current www folder: %@. Installation failed.",
_currentReleaseFS.wwwFolder.path, [(*error) underlyingErrorLocalizedDesription]];

}


if (errorMsg) {
*error = [NSError errorWithCode:kHCPFailedToCopyNewContentFilesErrorCode description:errorMsg];
}

return (*error == nil);
}


/**
* Save loaded configs to the www folder. They are now our current configs.
*/
- (void)saveNewConfigsToWwwFolder {
[_manifestStorage store:_newManifest inFolder:_newReleaseFS.wwwFolder];
[_configStorage store:_newConfig inFolder:_newReleaseFS.wwwFolder];
//[_manifestStorage store:_newManifest inFolder:_newReleaseFS.wwwFolder]; //BL
//[_configStorage store:_newConfig inFolder:_newReleaseFS.wwwFolder]; //BL

//use same folder for wkwebview issue #85. Probably better not use the date from chcp.json as a folder name since it's a lie now. BL
[_manifestStorage store:_newManifest inFolder:_currentReleaseFS.wwwFolder];
[_configStorage store:_newConfig inFolder:_currentReleaseFS.wwwFolder];


}

/**
Expand All @@ -286,6 +314,7 @@ - (void)cleanUpOnFailure {
*/
- (void)cleanUpOnSucess {
[_fileManager removeItemAtURL:_newReleaseFS.downloadFolder error:nil];

}

@end
5 changes: 5 additions & 0 deletions src/ios/Utils/HCPAssetsFolderHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@
*/
+ (void)installWwwFolderToExternalStorageFolder:(NSURL *)externalFolderURL;

+ (HCPAssetsFolderHelper *)sharedInstance;

- (NSError*)installWwwFolderToExternalStorageFolderSync:(NSURL *)externalFolderURL;


@end
36 changes: 35 additions & 1 deletion src/ios/Utils/HCPAssetsFolderHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ + (void)installWwwFolderToExternalStorageFolder:(NSURL *)externalFolderURL {
});
}

#pragma mark Private API

//expose this. BL
+ (HCPAssetsFolderHelper *)sharedInstance {
static HCPAssetsFolderHelper *sharedInstance = nil;
static dispatch_once_t onceToken;
Expand All @@ -47,6 +47,40 @@ + (HCPAssetsFolderHelper *)sharedInstance {
return sharedInstance;
}

//blocks. for startup. BL
- (NSError*)installWwwFolderToExternalStorageFolderSync:(NSURL *)externalFolderURL {
NSLog(@"%s in", __PRETTY_FUNCTION__);
self.isWorking = YES;
NSError *error = nil;
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isWWwFolderExists = [fileManager fileExistsAtPath:externalFolderURL.path];

// remove previous version of the www folder
if (isWWwFolderExists) {
[fileManager removeItemAtURL:[externalFolderURL URLByDeletingLastPathComponent] error:&error];
}

// create new www folder
if (![fileManager createDirectoryAtURL:[externalFolderURL URLByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:&error]) {
//[self dispatchErrorEvent:error];
NSLog(@"%s out err: %@", __PRETTY_FUNCTION__, error);

return error;
}

// copy www folder from bundle to cache folder
NSURL *localWww = [NSURL fileURLWithPath:[NSBundle pathToWwwFolder] isDirectory:YES];
[fileManager copyItemAtURL:localWww toURL:externalFolderURL error:&error];

self.isWorking = NO;
NSLog(@"%s out", __PRETTY_FUNCTION__);

return error;
}


#pragma mark Private API

- (void)__installWwwFolderToExternalStorageFolder:(NSURL *)externalFolderURL {
NSError *error = nil;
NSFileManager *fileManager = [NSFileManager defaultManager];
Expand Down

0 comments on commit eeb7c02

Please sign in to comment.