Merge branch 'vasavk/calendar' into 'develop'

Calendar - Minor issues fixes

See merge request BPHV_MIPS/vds_ios!228
This commit is contained in:
Bruce, Matt R 2024-05-20 16:08:52 +00:00
commit 3747e18aca
2 changed files with 77 additions and 16 deletions

View File

@ -67,7 +67,7 @@ open class CalendarBase: Control, Changeable {
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Private Properties // MARK: - Private Properties
//-------------------------------------------------- //--------------------------------------------------
internal var containerSize: CGSize { CGSize(width: 328, height: 376) } internal var containerSize: CGSize { CGSize(width: 328, height: 336) }
internal var calendar = Calendar.current internal var calendar = Calendar.current
private let cellItemSize = CGSize(width: 40, height: 40) private let cellItemSize = CGSize(width: 40, height: 40)
@ -358,7 +358,7 @@ extension CalendarBase: UICollectionViewDelegate, UICollectionViewDataSource, UI
} }
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize { public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
return CGSize(width: collectionView.frame.size.width, height: footerHeight) return CGSize(width: collectionView.frame.size.width, height: indicators.count > 0 ? footerHeight : 0)
} }
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {

View File

@ -20,17 +20,26 @@ class CalendarFooterReusableView: UICollectionReusableView {
private var surface: Surface = .light private var surface: Surface = .light
private var items: [CalendarBase.CalendarIndicatorModel] = [] private var items: [CalendarBase.CalendarIndicatorModel] = []
internal var containerSize: CGSize { CGSize(width: 304, height: 40) } internal var containerSize: CGSize { CGSize(width: 304, height: 40) }
internal var indicatorWidth = 8.0
var textLabel: Label = Label().with {
$0.translatesAutoresizingMaskIntoConstraints = false
$0.textAlignment = .left
$0.textStyle = .bodySmall
$0.numberOfLines = 1
}
internal var containerView = View().with { internal var containerView = View().with {
$0.clipsToBounds = true $0.clipsToBounds = true
} }
private let flowLayout = UICollectionViewFlowLayout().with { private let flowLayout = LeftAlignedCollectionViewFlowLayout().with {
$0.estimatedItemSize = UICollectionViewFlowLayout.automaticSize $0.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
$0.minimumLineSpacing = VDSLayout.space1X $0.minimumLineSpacing = VDSLayout.space1X
$0.minimumInteritemSpacing = VDSLayout.space4X $0.minimumInteritemSpacing = VDSLayout.space4X
$0.scrollDirection = .vertical $0.scrollDirection = .vertical
} }
open lazy var legendCollectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout).with { open lazy var legendCollectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout).with {
$0.isScrollEnabled = false $0.isScrollEnabled = false
$0.translatesAutoresizingMaskIntoConstraints = false $0.translatesAutoresizingMaskIntoConstraints = false
@ -40,9 +49,13 @@ class CalendarFooterReusableView: UICollectionReusableView {
$0.backgroundColor = .clear $0.backgroundColor = .clear
$0.delegate = self $0.delegate = self
$0.dataSource = self $0.dataSource = self
$0.register(LegendCollectionViewCell.self, forCellWithReuseIdentifier: LegendCollectionViewCell.identifier)
$0.register(LegendCollectionViewCell.self,
forCellWithReuseIdentifier: LegendCollectionViewCell.identifier)
} }
private var topConstraint: NSLayoutConstraint?
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Initializers // MARK: - Initializers
//-------------------------------------------------- //--------------------------------------------------
@ -64,18 +77,17 @@ class CalendarFooterReusableView: UICollectionReusableView {
isAccessibilityElement = false isAccessibilityElement = false
addSubview(containerView) addSubview(containerView)
containerView containerView.pinToSuperView()
.pinTopLessThanOrEqualTo(topAnchor, VDSLayout.space6X, .defaultLow)
.pinBottom()
.pinLeadingGreaterThanOrEqualTo(leadingAnchor, VDSLayout.space3X, .defaultHigh)
.pinTrailingLessThanOrEqualTo(trailingAnchor, VDSLayout.space3X, .defaultHigh)
.width(containerSize.width - (2*VDSLayout.space3X))
.heightLessThanEqualTo(containerSize.height)
// legend Collection View // legend Collection View
containerView.addSubview(legendCollectionView) containerView.addSubview(legendCollectionView)
legendCollectionView.pinTop().pinBottom().pinLeading().pinTrailing().pinCenterY().pinCenterX() legendCollectionView
.pinTopLessThanOrEqualTo(topAnchor, VDSLayout.space6X, .defaultLow)
.pinBottom()
.pinLeading(VDSLayout.space3X)
.pinTrailing(VDSLayout.space3X)
.width(containerSize.width - (2 * VDSLayout.space3X))
.heightGreaterThanEqualTo(16)
} }
/// Updating UI to show legend with titles. /// Updating UI to show legend with titles.
@ -83,6 +95,15 @@ class CalendarFooterReusableView: UICollectionReusableView {
self.items = indicators self.items = indicators
self.surface = surface self.surface = surface
legendCollectionView.reloadData() legendCollectionView.reloadData()
var height = legendCollectionView.collectionViewLayout.collectionViewContentSize.height
if height > 0 {
topConstraint?.isActive = false
height = height > containerSize.height ? containerSize.height : height
let top = containerSize.height - height
topConstraint = legendCollectionView.topAnchor.constraint(equalTo: topAnchor, constant: top)
topConstraint?.isActive = true
}
} }
} }
@ -105,6 +126,14 @@ extension CalendarFooterReusableView: UICollectionViewDelegate, UICollectionView
drawSemiCircle: indexPath.row == 2) drawSemiCircle: indexPath.row == 2)
return cell return cell
} }
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
textLabel.text = items[indexPath.row].label
let intrinsicSize = textLabel.intrinsicContentSize
let cellwidth = intrinsicSize.width + indicatorWidth + VDSLayout.space2X
return .init(width: min(cellwidth, collectionView.frame.width), height: intrinsicSize.height)
}
} }
private class LegendCollectionViewCell: UICollectionViewCell { private class LegendCollectionViewCell: UICollectionViewCell {
@ -128,6 +157,7 @@ private class LegendCollectionViewCell: UICollectionViewCell {
$0.translatesAutoresizingMaskIntoConstraints = false $0.translatesAutoresizingMaskIntoConstraints = false
$0.backgroundColor = .clear $0.backgroundColor = .clear
} }
private var legendIndicator: View = View().with { private var legendIndicator: View = View().with {
$0.translatesAutoresizingMaskIntoConstraints = false $0.translatesAutoresizingMaskIntoConstraints = false
$0.backgroundColor = .clear $0.backgroundColor = .clear
@ -136,7 +166,8 @@ private class LegendCollectionViewCell: UICollectionViewCell {
private lazy var stackView = UIStackView().with { private lazy var stackView = UIStackView().with {
$0.translatesAutoresizingMaskIntoConstraints = false $0.translatesAutoresizingMaskIntoConstraints = false
$0.distribution = .equalSpacing $0.alignment = .fill
$0.distribution = .fill
$0.spacing = VDSLayout.space2X $0.spacing = VDSLayout.space2X
$0.axis = .horizontal $0.axis = .horizontal
$0.backgroundColor = .clear $0.backgroundColor = .clear
@ -144,6 +175,8 @@ private class LegendCollectionViewCell: UICollectionViewCell {
private lazy var shapeLayer = CAShapeLayer() private lazy var shapeLayer = CAShapeLayer()
internal var indicatorSize: CGSize { CGSize(width: 8, height: 8) }
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Initializers // MARK: - Initializers
//-------------------------------------------------- //--------------------------------------------------
@ -160,15 +193,21 @@ private class LegendCollectionViewCell: UICollectionViewCell {
func setupCell() { func setupCell() {
addSubview(stackView) addSubview(stackView)
stackView.pinToSuperView() stackView.pinToSuperView()
}
func updateView() {
stackView.arrangedSubviews.forEach { $0.removeFromSuperview() }
legendIndicator.layer.sublayers?.forEach { $0.removeFromSuperlayer() }
legendIndicatorWrapper.addSubview(legendIndicator) legendIndicatorWrapper.addSubview(legendIndicator)
legendIndicator.pinLeading().pinTrailing().width(8).height(8).pinCenterY() legendIndicator.pinLeading().pinTrailing().width(indicatorSize.width).height(indicatorSize.height).pinCenterY()
stackView.addArrangedSubview(legendIndicatorWrapper) stackView.addArrangedSubview(legendIndicatorWrapper)
stackView.addArrangedSubview(title) stackView.addArrangedSubview(title)
} }
func updateTitle(text: String, color: UIColor, surface: Surface, clearFullcircle: Bool, drawSemiCircle: Bool) { func updateTitle(text: String, color: UIColor, surface: Surface, clearFullcircle: Bool, drawSemiCircle: Bool) {
updateView()
title.surface = surface title.surface = surface
title.text = text title.text = text
title.textColor = textColorConfiguration.getColor(surface) title.textColor = textColorConfiguration.getColor(surface)
@ -194,3 +233,25 @@ private class LegendCollectionViewCell: UICollectionViewCell {
legendIndicator.layer.addSublayer(shapeLayer) legendIndicator.layer.addSublayer(shapeLayer)
} }
} }
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
}
}