vds_ios/VDS/Components/Breadcrumbs/Breadcrumbs.swift

120 lines
4.9 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: - Private Properties
//--------------------------------------------------
///Collectionview width anchor
private var collectionViewWidthAnchor: NSLayoutConstraint?
///Selected page index
private var _selectedPageIndex: Int = 0
///A root view for the Breadcrumbs
private let containerView: View = View().with {
$0.translatesAutoresizingMaskIntoConstraints = false
}
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
///Collectionview to render Breadcrumbs indexes
private lazy var collectionView: UICollectionView = {
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
layout.itemSize = CGSize(width: 50, height: 25)
layout.minimumInteritemSpacing = VDSLayout.Spacing.space1X.value
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: - Public Properties
//--------------------------------------------------
// /// Array of Breadcrumb Items Titles that are shown as Breadcrumbs.
// open var breadcrumbs: [String?] = [] { didSet { setNeedsUpdate() } }
/// Array of Breadcrumb Items that are shown in the group.
open var breadcrumbItems: [ButtonBase] = [] { didSet { setNeedsUpdate() } }
/// Whether this object is enabled or not
open var selected: Bool = false { didSet { setNeedsUpdate() } }
/// Current Surface and this is used to pass down to child objects that implement Surfacable
override open var surface: Surface {
didSet {
breadcrumbItems.forEach { $0.surface = surface }
}
}
//--------------------------------------------------
// MARK: - Overrides
//--------------------------------------------------
/// Called once when a view is initialized and is used to Setup additional UI or other constants and configurations.
open override func setup() {
super.setup()
}
/// Executed on initialization for this View.
open override func initialSetup() {
super.initialSetup()
addSubview(containerView)
containerView.pinToSuperView()
//for TEST
containerView.widthAnchor.constraint(greaterThanOrEqualToConstant: 288).activate()
containerView.heightAnchor.constraint(equalToConstant: 150).activate()
containerView.addSubview(collectionView)
}
/// Resets to default settings.
open override func reset() {
super.reset()
setNeedsUpdate()
}
/// Used to make changes to the View based off a change events or from local properties.
open override func updateView() {
super.updateView()
}
}
extension Breadcrumbs: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
//--------------------------------------------------
// MARK: - UICollectionView Delegate & Datasource
//--------------------------------------------------
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { breadcrumbItems.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 showSlash = (indexPath.row == (breadcrumbItems.count - 1))
cell.update(_selectedPageIndex, currentIndex: indexPath.row, surface: surface, showSlash: showSlash)
return cell
}
public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// _selectedPageIndex = indexPath.row
// updateSelection()
// onPageDidSelect?(selectedPage)
}
}