wake-alarm/phone_app/lib/services/alarm_publisher.dart
Krzysztof kuhy Rudnicki 38fb4f3763 Add motion tokens and haptics to the phone app
Motion tokens hand-copied from DOCS-motion.md into theme.dart; every
page route goes through appRoute() (slow, decelerate in, accelerate
out, instant under Remove animations). mediumImpact on the picker
confirm, heavyImpact on a refused time. The Clock app line shows only
when the SET_ALARM handoff finished before its 3 s timeout. Sound is
skipped on purpose.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 08:09:48 +01:00

72 lines
3.2 KiB
Dart

import 'package:wake_alarm_sync/services/alarm_arming.dart';
import 'package:wake_alarm_sync/services/alarm_scheduler.dart';
import 'package:wake_alarm_sync/services/alarm_sync_service.dart';
import 'package:wake_alarm_sync/services/firebase_backend.dart';
import 'package:wake_alarm_sync/services/settings_sync.dart';
import 'package:wake_alarm_sync/services/sync_settings.dart';
/// Publishes the armed alarm to the PC and arms the phone's own alarm.
///
/// Split out of `home_screen.dart` for the 250-line gate, and because none of
/// this is UI: it returns a message for the screen to render rather than
/// calling `setState` itself, which also makes it testable without a widget.
class AlarmPublisher {
/// Creates a publisher; [arming] is injectable for tests.
AlarmPublisher({AlarmArming? arming})
: arming = arming ?? AlarmArming(AlarmScheduler());
/// Bridge that arms the phone's own alarm.
final AlarmArming arming;
/// Publishes [hour]:[minute] and arms the alarm, returning a status line.
///
/// Never throws: this runs unattended from the app's init seam, so a sync
/// failure must degrade to a message rather than take the screen down. The
/// alarm itself is already armed by `main()` regardless of what happens
/// here — publishing is how the PC learns the time, not how the phone rings.
Future<String> publish({
required AlarmSyncService? service,
required int hour,
required int minute,
}) async {
final settings = await SyncSettings.load();
// syncBackend() falls back to GitHub alone, or pushes to Firebase alone
// via the mirror, so either credential is enough to proceed.
final hasFirebase = await loadAccount() != null;
if (!settings.isConfigured && !hasFirebase) {
return 'Connect a sync backend in Settings first.';
}
// Distinct from the above on purpose. Both used to report "connect a
// backend", so a store that failed to open looked like a configuration
// the user had never done -- and the advice was to fix something that
// was not broken.
if (service == null) {
return 'Sync store unavailable — reopen the app.';
}
try {
await service.setLocal(hour, minute);
// Tags and home ride on the same record: stage local edits, sync
// (pull, merge, push), then take whatever won back into prefs. That
// last step is how a fresh install gets its tags back.
await stageLocalSettings(service);
await service.push(settings.token);
await applySyncedSettings(service);
final problem = await arming.arm(hour, minute);
if (problem != null) return problem;
final inClock = await arming.registerWithClockApp(hour, minute);
return syncedStatus(hour, minute, inClock: inClock);
} on Exception catch (e) {
return 'Error: $e';
}
}
}
/// The success line; names the Clock app only when the handoff to it
/// completed before its timeout, so a silent timeout never claims it.
String syncedStatus(int hour, int minute, {required bool inClock}) {
final time =
'${hour.toString().padLeft(2, '0')}:'
'${minute.toString().padLeft(2, '0')}';
return inClock ? 'Synced to PC ✓ Clock app set $time ✓' : 'Synced to PC ✓';
}