Convenience function

This commit is contained in:
Pfeil, Scott Robert 2020-06-05 16:34:14 -04:00
parent e90bf7c9db
commit fd3f3ea627
2 changed files with 13 additions and 4 deletions

View File

@ -34,6 +34,18 @@ import Foundation
return type.init(model: model, delegateObject, additionalData)
}
/// Convenience function for legacy classes
public func getMoleculeModelForJSON(_ json: [String: Any]) throws -> MoleculeModelProtocol? {
guard let moleculeName = json.optionalStringForKey(KeyMoleculeName),
let type = ModelRegistry.getType(for: moleculeName, with: MoleculeModelProtocol.self) else {
throw ModelRegistry.Error.decoderErrorModelNotMapped
}
guard let model = try type.decode(jsonDict: json) as? MoleculeModelProtocol else {
throw ModelRegistry.Error.decoderError
}
return model
}
/// Call to register all of the CoreUI molecules.
public static func registerObjects() {
// Stacks

View File

@ -93,10 +93,7 @@ import UIKit
/// Convenience setter for legacy files
public static func set(navigationController: UINavigationController, navigationJSON: [String: Any], viewController: UIViewController) throws {
guard let moleculeName = ModelRegistry.getType(for: navigationJSON.stringForkey(KeyMoleculeName), with: MoleculeModelProtocol.self) else {
throw ModelRegistry.Error.keyNotFound
}
guard let barModel = try moleculeName.decode(jsonDict: navigationJSON) as? (MoleculeModelProtocol & NavigationItemModelProtocol) else {
guard let barModel = try MoleculeObjectMapping.shared()?.getMoleculeModelForJSON(navigationJSON) as? (MoleculeModelProtocol & NavigationItemModelProtocol) else {
throw ModelRegistry.Error.decoderOther(message: "Model not a bar model")
}
set(navigationController: navigationController, navigationItemModel: barModel, viewController: viewController)