Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DON-1089: Fixing a scroll issue in BPKCalendar #2158

Closed

Conversation

yurareutskiy
Copy link
Contributor

@yurareutskiy yurareutskiy commented Feb 3, 2025

DON-1089

When scrolling back upwards sometimes from a future month to a previous month the header jumps down and causes the overlapping issue on the right
image

The solution was to calculate heights and keep them cached. Also make all cells in a LazyVGrid contained in one array, so just one ForEach would be used (apparently, there is an issue with using multiple ForEach)

More info:
https://skyscanner.atlassian.net/wiki/x/Ty9rTQ

Remember to include the following changes:

If you are curious about how we review, please read through the code review guidelines

@yurareutskiy yurareutskiy added swiftui minor Non breaking change labels Feb 3, 2025
@yurareutskiy yurareutskiy marked this pull request as ready for review February 3, 2025 09:38
@yurareutskiy yurareutskiy force-pushed the donburi/DON-1089_fix_scroll_issue_in_calendar branch 2 times, most recently from ee4a5c6 to 55375df Compare February 5, 2025 20:34
)
.frame(height: headerHeight)
.modifier(ReadSizeModifier {
headerHeight = max($0.height, headerHeight ?? 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about having the initial headerHeight as 0 instead of optional nil ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Answered to that below

@@ -32,7 +33,7 @@ struct CalendarContainer<MonthContent: View>: View {
LazyVStack(spacing: BPKSpacing.none) {
ForEach(0...monthsToShow, id: \.self) { monthIndex in
let firstDayOfMonth = firstDayOf(monthIndex: monthIndex)
monthContent(firstDayOfMonth)
monthContent(firstDayOfMonth, $accessoryViewHeight)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need accessoryViewHeight for monthContent ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so, the idea is to share between each month view the accessory view height, so it wouldn't need to calculate it each time
this should help with the consistency
the problem could be because the magnifier icon and the text have different heights (15.6 vs 16.0), so in my understanding, if one month has only one magnifiers, then when it happened to display a text, then it would need to recalculate the heights
and to avoid that issue I thought about this sharable height value, so every component already would aware of the target height

however (!), I've checked the latest changes with removing that sharable height and keeping it as a state (as it was before), and I haven't noticed anything wrong with that after some tests

what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can keep it as it was before. It seems other fixes helped us to reduce/escape the flakiness on this height. I couldn't reproduce the issue again.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure caching a height is a good solution here, it should be left to SwiftUI to calculate on real time based on constraints, I think there is something else happening here, perhaps something we're doing wrong when reusing month cells or similar? setting a frame for things should almost never be the solution unless specifically due to a design reason

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@frugoman
This change has been reverted ✅ It was just an open comment.

@@ -28,7 +40,8 @@ struct CalendarMonthGrid<
let calendar: Calendar
let validRange: ClosedRange<Date>

@State private var dayCellHeight: CGFloat = 0
@State private var dayCellHeight: CGFloat?
@Binding var accessoryViewHeight: CGFloat?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about having the initial accessoryViewHeight and dayCellHeight as 0 instead of optional nil ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we shouldn't set 0s here
it's important to set nil as an initial value, so swiftui could calculate it correctly and return the correct view when we call .frame() modifier
I tried to set to 0, it didn't work
that's an example

ScreenRecording_02-06-2025.13-26-57_1.MP4

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense as we want to get the size behaviour from the view for the first time.
https://developer.apple.com/documentation/swiftui/view/frame(width:height:alignment:)
Screenshot 2025-02-06 at 15 02 49

Thank you for checking.

@yurareutskiy yurareutskiy force-pushed the donburi/DON-1089_fix_scroll_issue_in_calendar branch from 920dd0c to 909b47b Compare February 6, 2025 11:57
Copy link
Contributor

@novinfard novinfard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work and investigation 🚀

@frugoman
Copy link
Contributor

Fixed in #2167

@frugoman frugoman closed this Feb 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
minor Non breaking change swiftui
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants