Digital ACT-191 ONEAPP-6827 story: updating cell data

This commit is contained in:
vasavk 2024-03-14 12:15:36 +05:30
parent f25e9af766
commit 812fac5a59
3 changed files with 64 additions and 61 deletions

View File

@ -13,16 +13,28 @@ 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
}
}()
@ -41,6 +53,12 @@ final class BreadcrumbCellItem: UICollectionViewCell {
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
//--------------------------------------------------
@ -56,27 +74,22 @@ final class BreadcrumbCellItem: UICollectionViewCell {
///Configuring the cell with default setup
private func setUp() {
//add stackview
//this is the horizontal stack that contains breadcrumb and separator
//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
.pinTop()
.pinLeading()
.pinTrailing(0, .defaultHigh)
.pinBottom(0, .defaultHigh)
stackView.pinToSuperView()
contentView.addSubview(stackView)
separator.backgroundColor = .clear
// stackView.backgroundColor = .red
// breadcrumb.backgroundColor = .green
// separator.backgroundColor = .yellow
}
///Updating UI based on selected index, current index along with surface
func update(_ selectedIndex: Int, currentIndex: Int, surface: Surface, showSlash: Bool) {
func update(surface: Surface, hideSlash: Bool, breadCrumbItem: BreadcrumbItem) {
breadcrumb = breadCrumbItem
breadcrumb.text = breadCrumbItem.text
separator.textColor = textColorConfiguration.getColor(surface)
separator.isHidden = showSlash
separator.isHidden = hideSlash
print("selected: \(breadcrumb.isSelected), hideSlash: \(hideSlash), text: \(String(describing: breadCrumbItem.text)))")
}
}

View File

@ -88,24 +88,24 @@ open class BreadcrumbItem: ButtonBase {
/// Used to make changes to the View based off a change events or from local properties.
open override func updateView() {
//always call last so the label is rendered
if (text != nil) {
var newText: String = text ?? ""
if isSelected {
if newText.contains(separator) {
let result = newText.dropLast(2)
newText = String(result)
}
} else {
if !newText.contains(separator) {
newText = (text ?? "") + separator
}
}
print("newText:\(newText), isSelected: \(isSelected)")
text = newText
if let titleLabel {
titleLabel.text = text
}
}
// if (text != nil) {
// var newText: String = text ?? ""
// if isSelected {
// if newText.contains(separator) {
// let result = newText.dropLast(2)
// newText = String(result)
// }
// } else {
// if !newText.contains(separator) {
// newText = (text ?? "") + separator
// }
// }
// print("newText:\(newText), isSelected: \(isSelected)")
// text = newText
// if let titleLabel {
// titleLabel.text = text
// }
// }
super.updateView()
}

View File

@ -18,21 +18,11 @@ 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
@ -49,15 +39,15 @@ open class Breadcrumbs: View {
//--------------------------------------------------
// 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() } }
override open var isEnabled: Bool {
didSet {
breadcrumbItems.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 {
@ -76,13 +66,9 @@ open class Breadcrumbs: View {
/// 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)
addSubview(collectionView)
collectionView.pinToSuperView()
collectionView.heightAnchor.constraint(equalToConstant: 100).activate()
}
/// Resets to default settings.
@ -94,6 +80,7 @@ open class Breadcrumbs: View {
/// Used to make changes to the View based off a change events or from local properties.
open override func updateView() {
super.updateView()
collectionView.reloadData()
}
}
@ -101,19 +88,22 @@ extension Breadcrumbs: UICollectionViewDelegate, UICollectionViewDataSource, UIC
//--------------------------------------------------
// MARK: - UICollectionView Delegate & Datasource
//--------------------------------------------------
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { breadcrumbItems.count }
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)
let breadcrumb : BreadcrumbItem = breadcrumbItems[indexPath.row] as! BreadcrumbItem
let hideSlash = (indexPath.row == (breadcrumbItems.count - 1))
cell.crumb = breadcrumb
cell.update(surface: surface, hideSlash: hideSlash, breadCrumbItem: breadcrumb)
return cell
}
public func collectionView(_ collectionView: UICollectionView, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
breadcrumbItems[indexPath.row].intrinsicContentSize
}
public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// _selectedPageIndex = indexPath.row
// updateSelection()
// onPageDidSelect?(selectedPage)
}
}