From 843c8054ad10bffc630a62cbc2cf9bbf1aa42f88 Mon Sep 17 00:00:00 2001 From: vasavk Date: Fri, 10 May 2024 22:40:37 +0530 Subject: [PATCH] Digital ACT-191 ONEAPP-7958 story: dynamic height update for calendar --- VDS/Components/Calendar/Calendar.swift | 28 +++++++++++++------- VDS/Components/Calendar/Date+Extension.swift | 4 ++- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/VDS/Components/Calendar/Calendar.swift b/VDS/Components/Calendar/Calendar.swift index bcba3885..9ec8e6fc 100644 --- a/VDS/Components/Calendar/Calendar.swift +++ b/VDS/Components/Calendar/Calendar.swift @@ -74,8 +74,9 @@ open class CalendarBase: View { private let headerHeight = 88.0 private let footerHeight = 40.0 private let calendarWidth = 304.0 - private let items = 35 - + + private var heightConstraint: NSLayoutConstraint? + private var containerHeightConstraint: NSLayoutConstraint? private var selectedIndexPath : IndexPath? private var dates: [Date] = [] private var days: [String] = [] @@ -121,15 +122,16 @@ open class CalendarBase: View { /// 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() - isAccessibilityElement = false + isAccessibilityElement = true + accessibilityLabel = "Calendar" addSubview(containerView) containerView .pinTop() .pinBottom() .pinLeadingGreaterThanOrEqualTo() .pinTrailingLessThanOrEqualTo() - .height(containerSize.height) .width(containerSize.width) + .heightGreaterThanEqualTo(containerSize.height) containerView.centerXAnchor.constraint(equalTo: centerXAnchor).activate() // Calendar View @@ -142,10 +144,10 @@ open class CalendarBase: View { .pinLeading(spacing) .pinTrailing(spacing) .width(calendarWidth) - .height(calendarHeight) + .heightGreaterThanEqualTo(calendarHeight) collectionView.centerXAnchor.constraint(equalTo: containerView.centerXAnchor).activate() } - + open override func updateView() { super.updateView() // range check between min & max dates @@ -154,8 +156,8 @@ open class CalendarBase: View { let fallsBetween = displayDate.isBetweeen(date: minDate, andDate: maxDate) displayDate = fallsBetween ? displayDate : minDate self.fetchDates(with: displayDate) - collectionView.reloadData() } + layer.backgroundColor = backgroundColorConfiguration.getColor(self).cgColor if hideContainerBorder { layer.borderColor = nil @@ -187,6 +189,8 @@ open class CalendarBase: View { // MARK: - Private Methods //-------------------------------------------------- func fetchDates(with aDate:Date) { + heightConstraint?.isActive = false + containerHeightConstraint?.isActive = false days.removeAll() self.dates = aDate.calendarDisplayDays for date in dates { @@ -197,6 +201,14 @@ open class CalendarBase: View { days.append(getDay(with: date)) } } + self.collectionView.reloadData() + var height = self.collectionView.collectionViewLayout.collectionViewContentSize.height + height = (height > 0) ? height : containerSize.height + heightConstraint = collectionView.heightAnchor.constraint(equalToConstant: height) + containerHeightConstraint = containerView.heightAnchor.constraint(equalToConstant: (height + (2 * VDSLayout.space4X))) + heightConstraint?.isActive = true + containerHeightConstraint?.isActive = true + self.layoutIfNeeded() } func getDay(with date:Date) -> String { @@ -259,7 +271,6 @@ extension CalendarBase: UICollectionViewDelegate, UICollectionViewDataSource, UI if ((aDate.monthInt <= maxDate.monthInt) && (aDate.yearInt == maxDate.yearInt)) || (aDate.yearInt < maxDate.yearInt) { displayDate = aDate self.fetchDates(with: displayDate) - self.collectionView.reloadData() } } header.previousClicked = { [weak self] in @@ -268,7 +279,6 @@ extension CalendarBase: UICollectionViewDelegate, UICollectionViewDataSource, UI if ((minDate.monthInt <= aDate.monthInt) && (minDate.yearInt == aDate.yearInt)) || (minDate.yearInt < aDate.yearInt) { displayDate = aDate self.fetchDates(with: displayDate) - self.collectionView.reloadData() } } header.update(with: surface, aDate: displayDate, nextEnabled: nextEnabled, previousEnabled: prevEnabled) diff --git a/VDS/Components/Calendar/Date+Extension.swift b/VDS/Components/Calendar/Date+Extension.swift index 2a016ecb..8d697049 100644 --- a/VDS/Components/Calendar/Date+Extension.swift +++ b/VDS/Components/Calendar/Date+Extension.swift @@ -92,7 +92,9 @@ extension Date { /// Check if the date falls between the given dates func isBetweeen(date date1: Date, andDate date2: Date) -> Bool { - return date1.compare(self) == self.compare(date2) + let from = Calendar.current.date(byAdding: .day, value: -1, to: date1)! + let to = Calendar.current.date(byAdding: .day, value: 1, to: date2)! + return from.compare(self) == self.compare(to) } /// Returns the month name of the given date