wake-alarm/phone_app/lib/screens/free_days_screen.dart
Krzysztof kuhy Rudnicki 146c427d6d Add the free-day picker, and stop the phone ringing on a free day
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
2026-09-11 13:45:42 +02:00

177 lines
5.2 KiB
Dart

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:wake_alarm_sync/screens/free_days_body.dart';
import 'package:wake_alarm_sync/services/free_day_pool.dart';
import 'package:wake_alarm_sync/services/free_day_sync.dart';
/// The one picker for the shared free-day pool.
///
/// A day marked here stands down every gate app at once — the PC lockers,
/// diet guard, leetcode guard, home guard, and this alarm — and none of them
/// mentions it afterwards. There is no notification, no low-budget warning
/// and no prompt at ring time; you find out how many are left by opening
/// this screen.
///
/// Syncs on open and after every change. A day marked on the PC reaches the
/// phone the next time this app launches or this screen opens — the ring
/// path takes no background dependency, so an overnight PC mark can miss
/// the morning ring. Marking on the phone is immediate.
class FreeDaysScreen extends StatefulWidget {
/// Creates the picker; the seams are injectable for tests.
const FreeDaysScreen({this.openPool, this.syncer, this.now, super.key});
/// Opens the pool; defaults to the on-disk one.
final Future<FreeDayPool> Function()? openPool;
/// Syncs the pool; defaults to Firebase.
final FreeDaySync? syncer;
/// The reference date, for tests.
final DateTime Function()? now;
@override
State<FreeDaysScreen> createState() => _FreeDaysScreenState();
}
class _FreeDaysScreenState extends State<FreeDaysScreen> {
FreeDayPool? _pool;
bool _busy = true;
String _status = '';
late final FreeDaySync _syncer = widget.syncer ?? FreeDaySync();
DateTime get _today =>
FreeDayPool.dateOnly(widget.now?.call() ?? DateTime.now());
@override
void initState() {
super.initState();
unawaited(_load());
}
Future<void> _load() async {
try {
_pool = await (widget.openPool ?? FreeDayPool.open)();
} on Exception catch (e) {
if (!mounted) return;
setState(() {
_busy = false;
_status = 'Error: pool unavailable ($e)';
});
return;
}
await _sync();
}
Future<void> _sync() async {
final pool = _pool;
if (pool == null) return;
setState(() {
_busy = true;
_status = 'Syncing…';
});
final message = await _syncer.sync(pool);
if (!mounted) return;
setState(() {
_busy = false;
_status = message;
});
}
Future<void> _pickDay() async {
final pool = _pool;
if (pool == null) return;
final today = _today;
final picked = await showDatePicker(
context: context,
initialDate: today,
firstDate: today,
lastDate: DateTime(today.year + 1, 12, 31),
helpText: 'Take a free day',
selectableDayPredicate: (day) => !pool.isFree(day),
);
if (picked == null || !mounted) return;
final reason = await _askReason();
if (!mounted) return;
await _apply(() => pool.mark(picked, reason: reason ?? '', now: today));
}
/// Optional, by design. Enter with an empty field and nothing is stored.
Future<String?> _askReason() {
final controller = TextEditingController();
return showDialog<String>(
context: context,
builder: (context) => AlertDialog(
title: const Text('Reason (optional)'),
content: TextField(
controller: controller,
autofocus: true,
decoration: const InputDecoration(hintText: 'e.g. travelling'),
),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(controller.text),
child: const Text('Mark free'),
),
],
),
);
}
Future<void> _release(DateTime day) async {
final pool = _pool;
if (pool == null) return;
await _apply(() => pool.release(day, now: _today));
}
/// Runs a pool edit, reports a refusal in the status line, and syncs.
Future<void> _apply(Future<void> Function() edit) async {
try {
await edit();
} on FreeDayPolicyError catch (e) {
setState(() => _status = e.message);
return;
}
await _sync();
}
@override
Widget build(BuildContext context) {
final pool = _pool;
return Scaffold(
appBar: AppBar(
title: const Text('Free days'),
actions: [
IconButton(
icon: const Icon(Icons.sync),
tooltip: 'Sync now',
onPressed: _busy ? null : () => unawaited(_sync()),
),
],
),
body: pool == null
? Center(child: Text(_status.isEmpty ? 'Loading…' : _status))
: FreeDaysBody(
year: _today.year,
remaining: pool.remaining(_today.year),
upcoming: [
for (final day in pool.freeDays())
if (!day.isBefore(_today))
(day: day, reason: pool.reasonFor(day)),
],
status: _status,
busy: _busy,
onRelease: (day) => unawaited(_release(day)),
),
floatingActionButton: pool == null
? null
: FloatingActionButton.extended(
onPressed: _busy ? null : () => unawaited(_pickDay()),
icon: const Icon(Icons.event_available),
label: const Text('Take a day'),
),
);
}
}