mirror of
https://github.com/kuhyx/wake-alarm.git
synced 2026-11-24 22:46:20 +01:00
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
132 lines
4.1 KiB
Dart
132 lines
4.1 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:wake_alarm_sync/services/alarm_scheduler.dart';
|
|
import 'package:wake_alarm_sync/services/room_proof_settings.dart';
|
|
|
|
/// Registers the two NFC tags the dismiss sequence walks between.
|
|
///
|
|
/// Both slots must hold **different** tags. With fewer than two distinct tags
|
|
/// the tag stages are unsatisfiable, so the ring screen falls straight through
|
|
/// to the bypass — a weaker morning, not a locked-out one.
|
|
class RoomProofScreen extends StatefulWidget {
|
|
/// Creates the screen; [scheduler] is injectable for tests.
|
|
const RoomProofScreen({super.key, this.scheduler});
|
|
|
|
/// Platform bridge used to drive NFC reader mode.
|
|
final AlarmScheduler? scheduler;
|
|
|
|
@override
|
|
State<RoomProofScreen> createState() => _RoomProofScreenState();
|
|
}
|
|
|
|
class _RoomProofScreenState extends State<RoomProofScreen> {
|
|
late final AlarmScheduler _scheduler = widget.scheduler ?? AlarmScheduler();
|
|
|
|
RoomProofSettings _settings = const RoomProofSettings();
|
|
int? _scanningSlot;
|
|
String _status = '';
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
unawaited(_load());
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
if (_scanningSlot != null) unawaited(_scheduler.stopTagRegistration());
|
|
_scheduler.clearNfcListener();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _load() async {
|
|
final settings = await RoomProofSettings.load();
|
|
if (!mounted) return;
|
|
setState(() => _settings = settings);
|
|
}
|
|
|
|
Future<void> _startScan(int slot) async {
|
|
_scheduler.onNfcTag((tagId) => unawaited(_onTag(slot, tagId)));
|
|
final started = await _scheduler.startTagRegistration();
|
|
if (!mounted) return;
|
|
setState(() {
|
|
_scanningSlot = started ? slot : null;
|
|
_status = started
|
|
? 'Tap tag ${slot + 1} against the phone…'
|
|
: 'NFC is unavailable or switched off.';
|
|
});
|
|
}
|
|
|
|
Future<void> _onTag(int slot, String tagId) async {
|
|
// The same tag in both slots would let one tap satisfy both stages, which
|
|
// removes the walk between them — the entire point of having two.
|
|
final other = _settings.tagAt(slot == 0 ? 1 : 0);
|
|
if (tagId == other) {
|
|
await _scheduler.stopTagRegistration();
|
|
if (!mounted) return;
|
|
setState(() {
|
|
_scanningSlot = null;
|
|
_status = 'That is already tag ${slot == 0 ? 2 : 1}. Use a different '
|
|
'tag, in a different room.';
|
|
});
|
|
return;
|
|
}
|
|
await RoomProofSettings.saveNfcTagAt(slot, tagId);
|
|
final settings = await RoomProofSettings.load();
|
|
await _scheduler.stopTagRegistration();
|
|
if (!mounted) return;
|
|
setState(() {
|
|
_settings = settings;
|
|
_scanningSlot = null;
|
|
_status = 'Tag ${slot + 1} registered.';
|
|
});
|
|
}
|
|
|
|
Widget _slotRow(int slot) {
|
|
final id = _settings.tagAt(slot);
|
|
final scanning = _scanningSlot == slot;
|
|
return Padding(
|
|
padding: const EdgeInsets.only(bottom: 24),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: <Widget>[
|
|
Text('Tag ${slot + 1}: ${id ?? "none registered"}'),
|
|
const SizedBox(height: 8),
|
|
FilledButton(
|
|
onPressed: _scanningSlot != null ? null : () => _startScan(slot),
|
|
child: Text(
|
|
scanning
|
|
? 'Waiting for tag…'
|
|
: 'Register tag ${slot + 1}${id == null ? "" : " again"}',
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(title: const Text('Room proof')),
|
|
body: Padding(
|
|
padding: const EdgeInsets.all(24),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: <Widget>[
|
|
const Text(
|
|
'Put the two tags in different rooms. Dismissing the alarm '
|
|
'means tapping tag 1, walking, then tapping tag 2.',
|
|
),
|
|
const SizedBox(height: 24),
|
|
for (var i = 0; i < RoomProofSettings.requiredTagCount; i++)
|
|
_slotRow(i),
|
|
Text(_status),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|