// // ModuleMolecule.swift // MVMCoreUI // // Created by Scott Pfeil on 6/25/19. // Copyright © 2019 Verizon Wireless. All rights reserved. // import UIKit open class ModuleMolecule: ViewConstrainingView { open var molecule: (UIView & MVMCoreUIMoleculeViewProtocol)? open override func updateView(_ size: CGFloat) { super.updateView(size) molecule?.updateView(size) } // 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 molecule == nil { if let moleculeView = MVMCoreUIMoleculeMappingObject.shared()?.createMolecule(forJSON: module, delegateObject: delegateObject, constrainIfNeeded: true) { addSubview(moleculeView) NSLayoutConstraint.activate(Array(NSLayoutConstraint.pinView(toSuperview: moleculeView, useMargins: false).values)) molecule = moleculeView } } else { molecule?.setWithJSON(module, delegateObject: delegateObject, additionalData: additionalData) } } open override func setAsMolecule() { super.setAsMolecule() updateViewHorizontalDefaults = false molecule?.setAsMolecule?() } open override func reset() { super.reset() molecule?.reset?() } public override static 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 override static 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 override static func requiredModules(_ json: [AnyHashable : Any]?, delegateObject: MVMCoreUIDelegateObject?, error: AutoreleasingUnsafeMutablePointer?) -> [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 } // MARK: - MVMCoreUIViewConstrainingProtocol open override func useStandardConstraints() -> Bool { return (molecule as? MVMCoreUIViewConstrainingProtocol)?.useStandardConstraints?() ?? true } open override func alignHorizontal(_ alignment: UIStackView.Alignment) { (molecule as? MVMCoreUIViewConstrainingProtocol)?.alignHorizontal?(alignment) } open override func alignVertical(_ alignment: UIStackView.Alignment) { (molecule as? MVMCoreUIViewConstrainingProtocol)?.alignVertical?(alignment) } open override func shouldSetHorizontalMargins(_ shouldSet: Bool) { (molecule as? MVMCoreUIViewConstrainingProtocol)?.shouldSetHorizontalMargins?(shouldSet) } open override func shouldSetVerticalMargins(_ shouldSet: Bool) { (molecule as? MVMCoreUIViewConstrainingProtocol)?.shouldSetVerticalMargins?(shouldSet) } }