93 lines
4.2 KiB
Swift
93 lines
4.2 KiB
Swift
//
|
|
// ModuleMolecule.swift
|
|
// MVMCoreUI
|
|
//
|
|
// Created by Scott Pfeil on 6/25/19.
|
|
// Copyright © 2019 Verizon Wireless. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
open class ModuleMoleculeModel: MoleculeProtocol {
|
|
public var backgroundColor: String?
|
|
public static var identifier: String = "moduleMolecule"
|
|
public var moduleName: String
|
|
|
|
enum CodingKeys: String, CodingKey {
|
|
case moduleName
|
|
}
|
|
|
|
required public init(from decoder: Decoder) throws {
|
|
let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
|
|
moduleName = try typeContainer.decode(String.self, forKey:.moduleName)
|
|
}
|
|
|
|
public func encode(to encoder: Encoder) throws {
|
|
var container = encoder.container(keyedBy: CodingKeys.self)
|
|
try container.encode(moduleName, forKey: .moduleName)
|
|
}
|
|
}
|
|
|
|
open class ModuleMolecule: Container {
|
|
var moduleMoleculeModel: ModuleMoleculeModel? {
|
|
get { return model as? ModuleMoleculeModel }
|
|
}
|
|
|
|
public override func setupView() {
|
|
super.setupView()
|
|
}
|
|
|
|
open override func setWithModel(_ model: MoleculeProtocol?, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [String : AnyHashable]?) {
|
|
#warning("need to change getter to get moduleModel instead to use.")
|
|
super.setWithModel(model, delegateObject, additionalData)
|
|
}
|
|
|
|
// MARK: - MVMCoreUIMoleculeViewProtocol
|
|
open override func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) {
|
|
super.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData)
|
|
|
|
guard let moduleName = json?.optionalStringForKey("moduleName"), let module = delegateObject?.moleculeDelegate?.getModuleWithName(moduleName) else {
|
|
// Critical error
|
|
return
|
|
}
|
|
|
|
if view == nil {
|
|
if let moleculeView = MVMCoreUIMoleculeMappingObject.shared()?.createMolecule(forJSON: module, delegateObject: delegateObject, constrainIfNeeded: false) {
|
|
addAndContain(moleculeView)
|
|
}
|
|
} else {
|
|
(view as? MVMCoreUIMoleculeViewProtocol)?.setWithJSON(module, delegateObject: delegateObject, additionalData: additionalData)
|
|
}
|
|
}
|
|
|
|
public class func estimatedHeight(forRow json: [AnyHashable : Any]?, delegateObject: MVMCoreUIDelegateObject?) -> CGFloat {
|
|
guard let moduleName = json?.optionalStringForKey("moduleName"), let module = delegateObject?.moleculeDelegate?.getModuleWithName(moduleName) else {
|
|
// Critical error
|
|
return 0
|
|
}
|
|
return MVMCoreUIMoleculeMappingObject.shared()?.getMoleculeClass(withJSON: module)?.estimatedHeight?(forRow: module, delegateObject: delegateObject) ?? 0
|
|
}
|
|
|
|
public class func name(forReuse molecule: [AnyHashable : Any]?, delegateObject: MVMCoreUIDelegateObject?) -> String? {
|
|
guard let moduleName = molecule?.optionalStringForKey("moduleName"), let module = delegateObject?.moleculeDelegate?.getModuleWithName(moduleName) else {
|
|
// Critical error
|
|
return "moduleMolecule<>"
|
|
}
|
|
return "moduleMolecule<" + (MVMCoreUIMoleculeMappingObject.shared()?.getMoleculeClass(withJSON: module)?.name?(forReuse: module, delegateObject: delegateObject) ?? module.stringForkey(KeyMoleculeName)) + ">"
|
|
}
|
|
|
|
public class func requiredModules(_ json: [AnyHashable : Any]?, delegateObject: MVMCoreUIDelegateObject?, error: AutoreleasingUnsafeMutablePointer<MVMCoreErrorObject?>?) -> [String]? {
|
|
let moduleName = json?.optionalStringForKey("moduleName")
|
|
if moduleName == nil || delegateObject?.moleculeDelegate?.getModuleWithName(moduleName) == nil {
|
|
if let errorObject = MVMCoreErrorObject(title: nil, message: MVMCoreGetterUtility.hardcodedString(withKey: HardcodedErrorUnableToProcess), code: CoreUIErrorCode.ErrorCodeModuleMolecule.rawValue, domain: ErrorDomainNative, location: String(describing: self)) {
|
|
error?.pointee = errorObject
|
|
MVMCoreUILoggingHandler.shared()?.addError(toLog: errorObject)
|
|
}
|
|
}
|
|
if let moduleName = moduleName {
|
|
return [moduleName]
|
|
}
|
|
return nil
|
|
}
|
|
}
|