diff --git a/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler+Extension.swift b/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler+Extension.swift index 5acdbca..273b8b9 100644 --- a/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler+Extension.swift +++ b/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler+Extension.swift @@ -8,6 +8,11 @@ import Foundation +public protocol MVMCoreActionHandlerProtocol: ModelHandlerProtocol { + init() + func handleAction(_ model: ActionModelProtocol, additionalData: [AnyHashable : Any]?, delegateObject: DelegateObject?) +} + public extension MVMCoreActionHandler { /// Converts the action to json for old action handler to handle. @@ -26,6 +31,41 @@ public extension MVMCoreActionHandler { } } + @objc func hasActionHandler(actionType: String?, actionInformation: [String: Any]?, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) -> Bool { + + guard //ensure there is a actinType + let actionType = actionType, + //ensure there is a serialized version of the Action + let actionInformation = actionInformation, + //esnure the actionModelType + let actionModelType = ModelRegistry.getType(for: actionType, with: ActionModelProtocol.self), + //ensure there is handlerType for the action of MVMCoreActionHandlerProtocol + let actionHandlerType = try? ModelRegistry.getHandlerType(for: actionModelType) as? MVMCoreActionHandlerProtocol.Type + else { return false } + + do { + //ensure the decoded actionModel is of ActionModelProtocol + guard let actionModel = try actionModelType.decode(jsonDict: actionInformation) as? ActionModelProtocol else { + throw ModelRegistry.Error.decoderOther(message: "Could not decode to ActionModelProtocol") + } + + //create the handler since we know it can initialize + let actionHandler = actionHandlerType.init() + + //call the handleAction of the handler + actionHandler.handleAction(actionModel, additionalData: additionalData, delegateObject: delegateObject) + + } catch { + //log the error + if let errorObject = MVMCoreErrorObject.createErrorObject(for: error, location: "") { + MVMCoreActionHandler.shared()?.defaultHandleActionError(errorObject, additionalData: additionalData) + } + } + + //found the handler, returning true no matter if there was a failure in the do...catch + return true + } + /// Start action on current thread. func syncHandleAction(with model: ActionModelProtocol, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) { guard let json = convertActionToJSON(model, delegateObject: delegateObject) else { return } diff --git a/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.m b/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.m index 4f307f5..5877975 100644 --- a/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.m +++ b/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.m @@ -387,7 +387,7 @@ NSString * const KeyActionTypeOpen = @"openPage"; } - (BOOL)handleOtherActions:(nullable NSString *)actionType actionInformation:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegateObject:(nullable DelegateObject *)delegateObject { - return NO; + return [self hasActionHandlerWithActionType:actionType actionInformation:actionInformation additionalData:additionalData delegateObject:delegateObject]; } - (void)unknownAction:(nullable NSString *)actionType actionInformation:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegateObject:(nullable DelegateObject *)delegateObject { diff --git a/MVMCore/MVMCore/Models/JSON/JSONValue.swift b/MVMCore/MVMCore/Models/JSON/JSONValue.swift index a603d23..43796cb 100644 --- a/MVMCore/MVMCore/Models/JSON/JSONValue.swift +++ b/MVMCore/MVMCore/Models/JSON/JSONValue.swift @@ -51,7 +51,7 @@ public enum JSONValue: Codable, Equatable { self = value ?? JSONValue.null } - func decode() throws -> T { + public func decode() throws -> T { let encoded = try JSONEncoder().encode(self) return try JSONDecoder().decode(T.self, from: encoded) } diff --git a/MVMCore/MVMCore/Models/Model/ModelRegistry.swift b/MVMCore/MVMCore/Models/Model/ModelRegistry.swift index 78d8657..f3fbae6 100644 --- a/MVMCore/MVMCore/Models/Model/ModelRegistry.swift +++ b/MVMCore/MVMCore/Models/Model/ModelRegistry.swift @@ -164,9 +164,13 @@ public struct ModelRegistry { // MARK: - Functions //-------------------------------------------------- + ///This returns a handler type. public static func getHandler(_ model: ModelProtocol) throws -> ModelHandlerProtocol.Type { - // Get the modelType - let modelType = type(of: model) + return try getHandlerType(for: type(of: model)) + } + + //get handler for the specific Model + public static func getHandlerType(for modelType: ModelProtocol.Type) throws -> ModelHandlerProtocol.Type{ // Get the category for the ModelProtocol guard let category = categories[modelType.categoryName] else {