fetchDayEvents method

Future<List<CalendarMonthEvent>> fetchDayEvents({
  1. String? templateId,
  2. required DateTime displayDate,
  3. required bool parentElementsOnly,
})

Implementation

Future<List<CalendarMonthEvent>> fetchDayEvents({
  String? templateId,
  required DateTime displayDate,
  required bool parentElementsOnly,
}) async {
  final allMonthEvents = await fetchMonthEvents(
    templateId: templateId,
    displayDate: displayDate,
    parentElementsOnly: parentElementsOnly,
  );

  // Filter events for the specific day
  final startOfDay = DateUtils.dateOnly(displayDate);
  final endOfDay = startOfDay.add(const Duration(days: 1));

  return allMonthEvents.where((event) {
    // The standard interval overlap check is: A.start < B.end && B.start < A.end
    // This handles multi-day events correctly.
    final overlaps = event.startDate.isBefore(endOfDay) &&
        event.endDate.isAfter(startOfDay);

    // We also add a check for events that start on the given day. This is a more robust
    // way to catch single-day events, especially if their end date representation is inconsistent
    // (e.g., start and end time are identical), which would cause the `overlaps` check to fail.
    final startsOnDay = DateUtils.isSameDay(event.startDate, displayDate);

    return startsOnDay || overlaps;
  }).toList();
}