refactored breadcrumbs to use selfsizing cv
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
e9bd1614ec
commit
7a533944b8
@ -19,7 +19,7 @@ open class Breadcrumbs: View {
|
|||||||
// MARK: - Public Properties
|
// MARK: - Public Properties
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
/// Array of ``BreadcrumbItem`` views for the Breadcrumbs.
|
/// Array of ``BreadcrumbItem`` views for the Breadcrumbs.
|
||||||
open var breadcrumbs: [BreadcrumbItem] = []
|
open var breadcrumbs: [BreadcrumbItem] = [] { didSet { setNeedsUpdate() } }
|
||||||
|
|
||||||
/// Array of ``BreadcurmbItemModel`` you are wanting to show.
|
/// Array of ``BreadcurmbItemModel`` you are wanting to show.
|
||||||
open var breadcrumbModels: [BreadcrumbItemModel] = [] { didSet { updateBreadcrumbItems() } }
|
open var breadcrumbModels: [BreadcrumbItemModel] = [] { didSet { updateBreadcrumbItems() } }
|
||||||
@ -47,12 +47,7 @@ open class Breadcrumbs: View {
|
|||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Private Properties
|
// MARK: - Private Properties
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// Sizes are from InVision design specs.
|
let layout: UICollectionViewFlowLayout = BreadcrumbsFlowLayout().with {
|
||||||
internal var containerSize: CGSize { CGSize(width: 45, height: 44) }
|
|
||||||
|
|
||||||
internal var heightConstraint: NSLayoutConstraint?
|
|
||||||
|
|
||||||
let layout: UICollectionViewFlowLayout = LeftAlignedCollectionViewFlowLayout().with {
|
|
||||||
$0.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
|
$0.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
|
||||||
$0.minimumInteritemSpacing = VDSLayout.Spacing.space1X.value
|
$0.minimumInteritemSpacing = VDSLayout.Spacing.space1X.value
|
||||||
$0.minimumLineSpacing = VDSLayout.Spacing.space1X.value
|
$0.minimumLineSpacing = VDSLayout.Spacing.space1X.value
|
||||||
@ -61,8 +56,8 @@ open class Breadcrumbs: View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
///Collectionview to render Breadcrumb Items
|
///Collectionview to render Breadcrumb Items
|
||||||
private lazy var collectionView: UICollectionView = {
|
private lazy var collectionView: SelfSizingCollectionView = {
|
||||||
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
|
let collectionView = SelfSizingCollectionView(frame: .zero, collectionViewLayout: layout)
|
||||||
collectionView.isScrollEnabled = false
|
collectionView.isScrollEnabled = false
|
||||||
collectionView.translatesAutoresizingMaskIntoConstraints = false
|
collectionView.translatesAutoresizingMaskIntoConstraints = false
|
||||||
collectionView.delegate = self
|
collectionView.delegate = self
|
||||||
@ -84,37 +79,26 @@ open class Breadcrumbs: View {
|
|||||||
breadcrumbItem.removeFromSuperview()
|
breadcrumbItem.removeFromSuperview()
|
||||||
}
|
}
|
||||||
|
|
||||||
breadcrumbs.removeAll()
|
|
||||||
// Create new breadcrumb items from the models
|
// Create new breadcrumb items from the models
|
||||||
for model in breadcrumbModels {
|
breadcrumbs = breadcrumbModels.compactMap({ model in
|
||||||
let breadcrumbItem = BreadcrumbItem()
|
let breadcrumbItem = BreadcrumbItem()
|
||||||
breadcrumbItem.text = model.text
|
breadcrumbItem.text = model.text
|
||||||
breadcrumbItem.link = model.link
|
breadcrumbItem.isEnabled = model.enabled
|
||||||
breadcrumbItem.isSelected = model.isSelected ?? false
|
breadcrumbItem.isSelected = model.selected
|
||||||
breadcrumbs.append(breadcrumbItem)
|
|
||||||
breadcrumbItem.onClick = { [weak self] breadcrumb in
|
breadcrumbItem.onClick = { [weak self] breadcrumb in
|
||||||
guard let self else { return }
|
guard let self, breadcrumb.isEnabled else { return }
|
||||||
if self.onBreadcrumbShouldSelect?(breadcrumb) ?? true {
|
if self.onBreadcrumbShouldSelect?(breadcrumb) ?? true {
|
||||||
model.onClick?(breadcrumb)
|
model.onClick?(breadcrumb)
|
||||||
self.onBreadcrumbDidSelect?(breadcrumb)
|
self.onBreadcrumbDidSelect?(breadcrumb)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
return breadcrumbItem
|
||||||
setNeedsUpdate()
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//------------------------------------------s--------
|
//------------------------------------------s--------
|
||||||
// MARK: - Overrides
|
// 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()
|
|
||||||
//create the wrapping view
|
|
||||||
heightConstraint = self.heightAnchor.constraint(equalToConstant: containerSize.height)
|
|
||||||
heightConstraint?.priority = .defaultHigh
|
|
||||||
heightConstraint?.isActive = true
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Executed on initialization for this View.
|
/// Executed on initialization for this View.
|
||||||
open override func initialSetup() {
|
open override func initialSetup() {
|
||||||
super.initialSetup()
|
super.initialSetup()
|
||||||
@ -135,17 +119,17 @@ open class Breadcrumbs: View {
|
|||||||
open override func updateView() {
|
open override func updateView() {
|
||||||
super.updateView()
|
super.updateView()
|
||||||
collectionView.reloadData()
|
collectionView.reloadData()
|
||||||
heightConstraint?.constant = collectionView.collectionViewLayout.collectionViewContentSize.height
|
|
||||||
heightConstraint?.isActive = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
open override func layoutSubviews() {
|
open override func layoutSubviews() {
|
||||||
super.layoutSubviews()
|
//Don't call super since we don't want an infinite loop
|
||||||
|
//super.layoutSubviews()
|
||||||
|
|
||||||
// Accounts for any collection size changes
|
// Accounts for any collection size changes
|
||||||
DispatchQueue.main.async { [weak self] in
|
DispatchQueue.main.async { [weak self] in
|
||||||
guard let self else { return }
|
guard let self else { return }
|
||||||
self.collectionView.collectionViewLayout.invalidateLayout()
|
self.collectionView.collectionViewLayout.invalidateLayout()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -153,17 +137,15 @@ extension Breadcrumbs: UICollectionViewDelegate, UICollectionViewDataSource {
|
|||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - UICollectionView Delegate & Datasource
|
// MARK: - UICollectionView Delegate & Datasource
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { breadcrumbs.count
|
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
||||||
|
breadcrumbs.count
|
||||||
}
|
}
|
||||||
|
|
||||||
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
||||||
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: BreadcrumbCellItem.identifier, for: indexPath) as? BreadcrumbCellItem else { return UICollectionViewCell() }
|
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: BreadcrumbCellItem.identifier, for: indexPath) as? BreadcrumbCellItem else { return UICollectionViewCell() }
|
||||||
let hideSlash = (indexPath.row == breadcrumbs.count - 1 || breadcrumbs.count == 1)
|
let hideSlash = indexPath.row == 0
|
||||||
cell.update(surface: surface, hideSlash: hideSlash, breadCrumbItem: breadcrumbs[indexPath.row])
|
cell.update(surface: surface, hideSlash: hideSlash, breadCrumbItem: breadcrumbs[indexPath.row])
|
||||||
return cell
|
return cell
|
||||||
}
|
}
|
||||||
|
|
||||||
public func collectionView(_ collectionView: UICollectionView, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
|
|
||||||
breadcrumbs[indexPath.row].intrinsicContentSize
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user