import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../../core/api_gateway.dart'; import '../../shell/shell_bar.dart'; import 'pipeline_models.dart'; import 'pipeline_repository.dart'; import 'pipelines_controller.dart'; import 'pending_run_store.dart'; import 'widgets/pipeline_controls.dart'; import 'widgets/pipeline_detail_panel.dart'; import 'widgets/pipeline_list_panel.dart'; export 'pipeline_models.dart' show PipelineDetail, PipelineVersion; /// Below this width the list and detail are separate pages; above it they sit /// side by side. One local breakpoint, not a styling system. const double kPipelinesWideBreakpoint = 720; /// The read-only Pipelines destination: list, filters/sort, detail, source and /// version history. /// /// Standalone use keeps the screen's own native scaffolding. The application /// shell sets [embedded] because it already owns the one navigation bar, and /// supplies [syncToken] so a reconnect refreshes in place. class PipelinesScreen extends StatefulWidget { const PipelinesScreen({ super.key, required this.gateway, this.endpoint = '', this.embedded = false, this.syncToken = 0, this.onRunStarted, this.onViewPipelineRuns, this.onOpenSchedules, this.initialPipelineId, this.initialVersion = 0, this.initialStageKey, }); final ApiGateway gateway; /// Validated websocket endpoint used to isolate stored launch intents. final String endpoint; /// True under the application shell, which owns the single navigation bar. final bool embedded; /// Advances when the shared socket is replaced. The mounted controller /// reloads the list and the selected detail without losing the selection. final int syncToken; /// Receives the run id an accepted launch names, so the composition can open /// the existing run route. The feature never navigates on its own. final ValueChanged? onRunStarted; final ValueChanged? onViewPipelineRuns; /// Opens every schedule; shown in the bar while the list is visible. final VoidCallback? onOpenSchedules; /// The pipeline a deep link names, opened after the list read starts. final int? initialPipelineId; /// The pinned version for [initialPipelineId]; 0 means latest. final int initialVersion; /// The definition stage the route names for [initialPipelineId], or null. /// /// The detail opens its Graph tab with this stage selected and its definition /// pane; a key the pinned version does not declare opens the Graph tab with /// nothing selected. Null keeps the ordinary default tab. final String? initialStageKey; @override State createState() => _PipelinesScreenState(); } class _PipelinesScreenState extends State { late PipelinesController _controller; /// Counts route stage requests so a re-read detail cannot replay an old one. int _stageRequest = 0; /// Whether the route's stage request has been handed to its named pin. /// /// The screen, not the detail panel, owns this because the narrow layout /// destroys the panel when it returns to the list: a deep link is a one-shot /// intent, so coming back to the same pipeline later keeps the ordinary tab. bool _stageRequestDone = false; @override void initState() { super.initState(); _controller = PipelinesController( PipelinesRepository(widget.gateway), pendingRunStore: SharedPreferencesPendingRunStore(widget.endpoint), )..load(); final id = widget.initialPipelineId; if (id != null) { // The detail read owns its own failure state; the list read continues. _controller.openPipeline(id, version: widget.initialVersion); } } @override void didUpdateWidget(PipelinesScreen oldWidget) { super.didUpdateWidget(oldWidget); if (oldWidget.gateway != widget.gateway || oldWidget.endpoint != widget.endpoint) { _controller.dispose(); _controller = PipelinesController( PipelinesRepository(widget.gateway), pendingRunStore: SharedPreferencesPendingRunStore(widget.endpoint), )..load(); } else if (oldWidget.syncToken != widget.syncToken) { // The socket was replaced; re-read through the existing controller so the // selected pipeline and requested version survive the reconnect. _controller.load(); } else if (oldWidget.initialPipelineId != widget.initialPipelineId || oldWidget.initialVersion != widget.initialVersion || oldWidget.initialStageKey != widget.initialStageKey) { _stageRequest++; _stageRequestDone = false; final id = widget.initialPipelineId; if (id == null) { _controller.clearSelection(); } else if (!_isRoutePin(_controller.detail)) { _controller.openPipeline(id, version: widget.initialVersion); } } } @override void dispose() { _controller.dispose(); super.dispose(); } /// Whether [detail] is the pipeline and pin the route names. /// /// The shown detail must have been *requested* for the route's version, not /// merely have resolved to it: a latest route (`initialVersion == 0`) never /// matches a detail still displayed from a pinned request, so a route change /// from a pin to latest re-reads latest. The web compares its requested /// version the same way. A pinned route additionally requires the reply's own /// concrete version. bool _isRoutePin(PipelineDetail? detail) { final id = widget.initialPipelineId; if (detail == null || id == null || detail.id != id) return false; final version = widget.initialVersion; if (_controller.selectedVersion != version) return false; return version <= 0 || detail.resolvedVersion == version; } /// The route's stage request while it is still pending, or null once spent. /// /// The request names one stage of one pinned pipeline. It is handed to the /// detail only until that pin's reply has been shown: the screen consumes the /// request as soon as the first reply for the current selection arrives, so a /// spent deep link cannot replay its pane when the operator later returns to /// the same pipeline. The panel still scopes the request to its named /// pipeline and version, which is what keeps it off an unrelated detail. String? _takeStageKey() { final key = widget.initialStageKey; final target = widget.initialPipelineId; if (key == null || key.isEmpty || target == null) return null; if (_stageRequestDone) return null; final detail = _controller.detail; if (detail == null) return key; _stageRequestDone = true; return _isRoutePin(detail) ? key : null; } @override Widget build(BuildContext context) { return AnimatedBuilder( animation: _controller, builder: (context, _) { final stageKey = _takeStageKey(); final body = LayoutBuilder( builder: (context, constraints) { final wide = constraints.maxWidth >= kPipelinesWideBreakpoint; if (!wide) { return _controller.hasSelection ? PipelineDetailPanel( controller: _controller, gateway: widget.gateway, showBack: true, onRunStarted: widget.onRunStarted, onViewPipelineRuns: widget.onViewPipelineRuns, stageKey: stageKey, stagePipelineId: widget.initialPipelineId, stageVersion: widget.initialVersion, stageRequest: _stageRequest, ) : PipelineListPanel(controller: _controller); } return Row( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Expanded( flex: 2, child: PipelineListPanel(controller: _controller), ), _divider(context), Expanded( flex: 3, child: PipelineDetailPanel( controller: _controller, gateway: widget.gateway, showBack: false, onRunStarted: widget.onRunStarted, onViewPipelineRuns: widget.onViewPipelineRuns, stageKey: stageKey, stagePipelineId: widget.initialPipelineId, stageVersion: widget.initialVersion, stageRequest: _stageRequest, ), ), ], ); }, ); if (widget.embedded) return _embeddedBody(context, body); return useCupertinoControls(context) ? _cupertinoShell(context, body) : _materialShell(context, body); }, ); } /// The shell owns the navigation bar here: the list puts filter, sort and /// refresh in it; an open pipeline on a phone keeps refresh only. Widget _embeddedBody(BuildContext context, Widget body) { if (ShellBar.available(context)) { final wide = MediaQuery.sizeOf(context).width >= kPipelinesWideBreakpoint; final listShown = wide || !_controller.hasSelection; return ShellBar( actions: [ if (listShown) ...pipelineListActions(context, _controller), if (listShown && widget.onOpenSchedules != null) _schedulesButton(context), _refreshButton(context), ], child: body, ); } return Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Align( alignment: AlignmentDirectional.centerEnd, child: Padding( padding: const EdgeInsets.symmetric(horizontal: 8), child: Row( mainAxisSize: MainAxisSize.min, children: [ ...pipelineListActions(context, _controller), if (widget.onOpenSchedules != null) _schedulesButton(context), _refreshButton(context), ], ), ), ), Expanded(child: body), ], ); } Widget _schedulesButton(BuildContext context) { if (useCupertinoControls(context)) { return Semantics( button: true, label: 'Schedules', child: CupertinoButton( key: const Key('pipelines-open-schedules'), padding: EdgeInsets.zero, onPressed: widget.onOpenSchedules, child: const Icon(CupertinoIcons.calendar), ), ); } return IconButton( key: const Key('pipelines-open-schedules'), tooltip: 'Schedules', onPressed: widget.onOpenSchedules, icon: const Icon(Icons.schedule), ); } Widget _refreshButton(BuildContext context) { if (useCupertinoControls(context)) { return Semantics( button: true, label: 'Refresh', child: CupertinoButton( padding: EdgeInsets.zero, onPressed: _controller.load, child: const Icon(CupertinoIcons.refresh), ), ); } return IconButton( tooltip: 'Refresh', onPressed: _controller.load, icon: const Icon(Icons.refresh), ); } Widget _divider(BuildContext context) { if (useCupertinoControls(context)) { return Container( width: 1, color: CupertinoColors.separator.resolveFrom(context), ); } return const VerticalDivider(width: 1); } Widget _materialShell(BuildContext context, Widget body) { return Scaffold( appBar: AppBar( title: const Text('Pipelines'), actions: [ ...pipelineListActions(context, _controller), if (widget.onOpenSchedules != null) _schedulesButton(context), _refreshButton(context), ], ), body: SafeArea(child: body), ); } Widget _cupertinoShell(BuildContext context, Widget body) { return CupertinoPageScaffold( navigationBar: CupertinoNavigationBar( middle: const Text('Pipelines'), trailing: Row( mainAxisSize: MainAxisSize.min, children: [ ...pipelineListActions(context, _controller), if (widget.onOpenSchedules != null) _schedulesButton(context), _refreshButton(context), ], ), ), child: SafeArea(top: false, child: body), ); } }