Skip to content
This repository has been archived by the owner on Oct 1, 2018. It is now read-only.

Commit

Permalink
Added fetch update options property to the plugin. You can use it to …
Browse files Browse the repository at this point in the history
…define default download preferences from the native side.

#153
  • Loading branch information
nikDemyankov committed May 24, 2016
1 parent 1770c4b commit 64e0f24
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 14 deletions.
11 changes: 10 additions & 1 deletion src/ios/HCPPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,22 @@

#import <Cordova/CDVPlugin.h>
#import <Cordova/CDV.h>
#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.
Expand Down
28 changes: 15 additions & 13 deletions src/ios/HCPPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -161,23 +161,24 @@ - (void)doLocalInit {
*
* @return <code>YES</code> if download process started; <code>NO</code> 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<NSString *, NSString *> *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
Expand Down Expand Up @@ -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 {
Expand Down
34 changes: 34 additions & 0 deletions src/ios/Updater/HCPFetchUpdateOptions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// HCPFetchUpdateOptions.h
//
// Created by Nikolay Demyankov on 24.05.16.
//

#import <Foundation/Foundation.h>

/**
* 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<NSString *, NSString *> *requestHeaders;

/**
* Constructor.
* Used internally in the plugin.
*
* @param dictionary dictionary with options from the JS side
*
* @return object instance
*/
- (instancetype)initWithDictionary:(NSDictionary *)dictionary;

@end
24 changes: 24 additions & 0 deletions src/ios/Updater/HCPFetchUpdateOptions.m
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 64e0f24

Please sign in to comment.