From 9a8cae6d9d295ce369a992ea28eaecba8a46616e Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Thu, 21 Oct 2021 15:04:03 -0500 Subject: [PATCH] added MVMCoreActionHandlerProtocol added Extension for ModelRegistry to get the actionHandler added method to be called by Legacy code to check to see if there is an action handler Signed-off-by: Matt Bruce --- .../MVMCoreActionHandler+Extension.swift | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler+Extension.swift b/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler+Extension.swift index 5acdbca..48484d9 100644 --- a/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler+Extension.swift +++ b/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler+Extension.swift @@ -8,6 +8,22 @@ import Foundation +public protocol MVMCoreActionHandlerProtocol: ModelHandlerProtocol { + init() + func handleAction(_ model: ActionModelProtocol, additionalData: [AnyHashable : Any]?, delegateObject: DelegateObject?) +} + +extension ModelRegistry { + public static func getActionHandler(_ model: ActionModelProtocol) throws -> MVMCoreActionHandlerProtocol { + do { + let type = try ModelRegistry.getHandler(model) as! MVMCoreActionHandlerProtocol.Type + return type.init() + } catch { + throw ModelRegistry.Error.other(message: error.localizedDescription) + } + } +} + public extension MVMCoreActionHandler { /// Converts the action to json for old action handler to handle. @@ -26,6 +42,41 @@ public extension MVMCoreActionHandler { } } + @objc func hasActionHandler(actionType: String?, actionInformation: [String: Any]?, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) -> Bool { + //ensure there is a Serialized version of the Action + 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") + } + + //get the action Handler for the actionModel created + let actionHandler = try ModelRegistry.getActionHandler(actionModel) + + //call the handleAction of the handler + actionHandler.handleAction(actionModel, additionalData: additionalData, delegateObject: delegateObject) + + //complete + return true + + } catch { + //log the error + if let errorObject = MVMCoreErrorObject.createErrorObject(for: error, location: "") { + MVMCoreActionHandler.shared()?.defaultHandleActionError(errorObject, additionalData: additionalData) + } + + //incomplete + return false + } + } + /// Start action on current thread. func syncHandleAction(with model: ActionModelProtocol, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) { guard let json = convertActionToJSON(model, delegateObject: delegateObject) else { return }