58 lines
1.6 KiB
Dart
58 lines
1.6 KiB
Dart
import '../models/focus_session.dart';
|
|
import '../models/habit.dart';
|
|
import '../models/reflection_entry.dart';
|
|
|
|
String todayKey() => dateKey(DateTime.now());
|
|
|
|
String dateKey(DateTime date) {
|
|
final local = DateTime(date.year, date.month, date.day);
|
|
return '${local.year.toString().padLeft(4, '0')}-'
|
|
'${local.month.toString().padLeft(2, '0')}-'
|
|
'${local.day.toString().padLeft(2, '0')}';
|
|
}
|
|
|
|
List<String> lastSevenDateKeys() {
|
|
final now = DateTime.now();
|
|
return List.generate(
|
|
7,
|
|
(index) => dateKey(now.subtract(Duration(days: 6 - index))),
|
|
);
|
|
}
|
|
|
|
int minutesForDate(List<FocusSession> sessions, String key) {
|
|
return sessions
|
|
.where((session) => dateKey(session.completedAt) == key)
|
|
.fold<int>(0, (total, session) => total + session.actualMinutes);
|
|
}
|
|
|
|
int currentStreak(
|
|
List<Habit> habits,
|
|
List<FocusSession> sessions,
|
|
List<ReflectionEntry> reflections,
|
|
) {
|
|
var streak = 0;
|
|
var cursor = DateTime.now();
|
|
while (true) {
|
|
final key = dateKey(cursor);
|
|
final hasHabit = habits.any((habit) => habit.completedDates.contains(key));
|
|
final hasSession = sessions.any(
|
|
(session) => dateKey(session.completedAt) == key,
|
|
);
|
|
final hasReflection = reflections.any(
|
|
(reflection) => reflection.dateKey == key,
|
|
);
|
|
if (!hasHabit && !hasSession && !hasReflection) {
|
|
return streak;
|
|
}
|
|
streak += 1;
|
|
cursor = cursor.subtract(const Duration(days: 1));
|
|
}
|
|
}
|
|
|
|
String formatSeconds(int seconds) {
|
|
final minutes = seconds ~/ 60;
|
|
final remaining = seconds % 60;
|
|
return '${minutes.toString().padLeft(2, '0')}:'
|
|
'${remaining.toString().padLeft(2, '0')}';
|
|
}
|