From b0a4068224386258cf3019dae462530037d21f25 Mon Sep 17 00:00:00 2001 From: Jaap Aarts Date: Sat, 2 Sep 2023 15:21:46 +0200 Subject: [PATCH] Fix calendar not adding a day for today if there are no later events (#453) --- lib/ui/screens/calendar_screen.dart | 2 +- lib/ui/widgets/events.dart | 49 ++++++++++++++++++++++------- test/unit/calendar_test.dart | 22 ++++++++++++- 3 files changed, 60 insertions(+), 13 deletions(-) diff --git a/lib/ui/screens/calendar_screen.dart b/lib/ui/screens/calendar_screen.dart index 09b1e2be7..e81bdc492 100644 --- a/lib/ui/screens/calendar_screen.dart +++ b/lib/ui/screens/calendar_screen.dart @@ -253,7 +253,7 @@ class CalendarScrollView extends StatelessWidget { calendarState.resultsDown.isNotEmpty, _monthGroupedEventsDown = calendarState.resultsDown.isEmpty ? List.empty() - : ensureContainsToday( + : ensureMonthsContainsToday( groupByMonth(calendarState.resultsDown) .sortedBy((element) => element.month), now), diff --git a/lib/ui/widgets/events.dart b/lib/ui/widgets/events.dart index 2d3bca3c0..738131333 100644 --- a/lib/ui/widgets/events.dart +++ b/lib/ui/widgets/events.dart @@ -17,7 +17,7 @@ class CalendarViewDay { : events = events.sortedBy((element) => element.start); } -List ensureContainsToday( +List ensureMonthsContainsToday( List events, DateTime now) { DateTime today = DateTime( now.year, @@ -28,30 +28,57 @@ List ensureContainsToday( now.year, now.month, ); + CalendarViewDay emptyDay = CalendarViewDay(day: today, events: []); for (var i = 0; i < events.length; i++) { if (events[i].month.isAfter(thisMonth)) { + // The current month does not exist yet + // Make a new month with only today events.insert(i, CalendarViewMonth(month: thisMonth, events: [])); - events[i].days.add(CalendarViewDay(day: today, events: [])); - + events[i].days.add(emptyDay); return events; } if (events[i].month == thisMonth) { - for (var j = 0; j < events[i].days.length; j++) { - if (events[i].days[j].day == today) return events; - if (events[i].days[j].day.isAfter(today)) { - events[i].days.insert(j, CalendarViewDay(day: today, events: [])); - return events; - } - } + events[i] = ensureMonthContainsDay(events[i], now.day); return events; } } + // We did not find the current month, and there was no month after the current month + // Add a new month to the end events.add(CalendarViewMonth(month: thisMonth, events: [])); - events.last.days.add(CalendarViewDay(day: today, events: [])); + events.last.days.add(emptyDay); return events; } +CalendarViewMonth ensureMonthContainsDay(CalendarViewMonth month, int day) { + DateTime thisMonth = month.month; + DateTime today = DateTime( + thisMonth.year, + thisMonth.month, + day, + ); + + CalendarViewDay emptyDay = CalendarViewDay(day: today, events: []); + // Try to find the right day, or a day after + for (var j = 0; j < month.days.length; j++) { + if (month.days[j].day == today) { + // There already exists a day for today in the calendar + // Nothing to be done + return month; + } + if (month.days[j].day.isAfter(today)) { + // We did not find today, but there was is a day after today + // Insert a new day + month.days.insert(j, emptyDay); + return month; + } + } + // We did not find today, and there was no day after today + // Add a new to to the end + month.days.add(emptyDay); + return month; +} + List groupByMonth( List eventList, ) => diff --git a/test/unit/calendar_test.dart b/test/unit/calendar_test.dart index a10c18c75..1c5832045 100644 --- a/test/unit/calendar_test.dart +++ b/test/unit/calendar_test.dart @@ -120,7 +120,27 @@ void main() { ); final calendarEvents = CalendarEvent.splitEventIntoCalendarEvents(event); - final monthGroupedEventsDown = ensureContainsToday( + final monthGroupedEventsDown = ensureMonthsContainsToday( + groupByMonth(calendarEvents).sortedBy((element) => element.month), + now); + + expect(monthGroupedEventsDown.length, 2); + expect(monthGroupedEventsDown[1].days.length, 1); + expect(monthGroupedEventsDown[1].days[0].events.length, 0); + }); + test('AddToEnd', () { + final now = DateTime.parse('2022-04-05 13:37'); + final event = FakeEvent( + pk: 1, + title: 'Lorem', + description: 'Ipsum', + start: DateTime.parse('2022-03-04 13:37'), + end: DateTime.parse('2022-03-04 14:37'), + location: 'Dolor', + ); + + final calendarEvents = CalendarEvent.splitEventIntoCalendarEvents(event); + final monthGroupedEventsDown = ensureMonthsContainsToday( groupByMonth(calendarEvents).sortedBy((element) => element.month), now);