wake-alarm/phone_app/lib/services/alarm_publisher.dart
Krzysztof kuhy Rudnicki f0de01f9aa
Some checks are pending
dependency freshness / dependencies on newest stable (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (mypy, mypy, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (pylint, pylint, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (rest, , mypy,pylint,free-days-interop) (push) Waiting to run
Tests / test (push) Waiting to run
Tests / free-days-interop (push) Waiting to run
release-apk / release (APK) (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
Say Armed for HH:MM only after a real arm
The home screen prefixed it to every publish() result, including the
full-screen-intent refusal and sync errors.

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

74 lines
3.3 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, naming the armed time; names the Clock app only when
/// the handoff to it completed before its timeout, so a silent timeout
/// never claims it. Only reached after `arm()` succeeded.
String syncedStatus(int hour, int minute, {required bool inClock}) {
final time =
'${hour.toString().padLeft(2, '0')}:'
'${minute.toString().padLeft(2, '0')}';
final synced = 'Armed for $time. Synced to PC ✓';
return inClock ? '$synced Clock app set ✓' : synced;
}