vds_ios/VDS/Components/Calendar/CalendarHeaderView.swift

196 lines
6.7 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: 320, height: 104) } //width:320/328
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 monthHeaderStackView = 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 daysView = View()
internal let daysOfWeek = Date.capitalizedFirstLettersOfWeekdays
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
}()
//--------------------------------------------------
// MARK: - Lifecycle
//--------------------------------------------------
open override func initialSetup() {
super.initialSetup()
}
open override func setup() {
super.setup()
isAccessibilityElement = false
// stackView
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(VDSLayout.space4X)
.pinLeading()
.pinTrailing()
.pinBottom(VDSLayout.space1X)
// month label view, previous and next buttons
stackView.addArrangedSubview(monthView)
monthView.backgroundColor = .orange
monthView.heightAnchor.constraint(equalToConstant: 40).isActive = true
// days Collection View
stackView.addArrangedSubview(daysCollectionView)
daysCollectionView.widthAnchor.constraint(equalTo: widthAnchor).activate()
daysCollectionView.heightAnchor.constraint(equalTo: monthView.heightAnchor).activate()
print("daysOfWeek: \(daysOfWeek)")
}
open override func updateView() {
super.updateView()
daysCollectionView.reloadData()
}
override open func layoutSubviews() {
super.layoutSubviews()
}
open override func reset() {
super.reset()
}
}
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)
title.backgroundColor = .clear
}
}