mirror of
https://github.com/kuhyx/wake-alarm.git
synced 2026-11-27 08:29:19 +01:00
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
63 lines
2.7 KiB
Dart
63 lines
2.7 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;
|
|
await arming.registerWithClockApp(hour, minute);
|
|
return 'Synced to PC ✓';
|
|
} on Exception catch (e) {
|
|
return 'Error: $e';
|
|
}
|
|
}
|
|
}
|