code review
This commit is contained in:
parent
4805c1ed43
commit
2426b626fe
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@ -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];
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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<T:ClientParameterProtocol, M: ModelProtocol>(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
|
||||
}
|
||||
}
|
||||
|
||||
@ -66,6 +66,22 @@ public struct ModelRegistry {
|
||||
|
||||
return AnyCodingKey(category.codingKey)
|
||||
}
|
||||
|
||||
|
||||
public static func getModelFor<T>(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 {
|
||||
|
||||
@ -9,7 +9,7 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "MVMCoreRequestParameters.h"
|
||||
|
||||
#define ENABLE_HARD_CODED_RESPONSE 0 && DEBUG
|
||||
#define ENABLE_HARD_CODED_RESPONSE 1 && DEBUG
|
||||
|
||||
#if ENABLE_HARD_CODED_RESPONSE
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user