wake-alarm/phone_app/lib/services/alarm_publisher.dart
Krzysztof kuhy Rudnicki 1c8a40d410 Split _sinks.py and home_screen.dart under the 250-line gate
_sinks.py 341 -> 249: the pactl *parsing* moves to _sink_parsing.py. That half
is pure text handling with no subprocess and no side effects, so it is
testable without touching the host's audio; _sinks.py keeps command execution
and the activate/restore orchestration.

The parsers are re-imported into _sinks under their original names, guarded by
__all__. The suite patches `wake_alarm._sinks._parse_cards` and friends, and a
moved symbol makes patch() stop intercepting *silently* — the test then
asserts nothing and still passes. Ruff proved the point mid-split: shortening
__all__ let it delete two of the re-exports and broke an import outright.
_parse_profile_line is only imported (never patched), so its test now imports
from the new module instead.

home_screen.dart 270 -> 249. This one was mine — the automatic-alarm change
pushed it over and I claimed in TODO-push-and-gate.md that nothing this
session touched was over the limit; that was wrong. Publishing moves to
AlarmPublisher, which returns a status line instead of calling setState, so it
is testable without a widget. _loadSavedTime became _loadSyncState: it no
longer loads a *time*, because the schedule is baked in and owned by
AlarmPolicy, so a stored value could only disagree with it.

465 Python at 100% coverage, 79 Dart, APK builds, and both documented CLIs
still print real output.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EckWmyfetYq3Mi8MAnUudh
2026-08-27 23:30:02 +02:00

50 lines
2.1 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/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) || service == null) {
return 'Connect a sync backend in Settings first.';
}
try {
await service.setLocal(hour, minute);
await service.push(settings.token);
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';
}
}
}