// // CollectionViewCell.swift // MVMCoreUI // // Created by Scott Pfeil on 4/6/20. // Copyright © 2020 Verizon Wireless. All rights reserved. // import Foundation /// A base collection view cell with basic mvm functionality. open class CollectionViewCell: UICollectionViewCell, MoleculeViewProtocol, MVMCoreViewProtocol { // Convenience helpers open var molecule: MoleculeViewProtocol? public let containerHelper = ContainerHelper() open var model: CollectionItemModelProtocol? private var initialSetupPerformed = false // MARK: - Inits 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() } } // MARK: - MVMCoreViewProtocol 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) { if let model = model as? ContainerModelProtocol { containerHelper.updateViewMargins(contentView, model: model, size: size) } (molecule as? MVMCoreViewProtocol)?.updateView(size) } open func reset() { molecule?.reset() backgroundColor = .white } // MARK: - MoleculeViewProtocol 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. if let model = model as? ContainerModelProtocol { containerHelper.set(with: model, for: molecule as? MVMCoreUIViewConstrainingProtocol) } } /// Convenience function. Adds a molecule to the view. open func addMolecule(_ molecule: MoleculeViewProtocol) { contentView.addSubview(molecule) containerHelper.constrainView(molecule) self.molecule = molecule } }