From 64c556a337524173a12a18bf5e8ae41046760fab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20Kaan=20K=C3=B6se?= Date: Tue, 31 Dec 2024 14:28:28 +0100 Subject: [PATCH] Fixing load more issue during flip switches. --- .../CalendarPageViewModel.cs | 34 ++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/Wino.Calendar.ViewModels/CalendarPageViewModel.cs b/Wino.Calendar.ViewModels/CalendarPageViewModel.cs index a2fd1c2d..e8b8351d 100644 --- a/Wino.Calendar.ViewModels/CalendarPageViewModel.cs +++ b/Wino.Calendar.ViewModels/CalendarPageViewModel.cs @@ -227,6 +227,8 @@ namespace Wino.Calendar.ViewModels DateTime? loadingDisplayDate = null, CalendarLoadDirection calendarLoadDirection = CalendarLoadDirection.Replace) { + isLoadMoreBlocked = calendarLoadDirection == CalendarLoadDirection.Replace; + // This is the part we arrange the flip view calendar logic. /* Loading for a month of the selected date is fine. @@ -504,28 +506,28 @@ namespace Wino.Calendar.ViewModels if (isLoadMoreBlocked) return; - // Send the loading message initiated by the app. - if (SelectedDateRangeIndex == DayRanges.Count - 1) - { - // Load next, starting from the end date. - _ = LoadMoreAsync(CalendarLoadDirection.Next); - } - else if (SelectedDateRangeIndex == 0) - { - // Load previous, starting from the start date. - - _ = LoadMoreAsync(CalendarLoadDirection.Previous); - } + _ = LoadMoreAsync(); } - private async Task LoadMoreAsync(CalendarLoadDirection direction) + private async Task LoadMoreAsync() { - Debug.WriteLine($"Loading {direction} items."); - try { await _calendarLoadingSemaphore.WaitAsync(); - await RenderDatesAsync(CalendarInitInitiative.App, calendarLoadDirection: direction); + + // Depending on the selected index, we'll load more dates. + // Day ranges may change while the async update is in progress. + // Therefore we wait for semaphore to be released before we continue. + // There is no need to load more if the current index is not in ideal position. + + if (SelectedDateRangeIndex == DayRanges.Count - 1) + { + await RenderDatesAsync(CalendarInitInitiative.App, calendarLoadDirection: CalendarLoadDirection.Next); + } + else if (SelectedDateRangeIndex == 0) + { + await RenderDatesAsync(CalendarInitInitiative.App, calendarLoadDirection: CalendarLoadDirection.Previous); + } } catch (Exception) {