diff --git a/MVMCore/MVMCore.xcodeproj/project.pbxproj b/MVMCore/MVMCore.xcodeproj/project.pbxproj index fc17f77..b06322b 100644 --- a/MVMCore/MVMCore.xcodeproj/project.pbxproj +++ b/MVMCore/MVMCore.xcodeproj/project.pbxproj @@ -21,6 +21,11 @@ /* End PBXAggregateTarget section */ /* Begin PBXBuildFile section */ + 016FF6EE259A4E6300F5E4AA /* ClientParameterModelProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 016FF6ED259A4E6300F5E4AA /* ClientParameterModelProtocol.swift */; }; + 016FF6F2259A4FCC00F5E4AA /* ClientParameterModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 016FF6F1259A4FCC00F5E4AA /* ClientParameterModel.swift */; }; + 016FF6F6259B9AED00F5E4AA /* ClientParameterRegistry.swift in Sources */ = {isa = PBXBuildFile; fileRef = 016FF6F5259B9AED00F5E4AA /* ClientParameterRegistry.swift */; }; + 016FF6FC259BA27E00F5E4AA /* ClientParameterProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 016FF6FB259BA27E00F5E4AA /* ClientParameterProtocol.swift */; }; + 01934FE725A4FFC2003DCD67 /* ClientParameterActionProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01934FE625A4FFC2003DCD67 /* ClientParameterActionProtocol.swift */; }; 01C851CF23CF7B260021F976 /* JSONMap.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01C851CE23CF7B260021F976 /* JSONMap.swift */; }; 01C851D123CF97FE0021F976 /* ActionBackModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01C851D023CF97FE0021F976 /* ActionBackModel.swift */; }; 01DF561421F90ADC00CC099B /* Dictionary+MFConvenience.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01DF561321F90ADC00CC099B /* Dictionary+MFConvenience.swift */; }; @@ -144,6 +149,11 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 016FF6ED259A4E6300F5E4AA /* ClientParameterModelProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClientParameterModelProtocol.swift; sourceTree = ""; }; + 016FF6F1259A4FCC00F5E4AA /* ClientParameterModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClientParameterModel.swift; sourceTree = ""; }; + 016FF6F5259B9AED00F5E4AA /* ClientParameterRegistry.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClientParameterRegistry.swift; sourceTree = ""; }; + 016FF6FB259BA27E00F5E4AA /* ClientParameterProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClientParameterProtocol.swift; sourceTree = ""; }; + 01934FE625A4FFC2003DCD67 /* ClientParameterActionProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClientParameterActionProtocol.swift; sourceTree = ""; }; 01C851CE23CF7B260021F976 /* JSONMap.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSONMap.swift; sourceTree = ""; }; 01C851D023CF97FE0021F976 /* ActionBackModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActionBackModel.swift; sourceTree = ""; }; 01DF561321F90ADC00CC099B /* Dictionary+MFConvenience.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Dictionary+MFConvenience.swift"; sourceTree = ""; }; @@ -286,6 +296,18 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + 016FF6EC259A4E3E00F5E4AA /* Client Parameters */ = { + isa = PBXGroup; + children = ( + 016FF6FB259BA27E00F5E4AA /* ClientParameterProtocol.swift */, + 016FF6ED259A4E6300F5E4AA /* ClientParameterModelProtocol.swift */, + 016FF6F1259A4FCC00F5E4AA /* ClientParameterModel.swift */, + 016FF6F5259B9AED00F5E4AA /* ClientParameterRegistry.swift */, + 01934FE625A4FFC2003DCD67 /* ClientParameterActionProtocol.swift */, + ); + path = "Client Parameters"; + sourceTree = ""; + }; 8876D5BF1FB50A9E00EB2E3D = { isa = PBXGroup; children = ( @@ -405,6 +427,7 @@ 946EE1B6237B66630036751F /* ActionType */ = { isa = PBXGroup; children = ( + 016FF6EC259A4E3E00F5E4AA /* Client Parameters */, 01F2A03523A80A7300D954D8 /* ActionModelProtocol.swift */, 946EE1BB237B691A0036751F /* ActionOpenPageModel.swift */, 01F2A03823A812DD00D954D8 /* ActionOpenUrlModel.swift */, @@ -820,6 +843,7 @@ AFED77A31FCCA29400BAE689 /* MVMCoreViewControllerNibMappingObject.m in Sources */, 8876D5EB1FB50AB000EB2E3D /* NSDecimalNumber+MFConvenience.m in Sources */, 01F2A03923A812DD00D954D8 /* ActionOpenUrlModel.swift in Sources */, + 01934FE725A4FFC2003DCD67 /* ClientParameterActionProtocol.swift in Sources */, AFBB96351FBA34310008D868 /* MVMCoreErrorConstants.m in Sources */, AF43A5881FBB67D6008E9347 /* MVMCoreActionUtility.m in Sources */, AFED77A61FCCA29400BAE689 /* MVMCoreViewControllerStoryBoardMappingObject.m in Sources */, @@ -830,6 +854,7 @@ 8876D5F51FB50AB000EB2E3D /* UILabel+MFCustom.m in Sources */, AFBB96B31FBA3B590008D868 /* MVMCoreGetterUtility.m in Sources */, AF43A7071FC4D7A2008E9347 /* MVMCoreObject.m in Sources */, + 016FF6F6259B9AED00F5E4AA /* ClientParameterRegistry.swift in Sources */, 94C014D924212360005811A9 /* ActionSettingModel.swift in Sources */, D2DEDCB723C63F3B00C44CC4 /* Clamping.swift in Sources */, 01DF561421F90ADC00CC099B /* Dictionary+MFConvenience.swift in Sources */, @@ -845,10 +870,13 @@ AFBB96941FBA3A9A0008D868 /* MVMCorePresentAnimationOperation.m in Sources */, 94C014D524211AF0005811A9 /* ActionCancelModel.swift in Sources */, AF43A5841FBB66DE008E9347 /* MVMCoreConstants.m in Sources */, + 016FF6F2259A4FCC00F5E4AA /* ClientParameterModel.swift in Sources */, D2DEDCBB23C65BC300C44CC4 /* Percent.swift in Sources */, AFBB966A1FBA3A570008D868 /* MVMCoreLoadRequestOperation.m in Sources */, + 016FF6FC259BA27E00F5E4AA /* ClientParameterProtocol.swift in Sources */, 8876D5F31FB50AB000EB2E3D /* UIFont+MFSpacing.m in Sources */, D282AAB82240342D00C46919 /* NSNumber+Extension.swift in Sources */, + 016FF6EE259A4E6300F5E4AA /* ClientParameterModelProtocol.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.h b/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.h index c677e53..374b968 100644 --- a/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.h +++ b/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.h @@ -158,4 +158,6 @@ extern NSString * _Nonnull const KeyActionTypeOpen; // By default, throws an error, calling defaultHandleActionError. + (void)defaultHandleUnknownActionType:(nullable NSString *)actionType actionInformation:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate __deprecated; +// Iterates through the clientParameters list. Gets values from the individual handlers and attaches the parameters to extraParameters. +- (void)setClientParameter:(nullable NSDictionary *)actionInformation completionHandler:(nonnull void (^)(NSDictionary * _Nullable jsonDictionary))completionHandler; @end diff --git a/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.m b/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.m index ee539d0..fa98ee6 100644 --- a/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.m +++ b/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.m @@ -26,6 +26,7 @@ #import "MVMCorePresentationDelegateProtocol.h" #import #import +#import "MVMCoreLoadingOverlayHandler.h" NSString * const KeyActionType = @"actionType"; NSString * const KeyActionTypeLinkAway = @"openURL"; @@ -107,22 +108,28 @@ NSString * const KeyActionTypeOpen = @"openPage"; // Loads the given page type. NSString *pageType = [actionInformation stringForKey:KeyPageType]; - if (pageType.length > 0) { - MVMCoreRequestParameters *requestParameters = [[MVMCoreRequestParameters alloc] initWithActionMap:actionInformation]; - - [self updateRequestParametersBeforeHandleOpenPageAction:requestParameters callBack:^(MVMCoreRequestParameters * _Nonnull requestParameters) { + + if (pageType.length == 0) { + // No page type to load, show error. + MVMCoreErrorObject *error = [[MVMCoreErrorObject alloc] initWithTitle:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorTitle] message:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorUnableToProcess] code:ErrorCodeNoPageType domain:ErrorDomainNative location:[NSString stringWithFormat:@"%@_%@",NSStringFromClass([delegateObject.actionDelegate class]),KeyActionTypeOpen]]; + [self handleActionError:error actionInformation:actionInformation additionalData:additionalData delegateObject:delegateObject]; + return; + } + + __weak typeof(self) weakSelf = self; + void (^performAction)(NSDictionary*) = ^(NSDictionary* actionMap) { + MVMCoreRequestParameters *requestParameters = [[MVMCoreRequestParameters alloc] initWithActionMap:actionMap]; + + [weakSelf updateRequestParametersBeforeHandleOpenPageAction:requestParameters callBack:^(MVMCoreRequestParameters * _Nonnull requestParameters) { if ([delegateObject.actionDelegate respondsToSelector:@selector(handleOpenPageForRequestParameters:actionInformation:additionalData:)]) { [delegateObject.actionDelegate handleOpenPageForRequestParameters:requestParameters actionInformation:actionInformation additionalData:additionalData]; } else { [MVMCoreActionHandler defaultHandleOpenPageForRequestParameters:requestParameters additionalData:additionalData delegateObject:delegateObject]; } }]; - } else { - - // No page type to load, show error. - MVMCoreErrorObject *error = [[MVMCoreErrorObject alloc] initWithTitle:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorTitle] message:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorUnableToProcess] code:ErrorCodeNoPageType domain:ErrorDomainNative location:[NSString stringWithFormat:@"%@_%@",NSStringFromClass([delegateObject.actionDelegate class]),KeyActionTypeOpen]]; - [self handleActionError:error actionInformation:actionInformation additionalData:additionalData delegateObject:delegateObject]; - } + }; + + [self setClientParameter:actionInformation completionHandler:performAction]; } - (void)shareAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegateObject:(nullable DelegateObject *)delegateObject { @@ -266,23 +273,28 @@ NSString * const KeyActionTypeOpen = @"openPage"; #pragma mark - open url functions - (void)linkAwayAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegateObject:(nullable DelegateObject *)delegateObject { - - // Gets the app url - NSURL *appURL = nil; - NSString *appURLString = [actionInformation string:KeyLinkAwayAppURL]; - if (appURLString.length > 0) { - appURL = [NSURL URLWithString:appURLString]; - } - - // Gets the browser url - NSURL *otherURL = nil; - NSString *otherURLString = [actionInformation string:KeyLinkAwayURL]; - if (otherURLString.length > 0) { - otherURL = [NSURL URLWithString:otherURLString]; - } - - // Provide the URL and App URL to be modified if needed by a subclass or delegate. - [self prepareLinkAwayWithURL:otherURL appURL:appURL actionInformation:actionInformation additionalData:additionalData delegateObject:delegateObject]; + + __weak typeof(self) weakSelf = self; + void (^performAction)(NSDictionary*) = ^(NSDictionary* actionMap) { + // Gets the app url + NSURL *appURL = nil; + NSString *appURLString = [actionMap string:KeyLinkAwayAppURL]; + if (appURLString.length > 0) { + appURL = [NSURL URLWithString:appURLString]; + } + + // Gets the browser url + NSURL *otherURL = nil; + NSString *otherURLString = [actionMap string:KeyLinkAwayURL]; + if (otherURLString.length > 0) { + otherURL = [NSURL URLWithString:otherURLString]; + } + + // Provide the URL and App URL to be modified if needed by a subclass or delegate. + [weakSelf prepareLinkAwayWithURL:otherURL appURL:appURL actionInformation:actionMap additionalData:additionalData delegateObject:delegateObject]; + }; + + [self setClientParameter:actionInformation completionHandler:performAction]; } - (void)prepareLinkAwayWithURL:(nullable NSURL *)url appURL:(nullable NSURL *)appURL actionInformation:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegateObject:(nullable DelegateObject *)delegateObject { @@ -411,22 +423,28 @@ NSString * const KeyActionTypeOpen = @"openPage"; // Loads the given page type. NSString *pageType = [actionInformation stringForKey:KeyPageType]; - if (pageType.length > 0) { - MVMCoreRequestParameters *requestParameters = [[MVMCoreRequestParameters alloc] initWithActionMap:actionInformation]; - - [self updateRequestParametersBeforeHandleOpenPageAction:requestParameters callBack:^(MVMCoreRequestParameters * _Nonnull requestParameters) { + + if (pageType.length == 0) { + // No page type to load, show error. + MVMCoreErrorObject *error = [[MVMCoreErrorObject alloc] initWithTitle:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorTitle] message:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorUnableToProcess] code:ErrorCodeNoPageType domain:ErrorDomainNative location:[NSString stringWithFormat:@"%@_%@",NSStringFromClass([delegate class]),KeyActionTypeOpen]]; + [self handleActionError:error actionInformation:actionInformation additionalData:additionalData delegate:delegate]; + return; + } + + __weak typeof(self) weakSelf = self; + void (^performAction)(NSDictionary*) = ^(NSDictionary* actionMap) { + MVMCoreRequestParameters *requestParameters = [[MVMCoreRequestParameters alloc] initWithActionMap:actionMap]; + + [weakSelf updateRequestParametersBeforeHandleOpenPageAction:requestParameters callBack:^(MVMCoreRequestParameters * _Nonnull requestParameters) { if ([delegate respondsToSelector:@selector(handleOpenPageForRequestParameters:actionInformation:additionalData:)]) { - [delegate handleOpenPageForRequestParameters:requestParameters actionInformation:actionInformation additionalData:additionalData]; + [delegate handleOpenPageForRequestParameters:requestParameters actionInformation:actionMap additionalData:additionalData]; } else { [MVMCoreActionHandler defaultHandleOpenPageForRequestParameters:requestParameters additionalData:additionalData delegate:delegate]; } }]; - } else { - - // No page type to load, show error. - MVMCoreErrorObject *error = [[MVMCoreErrorObject alloc] initWithTitle:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorTitle] message:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorUnableToProcess] code:ErrorCodeNoPageType domain:ErrorDomainNative location:[NSString stringWithFormat:@"%@_%@",NSStringFromClass([delegate class]),KeyActionTypeOpen]]; - [self handleActionError:error actionInformation:actionInformation additionalData:additionalData delegate:delegate]; - } + }; + + [self setClientParameter:actionInformation completionHandler:performAction]; } - (void)restartAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate { @@ -524,23 +542,76 @@ NSString * const KeyActionTypeOpen = @"openPage"; } - (void)linkAwayAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate { - - // Gets the app url - NSURL *appURL = nil; - NSString *appURLString = [actionInformation string:KeyLinkAwayAppURL]; - if (appURLString.length > 0) { - appURL = [NSURL URLWithString:appURLString]; + + __weak typeof(self) weakSelf = self; + void (^performAction)(NSDictionary*) = ^(NSDictionary* actionMap) { + // Gets the app url + NSURL *appURL = nil; + NSString *appURLString = [actionMap string:KeyLinkAwayAppURL]; + if (appURLString.length > 0) { + appURL = [NSURL URLWithString:appURLString]; + } + + // Gets the browser url + NSURL *otherURL = nil; + NSString *otherURLString = [actionMap string:KeyLinkAwayURL]; + if (otherURLString.length > 0) { + otherURL = [NSURL URLWithString:otherURLString]; + } + + // Provide the URL and App URL to be modified if needed by a subclass or delegate. + [weakSelf prepareLinkAwayWithURL:otherURL appURL:appURL actionInformation:actionMap additionalData:additionalData delegate:delegate]; + }; + + [self setClientParameter:actionInformation completionHandler:performAction]; +} + +- (void)setClientParameter:(nullable NSDictionary *)actionInformation completionHandler:(nonnull void (^)(NSDictionary * _Nullable jsonDictionary))completionHandler { + + NSDictionary *clientParametersMap = [actionInformation dict:KeyClientParameters]; + if (!clientParametersMap) { + completionHandler(actionInformation); + return; } - - // Gets the browser url - NSURL *otherURL = nil; - NSString *otherURLString = [actionInformation string:KeyLinkAwayURL]; - if (otherURLString.length > 0) { - otherURL = [NSURL URLWithString:otherURLString]; + + BOOL isBackgroudRequest = [actionInformation boolForKey:@"background"]; + + if (!isBackgroudRequest) { + [[MVMCoreLoadingOverlayHandler sharedLoadingOverlay] startLoading]; + } + + void (^stopLoadingOverlay)(void) = ^(void) { + if (!isBackgroudRequest) { + [[MVMCoreLoadingOverlayHandler sharedLoadingOverlay] stopLoading:true]; + } + }; + + NSError *error = nil; + [MVMCoreLoggingHandler logDebugMessageWithDelegate:@"Fetching client parameters"]; + [[[MVMCoreObject sharedInstance] clientParameterRegistry] getParametersWith:clientParametersMap + error:&error + completionHandler:^(NSDictionary * _Nullable clientParameters) { + [MVMCoreLoggingHandler logDebugMessageWithDelegate:@"Finshed fetching client parameters"]; + if (clientParameters) { + NSMutableDictionary *actionWithClientParameters = [actionInformation mutableCopy]; + NSMutableDictionary *extraParameters = [clientParameters mutableCopy]; + [extraParameters addEntriesFromDictionary:[actionWithClientParameters dictionaryForKey:KeyExtraParameters]]; + actionWithClientParameters[KeyExtraParameters] = extraParameters; + + stopLoadingOverlay(); + completionHandler(actionWithClientParameters); + } else { + [MVMCoreLoggingHandler logDebugMessageWithDelegate:@"No client parameters"]; + stopLoadingOverlay(); + completionHandler(actionInformation); + } + }]; + + if (error) { + stopLoadingOverlay(); + completionHandler(actionInformation); + [MVMCoreLoggingHandler addErrorToLog:[MVMCoreErrorObject createErrorObjectForNSError:error location:@"MVMCoreActionHandler->setClientParameter"]]; } - - // Provide the URL and App URL to be modified if needed by a subclass or delegate. - [self prepareLinkAwayWithURL:otherURL appURL:appURL actionInformation:actionInformation additionalData:additionalData delegate:delegate]; } - (void)prepareLinkAwayWithURL:(nullable NSURL *)url appURL:(nullable NSURL *)appURL actionInformation:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate { diff --git a/MVMCore/MVMCore/Constants/MVMCoreJSONConstants.h b/MVMCore/MVMCore/Constants/MVMCoreJSONConstants.h index f3d84ac..220b180 100644 --- a/MVMCore/MVMCore/Constants/MVMCoreJSONConstants.h +++ b/MVMCore/MVMCore/Constants/MVMCoreJSONConstants.h @@ -51,6 +51,8 @@ extern NSString * const KeyContextRoot; extern NSString * const KeyType; extern NSString * const KeyMVMRC; +extern NSString * const KeyClientParameters; + #pragma mark - JSON Values // Server driven response type diff --git a/MVMCore/MVMCore/Constants/MVMCoreJSONConstants.m b/MVMCore/MVMCore/Constants/MVMCoreJSONConstants.m index 71cccac..2127e2e 100644 --- a/MVMCore/MVMCore/Constants/MVMCoreJSONConstants.m +++ b/MVMCore/MVMCore/Constants/MVMCoreJSONConstants.m @@ -51,6 +51,8 @@ NSString * const KeyContextRoot = @"appContext"; NSString * const KeyType = @"type"; NSString * const KeyMVMRC = @"LaunchMVMRC"; +NSString * const KeyClientParameters = @"clientParameters"; + #pragma mark - JSON Values // Server driven response type diff --git a/MVMCore/MVMCore/Models/ActionType/ActionOpenPageModel.swift b/MVMCore/MVMCore/Models/ActionType/ActionOpenPageModel.swift index 40e7312..a20d343 100644 --- a/MVMCore/MVMCore/Models/ActionType/ActionOpenPageModel.swift +++ b/MVMCore/MVMCore/Models/ActionType/ActionOpenPageModel.swift @@ -7,7 +7,7 @@ // -@objcMembers open class ActionOpenPageModel: ActionModelProtocol { +@objcMembers open class ActionOpenPageModel: ActionModelProtocol, ClientParameterActionProtocol { //-------------------------------------------------- // MARK: - Properties //-------------------------------------------------- @@ -20,7 +20,8 @@ public var presentationStyle: String? public var tabBarIndex: Int? public var background: Bool? - + public var clientParameters: ClientParameterModel? + //-------------------------------------------------- // MARK: - Initialzier //-------------------------------------------------- diff --git a/MVMCore/MVMCore/Models/ActionType/ActionOpenUrlModel.swift b/MVMCore/MVMCore/Models/ActionType/ActionOpenUrlModel.swift index 03a1e95..bf7c020 100644 --- a/MVMCore/MVMCore/Models/ActionType/ActionOpenUrlModel.swift +++ b/MVMCore/MVMCore/Models/ActionType/ActionOpenUrlModel.swift @@ -7,7 +7,7 @@ // -@objcMembers public class ActionOpenUrlModel: ActionModelProtocol { +@objcMembers public class ActionOpenUrlModel: ActionModelProtocol, ClientParameterActionProtocol { //-------------------------------------------------- // MARK: - Properties //-------------------------------------------------- @@ -18,6 +18,7 @@ public var browserUrl: String public var extraParameters: JSONValueDictionary? public var analyticsData: JSONValueDictionary? + public var clientParameters: ClientParameterModel? public var appURL: String? //TODO: Should be removed in future releases. This should be MF specific. @@ -30,6 +31,7 @@ public var dontShowProgress: Bool? public var headerParameters: JSONValueDictionary? public var enableNativeScroll: Bool? + public var hideUrl: Bool? //-------------------------------------------------- // MARK: - Initialzier diff --git a/MVMCore/MVMCore/Models/ActionType/Client Parameters/ClientParameterActionProtocol.swift b/MVMCore/MVMCore/Models/ActionType/Client Parameters/ClientParameterActionProtocol.swift new file mode 100644 index 0000000..f969a95 --- /dev/null +++ b/MVMCore/MVMCore/Models/ActionType/Client Parameters/ClientParameterActionProtocol.swift @@ -0,0 +1,13 @@ +// +// ClientParameterActionProtocol.swift +// MVMCore +// +// Created by Suresh, Kamlesh on 1/5/21. +// Copyright © 2021 myverizon. All rights reserved. +// + +import Foundation + +public protocol ClientParameterActionProtocol { + var clientParameters: ClientParameterModel? { get set } +} diff --git a/MVMCore/MVMCore/Models/ActionType/Client Parameters/ClientParameterModel.swift b/MVMCore/MVMCore/Models/ActionType/Client Parameters/ClientParameterModel.swift new file mode 100644 index 0000000..37940d7 --- /dev/null +++ b/MVMCore/MVMCore/Models/ActionType/Client Parameters/ClientParameterModel.swift @@ -0,0 +1,31 @@ +// +// ClientParameterModel.swift +// MVMCore +// +// Created by Suresh, Kamlesh on 12/28/20. +// Copyright © 2020 myverizon. All rights reserved. +// + +import Foundation + +public class ClientParameterModel: Codable { + var timeout: Double? + var list: [ClientParameterModelProtocol] + + private enum CodingKeys: String, CodingKey { + case timeout + case list + } + + required public init(from decoder: Decoder) throws { + let typeContainer = try decoder.container(keyedBy: CodingKeys.self) + timeout = try typeContainer.decodeIfPresent(Double.self, forKey: .timeout) + list = try typeContainer.decodeModels(codingKey: .list) + } + + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encodeIfPresent(timeout, forKey: .timeout) + try container.encodeModels(list, forKey: .list) + } +} diff --git a/MVMCore/MVMCore/Models/ActionType/Client Parameters/ClientParameterModelProtocol.swift b/MVMCore/MVMCore/Models/ActionType/Client Parameters/ClientParameterModelProtocol.swift new file mode 100644 index 0000000..b742170 --- /dev/null +++ b/MVMCore/MVMCore/Models/ActionType/Client Parameters/ClientParameterModelProtocol.swift @@ -0,0 +1,28 @@ +// +// ClientParameterProtocol.swift +// MVMCore +// +// Created by Suresh, Kamlesh on 12/28/20. +// Copyright © 2020 myverizon. All rights reserved. +// + +import Foundation + +public protocol ClientParameterModelProtocol: ModelProtocol { + var type: String { get } +} + +public extension ClientParameterModelProtocol { + + var type: String { + get { Self.identifier } + } + + static var categoryCodingKey: String { + return "type" + } + + static var categoryName: String { + return "\(ClientParameterModelProtocol.self)" + } +} diff --git a/MVMCore/MVMCore/Models/ActionType/Client Parameters/ClientParameterProtocol.swift b/MVMCore/MVMCore/Models/ActionType/Client Parameters/ClientParameterProtocol.swift new file mode 100644 index 0000000..2c689ec --- /dev/null +++ b/MVMCore/MVMCore/Models/ActionType/Client Parameters/ClientParameterProtocol.swift @@ -0,0 +1,15 @@ +// +// ClientParameterProtocol.swift +// MVMCore +// +// Created by Suresh, Kamlesh on 12/29/20. +// Copyright © 2020 myverizon. All rights reserved. +// + +import Foundation + +public protocol ClientParameterProtocol { + init() + static var name: String { get } + func fetchClientParameters(for paramModel: ClientParameterModelProtocol, timingOutIn timeout: Double, completionHandler:@escaping (AnyHashable?) -> ()) +} diff --git a/MVMCore/MVMCore/Models/ActionType/Client Parameters/ClientParameterRegistry.swift b/MVMCore/MVMCore/Models/ActionType/Client Parameters/ClientParameterRegistry.swift new file mode 100644 index 0000000..1a4160d --- /dev/null +++ b/MVMCore/MVMCore/Models/ActionType/Client Parameters/ClientParameterRegistry.swift @@ -0,0 +1,127 @@ +// +// ClientParameterRegistry.swift +// MVMCore +// +// Created by Suresh, Kamlesh on 12/29/20. +// Copyright © 2020 myverizon. All rights reserved. +// + +import Foundation + + +@objcMembers open class ClientParameterRegistry: NSObject { + + private var mapping: [String: ClientParameterProtocol.Type] = [:] + + + public override init() { + super.init() + registerParameters() + } + + open func register(_ handler: T.Type) { + mapping[T.name] = handler + } + + open func createParametersHandler(_ actionType: String) -> ClientParameterProtocol? { + guard let parameterType = mapping[actionType] else { return nil } + return parameterType.init() + } + + static func getClientParameterModel(_ clientParameters: [String: Any]) throws -> ClientParameterModel? { + let data = try JSONSerialization.data(withJSONObject: clientParameters) + return try JSONDecoder().decode(ClientParameterModel.self, from: data) + } + + /// Sample clientParameters + ///{ + /// "timeout": 30, + /// "list": [ + /// { + /// "type": "currentLocation", + /// "inputParameters": { + /// "accuracy": 10, + /// "timeThreshold": 600 + /// } + /// } + /// ] + ///} + /// completionHandler can return flat dictinary or a map. It depends on the paramters handler + open func getParameters(with clientParameters: [String: Any], completionHandler:@escaping ([String: Any]?) -> ()) throws { + + guard let clientParameterModel = try ClientParameterRegistry.getClientParameterModel(clientParameters) else { + completionHandler(nil) + return + } + + var parametersList: [String: Any] = [:] + let timeout = clientParameterModel.timeout ?? 30.0 + + let parametersWorkQueue = DispatchQueue(label: "com.mva.clientparameter") + let group = DispatchGroup() + let defaultErrorString = "failed_to_collect." + + // Dispatch setup on queue to ensure setup is complete before completion callbacks. + parametersWorkQueue.async(group: group, qos: .userInitiated) { [weak self] in + guard let self = self else { return } + // Setup completion handlers. Barriered to ensure one happens after the other. + var complete = false + let timeoutWorkItem = DispatchWorkItem(qos: .userInitiated) { + completionHandler(parametersList); + complete = true + } + let completionWorkItem = DispatchWorkItem(qos: .userInitiated) { + timeoutWorkItem.cancel() + if !complete { // In the case of firing after timeout. + completionHandler(parametersList); + complete = true + } + } + + // Setup timeout. + parametersWorkQueue.asyncAfter(deadline: .now() + .seconds(Int(timeout)), execute: timeoutWorkItem) + + // Setup the parameter execution. + for parameterModel in clientParameterModel.list { + // Setup default timeout / nil error. This will be replaced as parameters are collected. + parametersList[parameterModel.type] = ["error": defaultErrorString] + group.enter() + // Dispatch asynchronous injection. + self.getParameterFromHandler(parameterModel, before: timeout) { (receivedParameter) in + // Queue the results for merge. + parametersWorkQueue.async { + if let receivedParameter = receivedParameter { + parametersList[parameterModel.type] = receivedParameter + } + group.leave() // Leaving is only done after setup (barriered). + } + } + } + + // Callback when all parameters have been merged. + group.notify(queue: parametersWorkQueue, work: completionWorkItem); + } + } + + func getParameterFromHandler( _ parameterModel: ClientParameterModelProtocol, before timeout: Double, completionHandler:@escaping (AnyHashable?) -> ()) { + guard let parameterHandler = createParametersHandler(parameterModel.type) else { + return completionHandler(nil) + } + parameterHandler.fetchClientParameters(for: parameterModel, timingOutIn: timeout, completionHandler: completionHandler) + } + + /// Add all registry here. + open func registerParameters() { } + + /// Register Default Core Client Paramter Objects + public func register(handler: T.Type, for model: M.Type) throws { + try ModelRegistry.register(model) + register(handler) + } + + /// Register Default Core Client Paramter Objects + public static func register(handler: T.Type, for model: M.Type) throws { + try ModelRegistry.register(model) + MVMCoreObject.sharedInstance()?.clientParameterRegistry?.register(handler) + } +} diff --git a/MVMCore/MVMCore/Singletons/MVMCoreObject.h b/MVMCore/MVMCore/Singletons/MVMCoreObject.h index 03a8411..023ad63 100644 --- a/MVMCore/MVMCore/Singletons/MVMCoreObject.h +++ b/MVMCore/MVMCore/Singletons/MVMCoreObject.h @@ -18,6 +18,8 @@ #import #import +@class ClientParameterRegistry; + @interface MVMCoreObject : NSObject @property (nullable, strong, nonatomic) MVMCoreSessionObject *session; @@ -26,6 +28,7 @@ @property (nullable, strong, nonatomic) MVMCoreActionHandler *actionHandler; @property (nullable, strong, nonatomic) MVMCoreSessionTimeHandler *sessionHandler; @property (nullable, strong, nonatomic) MVMCoreLoadHandler *loadHandler; +@property (nullable, strong, nonatomic) ClientParameterRegistry *clientParameterRegistry; // The delegates @property (nullable, strong, nonatomic) id globalLoadDelegate; diff --git a/MVMCore/MVMCore/Singletons/MVMCoreObject.m b/MVMCore/MVMCore/Singletons/MVMCoreObject.m index 8ad4cb5..6b60d65 100644 --- a/MVMCore/MVMCore/Singletons/MVMCoreObject.m +++ b/MVMCore/MVMCore/Singletons/MVMCoreObject.m @@ -7,6 +7,7 @@ // #import "MVMCoreObject.h" +#import @implementation MVMCoreObject @@ -27,6 +28,7 @@ self.actionHandler = [[MVMCoreActionHandler alloc] init]; self.loggingDelegate = [[MVMCoreLoggingHandler alloc] init]; self.loadHandler = [[MVMCoreLoadHandler alloc] init]; + self.clientParameterRegistry = [[ClientParameterRegistry alloc] init]; } @end