wake-alarm/phone_app/lib/services/weight_entry.dart
Krzysztof kuhy Rudnicki c3c129b068 Make the morning session the carrot: earn 11:00 by finishing it in time
On 2026-09-19 the phone was powered off after the bathroom tag and nothing
on the PC noticed: every finished session was published and nothing read
it. Now wake-alarm distils the phone's morning into an HMAC-signed
~/.local/state/wake_alarm/morning_session.json (wake-alarm-session.timer,
07:00-11:00 every 2 min) that screen-locker and leetcode-guard read to
defer their locks -- provisionally until min(start + 90 min, 09:30) while
the session is live, until 11:00 once it completed inside that cutoff,
and not at all for a session finished later (the phone switched on at
noon completes the legs then; that must un-lock nothing).

The phone publishes the session at start as well as at finish
(current_session, cleared with the finish stamp), so the PC can tell
"started, not finished" from "never started". A required 1-5 energy tap
joins the weigh-in and ships as latest_energy with a PC reader mirroring
_weight (the shared half is _field_reader).

Also fixes the vmbox harness, red since the 2026-09-11 home-tidy: the
guest tree lives at ~/wake-alarm, and the INSTALL-OK grep matched vm's
echo of the command line. Takes Flutter 3.47.5.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Tp54ewdXRLaimUuT8LgYyn
2026-09-19 16:40:51 +02:00

98 lines
2.9 KiB
Dart

/// Validation for the morning check-in: the weigh-in and the energy tap.
///
/// A typed number, not a Bluetooth scale — the ring path takes no plugins.
/// Two gates stand in for the hardware: the value has to be plausible, and
/// the bathroom tag has to follow it within [confirmWindow] (enforced by
/// `MorningSession`, which is what stops it being typed from bed).
library;
/// Lightest value accepted, in kg.
const double minWeightKg = 30;
/// Heaviest value accepted, in kg.
const double maxWeightKg = 250;
/// Largest overnight change accepted without retyping, in kg.
const double maxWeightJumpKg = 3;
/// How long a typed weight stays valid before the bathroom tag confirms it.
const Duration confirmWindow = Duration(minutes: 2);
/// Lowest energy rating; five taps, no typing, so nothing to validate beyond
/// the range.
const int minEnergy = 1;
/// Highest energy rating.
const int maxEnergy = 5;
/// What a weigh-in attempt came to.
enum WeightVerdict {
/// Accepted.
ok,
/// Not a number.
unparsable,
/// Outside [minWeightKg]..[maxWeightKg].
outOfRange,
/// More than [maxWeightJumpKg] from the last entry; type it again to
/// confirm.
needsConfirm,
}
/// A checked weigh-in: the verdict, the parsed value and a line for the UI.
class WeightCheck {
/// Creates a result.
const WeightCheck(this.verdict, {required this.detail, this.kg});
/// What happened.
final WeightVerdict verdict;
/// The parsed value, present whenever [detail] could name one.
final double? kg;
/// Human-readable reason, shown on the session screen.
final String detail;
/// Whether the value was accepted.
bool get accepted => verdict == WeightVerdict.ok;
}
/// Parses [text] as kilograms; a decimal comma is fine.
double? parseWeight(String text) =>
double.tryParse(text.trim().replaceAll(',', '.'));
/// Checks [text] against the range and the last known weight.
///
/// [confirming] is the value the user was just asked to retype: an identical
/// second entry passes the jump check, a different one starts over. Without
/// [last] there is nothing to jump from, so only the range applies.
WeightCheck checkWeight(String text, {double? last, double? confirming}) {
final kg = parseWeight(text);
if (kg == null) {
return const WeightCheck(
WeightVerdict.unparsable,
detail: 'Enter your weight in kg, e.g. 72.4',
);
}
if (kg < minWeightKg || kg > maxWeightKg) {
return WeightCheck(
WeightVerdict.outOfRange,
kg: kg,
detail: '$kg kg is not a body weight',
);
}
if (last != null && (kg - last).abs() > maxWeightJumpKg) {
if (confirming != kg) {
final delta = (kg - last).toStringAsFixed(1);
return WeightCheck(
WeightVerdict.needsConfirm,
kg: kg,
detail: 'That is $delta kg from last time — type it again to confirm',
);
}
}
return WeightCheck(WeightVerdict.ok, kg: kg, detail: '$kg kg noted');
}