mirror of
https://github.com/kuhyx/wake-alarm.git
synced 2026-11-26 07:07:14 +01:00
The shared pool (~/utils/freedays) was CLI-only: the gates on the PC stood down, but the phone kept ringing at 08:00 and there was nowhere to mark a day from bed. This is the one picker for every app. FreeDayPool is a Dart port of the Python rules with the identical record shape (id = ISO date; state/consumed/actor/reason on one tick; un-mark is a state flip, never a tombstone), verified by a real cross-language round trip: a log written here reads back through freedays.is_free_day and remaining() with the same verdicts and count, and the PC's live pool reads here as the same 27 of 35 that `freedays status` prints. The alarm fires from Kotlin with no engine running, so the pool is mirrored into a native SharedPreferences set after every mark, release and sync (channel method setFreeDays), and AlarmReceiver consults it before starting AlarmService. Fail closed: a missing or corrupt cache rings. All three branches exercised on the API 36 AVD by broadcasting the receiver. Firebase only for this namespace -- no GitHub mirror; the alarm record keeps its own because un-migrated installs depend on it. Known gap, documented rather than papered over: a day marked on the PC reaches the phone at the next app launch or picker open, not overnight. The ring path takes no background dependency by design, so mark on the phone, or open the app once after marking on the PC. Verified on the phone (release build, 23181JEGR08034): picker shows the budget, marks 2026-09-30 with a reason, pushes it to freedays-sync/devices/<uuid>, releases it with a full refund back to 35. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019pBgaXSJKyY3DarYpDatDF
249 lines
8.8 KiB
Dart
249 lines
8.8 KiB
Dart
import 'dart:async';
|
|
import 'dart:developer' as developer;
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:wake_alarm_sync/screens/free_days_screen.dart';
|
|
import 'package:wake_alarm_sync/screens/home_body.dart';
|
|
import 'package:wake_alarm_sync/screens/settings_screen.dart';
|
|
import 'package:wake_alarm_sync/services/alarm_policy.dart';
|
|
import 'package:wake_alarm_sync/services/alarm_publisher.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/free_day_pool.dart';
|
|
import 'package:wake_alarm_sync/services/free_day_sync.dart';
|
|
import 'package:wake_alarm_sync/services/sync_settings.dart';
|
|
|
|
/// Main screen: displays the current alarm time and syncs it to the PC via
|
|
/// the shared crdt_sync transport.
|
|
class HomeScreen extends StatefulWidget {
|
|
/// Creates a [HomeScreen].
|
|
const HomeScreen({super.key});
|
|
|
|
@override
|
|
State<HomeScreen> createState() => _HomeScreenState();
|
|
}
|
|
|
|
class _HomeScreenState extends State<HomeScreen> {
|
|
int _hour = AlarmPolicy.baselineHour;
|
|
int _minute = AlarmPolicy.baselineMinute;
|
|
|
|
/// True while a one-shot earlier start is in force for the next alarm.
|
|
bool _isOverridden = false;
|
|
bool _busy = false;
|
|
String _status = '';
|
|
bool _configured = false;
|
|
int _syncEpoch = 0;
|
|
AlarmSyncService? _service;
|
|
|
|
/// Platform bridge that arms the app's own alarm (item 1).
|
|
final AlarmPublisher _publisher = AlarmPublisher();
|
|
|
|
/// Platform bridge used to arm the fixed schedule.
|
|
final AlarmScheduler _scheduler = AlarmScheduler();
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
// STRICTLY SEQUENTIAL. These three used to run concurrently, and the
|
|
// last one reads what the first two write: `publish` takes the service as
|
|
// a *parameter*, so it captured `_service` while it was still null and
|
|
// every startup publish returned "connect a backend" while a backend was
|
|
// connected. `_applyPolicy` has the same shape -- publishing before it
|
|
// resolves would send the baseline instead of an active override.
|
|
unawaited(_bootstrapSync());
|
|
}
|
|
|
|
/// Resolves the policy and the sync store, then publishes -- in that order.
|
|
///
|
|
/// Publishing to the PC is automatic: it used to require the same tap that
|
|
/// armed the alarm, so a forgotten tap left the PC on its fallback.
|
|
Future<void> _bootstrapSync() async {
|
|
await _applyPolicy();
|
|
await _loadSyncState();
|
|
await _syncToPc();
|
|
await _refreshFreeDays();
|
|
}
|
|
|
|
/// Pulls the shared free-day pool and hands it to the native ring path.
|
|
///
|
|
/// Last, and silent: a day marked on the PC reaches the phone here, and
|
|
/// nothing on this screen reports it -- the pool is only ever consulted,
|
|
/// never announced. Failures are logged by [FreeDaySync] and swallowed;
|
|
/// an unreadable pool means the alarm rings as normal.
|
|
Future<void> _refreshFreeDays() async {
|
|
try {
|
|
final pool = await FreeDayPool.open();
|
|
await FreeDaySync().sync(pool);
|
|
} on Exception catch (e, stack) {
|
|
developer.log(
|
|
'free-day pool unavailable: $e',
|
|
name: 'WakeAlarmSync',
|
|
error: e,
|
|
stackTrace: stack,
|
|
);
|
|
}
|
|
}
|
|
|
|
/// Opens the sync store and records whether a backend is connected.
|
|
///
|
|
/// No longer loads a *time*: the schedule is baked in and owned by
|
|
/// [AlarmPolicy], so a stored value could only disagree with it. This exists
|
|
/// to supply the store for publishing and to drive the "connect a backend"
|
|
/// hint.
|
|
Future<void> _loadSyncState() async {
|
|
final SyncSettings settings;
|
|
final AlarmSyncService service;
|
|
try {
|
|
settings = await SyncSettings.load();
|
|
service = await AlarmSyncService.open();
|
|
} on Exception catch (e, stack) {
|
|
// Storage/plugins unavailable (e.g. under a widget test, or a keystore
|
|
// hiccup): keep the on-screen defaults rather than crashing. Logged
|
|
// rather than swallowed -- silence here is what made a broken store
|
|
// look like an unconfigured backend for weeks.
|
|
developer.log(
|
|
'sync store unavailable: $e',
|
|
name: 'WakeAlarmSync',
|
|
error: e,
|
|
stackTrace: stack,
|
|
);
|
|
return;
|
|
}
|
|
// Either backend counts: a device connected only to Firebase is fully
|
|
// configured, and gating on the GitHub token alone would lock it out of
|
|
// syncing entirely once the mirror is retired.
|
|
final firebaseAccount = await loadAccount();
|
|
if (!mounted) return;
|
|
_service = service;
|
|
setState(
|
|
() => _configured = settings.isConfigured || firebaseAccount != null,
|
|
);
|
|
}
|
|
|
|
/// Lets the user move **tomorrow's** alarm earlier, never later.
|
|
///
|
|
/// This is the only schedule edit the app offers. Earlier-only is what makes
|
|
/// it tamper-proof: every change available costs the user sleep, so there is
|
|
/// no lever a half-asleep user can pull to stay in bed. A later time, a
|
|
/// skipped day and an off switch are simply not expressible.
|
|
Future<void> _pickEarlier() async {
|
|
final current = _fmt(_hour, _minute);
|
|
final picked = await showTimePicker(
|
|
context: context,
|
|
initialTime: TimeOfDay(hour: _hour, minute: _minute),
|
|
helpText: 'Earlier than $current only',
|
|
);
|
|
if (picked == null) return;
|
|
final accepted = await AlarmPolicy.setEarlier(picked.hour, picked.minute);
|
|
if (!mounted) return;
|
|
if (!accepted) {
|
|
setState(
|
|
() => _status =
|
|
'That is not earlier than $current. The alarm can only be '
|
|
'moved earlier.',
|
|
);
|
|
return;
|
|
}
|
|
await _applyPolicy();
|
|
if (!mounted) return;
|
|
setState(() => _status = 'Armed for ${_fmt(_hour, _minute)} tomorrow.');
|
|
// The PC has to learn about the new time too. Without this the only
|
|
// publish attempt was the one at startup, so an alarm moved earlier here
|
|
// never reached the PC at all.
|
|
await _syncToPc();
|
|
}
|
|
|
|
/// Re-reads the policy and arms the alarm to match it.
|
|
Future<void> _applyPolicy() async {
|
|
final policy = await AlarmPolicy.load();
|
|
await _scheduler.armSchedule(policy.hour, policy.minute);
|
|
if (!mounted) return;
|
|
setState(() {
|
|
_hour = policy.hour;
|
|
_minute = policy.minute;
|
|
_isOverridden = policy.isOverridden;
|
|
});
|
|
}
|
|
|
|
Future<void> _syncToPc() async {
|
|
setState(() {
|
|
_busy = true;
|
|
_status = 'Syncing to PC…';
|
|
});
|
|
final message = await _publisher.publish(
|
|
service: _service,
|
|
hour: _hour,
|
|
minute: _minute,
|
|
);
|
|
if (!mounted) return;
|
|
setState(() {
|
|
_status = message;
|
|
_busy = false;
|
|
_syncEpoch++;
|
|
});
|
|
}
|
|
|
|
/// The baseline time, shown when a one-shot override is active.
|
|
String get _baseline =>
|
|
_fmt(AlarmPolicy.baselineHour, AlarmPolicy.baselineMinute);
|
|
String _fmt(int h, int m) =>
|
|
'${h.toString().padLeft(2, '0')}:${m.toString().padLeft(2, '0')}';
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Wake Alarm Sync'),
|
|
actions: [
|
|
IconButton(
|
|
icon: Icon(
|
|
Icons.event_available,
|
|
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
|
),
|
|
tooltip: 'Free days',
|
|
onPressed: () => Navigator.push<void>(
|
|
context,
|
|
MaterialPageRoute<void>(builder: (_) => const FreeDaysScreen()),
|
|
),
|
|
),
|
|
IconButton(
|
|
icon: Icon(
|
|
Icons.settings,
|
|
// Dimmed relative to the adjacent title text (rule 28).
|
|
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
|
),
|
|
onPressed: () async {
|
|
await Navigator.push<void>(
|
|
context,
|
|
MaterialPageRoute<void>(builder: (_) => const SettingsScreen()),
|
|
);
|
|
// Publish too, not just reload: a tag registered in Settings
|
|
// reaches the backend now, not on the next cold start.
|
|
await _loadSyncState();
|
|
await _syncToPc();
|
|
},
|
|
),
|
|
],
|
|
),
|
|
// SafeArea + scroll. This was a bare Center/Padding/Column, which
|
|
// cannot scroll, so the content simply overflowed with the black-and-
|
|
// yellow stripe pattern painted over the primary control. Two ways that
|
|
// happened: phone landscape leaves only ~360px of body height for ~405px
|
|
// of content, and the status line renders an unbounded raw
|
|
// `'Error: \$e'` with no maxLines, which overflows even in portrait.
|
|
body: HomeBody(
|
|
time: _fmt(_hour, _minute),
|
|
baseline: _baseline,
|
|
isOverridden: _isOverridden,
|
|
busy: _busy,
|
|
status: _status,
|
|
configured: _configured,
|
|
syncEpoch: _syncEpoch,
|
|
onPickEarlier: () => unawaited(_pickEarlier()),
|
|
),
|
|
);
|
|
}
|
|
}
|