Files
GroundedHelper/frontend/lib/Grounded/see/routine/ViewRoutineRunner.dart
alvocool add94f621c Add Progress over time and a targets and reminders setup
Progress gives a Day/Week/Month view of routines held against a weekly
target, compared with the same point of the previous period, with active
time and the daily streak. Targets & reminders sets routines and minutes
per week and schedules a heads-up before each routine. The runner's summary
now shows today's held count and streak. Finished runs are mirrored to a
device run log that feeds both.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KhQh614DMeYoQEH9SoMRUw
2026-11-08 20:17:58 +03:00

88 lines
2.8 KiB
Dart

import '../../about/external/data/LiveSession.dart';
import '../../about/external/initial/RoutineLogRequest.dart';
import '../../about/internal/application/RoutineRun.dart';
import '../../about/internal/application/RoutineRunRecord.dart';
import '../../configs/NotificationServiceConfig.dart';
import '../parent/ParentViewModel.dart';
import 'ConnectRoutineRunner.dart';
class ViewRoutineRunner extends ParentViewModel {
ConnectRoutineRunner connection;
ViewRoutineRunner(super.context, this.connection);
/// Mirrors the current phase into the ongoing notification, through the
/// same [LiveSession] shape the single-task runner uses.
void publishRun(RoutineRun run) {
LocalNotificationEngine.showSessionNotification(LiveSession(
commitmentId: run.chain.id ?? "",
title: run.phase.rest ? "Rest" : run.phase.step.title,
goalTitle: run.chain.title,
startedAt: run.startedAt,
accumulatedSeconds: run.phaseElapsed(),
resumedAt: run.running ? DateTime.now() : null,
requiredSeconds: run.phase.seconds,
backgroundedCount: run.backgroundedCount,
));
}
void loadHistory() async {
connection.onHistoryLoaded(await getDataManager().getRunLog());
}
/// The local mirror Progress reads. Capped so the device log stays small.
Future<void> _remember(RoutineRun run) async {
final List<RoutineRunRecord> records = await getDataManager().getRunLog();
records.add(RoutineRunRecord(
routineId: run.chain.id ?? "",
title: run.chain.title,
kind: run.chain.kind.name,
startedAt: run.startedAt,
seconds: run.elapsedSeconds(),
completedSteps: run.chain.completedSteps,
totalSteps: run.chain.steps.length,
));
const int cap = 500;
await getDataManager().setRunLog(records.length > cap
? records.sublist(records.length - cap)
: records);
}
void clearRun() {
LocalNotificationEngine.cancelSessionNotification();
}
/// Held or broken, the run is always logged — a broken chain goes in as
/// partial at the step it broke on.
void log(RoutineRun run) async {
if (!await hasNetwork(() => log(run))) return;
showLoading("Recording");
try {
await getDataManager().logRoutineChainEntry(RoutineLogRequest(
routineId: run.chain.id ?? "",
startedAt: run.startedAt.toIso8601String(),
elapsedSeconds: run.elapsedSeconds(),
completedSteps: run.chain.completedSteps,
totalSteps: run.chain.steps.length,
breakIndex: run.breakIndex,
backgroundedCount: run.backgroundedCount,
skippedCount: run.skippedCount,
).toJson());
await _remember(run);
clearRun();
closeLoading();
connection.onLogged(run.chain.complete);
} catch (e) {
handleError(e, () => log(run), () => dismissError(), "Retry");
}
}
}