vds_ios/VDS/Components/Calendar/CalendarHeaderView.swift

251 lines
9.0 KiB
Swift

//
// CalendarHeaderView.swift
// VDS
//
// Created by Kanamarlapudi, Vasavi on 30/04/24.
//
import Foundation
import UIKit
import VDSTokens
/// Header view to display month and year along with days of week
open class CalendarHeaderView: View {
//--------------------------------------------------
// MARK: - Initializers
//--------------------------------------------------
required public init() {
super.init(frame: .zero)
}
public override init(frame: CGRect) {
super.init(frame: .zero)
}
public required init?(coder: NSCoder) {
super.init(coder: coder)
}
//--------------------------------------------------
// MARK: - Public Properties
//--------------------------------------------------
//--------------------------------------------------
// MARK: - Private Properties
//--------------------------------------------------
internal var containerSize: CGSize { CGSize(width: 304, height: 88) }
private lazy var stackView = UIStackView().with {
$0.translatesAutoresizingMaskIntoConstraints = false
$0.distribution = .fill
$0.spacing = VDSLayout.space1X
$0.axis = .vertical
$0.backgroundColor = .clear
}
private lazy var monthYearHeaderStackView = UIStackView().with {
$0.translatesAutoresizingMaskIntoConstraints = false
$0.distribution = .fill
$0.spacing = VDSLayout.space1X
$0.axis = .horizontal
$0.backgroundColor = .clear
}
internal var containerView = View().with {
$0.clipsToBounds = true
$0.backgroundColor = .clear
}
internal var monthView = View()
internal var previousMonthView = View()
internal var nextMonthView = View()
internal var daysView = View()
internal let daysOfWeek = Date.capitalizedFirstLettersOfWeekdays
internal var previousButton = ButtonIcon().with {
$0.kind = .ghost
$0.iconName = .leftCaret
$0.iconOffset = .init(x: -2, y: 0)
$0.icon.size = .small
$0.size = .small
}
internal var nextButton = ButtonIcon().with {
$0.kind = .ghost
$0.iconName = .rightCaret
$0.iconOffset = .init(x: 2, y: 0)
$0.icon.size = .small
$0.size = .small
}
internal var monthYearLabel = Label().with {
$0.translatesAutoresizingMaskIntoConstraints = false
$0.textAlignment = .center
$0.numberOfLines = 1
$0.textStyle = .boldBodySmall
$0.backgroundColor = .clear
$0.isAccessibilityElement = false
}
private lazy var daysCollectionView: UICollectionView = {
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
collectionView.isScrollEnabled = false
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.delegate = self
collectionView.dataSource = self
collectionView.showsHorizontalScrollIndicator = false
collectionView.showsVerticalScrollIndicator = false
collectionView.backgroundColor = .clear
collectionView.register(collectionViewCell.self, forCellWithReuseIdentifier: collectionViewCell.identifier)
return collectionView
}()
internal var monthYearLabelTextColorConfiguration = SurfaceColorConfiguration(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark)
//--------------------------------------------------
// MARK: - Lifecycle
//--------------------------------------------------
open override func initialSetup() {
super.initialSetup()
}
open override func setup() {
super.setup()
isAccessibilityElement = false
// containerView
addSubview(containerView)
containerView
.pinTop()
.pinBottom()
.pinLeadingGreaterThanOrEqualTo()
.pinTrailingLessThanOrEqualTo()
.height(containerSize.height)
.width(containerSize.width)
containerView.centerXAnchor.constraint(equalTo: centerXAnchor).activate()
// stackview
containerView.addSubview(stackView)
stackView
.pinTop()
.pinBottom(VDSLayout.space1X)
.pinLeading()
.pinTrailing()
stackView.centerXAnchor.constraint(equalTo: containerView.centerXAnchor).activate()
// month label view, previous and next buttons
stackView.addArrangedSubview(monthView)
monthView.backgroundColor = .clear
monthView.heightAnchor.constraint(equalToConstant: 40).isActive = true
// month Header stack view
monthView.addSubview(monthYearHeaderStackView)
monthYearHeaderStackView.pinToSuperView()
// previous button
monthYearHeaderStackView.addArrangedSubview(previousMonthView)
previousMonthView.widthAnchor.constraint(equalToConstant: 40).activate()
previousMonthView.addSubview(previousButton)
let spacing = VDSLayout.space1X
previousButton.pinTop(spacing).pinBottom(spacing).pinLeading(spacing).pinTrailing(spacing)
// month year label
monthYearHeaderStackView.addArrangedSubview(monthYearLabel)
// next button
monthYearHeaderStackView.addArrangedSubview(nextMonthView)
nextMonthView.widthAnchor.constraint(equalToConstant: 40).activate()
nextMonthView.addSubview(nextButton)
nextButton.pinTop(spacing).pinBottom(spacing).pinLeading(spacing).pinTrailing(spacing)
// days Collection View
stackView.addArrangedSubview(daysCollectionView)
daysCollectionView.widthAnchor.constraint(equalTo: widthAnchor).activate()
daysCollectionView.heightAnchor.constraint(equalTo: monthView.heightAnchor).activate()
daysCollectionView.centerXAnchor.constraint(equalTo: containerView.centerXAnchor).activate()
}
open override func updateView() {
super.updateView()
monthYearLabel.surface = surface
previousButton.surface = surface
nextButton.surface = surface
daysCollectionView.reloadData()
monthYearLabel.text = "May 2024"
monthYearLabel.textColor = monthYearLabelTextColorConfiguration.getColor(surface)
}
override open func layoutSubviews() {
super.layoutSubviews()
}
open override func reset() {
super.reset()
monthYearLabel.textStyle = .boldBodySmall
}
}
extension CalendarHeaderView: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return daysOfWeek.count
}
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: collectionViewCell.identifier, for: indexPath) as? collectionViewCell else { return UICollectionViewCell() }
cell.updateTitle(text: daysOfWeek[indexPath.row], surface: surface)
return cell
}
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 40, height: 40)
}
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return VDSLayout.space1X
}
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return VDSLayout.space1X
}
}
private class collectionViewCell: UICollectionViewCell {
static let identifier: String = String(describing: collectionViewCell.self)
private let textColorConfiguration = SurfaceColorConfiguration(VDSColor.elementsSecondaryOnlight, VDSColor.elementsSecondaryOndark)
private var title = Label().with {
$0.translatesAutoresizingMaskIntoConstraints = false
$0.textAlignment = .center
$0.numberOfLines = 1
$0.textStyle = .bodySmall
$0.backgroundColor = .clear
$0.isAccessibilityElement = false
}
//--------------------------------------------------
// MARK: - Initializers
//--------------------------------------------------
override init(frame: CGRect) {
super.init(frame: frame)
setupCell()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setupCell()
}
func setupCell() {
addSubview(title)
title.pinToSuperView()
}
func updateTitle(text: String, surface: Surface) {
title.surface = surface
title.text = text
title.textColor = textColorConfiguration.getColor(surface)
}
}