getSessionCountsByDay method

Future<Map<DateTime, int>> getSessionCountsByDay({
  1. required DateTime since,
})

Returns a date-only → session-count map for sessions that started on or after since. Used to drive the progress-dashboard heatmap. Days with no activity are omitted; callers can default missing keys to 0.

Implementation

Future<Map<DateTime, int>> getSessionCountsByDay({
  required DateTime since,
}) 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)
      .gte('started_at', since.toUtc().toIso8601String());
  final counts = <DateTime, int>{};
  for (final row in response as List<dynamic>) {
    final raw = (row as Map<String, dynamic>)['started_at'] as String?;
    if (raw == null) continue;
    final ts = DateTime.parse(raw).toLocal();
    final day = DateTime(ts.year, ts.month, ts.day);
    counts[day] = (counts[day] ?? 0) + 1;
  }
  return counts;
}