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?)
}
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.
@ -43,24 +32,29 @@ 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,
guard //ensure there is a actinType
let actionType = actionType,
//ensure there is a serialized version of the Action
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 }
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 {
throw ModelRegistry.Error.decoderOther(message: "Could not decode to ActionModelProtocol")
}
//get the action Handler for the actionModel created
let actionHandler = try ModelRegistry.getActionHandler(actionModel)
//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: "") {
@ -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
}

View File

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

View File

@ -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 {