Share one minute clock across time labels and narrow shell and runs rebuilds

Relative-time labels follow one wall-minute RelativeTimeClock instead of a
timer per row, skip ticks while their tab is offstage and rebuild only when
their text changes. The shell rebuilds its top bar only for the focused
destination's published bar and its tab bar only for badge changes. Run
events that repeat a row's state no longer notify, and the runs list filters
and sorts once per build. Adds a rebuild/timer benchmark.
This commit is contained in:
2026-11-01 23:29:20 +00:00
parent bdf70af7e8
commit 3b996bdbc7
9 changed files with 621 additions and 164 deletions
+105
View File
@@ -0,0 +1,105 @@
import 'dart:async';
import 'package:flutter/widgets.dart';
/// One minute clock shared by every relative-time label.
///
/// A list of rows used to run one timer per row; this clock runs a single
/// timer, aligned to the start of each wall-clock minute, only while some
/// label listens and the app is visible. Labels re-read [now] when it
/// notifies and rebuild only if their text changed.
class RelativeTimeClock extends ChangeNotifier with WidgetsBindingObserver {
RelativeTimeClock();
/// The clock the app's labels share.
static final RelativeTimeClock shared = RelativeTimeClock();
DateTime _now = DateTime.now();
Timer? _timer;
bool _observing = false;
bool _hidden = false;
/// The clock's current minute-resolution time. It advances by the elapsed
/// delay on each tick, which keeps it consistent under a fake clock.
DateTime get now => _now;
/// Whether a tick is scheduled (a label listens and the app is visible).
@visibleForTesting
bool get ticking => _timer != null;
@override
void addListener(VoidCallback listener) {
final first = !hasListeners;
super.addListener(listener);
if (!first) return;
_now = DateTime.now();
_hidden = switch (WidgetsBinding.instance.lifecycleState) {
AppLifecycleState.hidden ||
AppLifecycleState.paused ||
AppLifecycleState.detached => true,
_ => false,
};
if (!_observing) {
WidgetsBinding.instance.addObserver(this);
_observing = true;
}
_schedule();
}
@override
void removeListener(VoidCallback listener) {
super.removeListener(listener);
if (hasListeners) return;
_stop();
if (_observing) {
WidgetsBinding.instance.removeObserver(this);
_observing = false;
}
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
_hidden = false;
_now = DateTime.now();
notifyListeners();
_schedule();
} else if (state == AppLifecycleState.hidden ||
// Not inactive: a desktop window that lost focus is still visible.
state == AppLifecycleState.paused ||
state == AppLifecycleState.detached) {
_hidden = true;
_stop();
}
}
void _schedule() {
_timer?.cancel();
_timer = null;
if (_hidden || !hasListeners) return;
final intoMinute = Duration(
seconds: _now.second,
milliseconds: _now.millisecond,
microseconds: _now.microsecond,
);
final delay = const Duration(minutes: 1) - intoMinute;
_timer = Timer(delay, () {
_timer = null;
_now = _now.add(delay);
notifyListeners();
_schedule();
});
}
void _stop() {
_timer?.cancel();
_timer = null;
}
@override
void dispose() {
_stop();
if (_observing) WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
}
+47 -62
View File
@@ -1,93 +1,79 @@
import 'dart:async';
import 'package:flutter/widgets.dart';
import 'relative_time.dart';
import 'relative_time_clock.dart';
/// A relative-time label that rebuilds only itself when its formatter can
/// change. It stops its timer while the app is backgrounded.
/// A relative-time label that rebuilds only itself, and only when its text
/// changes. Every label follows one shared minute [RelativeTimeClock] rather
/// than a timer of its own; a label whose tickers are muted (an offstage tab
/// or a covered route) skips the ticks and catches up when shown again.
class RelativeTimeLabel extends StatefulWidget {
const RelativeTimeLabel({super.key, required this.instant, this.now});
const RelativeTimeLabel({
super.key,
required this.instant,
this.now,
this.clock,
});
final DateTime? instant;
/// The time the label starts from; the shared clock advances it.
final DateTime? now;
/// The clock to follow; [RelativeTimeClock.shared] when null.
final RelativeTimeClock? clock;
@override
State<RelativeTimeLabel> createState() => _RelativeTimeLabelState();
}
class _RelativeTimeLabelState extends State<RelativeTimeLabel>
with WidgetsBindingObserver {
Timer? _timer;
DateTime _now = DateTime.now();
class _RelativeTimeLabelState extends State<RelativeTimeLabel> {
late RelativeTimeClock _clock;
late DateTime _now;
bool _shown = true;
bool _stale = false;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
_schedule();
_now = widget.now ?? DateTime.now();
_clock = widget.clock ?? RelativeTimeClock.shared;
_clock.addListener(_tick);
}
@override
void didUpdateWidget(RelativeTimeLabel oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.instant != widget.instant) _schedule();
final clock = widget.clock ?? RelativeTimeClock.shared;
if (clock == _clock) return;
_clock.removeListener(_tick);
_clock = clock..addListener(_tick);
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
_now = DateTime.now();
if (mounted) setState(() {});
_schedule(current: _now);
} else if (state == AppLifecycleState.hidden ||
// Not inactive: a desktop window that lost focus is still visible.
state == AppLifecycleState.paused ||
state == AppLifecycleState.detached) {
_timer?.cancel();
_timer = null;
}
void didChangeDependencies() {
super.didChangeDependencies();
_shown = TickerMode.valuesOf(context).enabled;
if (_shown && _stale) _advance();
}
void _schedule({DateTime? current}) {
_timer?.cancel();
final instant = widget.instant;
if (instant == null) return;
final now = current ?? widget.now ?? DateTime.now();
_now = now;
final next = _nextBoundary(instant, now);
final delay = next.difference(now);
_timer = Timer(delay.isNegative ? Duration.zero : delay, () {
if (!mounted) return;
_now = _now.add(delay);
setState(() {});
_schedule(current: _now);
});
/// Moves to the clock's time; true when the text changed.
bool _advance() {
_stale = false;
final next = _clock.now;
if (!next.isAfter(_now)) return false;
final before = formatRelativeTime(widget.instant, now: _now);
_now = next;
return formatRelativeTime(widget.instant, now: _now) != before;
}
DateTime _nextBoundary(DateTime instant, DateTime now) {
final difference = now.difference(instant);
final age = difference.abs();
final unit = age < const Duration(hours: 1)
? const Duration(minutes: 1)
: age < const Duration(days: 1)
? const Duration(hours: 1)
: age < const Duration(days: 7)
? const Duration(days: 1)
: null;
if (unit != null) {
final units = age.inMilliseconds ~/ unit.inMilliseconds;
final boundary = difference.isNegative
// Future labels use a decreasing count. Add a second so an exact
// boundary does not immediately schedule another zero-delay timer.
? instant.subtract(unit * units).add(const Duration(seconds: 1))
: instant.add(unit * (units + 1));
if (boundary.isAfter(now)) return boundary;
void _tick() {
if (!mounted || widget.instant == null) return;
if (!_shown) {
_stale = true;
return;
}
final midnight = DateTime(now.year, now.month, now.day + 1);
return midnight.isAfter(now)
? midnight
: now.add(const Duration(minutes: 1));
if (_advance()) setState(() {});
}
@override
@@ -102,8 +88,7 @@ class _RelativeTimeLabelState extends State<RelativeTimeLabel>
@override
void dispose() {
_timer?.cancel();
WidgetsBinding.instance.removeObserver(this);
_clock.removeListener(_tick);
super.dispose();
}
}