longestStreak static method

int longestStreak(
  1. List<DateTime> activityDates
)

Returns the longest streak ever from the activity dates.

Implementation

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

  final dates = activityDates.map(_dateOnly).toSet().toList()..sort();

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