getSessionDates method

Future<List<DateTime>> getSessionDates({
  1. int limit = 90,
})

Distinct session dates for streak calculation, descending.

Implementation

Future<List<DateTime>> getSessionDates({int limit = 90}) async {
  final userId = _client.auth.currentUser?.id;
  if (userId == null) return [];

  final response = await _client
      .from('learning_sessions')
      .select('started_at')
      .eq('user_id', userId)
      .not('ended_at', 'is', null)
      .order('started_at', ascending: false)
      .limit(limit * 3);

  final rows = response as List<dynamic>;
  final dates = <DateTime>{};
  for (final row in rows) {
    final dt = DateTime.parse(row['started_at'] as String);
    dates.add(DateTime(dt.year, dt.month, dt.day));
  }
  final sorted = dates.toList()..sort((a, b) => b.compareTo(a));
  return sorted.take(limit).toList();
}