Calendar
Calendar copied to clipboard
Month selection triggers multiple times and with invalid data as well
Library information:
- Version: 2.4.0-beta01
- Compose
Describe the bug**
Whenever I swipe left or right to a new month, the callback gets triggered multiple times. The months passed through are not correct and I have to take the data from the calendar state instead of the lambda value. Because of this I can't even close/re-open the calendar without it resetting to it's original month. Very annoying and hard to deal with since you will have to keep track of multiple variables and since the data is being looped through multiple times (AND changing in the meantime at random) it's pretty much impossible.
To Reproduce (if applicable)
Steps to reproduce the behavior: Just use the calendar and swipe left/right and put some logging statements in the callback. (I use the monthHeader one)
Expected behavior (if applicable)
Callback only triggered once with the correct data
Screenshots? (if applicable)
If applicable, add screenshots or screen recordings to help explain the problem.
Additional information
Add any other information about the bug here.
Are you sure it is the calendar calling those methods multiple times or something in your code triggering recomposition? Also, keep in mind that the months adjacent to the currently visible month can be bound in some cases, this is a normal LazyRow/Column behavior. If you can reproduce this in the sample project, I am happy to take a look.
Yeah I got same issue, which the callback called multiple times (may be recomposition) and logs shows different 'month' info rather than what the debugging device shows to me. I used calendarState's -> layoutinfo -> visiblemonthsInfo to find visible month info, instead using monthHeader's lambda 'CalendarMonth'.
@junseokseo9306 I ended up doing the same, the new Month = state.firstVisibleMonth.yearMonth, I didn't check for recomposition yet but whenever you add data to the calendar it will recompose anyway so that shouldn't be an issue imo. I guess for now the solution is to track the previous selected month, check it with the 'new month' you get from state.firstVisibleMonth.yearMonth and if they are different set thew previous to the new month and fire a callback with the new month as variable
@kizitonwose as I mentioned in my response to junseokseo, should that really be an issue? Because I add a lot of data to the calendar so it will recompose anyway. I have the idea that some sort of pagination gives this bug (loading previous/next month as well). Solution to work around it is what I mentioned above.
@frankhardenberg thanks for sharing. yeah i agree with the pagination issue.
Hi @kizitonwose , I noticed that this issue is happening when I try to change current month using animateScrollToMonth function ,so for now I only use scrollToMonth function and it is working fine. I need to mention that issue is not due to recomposition as the data returned in the callaback were different for example when I increase current month "3" to "4" what i get in the callback of month header is "4" then "5"
@YasserElnagarRobusta animateScrollToMonth
will draw each moth as it scrolls, so it makes sense that all bind methods are called accordingly.
@frankhardenberg Your approach is fine 👍🏼
I will close this as there seems to be nothing for me to do, feel free to comment if you have further questions.