65 lines
2.7 KiB
Swift
65 lines
2.7 KiB
Swift
//
|
|
// MoleculeCollectionViewCell.swift
|
|
// MVMCoreUI
|
|
//
|
|
// Created by Scott Pfeil on 7/2/19.
|
|
// Copyright © 2019 Verizon Wireless. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
open class MoleculeCollectionViewCell: UICollectionViewCell, MVMCoreUIMoleculeViewProtocol, MoleculeListCellProtocol {
|
|
open var molecule: (UIView & MVMCoreUIMoleculeViewProtocol)?
|
|
|
|
public override init(frame: CGRect) {
|
|
super.init(frame: .zero)
|
|
setupView()
|
|
}
|
|
|
|
public required init?(coder aDecoder: NSCoder) {
|
|
super.init(coder: aDecoder)
|
|
setupView()
|
|
}
|
|
|
|
public func setupView() {
|
|
}
|
|
|
|
public func setWithJSON(_ json: [AnyHashable : Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable : Any]?) {
|
|
guard let json = json, let moleculeJSON = json.optionalDictionaryForKey(KeyMolecule) else {
|
|
return
|
|
}
|
|
if molecule == nil {
|
|
if let moleculeView = MVMCoreUIMoleculeMappingObject.shared()?.createMolecule(forJSON: moleculeJSON, delegateObject: delegateObject, constrainIfNeeded: true) {
|
|
contentView.addSubview(moleculeView)
|
|
let standardConstraints = (moleculeView as? MVMCoreUIViewConstrainingProtocol)?.useStandardConstraints?() ?? true
|
|
NSLayoutConstraint.activate(Array(NSLayoutConstraint.pinView(toSuperview: moleculeView, useMargins: standardConstraints).values))
|
|
if standardConstraints {
|
|
let constraint = contentView.heightAnchor.constraint(equalToConstant: 80)
|
|
constraint.priority = .defaultLow
|
|
constraint.isActive = true
|
|
}
|
|
molecule = moleculeView
|
|
}
|
|
} else {
|
|
molecule?.setWithJSON(moleculeJSON, delegateObject: delegateObject, additionalData: additionalData)
|
|
}
|
|
if let castView = molecule as? MVMCoreUIViewConstrainingProtocol {
|
|
let standardConstraints = castView.useStandardConstraints?() ?? true
|
|
castView.shouldSetHorizontalMargins?(standardConstraints)
|
|
castView.shouldSetVerticalMargins?(standardConstraints)
|
|
}
|
|
backgroundColor = molecule?.backgroundColor
|
|
}
|
|
|
|
public static func name(forReuse molecule: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?) -> String? {
|
|
guard let molecule = molecule?.optionalDictionaryForKey(KeyMolecule) else {
|
|
return nil
|
|
}
|
|
return MVMCoreUIMoleculeMappingObject.shared()?.getMoleculeClass(withJSON: molecule)?.name?(forReuse: molecule, delegateObject: delegateObject) ?? molecule.optionalStringForKey(KeyMoleculeName)
|
|
}
|
|
|
|
public func updateView(_ size: CGFloat) {
|
|
molecule?.updateView(size)
|
|
}
|
|
}
|