import '../../../ui/motion/motion.dart'; import 'package:flutter/material.dart'; import '../../../core/api_gateway.dart'; import '../../../ui/adaptive/floating_bar_layout.dart'; import '../../../ui/layout/memo_subtree.dart'; import '../../attention/notify/notification_controller.dart'; import '../../attention/runtime/attention_controller.dart'; import '../../attention/runtime/attention_store.dart'; import '../../attention/ui/needs_you_runs_heading.dart'; import '../domain/run_models.dart'; import '../domain/run_tab_counts.dart'; import '../../../shell/shell_bar.dart'; import '../../../shell/shell_list_shortcuts.dart'; import 'run_list_row.dart'; import 'runs_controller.dart'; import 'runs_filter_options.dart'; import 'runs_selection_bar.dart'; import 'runs_stats_page.dart'; import 'runs_filters.dart'; export 'run_list_row.dart'; class RunsList extends StatelessWidget { const RunsList({ super.key, required this.controller, required this.onSelected, this.syncToken = 0, this.attentionStore = const SharedPreferencesAttentionStore(), this.attentionNow, this.notifications, this.attention, this.active = true, this.onOpenInChat, }); /// False while a run detail covers the list; the bar is then the run's. final bool active; final Future Function(BuildContext, JsonMap)? onOpenInChat; final RunsController controller; final ValueChanged onSelected; /// Advances when the shared socket is replaced, so the attention detector /// rebaselines instead of replaying a transition from before the gap. final int syncToken; /// Test and fixture seam for the persisted attention dismissals. final AttentionStore attentionStore; /// Deterministic local-day clock for the attention window. final DateTime Function()? attentionNow; /// App-level notification delivery; absent in list-only fixtures. final NotificationController? notifications; /// The app's shared attention controller; absent in list-only fixtures. final AttentionController? attention; @override Widget build(BuildContext context) { // Select mode replaces the bar: `N selected` and its size, Delete, Done. if (controller.selection.active) { final bar = runsSelectionBar(context, controller); if (!ShellBar.available(context)) { return _ownBar(context, [ Expanded( child: Text( [bar.title, ?bar.subtitle].join(' · '), key: runsSelectionCountKey, maxLines: 1, overflow: TextOverflow.ellipsis, ), ), ...bar.actions, ]); } return ShellBar( active: active, title: bar.title, subtitle: bar.subtitle, actions: bar.actions, child: _shortcutList(context), ); } final actions = [ // The Needs-you bell sits with the other actions, freeing the row. NeedsYouRunsHeading( rows: controller.runs, frames: controller.repository.gateway.frames, onOpen: (entry) => onSelected(entry.runId), syncToken: syncToken, store: attentionStore, now: attentionNow, notifications: notifications, attention: attention, ), ...RunsBarActions( controller, ).build(context, onStats: () => openRunsStats(context, controller)), if (controller.runs.isNotEmpty) runsSelectAction(context, controller), ]; // Outside the app shell (the list on its own) the actions head the list. if (!ShellBar.available(context)) { return _ownBar(context, [const Spacer(), ...actions]); } return ShellBar( active: active, actions: actions, // The Runs tab counts runs still executing. child: ShellBadge( kind: ShellBadgeKind.running, count: runningRunCount(controller.runs), child: _shortcutList(context), ), ); } /// The list under the shell's Ctrl+1…Ctrl+9, which open the Nth run shown /// (filtered and sorted as on screen). Select mode keeps keys for itself. Widget _shortcutList(BuildContext context) => ShellListShortcut( onOpenIndex: (index) { if (controller.selection.active) return false; final runs = controller.visibleRuns; if (index >= runs.length) return false; final id = runs[index]['id']; if (id is! num) return false; onSelected(id); return true; }, child: _list(context), ); Widget _ownBar(BuildContext context, List bar) => Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Padding( padding: const EdgeInsetsDirectional.fromSTEB(16, 4, 8, 0), child: Row(children: bar), ), Expanded(child: _list(context)), ], ); Widget _list(BuildContext context) { if (controller.loading && controller.runs.isEmpty) { return _state( context, icon: const CircularProgressIndicator(), title: 'Loading runs…', ); } if (controller.error != null && controller.runs.isEmpty) { return _emptyError(context); } // The notice and the applied filters lead the list, so they scroll // behind the top bar with the runs and start below it at rest. final header = [ if (controller.error != null) _inlineError(context), if (controller.selection.report != null) RunsDeleteReport(controller: controller), RunsFilters(controller: controller), ]; // Filtered and sorted once per build: the getter recomputes every read. final visible = controller.visibleRuns; if (visible.isNotEmpty) return _groupedRuns(context, header, visible); // A server with no runs at all is not a filter problem: say what the list // is for and where its first run comes from. A free-text query counts as // a filter too, so a search on an empty server reads as "nothing found". final noRunsAtAll = controller.runs.isEmpty && controller.activeFilterCount == 0 && controller.query.trim().isEmpty; return BelowTopBar( child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ ...header, Expanded( child: noRunsAtAll ? _state( context, icon: const Icon(Icons.inbox_outlined), title: 'No runs yet.', subtitle: 'Start one from Pipelines and its runs appear here.', ) : _state( context, icon: const Icon(Icons.inbox_outlined), title: 'No runs found.', subtitle: 'Try changing or clearing the filters.', ), ), ], ), ); } Widget _groupedRuns( BuildContext context, List header, List visible, ) { final entries = <_Entry>[]; if (flatRunSorts.contains(controller.sort)) { // An order by size is one flat list: day headings would split it. entries.addAll(visible.map((run) => (day: null, run: run))); } else { final groups = groupRunsByDay(visible, DateTime.now()); for (final group in groups) { entries.add((day: group, run: null)); entries.addAll(group.runs.map((run) => (day: null, run: run))); } } // Runs that start while the list is open enter with a short fade. return ArrivalScope( ids: [for (final run in visible) '${run['id']}'], child: _entriesList(entries, header), ); } Widget _entriesList(List<_Entry> entries, List header) { // The default padding is the media padding: the first row starts below // the top bar and the last clears the floating tab bar. return ListView.builder( itemCount: header.length + entries.length, itemBuilder: (context, index) { if (index < header.length) return header[index]; final entry = entries[index - header.length]; if (entry.day case final day?) { // A day heading reads as a divider between groups of runs. return RunsDayHeading( label: day.label, runs: day.runs, controller: controller, ); } // The last run of a day (before the next heading) has no divider. final next = index - header.length + 1; final last = next >= entries.length || entries[next].run == null; return Arrival( id: '${entry.run!['id']}', child: _runTile(context, entry.run!, divider: !last), ); }, ); } Widget _emptyError(BuildContext context) => Center( child: SingleChildScrollView( padding: const EdgeInsets.symmetric(horizontal: 24), child: Column( mainAxisSize: MainAxisSize.min, children: [ const Icon(Icons.error_outline), const SizedBox(height: 8), Text(controller.error!), const SizedBox(height: 8), FilledButton(onPressed: controller.load, child: const Text('Retry')), ], ), ), ); Widget _state( BuildContext context, { required Widget icon, required String title, String? subtitle, }) => Center( // A short pane (a list beside a detail, or the keyboard up) scrolls the // state instead of overflowing it. child: SingleChildScrollView( padding: const EdgeInsets.all(24), child: Column( mainAxisSize: MainAxisSize.min, children: [ icon, const SizedBox(height: 12), Text(title, textAlign: TextAlign.center), if (subtitle != null) ...[ const SizedBox(height: 4), Text( subtitle, textAlign: TextAlign.center, style: TextStyle( color: Theme.of(context).colorScheme.onSurfaceVariant, ), ), ], ], ), ), ); Widget _inlineError(BuildContext context) => Padding( padding: const EdgeInsets.symmetric(horizontal: 16), child: Row( children: [ Expanded(child: Text(controller.error!)), TextButton(onPressed: controller.load, child: const Text('Retry')), ], ), ); Widget _runTile(BuildContext context, JsonMap run, {bool divider = true}) { final id = run['id']; final selection = controller.selection; final now = DateTime.now(); // A run event for one run rebuilds only that run's row: the others keep // their built subtree (their menu, ripple and tooltip included). return MemoSubtree( memo: ( MemoMap(run), divider, selection.active, selection.contains(id), id == controller.selectedRunId, controller, onSelected, // An executing run's duration ticks inside the row, not from here. ), builder: (context) => _runRow(run, id, now, divider: divider), ); } Widget _runRow(JsonMap run, Object? id, DateTime now, {bool divider = true}) { final selection = controller.selection; return RunListRow( selecting: selection.active, checked: selection.contains(id), onToggle: () => selection.toggle(run), // A long-press enters select mode with this run selected. onLongPress: () => selection.enter(run), onDeleted: controller.applyDeletion, run: run, divider: divider, selected: id == controller.selectedRunId, now: now, onTap: id is num ? () => onSelected(id) : null, gateway: controller.repository.gateway, onRunChanged: controller.acceptRunDetail, onRunOpened: onSelected, onOpenInChat: onOpenInChat, ); } } typedef _Entry = ({({String label, List runs})? day, JsonMap? run});