vds_ios/VDS/Components/Breadcrumbs/BreadcrumbCellItem.swift

96 lines
3.5 KiB
Swift

//
// BreadcrumbCellItem.swift
// VDS
//
// Created by Kanamarlapudi, Vasavi on 11/03/24.
//
import UIKit
import VDSColorTokens
///This is customised view for Breadcrumb cell item
final class BreadcrumbCellItem: UICollectionViewCell {
///Identifier for the BreadcrumbCellItem
static let identifier: String = String(describing: BreadcrumbCellItem.self)
//--------------------------------------------------
// MARK: - Private Properties
//--------------------------------------------------
internal var crumbWidthConstraint: NSLayoutConstraint?
var crumb : BreadcrumbItem? {
didSet {
guard let crumb = crumb else { return }
breadcrumb = crumb
crumbWidthConstraint?.constant = crumb.intrinsicContentSize.width
crumbWidthConstraint?.isActive = true
}
}
internal var stackView: UIStackView = {
return UIStackView().with {
$0.translatesAutoresizingMaskIntoConstraints = false
$0.axis = .horizontal
$0.distribution = .fill
$0.alignment = .top
$0.spacing = VDSLayout.Spacing.space1X.value
}
}()
internal var breadcrumb = BreadcrumbItem().with {
$0.translatesAutoresizingMaskIntoConstraints = false
$0.titleLabel?.numberOfLines = 0
}
///separator label
private var separator: Label = Label().with {
$0.translatesAutoresizingMaskIntoConstraints = false
$0.textAlignment = .left
$0.numberOfLines = 0
$0.text = "/"
}
private let textColorConfiguration = SurfaceColorConfiguration(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark)
private var crumbTextColorConfiguration = ControlColorConfiguration().with {
$0.setSurfaceColors(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark, forState: .normal)
$0.setSurfaceColors(VDSColor.interactiveActiveOnlight, VDSColor.interactiveActiveOndark, forState: .highlighted)
$0.setSurfaceColors(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark, forState: .selected)
}
//--------------------------------------------------
// MARK: - Initializers
//--------------------------------------------------
override init(frame: CGRect) {
super.init(frame: frame)
setUp()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setUp()
}
///Configuring the cell with default setup
private func setUp() {
//add stackview - this is the horizontal stack that contains breadcrumb and separator
stackView.addArrangedSubview(breadcrumb)
crumbWidthConstraint = breadcrumb.widthAnchor.constraint(greaterThanOrEqualToConstant: 0).activate()
stackView.addArrangedSubview(separator)
stackView.setCustomSpacing(VDSLayout.Spacing.space1X.value, after: breadcrumb)
stackView.pinToSuperView()
contentView.addSubview(stackView)
separator.backgroundColor = .clear
}
///Updating UI based on selected index, current index along with surface
func update(surface: Surface, hideSlash: Bool, breadCrumbItem: BreadcrumbItem) {
breadcrumb = breadCrumbItem
breadcrumb.text = breadCrumbItem.text
separator.textColor = textColorConfiguration.getColor(surface)
separator.isHidden = hideSlash
print("selected: \(breadcrumb.isSelected), hideSlash: \(hideSlash), text: \(String(describing: breadCrumbItem.text)))")
}
}