currentStreak static method

int currentStreak(
  1. List<DateTime> activityDates
)

Returns the current streak: consecutive calendar days (up to today) with at least one completed session.

activityDates must be sorted descending (most recent first) and contain only the date portion (time zeroed out).

Implementation

static int currentStreak(List<DateTime> activityDates) {
  if (activityDates.isEmpty) return 0;

  final today = _dateOnly(DateTime.now());
  final dates = activityDates.map(_dateOnly).toSet().toList()
    ..sort((a, b) => b.compareTo(a));

  // Streak must include today or yesterday to be "current"
  final first = dates.first;
  final gap = today.difference(first).inDays;
  if (gap > 1) return 0;

  int streak = 1;
  for (int i = 1; i < dates.length; i++) {
    final diff = dates[i - 1].difference(dates[i]).inDays;
    if (diff == 1) {
      streak++;
    } else {
      break;
    }
  }
  return streak;
}