wake-alarm/phone_app/lib/services/room_proof.dart
Krzysztof kuhy Rudnicki 150a8cd7e5 Require two tags, the walk, and three answers to dismiss
The dismiss gate was an OR — NFC tag *or* QR *or* (25 steps + 2 min) —
followed by one challenge. QR never worked at ring time (no scanner
package, no CAMERA permission, `onQrScanned` had no caller), so the real
bar was "walk 25 steps, wait 2 minutes, answer one question" and the NFC
tag was already fully bypassable.

It is now a strict, latched sequence owned by `DismissGate`:

    tag 1 -> 25 steps + 2 min -> tag 2 -> 3 challenges

Every stage is required, in that order, and a stage once passed never
reverts. That last part also fixes a real bug: `_onNfcTag` and `_trySteps`
both assigned the room-proof result straight onto `_inRoom`, so a wrong
tag tapped after the gate had passed sent the user back to the start.

Two *distinct* tags are required; the same id in both slots is refused at
registration, since one tag satisfying both stages removes the walk that
is the whole point. The existing single-tag registration migrates into
slot 1 rather than being stranded.

Answers are cumulative, not consecutive: a wrong one costs the attempt and
nothing else.

The bypass is the only fallback and is deliberately narrow. After 5 minutes
of ringing, or immediately when NFC is off or fewer than two distinct tags
are registered, the two *tag* stages are skipped and the count rises to 6.
The walk is never skipped — jumping straight to the challenges would let
someone lie in bed for five minutes and answer questions without standing
up, which is weaker than the gate this replaces. Without some such hatch a
dead tag at 06:00 leaves an alarm that cannot be switched off at all.

Also deletes the `flash` challenge outright, along with the now-orphaned
`generateCode`/`kDismissCodeLength`/`_dismissChars`. It was the only kind
that removed its prompt from the screen, which is unsolvable for someone
who has just walked in from another room.

The ring screen takes an injectable clock: `Stopwatch` reads the wall
clock, which `tester.pump()` does not advance, so without that seam no
widget test could reach the two-minute floor or the five-minute bypass.

Verified on the Pixel 6a: the existing tag migrated into slot 1 and both
rows render. 102 Dart tests pass; analyzer clean.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PcdngP688mW6rJye3LRkSN
2026-08-30 15:09:00 +02:00

113 lines
3.5 KiB
Dart

import 'package:wake_alarm_sync/services/alarm_scheduler.dart';
import 'package:wake_alarm_sync/services/room_proof_settings.dart';
/// Which mechanism satisfied a stage of the separate-room requirement.
enum RoomProofKind {
/// Tapped the registered NFC tag for the stage that was live.
nfc,
/// Walked far enough, for long enough, since the alarm started.
steps,
}
/// Result of a room-proof attempt.
class RoomProofResult {
/// Creates a result; [kind] is null when nothing has satisfied yet.
const RoomProofResult({required this.satisfied, this.kind, this.detail = ''});
/// Whether this particular check passed.
final bool satisfied;
/// Which mechanism satisfied it, or null if none did.
final RoomProofKind? kind;
/// Human-readable reason, shown on the ring screen.
final String detail;
}
/// The individual checks behind the dismiss sequence.
///
/// **Every check is required, in order** — see `DismissGate`, which owns the
/// sequencing. This class only answers "did this one thing pass right now".
///
/// Steps deliberately require elapsed time as well as a count: the phone is
/// stationary while you walk *to* it, so a bare step count can be faked by
/// shaking it in bed. The pair is unchanged from when steps were a fallback.
class RoomProof {
/// Creates a room-proof checker over [scheduler]'s platform channel.
RoomProof({
required this.scheduler,
this.settings = const RoomProofSettings(),
});
/// Platform bridge used to read the step counter.
final AlarmScheduler scheduler;
/// The registered tags, one per sequence stage.
final RoomProofSettings settings;
/// Steps required, counted since the alarm started ringing.
static const int requiredSteps = 25;
/// Elapsed time also required, so shaking the phone cannot pass.
static const Duration requiredElapsed = Duration(minutes: 2);
/// Whether [tagId] is the tag registered for [slot].
///
/// A tag registered for a *different* slot fails here: tapping tag 2 while
/// stage 1 is live must not advance anything, or the two locations collapse
/// into one.
RoomProofResult checkNfc(String tagId, {required int slot}) {
final expected = settings.tagAt(slot);
if (expected == null) {
return RoomProofResult(
satisfied: false,
detail: 'No tag ${slot + 1} registered in Settings',
);
}
if (tagId != expected) {
return RoomProofResult(
satisfied: false,
detail: 'Not tag ${slot + 1}',
);
}
return RoomProofResult(
satisfied: true,
kind: RoomProofKind.nfc,
detail: 'Tag ${slot + 1} matched',
);
}
/// Enough steps AND enough elapsed time since the ring started.
///
/// Both are required together. [elapsed] is passed in rather than measured
/// here so the ring screen owns the single clock.
Future<RoomProofResult> checkSteps(Duration elapsed) async {
final steps = await scheduler.stepsSinceRingStart();
if (steps < 0) {
return const RoomProofResult(
satisfied: false,
detail: 'Step sensor unavailable',
);
}
if (steps < requiredSteps) {
return RoomProofResult(
satisfied: false,
detail: '$steps/$requiredSteps steps',
);
}
if (elapsed < requiredElapsed) {
final left = requiredElapsed - elapsed;
return RoomProofResult(
satisfied: false,
detail: 'Keep moving for ${left.inSeconds}s more',
);
}
return const RoomProofResult(
satisfied: true,
kind: RoomProofKind.steps,
detail: 'Walked far enough, long enough',
);
}
}