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 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; }