Merge branch 'bugfix/ActionHandler' into 'develop'

fixed bug in the new ActionHandler logic

See merge request BPHV_MIPS/mvm_core!181
This commit is contained in:
Pfeil, Scott Robert 2021-10-29 18:29:07 +00:00
commit b03532a364
3 changed files with 21 additions and 23 deletions

View File

@ -13,17 +13,6 @@ public protocol MVMCoreActionHandlerProtocol: ModelHandlerProtocol {
func handleAction(_ model: ActionModelProtocol, additionalData: [AnyHashable : Any]?, delegateObject: DelegateObject?) 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 { public extension MVMCoreActionHandler {
/// Converts the action to json for old action handler to handle. /// Converts the action to json for old action handler to handle.
@ -43,20 +32,25 @@ public extension MVMCoreActionHandler {
} }
@objc func hasActionHandler(actionType: String?, actionInformation: [String: Any]?, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) -> Bool { @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, guard //ensure there is a actinType
let actionType = actionType,
//ensure there is a serialized version of the Action
let actionInformation = actionInformation, let actionInformation = actionInformation,
let actionModelType = ModelRegistry.getType(for: actionType, with: ActionModelProtocol.self) //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 } else { return false }
do { do {
//deserialize the actionModel for the actionType found //ensure the decoded actionModel is of ActionModelProtocol
guard let actionModel = try actionModelType.decode(jsonDict: actionInformation) as? ActionModelProtocol else { guard let actionModel = try actionModelType.decode(jsonDict: actionInformation) as? ActionModelProtocol else {
throw ModelRegistry.Error.decoderOther(message: "Could not decode to ActionModelProtocol") throw ModelRegistry.Error.decoderOther(message: "Could not decode to ActionModelProtocol")
} }
//get the action Handler for the actionModel created //create the handler since we know it can initialize
let actionHandler = try ModelRegistry.getActionHandler(actionModel) let actionHandler = actionHandlerType.init()
//call the handleAction of the handler //call the handleAction of the handler
actionHandler.handleAction(actionModel, additionalData: additionalData, delegateObject: delegateObject) actionHandler.handleAction(actionModel, additionalData: additionalData, delegateObject: delegateObject)
@ -68,7 +62,7 @@ public extension MVMCoreActionHandler {
} }
} }
//complete //found the handler, returning true no matter if there was a failure in the do...catch
return true return true
} }

View File

@ -51,7 +51,7 @@ public enum JSONValue: Codable, Equatable {
self = value ?? JSONValue.null self = value ?? JSONValue.null
} }
func decode<T: Decodable>() throws -> T { public func decode<T: Decodable>() throws -> T {
let encoded = try JSONEncoder().encode(self) let encoded = try JSONEncoder().encode(self)
return try JSONDecoder().decode(T.self, from: encoded) return try JSONDecoder().decode(T.self, from: encoded)
} }

View File

@ -164,9 +164,13 @@ public struct ModelRegistry {
// MARK: - Functions // MARK: - Functions
//-------------------------------------------------- //--------------------------------------------------
///This returns a handler type.
public static func getHandler(_ model: ModelProtocol) throws -> ModelHandlerProtocol.Type { public static func getHandler(_ model: ModelProtocol) throws -> ModelHandlerProtocol.Type {
// Get the modelType return try getHandlerType(for: type(of: model))
let modelType = 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 // Get the category for the ModelProtocol
guard let category = categories[modelType.categoryName] else { guard let category = categories[modelType.categoryName] else {