screen-locker/stronglift_replacement/workout_app/lib/models/break_snapshot.dart
Krzysztof kuhy Rudnicki 5d0aa9b57a Add set haptics, finish chime, sound switch
dopamine-ux 05: selectionClick on recording a set or warmup,
mediumImpact + generated chime (music_theory, pinned tag) on
finish, dialog arrival on the slow motion token, AppDuration /
AppCurve in theme.dart, streak card counts up, all of it
collapsing under the OS reduced-motion flag. One Settings
"Sound" switch silences the chime and the break-end cue
(foreground service included, via a silent channel); haptics
stay. Removes the finished dopamine-ux-05 prompt.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWYdwRAtMg7ki6TKxuU
2026-11-01 21:46:09 +01:00

210 lines
6.5 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.breakFailSecs,
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,
this.soundOn = true,
});
/// 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',
'breakFailSecs',
'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')!,
breakFailSecs: i('breakFailSecs')!,
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,
// Absent means an older UI: stay audible rather than go silent.
soundOn: map['soundOn'] != false,
);
}
/// '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;
/// The running rest's exercise's fail rest, so a `−1 rep` press can re-cut
/// the rest while the UI is dead.
final int breakFailSecs;
/// Human label of the running rest, e.g. 'Rest' or 'Warmup rest'.
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;
/// Whether the break-end alert may make a sound. The service isolate cannot
/// read the settings table (it must never touch sqflite), so the UI says.
final bool soundOn;
/// 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,
'breakFailSecs': breakFailSecs,
'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,
'soundOn': soundOn,
};
/// 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,
breakFailSecs: breakFailSecs,
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,
soundOn: soundOn,
);
}