screen-locker/stronglift_replacement/workout_app/lib/models/break_snapshot.dart
Krzysztof kuhy Rudnicki b6e89b93dc
Some checks failed
dependency freshness / dependencies on newest stable (push) Has been cancelled
md-naming / markdown naming convention (push) Has been cancelled
pre-commit / pre-commit (push) Has been cancelled
Tests / test (push) Has been cancelled
Tests / lock-popup-safety (push) Has been cancelled
release-apk / release (APK) (push) Has been cancelled
workout_app: run the break countdown in a foreground service with notifications
The between-set break never finished once the phone's screen was off. Three
independent causes (worked through on-device on 2026-09-12; left
uncommitted awaiting the go-ahead, committed here as-is plus one pin bump):

1. The countdown was not wall-clock: `_breakRemaining--` on a
   Timer.periodic, which Android throttles and Doze suspends in a
   backgrounded isolate, so the counter stalled and _onBreakFinished()
   never fired. Now a BreakClock deadline on the wall clock.
2. An expired break was silently dropped on restore (`if (remaining > 0)`
   only) — now cued and logged.
3. No notification infrastructure at all: no package, no
   POST_NOTIFICATIONS / FOREGROUND_SERVICE / WAKE_LOCK.

Now: flutter_foreground_task runs the countdown in a service isolate that
survives BACK and the screen lock; flutter_local_notifications shows an
ongoing "Workout A · break m:ss / Next: …" notification with −1 rep / Done
actions and a "Break over" alert on its own channel (raw break_end sound,
vibration). Button presses go through a persisted BreakIntent queue the
screen drains on resume, so a press with the app killed is not lost.
Finish and Reset stop the service; a startup reaper handles the crash case.

Verified on the phone with the screen off and locked: service alive and
ticking while Dozing, both notifications on the lock screen, channel
importance 5 with the break_end sound. Not verifiable from adb (receiver is
RECEIVER_NOT_EXPORTED): an actual action-button tap, and a true recents
swipe — see stronglift_replacement/TODO-workout-app-v2.md.

Also fixed en route: the break-end sound had never played in any widget
test (AssetSource needs path_provider, which nothing faked, and the failure
was swallowed by catchError); the queue's prune wrote back a stale list.

This commit additionally:
- takes the freshness gate's bump flutter_local_notifications 22.3.1;
- restores web/package.json to HEAD (the tree held a stale pre-bump copy);
- gives scripts/check_flutter_coverage.sh a FLUTTER_TEST_CONCURRENCY knob:
  the default one-isolate-per-core run peaks over 4 GiB and is OOM-killed
  under the resource cap. Default behaviour unchanged.

Gate: flutter analyze clean, 511 tests, 3187/3187 lines (100%), pre-commit
green. Not re-installed on the phone for the 22.3.1 pin: no device attached
at commit time.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01C7vgacLhb4WJjxuVj2QLEP
2026-09-13 20:28:36 +02:00

192 lines
5.8 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/// What the foreground service needs to know about the running workout.
library;
import 'package:flutter/foundation.dart';
/// A complete picture of the workout, as the notification should show it.
///
/// Always sent whole, never as a delta. Android can restart the service isolate
/// independently of the UI, and a restarted isolate has no history to apply a
/// delta to — so the UI pushes the entire picture on every change instead.
@immutable
class BreakSnapshot {
/// Creates a snapshot.
const BreakSnapshot({
required this.workoutType,
required this.breakEndMs,
required this.breakDurationSecs,
required this.breakLabel,
required this.breakForExIdx,
required this.breakForSetIdx,
required this.nextExIdx,
required this.nextSetIdx,
required this.nextExName,
required this.nextSetNumber,
required this.nextTotalSets,
required this.nextReps,
required this.nextWeight,
required this.lastExIdx,
required this.lastSetIdx,
required this.setsRemaining,
this.finished = false,
});
/// Rebuilds a snapshot from the map sent across the isolate boundary.
///
/// Returns null rather than throwing: the payload crosses a platform channel,
/// and a malformed one must leave the notification as it was instead of
/// taking down the service isolate.
static BreakSnapshot? tryFromMap(Map<Object?, Object?> map) {
int? i(String k) => map[k] is int ? map[k]! as int : null;
final workoutType = map['workoutType'];
if (workoutType is! String) return null;
final nextExName = map['nextExName'];
if (nextExName is! String) return null;
final breakLabel = map['breakLabel'];
if (breakLabel is! String) return null;
final weight = map['nextWeight'];
if (weight is! num) return null;
final keys = [
'breakEndMs',
'breakDurationSecs',
'breakForExIdx',
'breakForSetIdx',
'nextExIdx',
'nextSetIdx',
'nextSetNumber',
'nextTotalSets',
'nextReps',
'lastExIdx',
'lastSetIdx',
'setsRemaining',
];
for (final k in keys) {
if (i(k) == null) return null;
}
return BreakSnapshot(
workoutType: workoutType,
breakEndMs: i('breakEndMs')!,
breakDurationSecs: i('breakDurationSecs')!,
breakLabel: breakLabel,
breakForExIdx: i('breakForExIdx')!,
breakForSetIdx: i('breakForSetIdx')!,
nextExIdx: i('nextExIdx')!,
nextSetIdx: i('nextSetIdx')!,
nextExName: nextExName,
nextSetNumber: i('nextSetNumber')!,
nextTotalSets: i('nextTotalSets')!,
nextReps: i('nextReps')!,
nextWeight: weight.toDouble(),
lastExIdx: i('lastExIdx')!,
lastSetIdx: i('lastSetIdx')!,
setsRemaining: i('setsRemaining')!,
finished: map['finished'] == true,
);
}
/// 'A' or 'B'.
final String workoutType;
/// Wall-clock deadline of the running rest, or 0 when none is running.
final int breakEndMs;
/// Full length of the running rest, for the countdown's denominator.
final int breakDurationSecs;
/// Human label of the running rest, e.g. 'Rest (3 min — well done!)'.
final String breakLabel;
/// Exercise the running rest belongs to, or -1.
final int breakForExIdx;
/// Set the running rest belongs to; -1 means a warmup rest.
final int breakForSetIdx;
/// Exercise the `Done` button would record, or -1 when the workout is over.
final int nextExIdx;
/// Set the `Done` button would record, or -1 when the workout is over.
final int nextSetIdx;
/// Display name of the next exercise.
final String nextExName;
/// 1-based number of the next set, for 'set 3/5'.
final int nextSetNumber;
/// How many sets that exercise has, for 'set 3/5'.
final int nextTotalSets;
/// Target reps of the next set.
final int nextReps;
/// Working weight of the next set, in kg.
final double nextWeight;
/// Exercise the `− 1 rep` button would decrement, or -1.
final int lastExIdx;
/// Set the `− 1 rep` button would decrement, or -1.
final int lastSetIdx;
/// Sets still untapped across the whole workout.
final int setsRemaining;
/// True once the workout has been finished and saved.
final bool finished;
/// True while a rest period is recorded as running.
bool get hasBreak => breakEndMs > 0;
/// True when there is a set left for `Done` to record.
bool get hasNextSet => nextExIdx >= 0 && nextSetIdx >= 0;
/// Flattens this snapshot for `sendDataToTask`, which takes primitives only.
Map<String, Object?> toMap() => {
'workoutType': workoutType,
'breakEndMs': breakEndMs,
'breakDurationSecs': breakDurationSecs,
'breakLabel': breakLabel,
'breakForExIdx': breakForExIdx,
'breakForSetIdx': breakForSetIdx,
'nextExIdx': nextExIdx,
'nextSetIdx': nextSetIdx,
'nextExName': nextExName,
'nextSetNumber': nextSetNumber,
'nextTotalSets': nextTotalSets,
'nextReps': nextReps,
'nextWeight': nextWeight,
'lastExIdx': lastExIdx,
'lastSetIdx': lastSetIdx,
'setsRemaining': setsRemaining,
'finished': finished,
};
/// Returns a copy with the listed fields replaced.
BreakSnapshot copyWith({
int? breakEndMs,
int? breakDurationSecs,
String? breakLabel,
int? setsRemaining,
bool? finished,
}) => BreakSnapshot(
workoutType: workoutType,
breakEndMs: breakEndMs ?? this.breakEndMs,
breakDurationSecs: breakDurationSecs ?? this.breakDurationSecs,
breakLabel: breakLabel ?? this.breakLabel,
breakForExIdx: breakForExIdx,
breakForSetIdx: breakForSetIdx,
nextExIdx: nextExIdx,
nextSetIdx: nextSetIdx,
nextExName: nextExName,
nextSetNumber: nextSetNumber,
nextTotalSets: nextTotalSets,
nextReps: nextReps,
nextWeight: nextWeight,
lastExIdx: lastExIdx,
lastSetIdx: lastSetIdx,
setsRemaining: setsRemaining ?? this.setsRemaining,
finished: finished ?? this.finished,
);
}