mirror of
https://github.com/kuhyx/wake-alarm.git
synced 2026-11-22 05:42:19 +01:00
Dart side of the alarm (items 1, 4, 3). main.dart had no init seam at all; it now has ensureInitialized(), a navigatorKey and a /ring route. - challenge.dart: all five kinds ported from _challenges.py so both halves behave identically. kDismissCodeLength/kDismissFlashSeconds are duplicated from _constants.py, not synced — noted in the file, since Dart cannot read Python. - alarm_scheduler.dart: the Dart side of the single MethodChannel. - ring_screen.dart: room proof first, then a challenge, then dismiss. Back is a no-op (PopScope) so the alarm cannot be escaped. - room_proof.dart: NFC or QR satisfies on its own; steps NEVER do alone — the phone is stationary while you walk to it, so steps only accrue after pickup and can be faked by shaking it in bed. Steps count only together with elapsed time, as the fallback when both others fail. - home_screen: arms the app's own alarm instead of asking the user to confirm in the Clock app (item 1's "no user interaction"). SET_ALARM stays as redundancy with SKIP_UI=true. Re-arm has three hooks and needs all three: startup, BOOT_COMPLETED, and dismiss. The plan assumed LogStore.changes would cover this; that stream does not exist in crdt_sync_dart (verified — the library has no Stream at all), so the picker's save is only one hook and a restart would otherwise arm nothing. Fixed a real gap the API 36 AVD exposed: POST_NOTIFICATIONS was never requested at runtime, so a fresh install sat at importance=NONE and the full-screen intent was rejected outright — the alarm would ring with no dismiss screen. MainActivity now requests it, and canUseFullScreenIntent() also checks areNotificationsEnabled() so the check gates rather than warns. Verified on the API 36 AVD: screen Asleep -> Awake, AlarmActivity became the top resumed activity over the lockscreen, service isForeground=true types=0x2 (mediaPlayback) category=alarm, no crash. flutter test: 54 pass. dart analyze: clean. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EckWmyfetYq3Mi8MAnUudh
148 lines
4.5 KiB
Dart
148 lines
4.5 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/challenge.dart';
|
|
import 'package:wake_alarm_sync/services/room_proof.dart';
|
|
|
|
/// The dismiss screen shown over the lockscreen while the alarm rings.
|
|
///
|
|
/// Two gates, in order: prove you are in the other room, then solve a
|
|
/// challenge. Only then does the sound stop.
|
|
class RingScreen extends StatefulWidget {
|
|
/// Creates the ring screen; both dependencies are injectable for tests.
|
|
const RingScreen({super.key, this.scheduler, this.generator});
|
|
|
|
/// Platform bridge; defaults to the real MethodChannel.
|
|
final AlarmScheduler? scheduler;
|
|
|
|
/// Challenge source; defaults to a secure-random generator.
|
|
final ChallengeGenerator? generator;
|
|
|
|
@override
|
|
State<RingScreen> createState() => _RingScreenState();
|
|
}
|
|
|
|
class _RingScreenState extends State<RingScreen> {
|
|
late final AlarmScheduler _scheduler = widget.scheduler ?? AlarmScheduler();
|
|
late final ChallengeGenerator _generator =
|
|
widget.generator ?? ChallengeGenerator();
|
|
late final RoomProof _proof = RoomProof(scheduler: _scheduler);
|
|
late Challenge _challenge = _generator.next();
|
|
|
|
final TextEditingController _input = TextEditingController();
|
|
final Stopwatch _elapsed = Stopwatch()..start();
|
|
|
|
/// Whether the room proof has been satisfied; the challenge is gated on it.
|
|
bool _inRoom = false;
|
|
String _status = '';
|
|
|
|
/// Hidden after [kDismissFlashSeconds] for the flash challenge only.
|
|
bool _codeVisible = true;
|
|
Timer? _flashTimer;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
unawaited(_scheduler.beginStepCount());
|
|
_startFlashTimerIfNeeded();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_flashTimer?.cancel();
|
|
_input.dispose();
|
|
_elapsed.stop();
|
|
super.dispose();
|
|
}
|
|
|
|
void _startFlashTimerIfNeeded() {
|
|
if (_challenge.kind != ChallengeKind.flash) return;
|
|
_flashTimer?.cancel();
|
|
_flashTimer = Timer(const Duration(seconds: kDismissFlashSeconds), () {
|
|
if (mounted) setState(() => _codeVisible = false);
|
|
});
|
|
}
|
|
|
|
Future<void> _trySteps() async {
|
|
final result = await _proof.checkSteps(_elapsed.elapsed);
|
|
if (!mounted) return;
|
|
setState(() {
|
|
_inRoom = result.satisfied;
|
|
_status = result.detail;
|
|
});
|
|
}
|
|
|
|
void _submit() {
|
|
if (!_challenge.accepts(_input.text)) {
|
|
setState(() {
|
|
_status = 'Wrong — here is another one';
|
|
_challenge = _generator.next();
|
|
_codeVisible = true;
|
|
_input.clear();
|
|
});
|
|
_startFlashTimerIfNeeded();
|
|
return;
|
|
}
|
|
unawaited(_scheduler.dismiss());
|
|
setState(() => _status = 'Dismissed');
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final theme = Theme.of(context);
|
|
return PopScope(
|
|
// The alarm cannot be escaped with back; dismissing is the only way out.
|
|
canPop: false,
|
|
child: Scaffold(
|
|
body: SafeArea(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(24),
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: <Widget>[
|
|
Text('Wake up', style: theme.textTheme.headlineMedium),
|
|
const SizedBox(height: 24),
|
|
if (!_inRoom) ..._roomGate(theme) else ..._challengeGate(theme),
|
|
const SizedBox(height: 16),
|
|
Text(_status, style: theme.textTheme.bodyMedium),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
List<Widget> _roomGate(ThemeData theme) => <Widget>[
|
|
Text(
|
|
'Go to the other room: tap the NFC tag, scan the QR code, '
|
|
'or walk ${RoomProof.requiredSteps} steps.',
|
|
textAlign: TextAlign.center,
|
|
style: theme.textTheme.bodyLarge,
|
|
),
|
|
const SizedBox(height: 16),
|
|
FilledButton(onPressed: _trySteps, child: const Text('Check steps')),
|
|
];
|
|
|
|
List<Widget> _challengeGate(ThemeData theme) => <Widget>[
|
|
Text(_challenge.hint, style: theme.textTheme.bodyLarge),
|
|
const SizedBox(height: 12),
|
|
Text(
|
|
_codeVisible ? _challenge.display : '••••••',
|
|
textAlign: TextAlign.center,
|
|
style: theme.textTheme.displaySmall,
|
|
),
|
|
const SizedBox(height: 16),
|
|
TextField(
|
|
controller: _input,
|
|
autofocus: true,
|
|
textCapitalization: TextCapitalization.characters,
|
|
decoration: const InputDecoration(labelText: 'Answer'),
|
|
onSubmitted: (_) => _submit(),
|
|
),
|
|
const SizedBox(height: 16),
|
|
FilledButton(onPressed: _submit, child: const Text('Dismiss')),
|
|
];
|
|
}
|