mvm_core_ui/MVMCoreUI/Molecules/MoleculeCollectionViewCell.swift
2019-08-13 15:38:06 -04:00

148 lines
6.2 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)?
open var json: [AnyHashable: Any]?
// In updateView, will set padding to default.
open var updateViewHorizontalDefaults = true
open var updateViewVerticalDefaults = true
open var allowsPeaking = false
var peakingLeftArrow = UIImageView(image: MVMCoreUIUtility.imageNamed("peakingRightArrow")?.withRenderingMode(.alwaysTemplate))
var peakingRightArrow = UIImageView(image: MVMCoreUIUtility.imageNamed("peakingRightArrow")?.withRenderingMode(.alwaysTemplate))
var peakingCover = MVMCoreUICommonViewsUtility.commonView()
public override init(frame: CGRect) {
super.init(frame: .zero)
setupView()
}
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupView()
}
public func setupView() {
guard peakingCover.superview == nil else {
return
}
isAccessibilityElement = false
contentView.isAccessibilityElement = false
if #available(iOS 11.0, *) {
insetsLayoutMarginsFromSafeArea = false
contentView.insetsLayoutMarginsFromSafeArea = false
contentView.preservesSuperviewLayoutMargins = false
}
// Covers the card when peaking.
peakingCover.backgroundColor = .white
peakingCover.alpha = 0
contentView.addSubview(peakingCover)
NSLayoutConstraint.constraintPinSubview(toSuperview: peakingCover)
// A small arrow on the next card for when peaking.
let ratio: CGFloat = 0.015
peakingLeftArrow.translatesAutoresizingMaskIntoConstraints = false
peakingLeftArrow.alpha = 0
peakingLeftArrow.tintColor = .black
contentView.addSubview(peakingLeftArrow)
peakingLeftArrow.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true
NSLayoutConstraint.scalingPinViewLeft(toSuper: peakingLeftArrow, ratio: ratio, anchor: contentView.widthAnchor)
peakingRightArrow.translatesAutoresizingMaskIntoConstraints = false
peakingRightArrow.transform = CGAffineTransform(scaleX: -1, y: 1) // Flip
peakingRightArrow.alpha = 0
peakingRightArrow.tintColor = .black
contentView.addSubview(peakingRightArrow)
peakingRightArrow.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true
NSLayoutConstraint.scalingPinViewRight(toSuper: peakingRightArrow, ratio: ratio, anchor: contentView.widthAnchor)
}
public func setWithJSON(_ json: [AnyHashable : Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable : Any]?) {
self.json = json
if let useHorizontalMargins = json?.optionalBoolForKey("useHorizontalMargins") {
updateViewHorizontalDefaults = useHorizontalMargins
}
if let useVerticalMargins = json?.optionalBoolForKey("useVerticalMargins") {
updateViewVerticalDefaults = useVerticalMargins
}
// Handles peaking.
allowsPeaking = json?.optionalBoolForKey("peakingUI") ?? true
if let peakingArrowColor = json?.optionalStringForKey("peakingArrowColor") {
let color = UIColor.mfGet(forHex: peakingArrowColor)
peakingLeftArrow.tintColor = color
peakingRightArrow.tintColor = color
}
if let backgroundColorString = json?.optionalStringForKey(KeyBackgroundColor) {
backgroundColor = UIColor.mfGet(forHex: backgroundColorString)
}
guard let moleculeJSON = json?.optionalDictionaryForKey(KeyMolecule) else {
return
}
if molecule == nil {
if let moleculeView = MVMCoreUIMoleculeMappingObject.shared()?.createMolecule(forJSON: moleculeJSON, delegateObject: delegateObject, constrainIfNeeded: true) {
contentView.insertSubview(moleculeView, at: 0)
NSLayoutConstraint.activate(Array(NSLayoutConstraint.pinView(toSuperview: moleculeView, useMargins: true).values))
molecule = moleculeView
}
} else {
molecule?.setWithJSON(moleculeJSON, delegateObject: delegateObject, additionalData: additionalData)
}
// This molecule will handle spacing by default.
if let castView = molecule as? MVMCoreUIViewConstrainingProtocol {
castView.shouldSetHorizontalMargins?(false)
castView.shouldSetVerticalMargins?(false)
}
accessibilityElements = molecule?.subviews
}
public func reset() {
molecule?.reset?()
updateViewVerticalDefaults = true
updateViewHorizontalDefaults = true
backgroundColor = .white
}
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)
MFStyler.setDefaultMarginsFor(contentView, size: size, horizontal: updateViewHorizontalDefaults, vertical: updateViewVerticalDefaults)
}
public func setPeaking(_ peaking: Bool, animated: Bool) {
guard allowsPeaking else {
return
}
let animation = {() in
self.peakingRightArrow.alpha = peaking ? 1 : 0
self.peakingLeftArrow.alpha = peaking ? 1 : 0
self.peakingCover.alpha = peaking ? 0.5 : 0
}
if animated {
UIView.animate(withDuration: 0.4, animations: animation)
} else {
animation()
}
}
}