From 2426b626fe8e68aadbb4a55ce27b42c029f488de Mon Sep 17 00:00:00 2001 From: "Suresh, Kamlesh" Date: Thu, 14 Jan 2021 19:18:32 -0500 Subject: [PATCH] code review --- .../ActionHandler+ClientParameters.swift | 20 ++++- .../ActionHandling/MVMCoreActionHandler.m | 4 +- .../ClientParameterProtocol.swift | 11 +-- .../ClientParameterRegistry.swift | 75 +++++++++---------- .../MVMCore/Models/Model/ModelRegistry.swift | 16 ++++ .../MFHardCodedServerResponse.h | 2 +- 6 files changed, 73 insertions(+), 55 deletions(-) diff --git a/MVMCore/MVMCore/ActionHandling/ActionHandler+ClientParameters.swift b/MVMCore/MVMCore/ActionHandling/ActionHandler+ClientParameters.swift index fe52f39..bbd951a 100644 --- a/MVMCore/MVMCore/ActionHandling/ActionHandler+ClientParameters.swift +++ b/MVMCore/MVMCore/ActionHandling/ActionHandler+ClientParameters.swift @@ -17,19 +17,31 @@ public extension MVMCoreActionHandler { return } - MVMCoreLoadingOverlayHandler.sharedLoadingOverlay()?.startLoading() + let isBackgroudRequest = actionMap?.boolForKey("background") ?? false - ClientParameterRegistry.injectParams(with: clientParameters) { (clientParams) in + if !isBackgroudRequest { + MVMCoreLoadingOverlayHandler.sharedLoadingOverlay()?.startLoading() + } + + let stopLoadingOverlay = {() in + if !isBackgroudRequest { + MVMCoreLoadingOverlayHandler.sharedLoadingOverlay()?.stopLoading(true) + } + } + + ClientParameterRegistry.injectParameters(with: clientParameters) { (clientParams) in guard let clientParams = clientParams else { + stopLoadingOverlay() completionHandler(actionMap) return } - var extraParams:[String : Any] = actionMap?.optionalDictionaryForKey(KeyExtraParameters) ?? [:] + + var extraParams: [String : Any] = actionMap?.optionalDictionaryForKey(KeyExtraParameters) ?? [:] extraParams.merge(clientParams) { (_, new) in new } var actionMapM = actionMap actionMapM?[KeyExtraParameters] = extraParams - MVMCoreLoadingOverlayHandler.sharedLoadingOverlay()?.stopLoading(true) + stopLoadingOverlay() completionHandler(actionMapM) } } diff --git a/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.m b/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.m index 68cfc07..90374e5 100644 --- a/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.m +++ b/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.m @@ -290,7 +290,7 @@ NSString * const KeyActionTypeOpen = @"openPage"; } // Provide the URL and App URL to be modified if needed by a subclass or delegate. - [self prepareLinkAwayWithURL:otherURL appURL:appURL actionInformation:actionMap additionalData:additionalData delegateObject:delegateObject]; + [weakSelf prepareLinkAwayWithURL:otherURL appURL:appURL actionInformation:actionMap additionalData:additionalData delegateObject:delegateObject]; }; [self preprocessRequest:actionInformation actionBlock:performAction]; @@ -571,7 +571,7 @@ NSString * const KeyActionTypeOpen = @"openPage"; } // Provide the URL and App URL to be modified if needed by a subclass or delegate. - [self prepareLinkAwayWithURL:otherURL appURL:appURL actionInformation:actionMap additionalData:additionalData delegate:delegate]; + [weakSelf prepareLinkAwayWithURL:otherURL appURL:appURL actionInformation:actionMap additionalData:additionalData delegate:delegate]; }; [self preprocessRequest:actionInformation actionBlock:performAction]; diff --git a/MVMCore/MVMCore/Models/ActionType/Client Parameters/ClientParameterProtocol.swift b/MVMCore/MVMCore/Models/ActionType/Client Parameters/ClientParameterProtocol.swift index 3420cf9..a954ac4 100644 --- a/MVMCore/MVMCore/Models/ActionType/Client Parameters/ClientParameterProtocol.swift +++ b/MVMCore/MVMCore/Models/ActionType/Client Parameters/ClientParameterProtocol.swift @@ -21,15 +21,8 @@ extension ClientParameterProtocol { guard let dataModel = paramModel, let json = dataModel.toJSON() else { // JSON parsing failed - if let errorObject = MVMCoreErrorObject(title: MVMCoreGetterUtility.hardcodedString(withKey: HardcodedErrorTitle), - message: MVMCoreGetterUtility.hardcodedString(withKey: HardcodedErrorUnableToProcess), - messageToLog: MVMCoreGetterUtility.hardcodedString(withKey: HardcodedErrorUnableToProcess), - code: ErrorCode.parsingJSON.rawValue, - domain: ErrorDomainNative, - location: String(describing: Self.name)) { - //MFLoggingHandler.addError(toLog: errorObject) - } - return nil + MVMCoreLoggingHandler.logDebugMessage(withDelegate: "ClientParameterProtocol JSON parsing failed") + return nil } return json } diff --git a/MVMCore/MVMCore/Models/ActionType/Client Parameters/ClientParameterRegistry.swift b/MVMCore/MVMCore/Models/ActionType/Client Parameters/ClientParameterRegistry.swift index 7cdcefa..fe91785 100644 --- a/MVMCore/MVMCore/Models/ActionType/Client Parameters/ClientParameterRegistry.swift +++ b/MVMCore/MVMCore/Models/ActionType/Client Parameters/ClientParameterRegistry.swift @@ -18,25 +18,35 @@ import Foundation mapping[T.name] = handler } - public func createParamHandler(_ actionType: String) -> ClientParameterProtocol? { - guard let patamType = mapping[actionType] else { return nil } - return patamType.init() + public func createParametersHandler(_ actionType: String) -> ClientParameterProtocol? { + guard let parameterType = mapping[actionType] else { return nil } + return parameterType.init() } - static func injectParams(with clientParameters: [String: Any]?, + static func getClientParameterModel(_ clientParameters: [String: Any]) -> ClientParameterModel? { + do { + let data = try JSONSerialization.data(withJSONObject: clientParameters) + return try JSONDecoder().decode(ClientParameterModel.self, from: data) + } catch { + MVMCoreLoggingHandler.logDebugMessage(withDelegate: "Error parsing getClientParameterModel: \(error)") + return nil + } + } + + static func injectParameters(with clientParameters: [String: Any], completionHandler:@escaping ([String: Any]?) -> ()) { let clientParameterRegistry = ClientParameterRegistry.shared - guard let clientParameters = clientParameters, - let paramsModelList = clientParameters.optionalArrayForKey("list") as? [[String: Any]] else { + + guard let clientParameterModel = ClientParameterRegistry.getClientParameterModel(clientParameters) else { completionHandler(nil) return } - var paramsList: [String: Any] = [:] - let timeout = clientParameters["timeout"] as? Double ?? 30.0 - - let parametersWorkQueue = DispatchQueue(label: "com.mva.clientparam") + 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." @@ -45,13 +55,13 @@ import Foundation // Setup completion handlers. Barriered to ensure one happens after the other. var complete = false let timeoutWorkItem = DispatchWorkItem(qos: .userInitiated) { - completionHandler(paramsList); + completionHandler(parametersList); complete = true } let completionWorkItem = DispatchWorkItem(qos: .userInitiated) { timeoutWorkItem.cancel() if !complete { // In the case of firing after timeout. - completionHandler(paramsList); + completionHandler(parametersList); complete = true } } @@ -60,38 +70,33 @@ import Foundation parametersWorkQueue.asyncAfter(deadline: .now() + .seconds(Int(timeout)), execute: timeoutWorkItem) // Setup the parameter execution. - for param in paramsModelList { - guard let paramModel = try? Self.getModel(param) else { - if let paramType = param.optionalStringForKey("type") { - paramsList[paramType] = ["error": "Parsing error."] - } - continue - } + for parameterModel in clientParameterModel.list { // Setup default timeout / nil error. This will be replaced as parameters are collected. - paramsList[paramModel.type] = ["error": defaultErrorString] + parametersList[parameterModel.type] = ["error": defaultErrorString] group.enter() // Dispatch asynchronous injection. - clientParameterRegistry.injectParam(paramModel, before: timeout) { (clientParam) in + clientParameterRegistry.injectParameter(parameterModel, before: timeout) { (clientParam) in // Queue the results for merge. parametersWorkQueue.async { if let clientParam = clientParam { - paramsList[paramModel.type] = clientParam + parametersList[parameterModel.type] = clientParam } group.leave() // Leaving is only done after setup (barriered). } } } + // Callback when all parameters have been merged. group.notify(queue: parametersWorkQueue, work: completionWorkItem); } } - func injectParam( _ paramModel: ClientParameterModelProtocol, before timeout: Double, completionHandler:@escaping ([String: Any]?) -> ()) { - guard let paramHandler = ClientParameterRegistry.shared.createParamHandler(paramModel.type) else { + func injectParameter( _ parameterModel: ClientParameterModelProtocol, before timeout: Double, completionHandler:@escaping ([String: Any]?) -> ()) { + guard let parammeterHandler = ClientParameterRegistry.shared.createParametersHandler(parameterModel.type) else { return completionHandler(nil) } - paramHandler.fetchClientParameters(for: paramModel, timingOutIn: timeout, completionHandler: completionHandler) + parammeterHandler.fetchClientParameters(for: parameterModel, timingOutIn: timeout, completionHandler: completionHandler) } /// Add all registry here. @@ -99,20 +104,12 @@ import Foundation /// Register Default Core Bridge Objects public static func register(handler: T.Type, for model: M.Type) { - try? ModelRegistry.register(model) + + do { + try ModelRegistry.register(model) + } catch { + MVMCoreLoggingHandler.logDebugMessage(withDelegate: "Error registering: \(error)") + } ClientParameterRegistry.shared.register(handler) } - - public static func getModel(_ json: [String: Any]) throws -> ClientParameterModelProtocol { - guard let paramType = json.optionalStringForKey("type") else { - throw ModelRegistry.Error.other(message: "parameterType missing") - } - guard let type = ModelRegistry.getType(for: paramType, with: ClientParameterModelProtocol.self) else { - throw ModelRegistry.Error.decoderErrorModelNotMapped() - } - guard let model = try type.decode(jsonDict: json) as? ClientParameterModelProtocol else { - throw ModelRegistry.Error.decoderError - } - return model - } } diff --git a/MVMCore/MVMCore/Models/Model/ModelRegistry.swift b/MVMCore/MVMCore/Models/Model/ModelRegistry.swift index 93407d1..59dc0c7 100644 --- a/MVMCore/MVMCore/Models/Model/ModelRegistry.swift +++ b/MVMCore/MVMCore/Models/Model/ModelRegistry.swift @@ -66,6 +66,22 @@ public struct ModelRegistry { return AnyCodingKey(category.codingKey) } + + + public static func getModelFor(json: [String: Any], type: T.Type) throws -> ModelProtocol? { + + guard let categoryKey = ModelRegistry.getCategory(for: type)?.codingKey else { + throw ModelRegistry.Error.other(message: "coding key missing") + } + guard let parameterType = json.optionalStringForKey(categoryKey) else { + throw ModelRegistry.Error.other(message: "type missing") + } + + guard let type = ModelRegistry.getType(for: parameterType, with: type) else { + throw ModelRegistry.Error.decoderErrorModelNotMapped() + } + return try type.decode(jsonDict: json) + } } extension KeyedDecodingContainer where Key: CodingKey { diff --git a/MVMCore/MVMCore/Utility/HardCodedServerResponse/MFHardCodedServerResponse.h b/MVMCore/MVMCore/Utility/HardCodedServerResponse/MFHardCodedServerResponse.h index eb9fba9..efa33b6 100644 --- a/MVMCore/MVMCore/Utility/HardCodedServerResponse/MFHardCodedServerResponse.h +++ b/MVMCore/MVMCore/Utility/HardCodedServerResponse/MFHardCodedServerResponse.h @@ -9,7 +9,7 @@ #import #import "MVMCoreRequestParameters.h" -#define ENABLE_HARD_CODED_RESPONSE 0 && DEBUG +#define ENABLE_HARD_CODED_RESPONSE 1 && DEBUG #if ENABLE_HARD_CODED_RESPONSE