import 'dart:async'; import 'package:flutter/widgets.dart'; import '../../core/api_gateway.dart'; import '../../shell/destination.dart'; import '../../shell/detail_page.dart'; import '../../ui/adaptive/host.dart'; import '../attention/notify/notification_controller.dart'; import '../attention/runtime/attention_controller.dart'; import '../attention/runtime/attention_store.dart'; import 'actions/decision_action.dart'; import 'data/runs_repository.dart'; import 'domain/run_decisions.dart'; import 'ui/run_pipeline_link.dart'; import 'ui/run_detail_view.dart'; import 'ui/runs_controller.dart'; import 'ui/runs_list.dart'; /// The runs destination's list/detail switch. /// /// The controller's `selectedRunId` is the only selection source, so an /// initial run, a pending detail, a failed detail, Back and a route change all /// navigate the same way. The shell owns the app bar; this screen adds no /// second one. class RunsScreen extends StatefulWidget { const RunsScreen({ super.key, required this.gateway, this.initialRunId, this.initialPipelineId, this.initialStageKey, this.initialVisit, this.initialDecide = false, this.syncToken = 0, this.listRefreshToken = 0, this.onRunSelected, this.onBack, this.onPipelineFilterChanged, this.onOpenPipeline, this.attentionStore = const SharedPreferencesAttentionStore(), this.attentionNow, this.notifications, this.attention, }); final ApiGateway gateway; /// The run the current route names, or null for the list. final int? initialRunId; final int? initialPipelineId; /// The recorded stage visit the route names inside [initialRunId]. The detail /// applies it once per route target; the feature never invents a visit. final String? initialStageKey; final int? initialVisit; /// The route's `?action=decide`: once the named run's detail is loaded, open /// the recorded decision prompt when the run is genuinely waiting. Consumed /// once per route target, so a rebuild or a live re-read never reopens it. final bool initialDecide; /// Advances when the shared socket is replaced. The loaded screen refreshes /// in place instead of being remounted, so its selection survives. final int syncToken; /// Advances when a launch accepted outside this screen must be reflected by a /// fresh `list_runs` read. It never inserts a row of its own. final int listRefreshToken; final ValueChanged? onRunSelected; final ValueChanged? onBack; final ValueChanged? onPipelineFilterChanged; /// Opens a pipeline from the run detail's pipeline name. final OpenPipeline? onOpenPipeline; /// Persisted attention dismissals and the clock for its local-day window. /// The app uses SharedPreferences; tests and fixtures inject a memory store. final AttentionStore attentionStore; final DateTime Function()? attentionNow; /// App-level notification delivery for live attention transitions. final NotificationController? notifications; /// The app's shared attention controller, mirrored on the shell's Runs tab. final AttentionController? attention; @override State createState() => _RunsScreenState(); } class _RunsScreenState extends State { late final RunsController controller; num? _shownRunId; int? _reportedPipelineId; bool _applyingRoutePipeline = false; /// The route's decision request is one-shot: the first loaded detail for the /// named run consumes it, whether or not that run is waiting. bool _decideConsumed = false; /// The open run is its own page over the tabs. final DetailPageHost _page = DetailPageHost(); @override void initState() { super.initState(); controller = RunsController(RunsRepository(widget.gateway)); _reportedPipelineId = widget.initialPipelineId; if (widget.initialPipelineId != null) { controller.clearFilters(); controller.setPipeline(widget.initialPipelineId); } controller.listen(widget.gateway.frames); controller.addListener(_selectionChanged); controller.addListener(_pipelineFilterChanged); controller.addListener(_detailChanged); controller.load(widget.initialRunId); } @override void didUpdateWidget(covariant RunsScreen oldWidget) { super.didUpdateWidget(oldWidget); if (widget.initialRunId != oldWidget.initialRunId || widget.initialDecide != oldWidget.initialDecide) { _decideConsumed = false; } if (widget.initialPipelineId != oldWidget.initialPipelineId) { final id = widget.initialPipelineId; // A run detail replaces the list URL but retains its active list filter. // Only a route back to the list can explicitly clear that facet. if (id != null || widget.initialRunId == null) { _applyingRoutePipeline = true; try { if (id != null) controller.clearFilters(); controller.setPipeline(id); _reportedPipelineId = id; } finally { _applyingRoutePipeline = false; } } } if (widget.syncToken != oldWidget.syncToken) { controller.resync(); } // A launch accepted on another destination invalidates the list only. The // route change below selects the returned run separately, so this is the // one fresh server read that makes the new row appear. if (widget.listRefreshToken != oldWidget.listRefreshToken) { controller.load(); } if (widget.initialRunId != oldWidget.initialRunId) { final id = widget.initialRunId; if (id == null) { if (controller.selectedRunId != null) controller.back(); } else if (controller.selectedRunId != id) { controller.select(id); } } if (widget.initialRunId != oldWidget.initialRunId || widget.initialDecide != oldWidget.initialDecide) { _detailChanged(); } } /// Rebuild the switch only when the selection changes. Detail progress and /// live updates are already watched by [RunDetailView] and [RunsList], so /// this avoids a second whole-detail rebuild per notification. void _selectionChanged() { final selected = controller.selectedRunId; if (selected == _shownRunId) return; _shownRunId = selected; if (mounted) setState(() {}); } void _pipelineFilterChanged() { final id = controller.pipelineId; if (_applyingRoutePipeline) { _reportedPipelineId = id; return; } if (id == _reportedPipelineId) return; _reportedPipelineId = id; if (controller.selectedRunId == null) { widget.onPipelineFilterChanged?.call(id); } } /// Opens the route's decision prompt after the named detail has arrived. /// A non-waiting run consumes the request without a prompt, and the prompt /// itself only closes; the detail underneath is left as it is. void _detailChanged() { if (!widget.initialDecide || _decideConsumed) return; final run = controller.detail; final selected = controller.selectedRunId; if (run == null || selected == null || run['id'] != selected) return; _decideConsumed = true; if (pendingDecision(run) == null) return; WidgetsBinding.instance.addPostFrameCallback((_) { if (!mounted || controller.selectedRunId != selected) return; final current = controller.detail; if (current == null || pendingDecision(current) == null) return; unawaited( showRunDecisionPrompt( context: context, run: current, gateway: widget.gateway, onRunChanged: controller.acceptRunDetail, cupertino: usesCupertinoHost(context), isCurrent: () => mounted && controller.selectedRunId == selected, ), ); }); } void _select(num id) { controller.select(id); widget.onRunSelected?.call(id.toInt()); } void _back() { controller.back(); widget.onBack?.call(controller.pipelineId); } @override void dispose() { controller.removeListener(_selectionChanged); controller.removeListener(_pipelineFilterChanged); controller.removeListener(_detailChanged); _page.dispose(); controller.dispose(); super.dispose(); } Widget _detail(VoidCallback onBack) => RunDetailView( controller: controller, gateway: widget.gateway, onBack: onBack, onRunOpened: _select, onOpenPipeline: widget.onOpenPipeline, initialStageKey: widget.initialStageKey, initialVisit: widget.initialVisit, ); @override Widget build(BuildContext context) { final selected = controller.selectedRunId != null; // On a phone an open run is its own page over the tabs; a wide window // keeps it in the tab, beside the destination rail. final narrow = MediaQuery.sizeOf(context).width < kDetailPageMaxWidth; _page.sync( context, selected: selected && narrow, destination: AppDestination.runs, onClosed: _back, builder: (context) => _detail(() => Navigator.of(context).maybePop()), ); final list = AnimatedBuilder( animation: controller, builder: (context, _) => RunsList( active: !selected, controller: controller, onSelected: _select, syncToken: widget.syncToken, attentionStore: widget.attentionStore, attentionNow: widget.attentionNow, notifications: widget.notifications, attention: widget.attention, ), ); if (narrow) return list; return Stack( children: [ Offstage(offstage: selected, child: list), if (selected) _detail(_back), ], ); } }