vds_ios/VDS/Components/Calendar/CalendarFooterReusableView.swift
2024-05-30 10:12:55 -04:00

270 lines
9.9 KiB
Swift

//
// CalendarFooterReusableView.swift
// VDS
//
// Created by Kanamarlapudi, Vasavi on 29/04/24.
//
import UIKit
import VDSCoreTokens
/// Footer view to show indicators data.
class CalendarFooterReusableView: UICollectionReusableView {
///Identifier for the Calendar Footer Reusable View.
static let identifier: String = String(describing: CalendarFooterReusableView.self)
//--------------------------------------------------
// MARK: - Private Properties
//--------------------------------------------------
private var surface: Surface = .light
private var items: [CalendarBase.CalendarIndicatorModel] = []
internal var containerSize: CGSize { CGSize(width: 304, height: 40) }
internal var indicatorWidth = 8.0
var textLabel: Label = Label().with {
$0.translatesAutoresizingMaskIntoConstraints = false
$0.textAlignment = .left
$0.textStyle = .bodySmall
$0.numberOfLines = 1
}
internal var containerView = View().with {
$0.clipsToBounds = true
$0.isAccessibilityElement = true
$0.accessibilityLabel = "Legend"
}
private let flowLayout = LeftAlignedCollectionViewFlowLayout().with {
$0.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
$0.minimumLineSpacing = VDSLayout.space1X
$0.minimumInteritemSpacing = VDSLayout.space4X
$0.scrollDirection = .vertical
}
open lazy var legendCollectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout).with {
$0.isScrollEnabled = false
$0.translatesAutoresizingMaskIntoConstraints = false
$0.showsVerticalScrollIndicator = false
$0.showsHorizontalScrollIndicator = false
$0.isAccessibilityElement = true
$0.backgroundColor = .clear
$0.delegate = self
$0.dataSource = self
$0.register(LegendCollectionViewCell.self,
forCellWithReuseIdentifier: LegendCollectionViewCell.identifier)
}
private var topConstraint: NSLayoutConstraint?
private var legendLabels: [Label] = []
//--------------------------------------------------
// MARK: - Initializers
//--------------------------------------------------
override init(frame: CGRect) {
super.init(frame: frame)
setUp()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setUp()
}
open override var accessibilityElements: [Any]? {
get {
return [containerView, legendLabels]
}
set { super.accessibilityElements = newValue }
}
//--------------------------------------------------
// MARK: - Private Methods
//--------------------------------------------------
/// Configuring the cell with default setup.
private func setUp() {
isAccessibilityElement = false
addSubview(containerView)
containerView.pinToSuperView()
// legend Collection View
containerView.addSubview(legendCollectionView)
legendCollectionView
.pinTopLessThanOrEqualTo(topAnchor, VDSLayout.space6X, .defaultLow)
.pinBottom()
.pinLeading(VDSLayout.space3X)
.pinTrailing(VDSLayout.space3X)
.width(containerSize.width - (2 * VDSLayout.space3X))
.heightGreaterThanEqualTo(16)
}
/// Updating UI to show legend with titles.
func update(with surface: Surface, indicators: [CalendarBase.CalendarIndicatorModel]) {
self.items = indicators
self.surface = surface
legendLabels.removeAll()
legendCollectionView.reloadData()
var height = legendCollectionView.collectionViewLayout.collectionViewContentSize.height
if height > 0 {
topConstraint?.isActive = false
height = height > containerSize.height ? containerSize.height : height
let top = containerSize.height - height
topConstraint = legendCollectionView.topAnchor.constraint(equalTo: topAnchor, constant: top)
topConstraint?.isActive = true
}
}
}
extension CalendarFooterReusableView: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return items.count
}
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard collectionView == legendCollectionView,
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: LegendCollectionViewCell.identifier, for: indexPath) as? LegendCollectionViewCell,
indexPath.row <= items.count else { return UICollectionViewCell() }
let text = items[indexPath.row].label
cell.updateTitle(text: text,
color: VDSColor.elementsSecondaryOnlight,
surface: self.surface,
clearFullcircle: indexPath.row == 1,
drawSemiCircle: indexPath.row == 2)
legendLabels.append(cell.title)
return cell
}
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
textLabel.text = items[indexPath.row].label
let intrinsicSize = textLabel.intrinsicContentSize
let cellwidth = intrinsicSize.width + indicatorWidth + VDSLayout.space2X
return .init(width: min(cellwidth, collectionView.frame.width), height: intrinsicSize.height)
}
}
private class LegendCollectionViewCell: UICollectionViewCell {
static let identifier: String = String(describing: LegendCollectionViewCell.self)
private let textColorConfiguration = SurfaceColorConfiguration(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark)
private let indicatorColorConfiguration = SurfaceColorConfiguration(VDSColor.elementsSecondaryOnlight, VDSColor.elementsSecondaryOndark)
open var title: Label = Label().with {
$0.translatesAutoresizingMaskIntoConstraints = false
$0.textAlignment = .left
$0.numberOfLines = 1
$0.textStyle = .bodySmall
$0.isAccessibilityElement = true
$0.backgroundColor = .clear
}
private var legendIndicatorWrapper: View = View().with {
$0.translatesAutoresizingMaskIntoConstraints = false
$0.backgroundColor = .clear
}
private var legendIndicator: View = View().with {
$0.translatesAutoresizingMaskIntoConstraints = false
$0.backgroundColor = .clear
$0.layer.borderWidth = 1.0
}
private lazy var stackView = UIStackView().with {
$0.translatesAutoresizingMaskIntoConstraints = false
$0.alignment = .fill
$0.distribution = .fill
$0.spacing = VDSLayout.space2X
$0.axis = .horizontal
$0.backgroundColor = .clear
}
private lazy var shapeLayer = CAShapeLayer()
internal var indicatorSize: CGSize { CGSize(width: 8, height: 8) }
//--------------------------------------------------
// MARK: - Initializers
//--------------------------------------------------
override init(frame: CGRect) {
super.init(frame: frame)
setupCell()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setupCell()
}
func setupCell() {
addSubview(stackView)
stackView.pinToSuperView()
}
func updateView() {
stackView.arrangedSubviews.forEach { $0.removeFromSuperview() }
legendIndicator.layer.sublayers?.forEach { $0.removeFromSuperlayer() }
legendIndicatorWrapper.addSubview(legendIndicator)
legendIndicator.pinLeading().pinTrailing().width(indicatorSize.width).height(indicatorSize.height).pinCenterY()
stackView.addArrangedSubview(legendIndicatorWrapper)
stackView.addArrangedSubview(title)
}
func updateTitle(text: String, color: UIColor, surface: Surface, clearFullcircle: Bool, drawSemiCircle: Bool) {
updateView()
title.surface = surface
title.text = text
title.textColor = textColorConfiguration.getColor(surface)
legendIndicator.backgroundColor = drawSemiCircle ? .clear : (clearFullcircle ? .clear : color)
legendIndicator.layer.borderColor = indicatorColorConfiguration.getColor(surface).cgColor
self.layoutIfNeeded()
legendIndicator.layer.cornerRadius = legendIndicator.frame.size.height / 2.0
guard drawSemiCircle else { return }
let center = CGPoint(x: legendIndicator.frame.size.width/2, y: legendIndicator.frame.size.height/2)
let path = UIBezierPath()
path.move(to: center)
path.addArc(withCenter: center, radius: center.x, startAngle: 2 * .pi, endAngle: .pi, clockwise: true)
path.close()
shapeLayer.path = path.cgPath
shapeLayer.fillColor = color.cgColor
guard legendIndicator.layer.sublayers?.contains(shapeLayer) ?? true else { return }
legendIndicator.layer.addSublayer(shapeLayer)
}
}
class LeftAlignedCollectionViewFlowLayout: UICollectionViewFlowLayout {
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let attributes = super.layoutAttributesForElements(in: rect)
var leftMargin = sectionInset.left
var maxY: CGFloat = -1.0
attributes?.forEach { layoutAttribute in
if layoutAttribute.frame.origin.y >= maxY {
leftMargin = sectionInset.left
}
layoutAttribute.frame.origin.x = leftMargin
leftMargin += layoutAttribute.frame.width + minimumInteritemSpacing
maxY = max(layoutAttribute.frame.maxY , maxY)
}
return attributes
}
}