Skip to content

Commit

Permalink
Fix calendar not adding a day for today if there are no later events (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
JAicewizard committed Sep 20, 2023
1 parent 9b7279b commit b0a4068
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/ui/screens/calendar_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
49 changes: 38 additions & 11 deletions lib/ui/widgets/events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class CalendarViewDay {
: events = events.sortedBy((element) => element.start);
}

List<CalendarViewMonth> ensureContainsToday(
List<CalendarViewMonth> ensureMonthsContainsToday(
List<CalendarViewMonth> events, DateTime now) {
DateTime today = DateTime(
now.year,
Expand All @@ -28,30 +28,57 @@ List<CalendarViewMonth> 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<CalendarViewMonth> groupByMonth(
List<CalendarEvent> eventList,
) =>
Expand Down
22 changes: 21 additions & 1 deletion test/unit/calendar_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit b0a4068

Please sign in to comment.