mirror of
https://github.com/kuhyx/wake-alarm.git
synced 2026-11-26 10:57:14 +01:00
showDatePicker asserts that initialDate satisfies selectableDayPredicate, so with today already free the picker asserted in debug and, in release, pre-selected a disabled day. The cursor now starts on the first non-free day. And the reason dialog had no Cancel: a barrier tap marked the day anyway. Both covered by widget tests; the first fails without the fix. The cross-language check that proved the Dart port was a one-off probe, deleted after it ran. It is now scripts/free_days_interop/check.sh, a pre-commit hook on both inputs: Dart writes one pool through every policy branch, Python replays the same edits into a scratch pool, and every semantic reading has to agree. The Python package is the oracle -- Dart's own count compared to what Python reads from Dart's file cannot see a drift in what Dart writes. Proved by dropping the sticky consumed flag from release: remaining dart=33 python=32. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019pBgaXSJKyY3DarYpDatDF
190 lines
5.8 KiB
Dart
190 lines
5.8 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;
|
|
// showDatePicker asserts that initialDate satisfies the predicate, so
|
|
// when today is already free the cursor starts on the next day that is
|
|
// not. Day arithmetic, not Duration: adding 24h across a DST change
|
|
// lands on the same date twice.
|
|
var initial = today;
|
|
while (pool.isFree(initial)) {
|
|
initial = DateTime(initial.year, initial.month, initial.day + 1);
|
|
}
|
|
final picked = await showDatePicker(
|
|
context: context,
|
|
initialDate: initial,
|
|
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();
|
|
// Null is Cancel or a barrier tap; an empty string is "no reason".
|
|
if (reason == null || !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(),
|
|
child: const Text('Cancel'),
|
|
),
|
|
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'),
|
|
),
|
|
);
|
|
}
|
|
}
|