feat: add journey stats and history detail

This commit is contained in:
2026-06-12 01:26:19 +08:00
parent b287a56731
commit eb8b209c0c
10 changed files with 701 additions and 1 deletions
+50
View File
@@ -0,0 +1,50 @@
import '../core/date_keys.dart';
import 'app_state.dart';
class JourneyStats {
const JourneyStats({
required this.currentStreakDays,
required this.weekFocusMinutes,
required this.activeDaysThisWeek,
required this.habitCompletionRate,
});
final int currentStreakDays;
final int weekFocusMinutes;
final int activeDaysThisWeek;
final double habitCompletionRate;
static JourneyStats fromState(AppState state) {
final dateKeys = lastSevenDateKeys();
final dateKeySet = dateKeys.toSet();
final weekFocusMinutes = dateKeys.fold<int>(
0,
(total, key) => total + minutesForDate(state.sessions, key),
);
final activeDays = dateKeys.where((key) => _hasProgress(state, key)).length;
final habitCompletions = state.habits.fold<int>(
0,
(total, habit) =>
total + habit.completedDates.where(dateKeySet.contains).length,
);
final habitOpportunities = state.habits.length * dateKeys.length;
return JourneyStats(
currentStreakDays: currentStreak(
state.habits,
state.sessions,
state.reflections,
),
weekFocusMinutes: weekFocusMinutes,
activeDaysThisWeek: activeDays,
habitCompletionRate:
habitOpportunities == 0 ? 0 : habitCompletions / habitOpportunities,
);
}
static bool _hasProgress(AppState state, String key) {
return state.habits.any((habit) => habit.completedDates.contains(key)) ||
state.sessions.any((session) => dateKey(session.completedAt) == key) ||
state.reflections.any((reflection) => reflection.dateKey == key);
}
}