vds_ios/VDS/Components/Breadcrumbs/BreadcrumbCellItem.swift

100 lines
3.3 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 stackView: UIStackView = {
return UIStackView().with {
$0.translatesAutoresizingMaskIntoConstraints = false
$0.axis = .horizontal
$0.distribution = .fill
$0.alignment = .fill
$0.spacing = VDSLayout.Spacing.space1X.value
}
}()
internal var breadCrumbItem: BreadcrumbItem?
///separator label
private var separator: Label = Label().with {
$0.translatesAutoresizingMaskIntoConstraints = false
$0.textAlignment = .left
$0.numberOfLines = 1
$0.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
$0.setContentHuggingPriority(.defaultHigh, for: .horizontal)
$0.text = "/"
}
private let textColorConfiguration = SurfaceColorConfiguration(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark)
//--------------------------------------------------
// 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() {
separator.textColorConfiguration = textColorConfiguration.eraseToAnyColorable()
contentView.addSubview(stackView)
stackView.pinToSuperView()
separator.backgroundColor = .clear
}
///Updating the breadCrumbItem and UI based on the selected flag along with the surface
func update(surface: Surface, hideSlash: Bool, breadCrumbItem: BreadcrumbItem) {
separator.surface = surface
breadCrumbItem.surface = surface
stackView.addArrangedSubview(separator)
stackView.addArrangedSubview(breadCrumbItem)
stackView.setCustomSpacing(VDSLayout.Spacing.space1X.value, after: separator)
separator.textColor = textColorConfiguration.getColor(surface)
separator.isHidden = hideSlash
self.breadCrumbItem = breadCrumbItem
layoutIfNeeded()
}
}
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
}
}