wake-alarm/phone_app/lib/screens/room_proof_screen.dart
Krzysztof kuhy Rudnicki dc2d54668b Phase 4/3: close the ring screen on dismiss, and make NFC registerable
Two defects that each defeated the alarm in a different way:

1. Nothing finished AlarmActivity after a correct answer. Sound stopped and
   the alarm re-armed, but the dismiss screen stayed up over the lockscreen
   forever — back is disabled in both halves by design — and the live instance
   kept _inRoom=true with the solved answer still typed, so the next ring
   could reuse an already-passed gate. AlarmActivity now handles
   closeRingScreen; the shared AlarmChannel deliberately holds
   applicationContext and cannot finish an activity, hence the split handler.

2. Nothing could write roomProof.nfcTagId — there was no registration UI — so
   checkNfc always answered "no tag registered" and checkQr was never called.
   Functionally that left steps as the only satisfiable proof, which is
   exactly the state the decision rules out ("NFC or QR qualifies; steps alone
   does NOT") and removes the redundancy whose purpose is that one failing
   mechanism at 06:00 must never block dismissal. Added RoomProofScreen
   (reader mode from MainActivity, since registration needs an Activity) and
   linked it from Settings.

Also: StepCounter kept one listener instead of registering a fresh one per
ring and never unregistering, and the ring screen drops its NFC handler on
dispose.

New tests cover what had been dead code: a registered tag satisfies the proof
and reveals the challenge, a foreign tag does not, and a correct answer
invokes BOTH dismiss and closeRingScreen. The emulator cannot reach the
dismiss path at all (no step sensor, no NFC), which is why it is covered by
widget tests rather than claimed from the AVD run.

Documented ringNow/main_ring_probe.dart in AGENTS.md as a deliberate manual
test handle, the phone-side equivalent of wake-alarm.service.

flutter test: 69 pass. dart analyze lib/ test/: clean. pre-commit: passed.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EckWmyfetYq3Mi8MAnUudh
2026-08-27 21:39:01 +02:00

117 lines
3.5 KiB
Dart

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 separate-room proofs: the NFC tag and the printed QR code.
///
/// Without a registered tag, NFC can never satisfy the room proof and the
/// steps fallback becomes the only way to dismiss — which is exactly the
/// single-mechanism dependency the redundancy exists to avoid.
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();
final TextEditingController _qr = TextEditingController();
String? _tagId;
bool _scanning = false;
String _status = '';
@override
void initState() {
super.initState();
unawaited(_load());
}
@override
void dispose() {
if (_scanning) unawaited(_scheduler.stopTagRegistration());
_scheduler.clearNfcListener();
_qr.dispose();
super.dispose();
}
Future<void> _load() async {
final settings = await RoomProofSettings.load();
if (!mounted) return;
setState(() {
_tagId = settings.nfcTagId;
_qr.text = settings.qrPayload ?? '';
});
}
Future<void> _startScan() async {
_scheduler.onNfcTag(_onTag);
final started = await _scheduler.startTagRegistration();
if (!mounted) return;
setState(() {
_scanning = started;
_status = started
? 'Tap the tag against the phone…'
: 'NFC is unavailable or switched off.';
});
}
Future<void> _onTag(String tagId) async {
await RoomProofSettings.saveNfcTag(tagId);
await _scheduler.stopTagRegistration();
if (!mounted) return;
setState(() {
_tagId = tagId;
_scanning = false;
_status = 'Tag registered.';
});
}
Future<void> _saveQr() async {
await RoomProofSettings.saveQrPayload(_qr.text.trim());
if (!mounted) return;
setState(() => _status = 'QR code saved.');
}
@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>[
Text('NFC tag: ${_tagId ?? "none registered"}'),
const SizedBox(height: 12),
FilledButton(
onPressed: _scanning ? null : _startScan,
child: Text(_scanning ? 'Waiting for tag…' : 'Register NFC tag'),
),
const SizedBox(height: 32),
TextField(
controller: _qr,
decoration: const InputDecoration(
labelText: 'QR code text',
helperText: 'Text encoded in the code printed for that room',
),
),
const SizedBox(height: 12),
FilledButton(onPressed: _saveQr, child: const Text('Save QR code')),
const SizedBox(height: 24),
Text(_status),
],
),
),
);
}
}
/// Local helper so this file need not import dart:async for fire-and-forget.
void unawaited(Future<void> future) {}