vds_ios/VDS/Components/Breadcrumbs/Breadcrumbs.swift
Matt Bruce f862c8bd1c fix bug, must find other solution later
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2024-03-21 11:20:11 -05:00

155 lines
6.1 KiB
Swift

//
// Breadcrumbs.swift
// VDS
//
// Created by Kanamarlapudi, Vasavi on 11/03/24.
//
import Foundation
import VDSColorTokens
import Combine
/// A Breadcrumbs contains BreadcrumbItems.
/// It contains Breadcrumb Item Default, Breadcrumb Item Selected, Separator.
/// Breadcrumbs are secondary navigation that use a hierarchy of internal links to tell customers where they are in an experience. Each breadcrumb links to its respective page, except for that of current page.
@objc(VDSBreadcrumbs)
open class Breadcrumbs: View {
//--------------------------------------------------
// MARK: - Public Properties
//--------------------------------------------------
/// Array of ``BreadcrumbItem`` views for the Breadcrumbs.
open var breadcrumbs: [BreadcrumbItem] = [] { didSet { setNeedsUpdate() } }
/// Array of ``BreadcurmbItemModel`` you are wanting to show.
open var breadcrumbModels: [BreadcrumbItemModel] = [] { didSet { updateBreadcrumbItems() } }
/// Whether this object is enabled or not
override open var isEnabled: Bool {
didSet {
breadcrumbs.forEach { $0.isEnabled = isEnabled }
}
}
/// Current Surface and this is used to pass down to child objects that implement Surfacable
override open var surface: Surface {
didSet {
breadcrumbs.forEach { $0.surface = surface }
}
}
/// A callback when the selected item changes. Passes parameters (crumb).
open var onBreadcrumbDidSelect: ((BreadcrumbItem) -> Void)?
/// A callback when the Tab determine if a item should be selected.
open var onBreadcrumbShouldSelect:((BreadcrumbItem) -> Bool)?
//--------------------------------------------------
// MARK: - Private Properties
//--------------------------------------------------
let layout: UICollectionViewFlowLayout = BreadcrumbsFlowLayout().with {
$0.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
$0.minimumInteritemSpacing = VDSLayout.Spacing.space1X.value
$0.minimumLineSpacing = VDSLayout.Spacing.space1X.value
$0.sectionInset = .zero
$0.scrollDirection = .vertical
}
///Collectionview to render Breadcrumb Items
private lazy var collectionView: SelfSizingCollectionView = {
let collectionView = SelfSizingCollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.isScrollEnabled = false
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.delegate = self
collectionView.dataSource = self
collectionView.showsHorizontalScrollIndicator = false
collectionView.showsVerticalScrollIndicator = false
collectionView.register(BreadcrumbCellItem.self, forCellWithReuseIdentifier: BreadcrumbCellItem.identifier)
collectionView.backgroundColor = .clear
return collectionView
}()
//--------------------------------------------------
// MARK: - Private Methods
//--------------------------------------------------
/// Removes all of the Breadcrumbs and creates new ones from the Breadcrumb Models property.
private func updateBreadcrumbItems() {
// Clear existing breadcrumbs
for breadcrumbItem in breadcrumbs {
breadcrumbItem.removeFromSuperview()
}
// Create new breadcrumb items from the models
breadcrumbs = breadcrumbModels.compactMap({ model in
let breadcrumbItem = BreadcrumbItem()
breadcrumbItem.text = model.text
breadcrumbItem.isEnabled = model.enabled
breadcrumbItem.isSelected = model.selected
breadcrumbItem.onClick = { [weak self] breadcrumb in
guard let self, breadcrumb.isEnabled else { return }
if self.onBreadcrumbShouldSelect?(breadcrumb) ?? true {
model.onClick?(breadcrumb)
self.onBreadcrumbDidSelect?(breadcrumb)
}
}
return breadcrumbItem
})
}
//------------------------------------------s--------
// MARK: - Overrides
//--------------------------------------------------
/// Executed on initialization for this View.
open override func initialSetup() {
super.initialSetup()
addSubview(collectionView)
collectionView.pinToSuperView()
}
/// Resets to default settings.
open override func reset() {
super.reset()
shouldUpdateView = false
breadcrumbs.forEach { $0.reset() }
shouldUpdateView = true
setNeedsUpdate()
}
/// Used to make changes to the View based off a change events or from local properties.
open override func updateView() {
super.updateView()
collectionView.reloadData()
}
open override func layoutSubviews() {
//Turn off the ability to execute updateView() in the super
//since we don't want an infinite loop
shouldUpdateView = false
super.layoutSubviews()
shouldUpdateView = true
// Accounts for any collection size changes
DispatchQueue.main.async { [weak self] in
guard let self else { return }
self.collectionView.collectionViewLayout.invalidateLayout()
}
}
}
extension Breadcrumbs: UICollectionViewDelegate, UICollectionViewDataSource {
//--------------------------------------------------
// MARK: - UICollectionView Delegate & Datasource
//--------------------------------------------------
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
breadcrumbs.count
}
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: BreadcrumbCellItem.identifier, for: indexPath) as? BreadcrumbCellItem else { return UICollectionViewCell() }
let hideSlash = indexPath.row == 0
cell.update(surface: surface, hideSlash: hideSlash, breadCrumbItem: breadcrumbs[indexPath.row])
return cell
}
}