Compare commits
1 Commits
develop
...
feature/ge
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
393cd62064 |
@ -8,16 +8,42 @@
|
|||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
public protocol MVMCoreActionHandlerProtocol: ModelHandlerProtocol {
|
public protocol Initable {
|
||||||
init()
|
init()
|
||||||
|
}
|
||||||
|
|
||||||
|
public protocol MVMCoreActionHandlerProtocol: ModelHandlerProtocol, Initable {
|
||||||
func handleAction(_ model: ActionModelProtocol, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?)
|
func handleAction(_ model: ActionModelProtocol, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?)
|
||||||
}
|
}
|
||||||
|
|
||||||
extension ModelRegistry {
|
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 {
|
do {
|
||||||
let type = try ModelRegistry.getHandler(model) as! MVMCoreActionHandlerProtocol.Type
|
//get the handler and ensure it is initable
|
||||||
return type.init()
|
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 {
|
} catch {
|
||||||
throw ModelRegistry.Error.other(message: error.localizedDescription)
|
throw ModelRegistry.Error.other(message: error.localizedDescription)
|
||||||
}
|
}
|
||||||
@ -47,18 +73,11 @@ public extension MVMCoreActionHandler {
|
|||||||
guard let actionType = actionType, let actionInformation = actionInformation else { return false }
|
guard let actionType = actionType, let actionInformation = actionInformation else { return false }
|
||||||
|
|
||||||
do {
|
do {
|
||||||
//get the actionModelType
|
//deserialize the actionModel for the actionType found.
|
||||||
guard let actionModelType = ModelRegistry.getType(for: actionType, with: ActionModelProtocol.self) else {
|
let actionModel = try ModelRegistry.getModel(actionType, jsonDict: actionInformation, with: ActionModelProtocol.self)
|
||||||
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
|
//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
|
//call the handleAction of the handler
|
||||||
actionHandler.handleAction(actionModel, additionalData: additionalData, delegateObject: delegateObject)
|
actionHandler.handleAction(actionModel, additionalData: additionalData, delegateObject: delegateObject)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user