From 64e0f240c00e70263d4f5a3c9b7453d848961c8d Mon Sep 17 00:00:00 2001 From: Nikolay Demyankov Date: Tue, 24 May 2016 12:39:42 +0200 Subject: [PATCH] Added fetch update options property to the plugin. You can use it to define default download preferences from the native side. https://github.com/nordnet/cordova-hot-code-push/issues/153 --- src/ios/HCPPlugin.h | 11 +++++++- src/ios/HCPPlugin.m | 28 ++++++++++---------- src/ios/Updater/HCPFetchUpdateOptions.h | 34 +++++++++++++++++++++++++ src/ios/Updater/HCPFetchUpdateOptions.m | 24 +++++++++++++++++ 4 files changed, 83 insertions(+), 14 deletions(-) create mode 100644 src/ios/Updater/HCPFetchUpdateOptions.h create mode 100644 src/ios/Updater/HCPFetchUpdateOptions.m diff --git a/src/ios/HCPPlugin.h b/src/ios/HCPPlugin.h index db10b6da..41a37f90 100644 --- a/src/ios/HCPPlugin.h +++ b/src/ios/HCPPlugin.h @@ -6,13 +6,22 @@ #import #import +#import "HCPFetchUpdateOptions.h" /** * Plugin main class */ @interface HCPPlugin : CDVPlugin -// methods, invoked from JavaScript +#pragma mark Properties + +/** + * Fetch update preferences. Used by default if none provided from JS side. + * Can be used to controll plugin's workflow from the native side. + */ +@property (nonatomic, strong) HCPFetchUpdateOptions *defaultFetchUpdateOptions; + +#pragma mark Methods, invoked from JavaScript /** * Initialize application with callback from web side. diff --git a/src/ios/HCPPlugin.m b/src/ios/HCPPlugin.m index 1ba079ce..29ff796a 100644 --- a/src/ios/HCPPlugin.m +++ b/src/ios/HCPPlugin.m @@ -22,7 +22,7 @@ #import "HCPAssetsFolderHelper.h" #import "NSError+HCPExtension.h" #import "HCPCleanupHelper.h" -#import "NSDictionary+HCPFetchUpdateOptions.h" +#import "HCPUpdateRequest.h" @interface HCPPlugin() { HCPFilesStructure *_filesStructure; @@ -161,23 +161,24 @@ - (void)doLocalInit { * * @return YES if download process started; NO otherwise */ -- (BOOL)_fetchUpdate:(NSString *)callbackId withOptions:(NSDictionary *)options { +- (BOOL)_fetchUpdate:(NSString *)callbackId withOptions:(HCPFetchUpdateOptions *)options { if (!_isPluginReadyForWork) { return NO; } - NSURL *configURL = [options configURL]; - if (!configURL) { - configURL = _pluginXmlConfig.configUrl; + if (!options && self.defaultFetchUpdateOptions) { + options = self.defaultFetchUpdateOptions; } - NSDictionary *headers = [options requestHeaders]; + + HCPUpdateRequest *request = [[HCPUpdateRequest alloc] init]; + request.configURL = options.configFileURL ? options.configFileURL : _pluginXmlConfig.configUrl; + request.requestHeaders = options.requestHeaders; + request.currentWebVersion = _pluginInternalPrefs.currentReleaseVersionName; + request.currentNativeVersion = _pluginXmlConfig.nativeInterfaceVersion; NSError *error = nil; - [[HCPUpdateLoader sharedInstance] downloadUpdateWithConfigUrl:configURL - currentWebVersion:_pluginInternalPrefs.currentReleaseVersionName - currentNativeVersion:_pluginXmlConfig.nativeInterfaceVersion - error:&error - headers:headers]; + [[HCPUpdateLoader sharedInstance] executeDownloadRequest:request error:&error]; + if (error) { if (callbackId) { CDVPluginResult *errorResult = [CDVPluginResult pluginResultWithActionName:kHCPUpdateDownloadErrorEvent @@ -725,9 +726,10 @@ - (void)jsFetchUpdate:(CDVInvokedUrlCommand *)command { [self sendPluginNotReadyToWorkMessageForEvent:kHCPUpdateDownloadErrorEvent callbackID:command.callbackId]; } - NSDictionary *options = command.arguments.count ? command.arguments[0] : nil; + NSDictionary *optionsFromJS = command.arguments.count ? command.arguments[0] : nil; + HCPFetchUpdateOptions *fetchOptions = [[HCPFetchUpdateOptions alloc] initWithDictionary:optionsFromJS]; - [self _fetchUpdate:command.callbackId withOptions:options]; + [self _fetchUpdate:command.callbackId withOptions:fetchOptions]; } - (void)jsInstallUpdate:(CDVInvokedUrlCommand *)command { diff --git a/src/ios/Updater/HCPFetchUpdateOptions.h b/src/ios/Updater/HCPFetchUpdateOptions.h new file mode 100644 index 00000000..db161503 --- /dev/null +++ b/src/ios/Updater/HCPFetchUpdateOptions.h @@ -0,0 +1,34 @@ +// +// HCPFetchUpdateOptions.h +// +// Created by Nikolay Demyankov on 24.05.16. +// + +#import + +/** + * Model for fetch update options. + */ +@interface HCPFetchUpdateOptions : NSObject + +/** + * URL to the config file (chcp.json). + */ +@property (nonatomic, strong) NSURL *configFileURL; + +/** + * Additional request headers. + */ +@property (nonatomic, strong) NSDictionary *requestHeaders; + +/** + * Constructor. + * Used internally in the plugin. + * + * @param dictionary dictionary with options from the JS side + * + * @return object instance + */ +- (instancetype)initWithDictionary:(NSDictionary *)dictionary; + +@end diff --git a/src/ios/Updater/HCPFetchUpdateOptions.m b/src/ios/Updater/HCPFetchUpdateOptions.m new file mode 100644 index 00000000..210d99d6 --- /dev/null +++ b/src/ios/Updater/HCPFetchUpdateOptions.m @@ -0,0 +1,24 @@ +// +// HCPFetchUpdateOptions.m +// +// Created by Nikolay Demyankov on 24.05.16. +// + +#import "HCPFetchUpdateOptions.h" + +static NSString *const CONFIG_URL_JSON_KEY = @"config-file"; +static NSString *const REQUEST_HEADERS_JSON_KEY = @"request-headers"; + +@implementation HCPFetchUpdateOptions + +- (instancetype)initWithDictionary:(NSDictionary *)dictionary { + self = [super init]; + if (self) { + self.configFileURL = dictionary[CONFIG_URL_JSON_KEY] ? [NSURL URLWithString:dictionary[CONFIG_URL_JSON_KEY]] : nil; + self.requestHeaders = dictionary[REQUEST_HEADERS_JSON_KEY]; + } + + return self; +} + +@end