refactored to use the new handler registration in ModelRestry

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2021-03-15 16:37:32 -05:00
parent 0eaaada439
commit 537da03d2c
3 changed files with 7 additions and 9 deletions

View File

@ -9,9 +9,6 @@
import Foundation import Foundation
@objcMembers public class MoleculeObjectMapping: NSObject { @objcMembers public class MoleculeObjectMapping: NSObject {
public var moleculeMapping: [String: MoleculeViewProtocol.Type] = [:]
/// Returns the mapping object stored in the singleton /// Returns the mapping object stored in the singleton
public static func shared() -> Self? { public static func shared() -> Self? {
return MVMCoreActionUtility.initializerClassCheck(CoreUIObject.sharedInstance()?.moleculeMap, classToVerify: self) as? Self return MVMCoreActionUtility.initializerClassCheck(CoreUIObject.sharedInstance()?.moleculeMap, classToVerify: self) as? Self
@ -19,18 +16,17 @@ import Foundation
/// Registers the model with the model registry and the view with the mapper. /// Registers the model with the model registry and the view with the mapper.
public func register<M: ModelProtocol, V: MoleculeViewProtocol>(viewClass: V.Type, viewModelClass: M.Type) { public func register<M: ModelProtocol, V: MoleculeViewProtocol>(viewClass: V.Type, viewModelClass: M.Type) {
try? ModelRegistry.register(viewModelClass) try? ModelRegistry.register(handler: viewClass, for: viewModelClass)
moleculeMapping.updateValue(viewClass, forKey: viewModelClass.identifier)
} }
/// Returns the type of molecule view for the given model /// Returns the type of molecule view for the given model
public func getMoleculeClass(_ model: MoleculeModelProtocol) -> MoleculeViewProtocol.Type? { public func getMoleculeClass(_ model: MoleculeModelProtocol) -> MoleculeViewProtocol.Type? {
return moleculeMapping[model.moleculeName] return ModelRegistry.getHandler(model) as? MoleculeViewProtocol.Type
} }
/// Creates a molecule with the given model. /// Creates a molecule with the given model.
public func createMolecule(_ model: MoleculeModelProtocol, delegateObject: MVMCoreUIDelegateObject? = nil, additionalData: [AnyHashable: Any]? = nil) -> MoleculeViewProtocol? { public func createMolecule(_ model: MoleculeModelProtocol, delegateObject: MVMCoreUIDelegateObject? = nil, additionalData: [AnyHashable: Any]? = nil) -> MoleculeViewProtocol? {
guard let type = moleculeMapping[model.moleculeName] else { return nil } guard let type = getMoleculeClass(model) else { return nil }
return type.init(model: model, delegateObject, additionalData) return type.init(model: model, delegateObject, additionalData)
} }
@ -286,3 +282,4 @@ import Foundation
} }
} }
} }

View File

@ -199,7 +199,7 @@ open class Stack<T>: Container where T: (StackModelProtocol & MoleculeModelProto
} }
var name = "stack<" var name = "stack<"
for case let item in model.molecules { for case let item in model.molecules {
if let moleculeClass = MoleculeObjectMapping.shared()?.moleculeMapping[item.moleculeName], if let moleculeClass = MoleculeObjectMapping.shared()?.getMoleculeClass(item),
let nameForReuse = moleculeClass.nameForReuse(with: item, delegateObject) { let nameForReuse = moleculeClass.nameForReuse(with: item, delegateObject) {
name.append(nameForReuse + ",") name.append(nameForReuse + ",")
} else { } else {

View File

@ -9,7 +9,7 @@
import UIKit import UIKit
import MVMCore.MVMCoreViewProtocol import MVMCore.MVMCoreViewProtocol
public protocol MoleculeViewProtocol: UIView { public protocol MoleculeViewProtocol: UIView, ModelHandlerProtocol{
/// Initializes the view with the model /// Initializes the view with the model
init(model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) init(model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?)
@ -31,6 +31,7 @@ public protocol MoleculeViewProtocol: UIView {
} }
extension MoleculeViewProtocol { extension MoleculeViewProtocol {
/// Calls set with model /// Calls set with model
public init(model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) { public init(model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
self.init(frame: .zero) self.init(frame: .zero)