updated to the registory for generic init for handler and model
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
6c8bb9ce4b
commit
393cd62064
@ -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<T>(_ 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<H>(_ 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)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user