feat(app): mirror the attention count on the Runs tab

This commit is contained in:
2026-10-22 18:40:55 +01:00
parent 1bcc26a4fd
commit eb80092860
9 changed files with 454 additions and 15 deletions
+66 -1
View File
@@ -1,6 +1,7 @@
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
@@ -10,6 +11,8 @@ import '../shell/adaptive_shell.dart';
import '../shell/destination.dart';
import '../features/attention/notify/notification_controller.dart';
import '../features/attention/notify/notification_store.dart';
import '../features/attention/runtime/attention_controller.dart';
import '../features/attention/runtime/attention_store.dart';
import '../features/runs/ui/run_search_selection.dart';
import '../features/search/domain/search_target.dart';
import '../features/settings/settings_screen.dart';
@@ -89,6 +92,20 @@ class _PipelinesAppState extends State<PipelinesApp>
store: const SharedPreferencesNotificationStore(),
);
/// One attention surface for the app: the Runs heading's bell draws from it
/// and the shell mirrors its count on the Runs tab, so the badge is visible
/// from every destination. The Runs list seeds the rows when it loads; pushed
/// `run_event` frames are folded here so the count moves while another
/// destination is shown, and a replaced socket drops the status baseline.
late final AttentionController _attention = AttentionController(
store: const SharedPreferencesAttentionStore(),
);
StreamSubscription<JsonMap>? _attentionFrames;
int _attentionEpoch = 0;
/// The count the shell's Runs tab shows; only a changed count rebuilds it.
final ValueNotifier<int> _attentionCount = ValueNotifier<int>(0);
@override
void initState() {
super.initState();
@@ -115,6 +132,12 @@ class _PipelinesAppState extends State<PipelinesApp>
_cupertino = widget.cupertino ?? _platformIsCupertino;
_notifications.onOpenRun = (id) => _delegate.goTo(AppRoute.run(id));
unawaited(_notifications.load());
unawaited(_attention.load());
_attentionFrames = _session.gateway.frames
.where((frame) => frame['type'] == 'run_event')
.listen(_attention.observeFrame);
_session.addListener(_attentionSessionChanged);
_attention.addListener(_attentionChanged);
_delegate = AppRouterDelegate(
initial: initial,
cupertino: _cupertino,
@@ -137,6 +160,11 @@ class _PipelinesAppState extends State<PipelinesApp>
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
_session.removeListener(_attentionSessionChanged);
unawaited(_attentionFrames?.cancel());
_attention.removeListener(_attentionChanged);
_attention.dispose();
_attentionCount.dispose();
_notifications.dispose();
_delegate.removeListener(_routeChanged);
_delegate.dispose();
@@ -161,6 +189,33 @@ class _PipelinesAppState extends State<PipelinesApp>
unawaited(_session.retry());
}
/// A replaced socket drops the attention status baseline so the gap cannot
/// replay a transition; the Runs list's next read reseeds the rows.
void _attentionSessionChanged() {
final epoch = _session.connectionEpoch;
if (epoch == _attentionEpoch) return;
_attentionEpoch = epoch;
_attention.resetForReconnect();
}
/// Mirrors the controller's entry count for the shell. The Runs list seeds
/// the rows from inside its own build, and the shell sits above that layout
/// scope, so a count that changes mid-build lands after the frame; a pushed
/// frame arrives outside any build and applies at once.
void _attentionChanged() {
if (!mounted) return;
final count = _attention.entries.length;
if (count == _attentionCount.value) return;
if (SchedulerBinding.instance.schedulerPhase ==
SchedulerPhase.persistentCallbacks) {
SchedulerBinding.instance.addPostFrameCallback(
(_) => _attentionChanged(),
);
return;
}
_attentionCount.value = count;
}
@override
Widget build(BuildContext context) =>
MyApp.router(cupertino: _cupertino, routerConfig: _config);
@@ -277,7 +332,16 @@ class _PipelinesAppState extends State<PipelinesApp>
_delegate.goTo(AppRoute.run(id));
}
Widget _buildShell(BuildContext context, AppRoute route) => AdaptiveShell(
/// The shell rebuilds its Runs tab badge from the attention count alone; the
/// destination bodies keep their state through it.
Widget _buildShell(BuildContext context, AppRoute route) =>
ValueListenableBuilder<int>(
valueListenable: _attentionCount,
builder: (context, count, _) => _shellFor(route, count),
);
Widget _shellFor(AppRoute route, int attentionCount) => AdaptiveShell(
attentionCount: attentionCount,
cupertino: _cupertino,
destination: _destinationOf(route.kind),
onDestinationSelected: (destination) =>
@@ -307,6 +371,7 @@ class _PipelinesAppState extends State<PipelinesApp>
initialDecide: route.decide,
listRefreshToken: _runsRefresh,
notifications: _notifications,
attention: _attention,
onRunSelected: (id) => _delegate.goTo(AppRoute.run(id)),
onBack: () => _delegate.goTo(AppRoute.runs),
),
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import '../features/attention/notify/notification_controller.dart';
import '../features/attention/runtime/attention_controller.dart';
import '../features/runs/screen.dart';
import '../ui/adaptive/host.dart';
import 'app_session.dart';
@@ -26,6 +27,7 @@ class RunsDestination extends StatelessWidget {
this.onBack,
this.listRefreshToken = 0,
this.notifications,
this.attention,
});
final AppSession session;
@@ -52,6 +54,9 @@ class RunsDestination extends StatelessWidget {
/// The app-level notification capability shared with Settings.
final NotificationController? notifications;
/// The app-level attention controller the shell's Runs tab badge reads.
final AttentionController? attention;
@override
Widget build(BuildContext context) => AnimatedBuilder(
animation: session,
@@ -93,6 +98,7 @@ class RunsDestination extends StatelessWidget {
syncToken: session.connectionEpoch,
listRefreshToken: listRefreshToken,
notifications: notifications,
attention: attention,
onRunSelected: onRunSelected,
onBack: onBack,
);