Digital ACT-191 ONEAPP-7016 story: handling active dates, inactive dates, min date, max date with updated values

This commit is contained in:
vasavk 2024-05-07 11:13:56 +05:30
parent 7fe5123a69
commit 8560d7a6d4
3 changed files with 69 additions and 46 deletions

View File

@ -40,44 +40,34 @@ open class CalendarBase: View {
/// Enable specific days. Pass an array of string value in date format e.g. ['07/21/2024', '07/24/2024', 07/28/2024']. /// Enable specific days. Pass an array of string value in date format e.g. ['07/21/2024', '07/24/2024', 07/28/2024'].
/// All other dates will be inactive /// All other dates will be inactive
open var activeDates: [Date] = [] open var activeDates: [Date] = [] { didSet { setNeedsUpdate() } }
/// Disable specific days. Pass an array of string value in date format e.g. ['07/21/2024', '07/24/2024', 07/28/2024']. /// Disable specific days. Pass an array of string value in date format e.g. ['07/21/2024', '07/24/2024', 07/28/2024'].
/// All other dates will be active. /// All other dates will be active.
open var inactiveDates: [Date] = [] open var inactiveDates: [Date] = [] { didSet { setNeedsUpdate() } }
/// If provided, the calendar will allow a selection to be made from this date forward. Defaults to today. /// If provided, the calendar will allow a selection to be made from this date forward. Defaults to today.
open var minDate: Date? open var minDate: Date = Date() { didSet { setNeedsUpdate() } }
/// If provided, the calendar will allow a selection to be made up to this date. /// If provided, the calendar will allow a selection to be made up to this date.
open var maxDate: Date? open var maxDate: Date = Date() { didSet { setNeedsUpdate() } }
/// If provided, this is the date that will show as selected by the Calendar. /// If provided, this is the date that will show as selected by the Calendar.
/// If no value is provided, the current date will be used. If null is provided, no date will be selected. /// If no value is provided, the current date will be used. If null is provided, no date will be selected.
open var selectedDate: Date { open var selectedDate: Date = Date() { didSet { setNeedsUpdate() } }
get { return _selectedDate }
set {
_selectedDate = newValue
setNeedsUpdate()
}
}
/// If provided, the calendar will be rendered with transparent background. /// If provided, the calendar will be rendered with transparent background.
open var transparentBackground: Bool = false open var transparentBackground: Bool = false { didSet { setNeedsUpdate() } }
/// Array of ``CalendarIndicatorModel`` you are wanting to show on legend. /// Array of ``CalendarIndicatorModel`` you are wanting to show on legend.
open var indicators: [CalendarIndicatorModel] = [] { didSet { setNeedsUpdate() } } open var indicators: [CalendarIndicatorModel] = [] { didSet { setNeedsUpdate() } }
// /// Array of indicators for the legend.
// open var indicatorData: [CalendarIndicatorModel] = [] { didSet { setNeedsUpdate() } }
/// A callback when the date changes. Passes parameters (selectedDate). /// A callback when the date changes. Passes parameters (selectedDate).
public var onChangeSelectedDate: ((Date) -> Void)? public var onChangeSelectedDate: ((Date) -> Void)?
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Private Properties // MARK: - Private Properties
//-------------------------------------------------- //--------------------------------------------------
internal var _selectedDate: Date = Date()
internal var containerSize: CGSize { CGSize(width: 328, height: 376) } //width:320/328 internal var containerSize: CGSize { CGSize(width: 328, height: 376) } //width:320/328
private let cellItemSize = CGSize(width: 40, height: 40) private let cellItemSize = CGSize(width: 40, height: 40)
@ -228,7 +218,7 @@ extension CalendarBase: UICollectionViewDelegate, UICollectionViewDataSource, UI
} }
} }
} }
cell.update(with: surface, indicators: indicators, text: days[indexPath.row], indicatorCount: indicatorCount, selectedDate: selectedDate, hideDate: hideCurrentDateIndicator) cell.update(with: surface, indicators: indicators, text: days[indexPath.row], indicatorCount: indicatorCount, selectedDate: selectedDate, hideDate: hideCurrentDateIndicator, minDate: minDate, maxDate: maxDate, activeDates: activeDates, inactiveDates: inactiveDates)
if (self.days[indexPath.row] == self.getDay(with: selectedDate)) { selectedIndexPath = indexPath } if (self.days[indexPath.row] == self.getDay(with: selectedDate)) { selectedIndexPath = indexPath }
return cell return cell
} }

View File

@ -9,7 +9,7 @@ import Foundation
import UIKit import UIKit
import VDSTokens import VDSTokens
/// Calendar collection view for Date view /// Calendar collection view cell for Date view
final class CalendarDateCollectionViewCell: UICollectionViewCell { final class CalendarDateCollectionViewCell: UICollectionViewCell {
///Identifier for the Calendar Date Cell ///Identifier for the Calendar Date Cell
@ -47,9 +47,10 @@ final class CalendarDateCollectionViewCell: UICollectionViewCell {
private let selectedTextColorConfiguration = SurfaceColorConfiguration(VDSColor.elementsPrimaryInverseOnlight, VDSColor.elementsPrimaryInverseOndark) private let selectedTextColorConfiguration = SurfaceColorConfiguration(VDSColor.elementsPrimaryInverseOnlight, VDSColor.elementsPrimaryInverseOndark)
private let selectedBackgroundColor = SurfaceColorConfiguration(VDSColor.backgroundPrimaryInverseLight, VDSColor.backgroundPrimaryInverseDark) private let selectedBackgroundColor = SurfaceColorConfiguration(VDSColor.backgroundPrimaryInverseLight, VDSColor.backgroundPrimaryInverseDark)
private let selectedCellIndicatorColorConfiguration = SurfaceColorConfiguration(VDSColor.paletteGray65, VDSColor.paletteGray44) private let selectedCellIndicatorColorConfiguration = SurfaceColorConfiguration(VDSColor.paletteGray65, VDSColor.paletteGray44)
private let unselectedTextColorConfiguration = SurfaceColorConfiguration(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark) private let unselectedTextColorConfiguration = SurfaceColorConfiguration(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark)
private let unselectedCellIndicatorColorConfiguration = SurfaceColorConfiguration(VDSColor.elementsSecondaryOnlight, VDSColor.elementsSecondaryOndark) private let unselectedCellIndicatorColorConfiguration = SurfaceColorConfiguration(VDSColor.elementsSecondaryOnlight, VDSColor.elementsSecondaryOndark)
private let disabledTextColorConfiguration = SurfaceColorConfiguration(VDSColor.interactiveDisabledOnlight, VDSColor.interactiveDisabledOndark)
private let disabledBackgroundColor = SurfaceColorConfiguration(VDSFormControlsColor.backgroundOnlight, VDSFormControlsColor.backgroundOndark)
private let currentDate = Date() private let currentDate = Date()
@ -66,6 +67,7 @@ final class CalendarDateCollectionViewCell: UICollectionViewCell {
setUp() setUp()
} }
/// Configuring the cell with default setup
private func setUp() { private func setUp() {
isAccessibilityElement = false isAccessibilityElement = false
contentView.addSubview(containerView) contentView.addSubview(containerView)
@ -89,14 +91,45 @@ final class CalendarDateCollectionViewCell: UICollectionViewCell {
stackView.centerXAnchor.constraint(equalTo: centerXAnchor).activate() stackView.centerXAnchor.constraint(equalTo: centerXAnchor).activate()
} }
func update(with surface: Surface, indicators: [CalendarBase.CalendarIndicatorModel], text: String, indicatorCount: Int, selectedDate: Date, hideDate: Bool) { /// Updating UI based on selected date, modified indicators data along with surface
numberLabel.text = text /// Enable/disable cell based on min date, max date, active dates, inactive dates
func update(with surface: Surface, indicators: [CalendarBase.CalendarIndicatorModel], text: String, indicatorCount: Int, selectedDate: Date, hideDate: Bool, minDate: Date, maxDate: Date, activeDates: [Date], inactiveDates: [Date]) {
numberLabel.surface = surface numberLabel.surface = surface
// numberLabel.isEnabled = isEnabled numberLabel.text = text
stackView.arrangedSubviews.forEach { $0.removeFromSuperview() } stackView.arrangedSubviews.forEach { $0.removeFromSuperview() }
// disabled cells based on min date, max date.
if let day:Int = Int(numberLabel.text), day < minDate.dayInt || day > maxDate.dayInt {
numberLabel.isEnabled = false
numberLabel.textColor = disabledTextColorConfiguration.getColor(surface)
layer.backgroundColor = disabledBackgroundColor.getColor(surface).cgColor
} else {
numberLabel.isEnabled = false
// handing active dates
if activeDates.count > 0 && inactiveDates.count == 0 {
for x in (0...(activeDates.count-1)) {
if let day:Int = Int(numberLabel.text), day == activeDates[x].dayInt {
numberLabel.isEnabled = true
}
}
} else {
numberLabel.isEnabled = true
}
}
// handling inactive dates
if inactiveDates.count > 0 {
for x in (0...(inactiveDates.count-1)) {
if let day:Int = Int(numberLabel.text), day == inactiveDates[x].dayInt {
numberLabel.isEnabled = false
numberLabel.textColor = disabledTextColorConfiguration.getColor(surface)
layer.backgroundColor = disabledBackgroundColor.getColor(surface).cgColor
}
}
}
// update text color, bg color, corner radius // update text color, bg color, corner radius
if numberLabel.text == self.getDay(with: selectedDate) { if (numberLabel.text == self.getDay(with: selectedDate)) && numberLabel.isEnabled {
numberLabel.textColor = selectedTextColorConfiguration.getColor(surface) numberLabel.textColor = selectedTextColorConfiguration.getColor(surface)
layer.backgroundColor = selectedBackgroundColor.getColor(surface).cgColor layer.backgroundColor = selectedBackgroundColor.getColor(surface).cgColor
layer.cornerRadius = VDSFormControls.borderRadius layer.cornerRadius = VDSFormControls.borderRadius
@ -112,11 +145,8 @@ final class CalendarDateCollectionViewCell: UICollectionViewCell {
stackView.widthAnchor.constraint(equalToConstant: CGFloat(width)).activate() stackView.widthAnchor.constraint(equalToConstant: CGFloat(width)).activate()
for x in (0...(indicators.count-1)) { for x in (0...(indicators.count-1)) {
if (self.numberLabel.text == self.getDay(with: indicators[x].date)) { if (self.numberLabel.text == self.getDay(with: indicators[x].date)) {
if numberLabel.text == self.getDay(with: selectedDate) { let color = (numberLabel.text == self.getDay(with: selectedDate)) ? selectedCellIndicatorColorConfiguration.getColor(surface) : unselectedCellIndicatorColorConfiguration.getColor(surface)
addIndicator(with: selectedCellIndicatorColorConfiguration.getColor(surface), surface: surface, clearFullCircle: (x == 1), drawSemiCircle: (x == 2)) addIndicator(with: color, surface: surface, clearFullCircle: (x == 1), drawSemiCircle: (x == 2))
} else {
addIndicator(with: unselectedCellIndicatorColorConfiguration.getColor(surface), surface: surface, clearFullCircle: (x == 1), drawSemiCircle: (x == 2))
}
} }
} }
} }

View File

@ -33,7 +33,7 @@ open class CalendarHeaderView: View {
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Private Properties // MARK: - Private Properties
//-------------------------------------------------- //--------------------------------------------------
internal var containerSize: CGSize { CGSize(width: 304, height: 88) } //width:320/328 internal var containerSize: CGSize { CGSize(width: 304, height: 88) }
private lazy var stackView = UIStackView().with { private lazy var stackView = UIStackView().with {
$0.translatesAutoresizingMaskIntoConstraints = false $0.translatesAutoresizingMaskIntoConstraints = false
@ -112,7 +112,7 @@ open class CalendarHeaderView: View {
super.setup() super.setup()
isAccessibilityElement = false isAccessibilityElement = false
// stackView // containerView
addSubview(containerView) addSubview(containerView)
containerView containerView
.pinTop() .pinTop()
@ -127,9 +127,9 @@ open class CalendarHeaderView: View {
containerView.addSubview(stackView) containerView.addSubview(stackView)
stackView stackView
.pinTop() .pinTop()
.pinBottom(VDSLayout.space1X)
.pinLeading() .pinLeading()
.pinTrailing() .pinTrailing()
.pinBottom(VDSLayout.space1X)
stackView.centerXAnchor.constraint(equalTo: containerView.centerXAnchor).activate() stackView.centerXAnchor.constraint(equalTo: containerView.centerXAnchor).activate()
// month label view, previous and next buttons // month label view, previous and next buttons
@ -145,6 +145,8 @@ open class CalendarHeaderView: View {
monthYearHeaderStackView.addArrangedSubview(previousMonthView) monthYearHeaderStackView.addArrangedSubview(previousMonthView)
previousMonthView.widthAnchor.constraint(equalToConstant: 40).activate() previousMonthView.widthAnchor.constraint(equalToConstant: 40).activate()
previousMonthView.addSubview(previousButton) previousMonthView.addSubview(previousButton)
let spacing = VDSLayout.space1X
previousButton.pinTop(spacing).pinBottom(spacing).pinLeading(spacing).pinTrailing(spacing)
// month year label // month year label
monthYearHeaderStackView.addArrangedSubview(monthYearLabel) monthYearHeaderStackView.addArrangedSubview(monthYearLabel)
@ -153,6 +155,7 @@ open class CalendarHeaderView: View {
monthYearHeaderStackView.addArrangedSubview(nextMonthView) monthYearHeaderStackView.addArrangedSubview(nextMonthView)
nextMonthView.widthAnchor.constraint(equalToConstant: 40).activate() nextMonthView.widthAnchor.constraint(equalToConstant: 40).activate()
nextMonthView.addSubview(nextButton) nextMonthView.addSubview(nextButton)
nextButton.pinTop(spacing).pinBottom(spacing).pinLeading(spacing).pinTrailing(spacing)
// days Collection View // days Collection View
stackView.addArrangedSubview(daysCollectionView) stackView.addArrangedSubview(daysCollectionView)
@ -177,6 +180,7 @@ open class CalendarHeaderView: View {
open override func reset() { open override func reset() {
super.reset() super.reset()
monthYearLabel.textStyle = .boldBodySmall
} }
} }
@ -242,6 +246,5 @@ private class collectionViewCell: UICollectionViewCell {
title.surface = surface title.surface = surface
title.text = text title.text = text
title.textColor = textColorConfiguration.getColor(surface) title.textColor = textColorConfiguration.getColor(surface)
title.backgroundColor = .clear
} }
} }