diff --git a/MVMCore/MVMCore.xcodeproj/project.pbxproj b/MVMCore/MVMCore.xcodeproj/project.pbxproj index fc17f77..aa096b4 100644 --- a/MVMCore/MVMCore.xcodeproj/project.pbxproj +++ b/MVMCore/MVMCore.xcodeproj/project.pbxproj @@ -21,6 +21,12 @@ /* 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 */; }; + 016FF706259D180000F5E4AA /* ActionHandler+ClientParameters.swift in Sources */ = {isa = PBXBuildFile; fileRef = 016FF705259D180000F5E4AA /* ActionHandler+ClientParameters.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 +150,12 @@ /* 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 = ""; }; + 016FF705259D180000F5E4AA /* ActionHandler+ClientParameters.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ActionHandler+ClientParameters.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 +298,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 +429,7 @@ 946EE1B6237B66630036751F /* ActionType */ = { isa = PBXGroup; children = ( + 016FF6EC259A4E3E00F5E4AA /* Client Parameters */, 01F2A03523A80A7300D954D8 /* ActionModelProtocol.swift */, 946EE1BB237B691A0036751F /* ActionOpenPageModel.swift */, 01F2A03823A812DD00D954D8 /* ActionOpenUrlModel.swift */, @@ -560,6 +585,7 @@ AFBB96B51FBA3CEC0008D868 /* MVMCoreActionDelegateProtocol.h */, AFBB96B61FBA3CEC0008D868 /* MVMCoreActionHandler.h */, AFBB96B71FBA3CEC0008D868 /* MVMCoreActionHandler.m */, + 016FF705259D180000F5E4AA /* ActionHandler+ClientParameters.swift */, ); path = ActionHandling; sourceTree = ""; @@ -784,6 +810,7 @@ buildActionMask = 2147483647; files = ( AFED77A71FCCA29400BAE689 /* MVMCoreViewControllerProgrammaticMappingObject.m in Sources */, + 016FF706259D180000F5E4AA /* ActionHandler+ClientParameters.swift in Sources */, 946EE1A7237B5B1C0036751F /* ModelRegistry.swift in Sources */, AFBB96641FBA3A570008D868 /* MVMCoreLoadHandler.m in Sources */, AFFCFA671FCCC0D700FD0730 /* MVMCoreLoadingOverlayHandler.m in Sources */, @@ -820,6 +847,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 +858,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 +874,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/ActionHandler+ClientParameters.swift b/MVMCore/MVMCore/ActionHandling/ActionHandler+ClientParameters.swift new file mode 100644 index 0000000..a2c3841 --- /dev/null +++ b/MVMCore/MVMCore/ActionHandling/ActionHandler+ClientParameters.swift @@ -0,0 +1,56 @@ +// +// ActionHandler+ClientParameters.swift +// MVMCore +// +// Created by Suresh, Kamlesh on 12/30/20. +// Copyright © 2020 myverizon. All rights reserved. +// + +import Foundation + +public extension MVMCoreActionHandler { + + /// Iterates through the clientParameters list. Gets values from the individual handlers and attaches the parameters to extraParameters. + @objc func setClientParameter(with actionMap: [String: Any]?, completionHandler: @escaping ([String : Any]?) -> ()) { + + guard let actionMapWithClientParameters = actionMap, let clientParameters = actionMapWithClientParameters.optionalDictionaryForKey("clientParameters") else { + completionHandler(actionMap) + return + } + + let isBackgroudRequest = actionMapWithClientParameters.boolForKey("background") + + if !isBackgroudRequest { + MVMCoreLoadingOverlayHandler.sharedLoadingOverlay()?.startLoading() + } + + let stopLoadingOverlay = {() in + if !isBackgroudRequest { + MVMCoreLoadingOverlayHandler.sharedLoadingOverlay()?.stopLoading(true) + } + } + + do { + try MVMCoreObject.sharedInstance()?.clientParameterRegistry?.getParameters(with: clientParameters) { (clientParams) in + guard let clientParams = clientParams else { + stopLoadingOverlay() + completionHandler(actionMapWithClientParameters) + return + } + + var extraParams: [String : Any] = actionMapWithClientParameters.dictionaryForKey(KeyExtraParameters) + extraParams.merge(clientParams) { (_, new) in new } + var actionMapM = actionMapWithClientParameters + actionMapM[KeyExtraParameters] = extraParams + + stopLoadingOverlay() + completionHandler(actionMapM) + } + } catch { + stopLoadingOverlay() + completionHandler(actionMapWithClientParameters) + MVMCoreLoggingHandler.logDebugMessage(withDelegate: "Error clientParamters: \(error)") + } + + } +} diff --git a/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.m b/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.m index ee539d0..9362dd6 100644 --- a/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.m +++ b/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.m @@ -107,22 +107,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 setClientParameterWith:actionInformation completionHandler:performAction]; } - (void)shareAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegateObject:(nullable DelegateObject *)delegateObject { @@ -266,23 +272,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 setClientParameterWith: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 +422,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 setClientParameterWith:actionInformation completionHandler:performAction]; } - (void)restartAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate { @@ -524,23 +541,28 @@ 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]; - } - - // 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 delegate:delegate]; + + __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 setClientParameterWith:actionInformation completionHandler:performAction]; } - (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/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..96e51f5 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. 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..ba698f4 --- /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 + 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