mvm_core_ui/MVMCoreUI/Molecules/MoleculeCollectionViewCell.swift
Pfeil, Scott Robert 8866e91caa add peaking logic
2019-07-11 16:11:58 -04:00

121 lines
5.3 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 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
}
// 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]?) {
// 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
}
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)
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)
}
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
print("\(self.peakingCover.alpha)")
}
if animated {
UIView.animate(withDuration: 0.4, animations: animation)
} else {
animation()
}
}
}