wake-alarm/phone_app/lib/services/session_handoff.dart
Krzysztof kuhy Rudnicki cf0d243ffd Count a paused alarm as a bypass
The launcher "Wstrzymaj aplikację" paused WakeAlarm mid-ring on 11-20 and
11-22. WakeAlarm is now a device admin, and a pause, admin removal,
revoked notifications or full-screen intent, or a killed ring on a live
morning is a bypass: a miss on any weekday, the phone rings again, and
the PC rings at once until a newer dismiss or 30 min.

The PC rings also go to every connected monitor, since the speakers are
off at the knob overnight; a one-shot silent probe checks them at A-5.

DOCS-bypass.md has the layers, causes and measurements.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 11:49:44 +01:00

82 lines
3.2 KiB
Dart

import 'dart:async';
import 'package:wake_alarm_sync/services/alarm_scheduler.dart';
import 'package:wake_alarm_sync/services/enforcement_publish.dart';
import 'package:wake_alarm_sync/services/morning_session.dart';
import 'package:wake_alarm_sync/services/session_rules.dart';
import 'package:wake_alarm_sync/services/session_store.dart';
import 'package:wake_alarm_sync/services/session_tag_settings.dart';
/// What the ring screen does once the dismiss sequence is satisfied.
///
/// The ring no longer ends the morning. The first dismiss of the day starts
/// the session; the dismiss of a re-ring resumes it, one re-ring richer.
/// Either way the ring screen hands over to the session screen instead of
/// closing the activity — the activity is what keeps NFC and the screen on.
class SessionHandoff {
/// Creates a handoff; every dependency is injectable for tests.
SessionHandoff({
required this.scheduler,
SessionStore? store,
DateTime Function()? now,
Future<bool> Function()? publishNow,
}) : store = store ?? SessionStore(),
_now = now ?? DateTime.now,
_publishNow = publishNow ?? publishEnforcementNow;
/// Platform bridge, for the NFC availability check.
final AlarmScheduler scheduler;
/// Session persistence.
final SessionStore store;
final DateTime Function() _now;
/// Pushes the `dismissed` event at once: the PC rings for a bypass until
/// it reads one newer than the bypass.
final Future<bool> Function() _publishNow;
/// Starts or resumes today's session; false when the ring should just end.
///
/// [away] is the ring's verdict that the phone is not at home: no scale,
/// no tags, no session. A live session from a *previous* day is archived
/// as it stands (failed, since it never finished) rather than resumed —
/// yesterday's clocks would re-ring the moment the screen came up.
Future<bool> afterDismiss({required bool away}) async {
// Reaching this call at all means the dismiss gate was satisfied --
// recorded before anything else so an interrupted await below still
// leaves the native side knowing the ring was answered.
await scheduler.markRingDismissed();
// Not awaited: the session must not wait on the network.
unawaited(_publishNow());
final now = _now();
final today = SessionRules.dateKey(now);
final current = await store.loadCurrent();
if (current != null && current.date == today) {
if (!current.isActive) return false;
current.recordReRing(now);
await store.save(current);
return true;
}
if (current != null) {
current
..outcome = SessionOutcome.failed
..updatedAt = now;
await store.archive(current);
}
if (away) return false;
// One session per day. A ring after today's has finished (a probe, or
// the alarm moved earlier and rung twice) must not start the morning
// over -- the weigh-in and the walk have been done.
final history = await store.history();
if (history.any((s) => s.date == today)) return false;
final session = SessionRules.start(
now: now,
tags: await SessionTagSettings.load(),
nfcAvailable: await scheduler.hasNfc(),
);
await store.save(session);
return true;
}
}