// // CollectionViewCell.swift // MVMCoreUI // // Created by Scott Pfeil on 4/6/20. // Copyright © 2020 Verizon Wireless. All rights reserved. // import Foundation open class CollectionViewCell: UICollectionViewCell, MoleculeViewProtocol, MVMCoreViewProtocol { // Convenience helpers open var molecule: MoleculeViewProtocol? public let containerHelper = ContainerHelper() open var model: CollectionItemModelProtocol? private var initialSetupPerformed = false public override init(frame: CGRect) { super.init(frame: .zero) initialSetup() } public required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) initialSetup() } private func initialSetup() { if !initialSetupPerformed { initialSetupPerformed = true setupView() } } open func setupView() { isAccessibilityElement = false contentView.isAccessibilityElement = false insetsLayoutMarginsFromSafeArea = false contentView.insetsLayoutMarginsFromSafeArea = false contentView.preservesSuperviewLayoutMargins = false MVMCoreUIUtility.setMarginsFor(self, leading: 0, top: 0, trailing: 0, bottom: 0) } open func updateView(_ size: CGFloat) { containerHelper.updateViewMargins(contentView, model: model, size: size) DispatchQueue.main.async { print("\(self.contentView.directionalLayoutMargins.leading)") } } open func reset() { molecule?.reset() backgroundColor = .white } open func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) { guard let model = model as? CollectionItemModelProtocol else { return } self.model = model if let moleculeModel = model as? MoleculeModelProtocol, let backgroundColor = moleculeModel.backgroundColor { self.backgroundColor = backgroundColor.uiColor } // align if needed. containerHelper.set(with: model, for: molecule as? MVMCoreUIViewConstrainingProtocol) } /// Convenience function. Adds the molecule to the view. open func addMolecule(_ molecule: MoleculeViewProtocol) { contentView.addSubview(molecule) containerHelper.constrainView(molecule) self.molecule = molecule } }