preferredTodayCourseIndex function

int? preferredTodayCourseIndex(
  1. List<CourseScheduleMeeting> meetings, {
  2. required DateTime now,
  3. Duration imminentThreshold = const Duration(minutes: 30),
})

Selects the card that should receive focus on the home carousel.

A course starting within imminentThreshold takes priority over an overlapping ongoing course. Otherwise the ongoing course is preferred, followed by the next course later today. Returns null after today's final course has ended.

Implementation

int? preferredTodayCourseIndex(
  List<CourseScheduleMeeting> meetings, {
  required DateTime now,
  Duration imminentThreshold = const Duration(minutes: 30),
}) {
  final imminentLimit = now.add(imminentThreshold);
  final imminentIndex = meetings.indexWhere(
    (meeting) =>
        meeting.start.isAfter(now) && !meeting.start.isAfter(imminentLimit),
  );
  if (imminentIndex >= 0) return imminentIndex;

  final ongoingIndex = meetings.indexWhere((meeting) => meeting.isOngoing);
  if (ongoingIndex >= 0) return ongoingIndex;

  return nextCourseIndex(meetings, now: now);
}