vds_ios/VDS/Components/RadioSwatch/RadioSwatchGroup.swift
Matt Bruce 572a7dd123 added valueChanged event
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2022-08-30 17:07:21 -05:00

227 lines
8.2 KiB
Swift

//
// RadioSwatchGroup.swift
// VDS
//
// Created by Matt Bruce on 8/25/22.
//
import Foundation
import UIKit
public class RadioSwatchGroup: Control<DefaultRadioSwatchGroupModel>, Changable {
public typealias ModelHandlerType = RadioSwatch
//--------------------------------------------------
// MARK: - Public Properties
//--------------------------------------------------
@Proxy(\.model.selectedModel)
public var selectedModel: ModelHandlerType.ModelType? {
didSet {
sendActions(for: .valueChanged)
}
}
public var onChange: Blocks.ActionBlock?
//--------------------------------------------------
// MARK: - Private Properties
//--------------------------------------------------
private var contentView: UIView = {
return UIView().with {
$0.translatesAutoresizingMaskIntoConstraints = false
$0.backgroundColor = .clear
}
}()
private var label = Label()
private let cellSize: CGFloat = 48.0
private let lineSpacing: CGFloat = 12.0
private let itemSpacing: CGFloat = 16.0
public var collectionViewHeight: NSLayoutConstraint?
public var collectionViewWidth: NSLayoutConstraint?
private lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout().with {
$0.minimumLineSpacing = lineSpacing
$0.minimumInteritemSpacing = itemSpacing
}
return UICollectionView(frame: .zero, collectionViewLayout: layout).with {
$0.backgroundColor = .clear
$0.showsHorizontalScrollIndicator = false
$0.showsVerticalScrollIndicator = false
$0.translatesAutoresizingMaskIntoConstraints = false
$0.register(CollectionViewCell<ModelHandlerType>.self, forCellWithReuseIdentifier: "collectionViewCell")
}
}()
//--------------------------------------------------
// MARK: - Overrides
//--------------------------------------------------
override public var disabled: Bool {
didSet {
updateSelectors()
}
}
override public var surface: Surface {
didSet {
updateSelectors()
}
}
open override func setup() {
super.setup()
isAccessibilityElement = true
accessibilityTraits = .button
addSubview(contentView)
contentView.addSubview(label)
contentView.addSubview(collectionView)
NSLayoutConstraint.activate([
contentView.topAnchor.constraint(equalTo: topAnchor),
contentView.leadingAnchor.constraint(equalTo: leadingAnchor),
contentView.trailingAnchor.constraint(equalTo: trailingAnchor),
contentView.bottomAnchor.constraint(equalTo: bottomAnchor),
label.topAnchor.constraint(equalTo: contentView.topAnchor),
label.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
label.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
collectionView.topAnchor.constraint(equalTo: label.bottomAnchor, constant: 24),
collectionView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
collectionView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
collectionView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor)
])
collectionViewHeight = collectionView.heightAnchor.constraint(greaterThanOrEqualToConstant: cellSize)
collectionViewHeight?.isActive = true
collectionViewWidth = collectionView.widthAnchor.constraint(greaterThanOrEqualToConstant: cellSize * 10)
collectionViewWidth?.isActive = true
}
open override func shouldUpdateView(viewModel: ModelType) -> Bool {
return viewModel != model
}
public override func initialSetup() {
super.initialSetup()
collectionView.delegate = self
collectionView.dataSource = self
}
open override func updateView(viewModel: ModelType) {
label.set(with: viewModel.labelModel)
collectionView.reloadData()
setNeedsLayout()
}
//--------------------------------------------------
// MARK: - Lifecycle
//--------------------------------------------------
// open override func layoutSubviews() {
// super.layoutSubviews()
// // Accounts for any collection size changes
// //setHeight()
//// DispatchQueue.main.async { [weak self] in
//// self?.collectionView.collectionViewLayout.invalidateLayout()
//// }
// }
open func setHeight() {
let bounds = collectionView.bounds
if bounds.width <= 0 {
return
}
if model.selectors.count > 0 {
// Calculate the height
let swatchesInRow = floor(CGFloat(bounds.width/(cellSize + itemSpacing)))
let numberOfRows = ceil(CGFloat(model.selectors.count)/swatchesInRow)
let height = (numberOfRows * cellSize) + (itemSpacing * (numberOfRows-1))
if let oldHeight = collectionViewHeight?.constant,
height != oldHeight {
}
collectionViewHeight?.constant = CGFloat(height)
} else {
collectionViewHeight?.constant = 0
}
}
//Refactor into new CollectionView Selector protocol
public func updateSelectors(){
let selectors = model.selectors.compactMap { existing in
return existing.copyWith {
$0.disabled = disabled
$0.surface = surface
}
}
model.selectors = selectors
}
public func replace(viewModel: ModelHandlerType.ModelType){
if let index = model.selectors.firstIndex(where: { element in
return element.inputId == viewModel.inputId
}) {
model.selectors[index] = viewModel
}
}
}
extension RadioSwatchGroup: UICollectionViewDelegateFlowLayout {
open func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: cellSize, height: cellSize)
}
}
extension RadioSwatchGroup: UICollectionViewDelegate {
open func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
return !model.selectors[indexPath.row].disabled
}
open func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
guard let cell = collectionView.cellForItem(at: indexPath) as? CollectionViewCell<ModelHandlerType> else { return }
//reset the old model
if let selectedModel {
let oldSelectedModel = selectedModel.copyWith {
$0.selected = false
}
replace(viewModel: oldSelectedModel)
}
//set the new model
let newSelectedModel = cell.model.copyWith {
$0.selected = true
}
label.text = newSelectedModel.text
replace(viewModel: newSelectedModel)
selectedModel = newSelectedModel
}
open func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
guard let cell = collectionView.cellForItem(at: indexPath) as? CollectionViewCell<ModelHandlerType> else { return }
cell.isSelected = false
}
}
extension RadioSwatchGroup: UICollectionViewDataSource {
public func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return model.selectors.count
}
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath) as? CollectionViewCell<ModelHandlerType>
let model = model.selectors[indexPath.row]
cell?.modelHandler.isUserInteractionEnabled = false
cell?.set(with: model)
return cell ?? UICollectionViewCell()
}
}