// // GenericMolecule.swift // JSONCreator // // Created by Matt Bruce on 10/11/22. // Copyright © 2022 Verizon Wireless. All rights reserved. // import Foundation import MVMCore import MVMCoreUI import UIKit import VDS open class GenericMolecule: UIView, MoleculeViewProtocol, MVMCoreViewProtocol, ViewMaskingProtocol { //-------------------------------------------------- // MARK: - Properties //-------------------------------------------------- public var model: MoleculeModelType! public var delegateObject: MVMCoreUIDelegateObject? public var additionalData: [AnyHashable: Any]? private var initialSetupPerformed = false public var shouldMaskWhileRecording: Bool { return model?.shouldMaskRecordedView ?? false } //-------------------------------------------------- // MARK: - Initialization //-------------------------------------------------- public override init(frame: CGRect) { super.init(frame: .zero) initialSetup() } public convenience init() { self.init(frame: .zero) } public required init?(coder: NSCoder) { super.init(coder: coder) initialSetup() } open func initialSetup() { if !initialSetupPerformed { initialSetupPerformed = true setupView() } } public required init(model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) { super.init(frame: .zero) guard let casted = model as? MoleculeModelType else { fatalError("Wrong MoleculeModel sent to \(self). Expected: \(MoleculeModelType.self) but received: \(type(of: model).self)") } self.model = casted initialSetup() set(with: model, delegateObject, additionalData) } //-------------------------------------------------- // MARK: - MoleculeViewProtocol //-------------------------------------------------- open func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) { guard let castedModel = model as? MoleculeModelType else { return } self.model = castedModel self.delegateObject = delegateObject self.additionalData = additionalData viewModelUpdate(model: castedModel) } open func viewModelUpdate(model: MoleculeModelType) { } open func reset() { backgroundColor = .clear } // MARK: Overridables // Base classes need to implement these functions otherwise swift won't respect the subclass functions and use the ones in the protocol extension instead. open class func nameForReuse(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> String? { model.moleculeName } open class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? { nil } open class func requiredModules(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, error: AutoreleasingUnsafeMutablePointer?) -> [String]? { nil } open func updateView(_ size: CGFloat) { } /// Will be called only once. open func setupView() { translatesAutoresizingMaskIntoConstraints = false insetsLayoutMarginsFromSafeArea = false MVMCoreUIUtility.setMarginsFor(self, leading: 0, top: 0, trailing: 0, bottom: 0) } }