import '../../../ui/layout/section_heading.dart'; import '../../../ui/motion/motion.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../../../core/api_gateway.dart'; import '../../../ui/adaptive/host.dart'; import '../../../ui/status/run_status_icon.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 '../../../shell/shell_bar.dart'; import 'run_path_mini.dart'; import 'runs_controller.dart'; import 'runs_stats_page.dart'; import 'runs_filters.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, }); /// False while a run detail covers the list; the bar is then the run's. final bool active; 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) { 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)), ]; // Outside the app shell (the list on its own) the actions head the list. if (!ShellBar.available(context)) { return Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Padding( padding: const EdgeInsetsDirectional.fromSTEB(8, 4, 8, 0), child: Row( mainAxisAlignment: MainAxisAlignment.end, children: actions, ), ), Expanded(child: _list(context)), ], ); } return ShellBar( active: active, actions: actions, // The Runs tab counts runs still executing. child: ShellBadge( kind: ShellBadgeKind.running, count: controller.runs.where((run) => run['executing'] == true).length, child: _list(context), ), ); } Widget _list(BuildContext context) { if (controller.loading && controller.runs.isEmpty) { return Center( child: usesCupertinoHost(context) ? const CupertinoActivityIndicator() : const CircularProgressIndicator(), ); } if (controller.error != null && controller.runs.isEmpty) { return _emptyError(context); } return Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ if (controller.error != null) _inlineError(context), RunsFilters(controller: controller), Expanded( child: controller.visibleRuns.isEmpty ? const Center(child: Text('No runs found.')) : _groupedRuns(context), ), ], ); } Widget _groupedRuns(BuildContext context) { final groups = groupRunsByDay(controller.visibleRuns, DateTime.now()); final entries = <({String? label, JsonMap? run})>[]; for (final group in groups) { entries.add((label: group.label, run: null)); entries.addAll(group.runs.map((run) => (label: null, run: run))); } // Runs that start while the list is open enter with a short fade. return ArrivalScope( ids: [for (final run in controller.visibleRuns) '${run['id']}'], child: _entriesList(entries), ); } Widget _entriesList(List<({String? label, JsonMap? run})> entries) { return ListView.builder( itemCount: entries.length, itemBuilder: (context, index) { final entry = entries[index]; if (entry.label case final label?) { // A day heading reads as a divider between groups of runs. return Padding( padding: const EdgeInsets.only(top: 8), child: Semantics( header: true, child: SectionHeading(title: label, minHeight: 32), ), ); } return Arrival( id: '${entry.run!['id']}', child: _runTile(context, entry.run!), ); }, ); } Widget _emptyError(BuildContext context) => Center( child: Column( mainAxisSize: MainAxisSize.min, children: [ Text(controller.error!), const SizedBox(height: 8), usesCupertinoHost(context) ? CupertinoButton( onPressed: controller.load, child: const Text('Retry'), ) : FilledButton( onPressed: controller.load, child: const Text('Retry'), ), ], ), ); Widget _inlineError(BuildContext context) => Padding( padding: const EdgeInsets.symmetric(horizontal: 16), child: Row( children: [ Expanded(child: Text(controller.error!)), if (usesCupertinoHost(context)) CupertinoButton( onPressed: controller.load, child: const Text('Retry'), ) else TextButton(onPressed: controller.load, child: const Text('Retry')), ], ), ); Widget _runTile(BuildContext context, JsonMap run) { final id = run['id']; return RunListRow( run: run, now: DateTime.now(), onTap: id is num ? () => onSelected(id) : null, ); } } /// One run in the list with its facts in the row's corners: the run and its /// number on top, the pipeline and duration below, then when it ran and how /// far it got. class RunListRow extends StatelessWidget { const RunListRow({ super.key, required this.run, required this.now, this.onTap, }); final JsonMap run; final DateTime now; final VoidCallback? onTap; @override Widget build(BuildContext context) { final cupertino = usesCupertinoHost(context); final identity = runIdentity(run['title'], run['name']); final id = run['id']; final title = identity.label.isEmpty ? 'Run $id' : identity.label; final facts = runRowFacts(run, now); // A run without its own title is named by its pipeline, so the pipeline // line does not repeat it. final pipeline = identity.pipeline?.trim(); final secondary = cupertino ? CupertinoColors.secondaryLabel.resolveFrom(context) : Theme.of(context).colorScheme.onSurfaceVariant; final base = cupertino ? CupertinoTheme.of(context).textTheme.textStyle : Theme.of(context).textTheme.bodyMedium ?? const TextStyle(); final titleStyle = base.copyWith(fontWeight: FontWeight.w600); final small = base.copyWith( color: secondary, fontSize: (base.fontSize ?? 14) - 1, ); Widget line(String? start, String? end, TextStyle style, {Key? key}) => Row( key: key, crossAxisAlignment: CrossAxisAlignment.start, children: [ Expanded( child: Text( start ?? '', style: style, maxLines: 1, overflow: TextOverflow.ellipsis, ), ), if (end != null) ...[ const SizedBox(width: 12), Text(end, style: style), ], ], ); final scheduled = run['scheduled'] == true; final body = Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ Padding( padding: const EdgeInsets.only(top: 2, right: 12), child: RunStatusIcon( state: runStateOf(run['status']), label: runStatusLabel(run), ), ), Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ line(title, id == null ? null : '#$id', titleStyle), const SizedBox(height: 2), line( [ if (pipeline != null) pipeline.isEmpty ? 'Pipeline unavailable' : pipeline, if (scheduled) 'Scheduled', ].join(' ยท '), facts.duration, small, ), line(facts.when, facts.progress, small), // The run's path, each stage in its own result colour. Padding( padding: const EdgeInsets.only(top: 4), child: Align( alignment: AlignmentDirectional.centerEnd, child: RunPathMini(run: run), ), ), ], ), ), ], ); final padded = Padding( padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10), child: body, ); return Semantics( button: onTap != null, child: cupertino ? CupertinoButton( key: ValueKey('run-row-$id'), padding: EdgeInsets.zero, minimumSize: Size.zero, pressedOpacity: 0.6, onPressed: onTap, child: DefaultTextStyle(style: base, child: padded), ) : InkWell(key: ValueKey('run-row-$id'), onTap: onTap, child: padded), ); } }