From 393cd620647c808ed141b8a9e3e0fe9f3a0b9d05 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Fri, 22 Oct 2021 14:52:14 -0500 Subject: [PATCH] updated to the registory for generic init for handler and model Signed-off-by: Matt Bruce --- .../MVMCoreActionHandler+Extension.swift | 49 +++++++++++++------ 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler+Extension.swift b/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler+Extension.swift index 48484d9..f490db3 100644 --- a/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler+Extension.swift +++ b/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler+Extension.swift @@ -8,16 +8,42 @@ import Foundation -public protocol MVMCoreActionHandlerProtocol: ModelHandlerProtocol { +public protocol Initable { init() - func handleAction(_ model: ActionModelProtocol, additionalData: [AnyHashable : Any]?, delegateObject: DelegateObject?) +} + +public protocol MVMCoreActionHandlerProtocol: ModelHandlerProtocol, Initable { + func handleAction(_ model: ActionModelProtocol, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) } extension ModelRegistry { - public static func getActionHandler(_ model: ActionModelProtocol) throws -> MVMCoreActionHandlerProtocol { + public static func getModel(_ modelType: String, jsonDict: [String: Any], with type: T.Type) throws -> T { + //get the modelType + guard let foundModelType = ModelRegistry.getType(for: modelType, with: T.self) else { + throw ModelRegistry.Error.decoderErrorModelNotMapped() + } + + //deserialize the model for the modelType found + guard let model = try foundModelType.decode(jsonDict: jsonDict) as? T else { + throw ModelRegistry.Error.decoderOther(message: "Could not decode to \(T.self)") + } + return model + } + + public static func getHandler(_ model: ModelProtocol, with type: H.Type) throws -> H { do { - let type = try ModelRegistry.getHandler(model) as! MVMCoreActionHandlerProtocol.Type - return type.init() + //get the handler and ensure it is initable + guard let initable = try ModelRegistry.getHandler(model) as? Initable.Type else { + throw ModelRegistry.Error.other(message: "\(H.self) type handler isn't of type Initiable") + } + + //init the handler type and ensure it is on type you passed in + guard let handler = initable.init() as? H else { + throw ModelRegistry.Error.other(message: "\(model.self) doesn't have a handler of \(H.self) type") + } + + //return the handler + return handler } catch { throw ModelRegistry.Error.other(message: error.localizedDescription) } @@ -47,18 +73,11 @@ public extension MVMCoreActionHandler { guard let actionType = actionType, let actionInformation = actionInformation else { return false } do { - //get the actionModelType - guard let actionModelType = ModelRegistry.getType(for: actionType, with: ActionModelProtocol.self) else { - throw ModelRegistry.Error.decoderErrorModelNotMapped() - } - - //deserialize the actionModel for the actionType found - guard let actionModel = try actionModelType.decode(jsonDict: actionInformation) as? ActionModelProtocol else { - throw ModelRegistry.Error.decoderOther(message: "Could not decode to ActionModelProtocol") - } + //deserialize the actionModel for the actionType found. + let actionModel = try ModelRegistry.getModel(actionType, jsonDict: actionInformation, with: ActionModelProtocol.self) //get the action Handler for the actionModel created - let actionHandler = try ModelRegistry.getActionHandler(actionModel) + let actionHandler = try ModelRegistry.getHandler(actionModel, with: MVMCoreActionHandlerProtocol.self) //call the handleAction of the handler actionHandler.handleAction(actionModel, additionalData: additionalData, delegateObject: delegateObject)