Files
pipelines/app/lib/features/pipelines/screen.dart
T

382 lines
14 KiB
Dart

import '../../ui/adaptive/segmented_bar.dart';
import '../../shell/destination.dart';
import '../../shell/detail_page.dart';
import '../../shell/list_detail.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../core/api_gateway.dart';
import '../../shell/shell_bar.dart';
import '../../ui/layout/content_width.dart';
import 'pipeline_models.dart';
import 'pipeline_repository.dart';
import 'pipelines_controller.dart';
import 'pending_run_store.dart';
import 'widgets/pipeline_bar_buttons.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;
/// 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.legacyEndpoints = const [],
this.embedded = false,
this.active = true,
this.syncToken = 0,
this.onRunStarted,
this.onViewPipelineRuns,
this.onSelectionChanged,
this.onOpenSchedules,
this.onOpenSettings,
this.initialPipelineId,
this.initialVersion = 0,
this.initialStageKey,
});
final ApiGateway gateway;
/// Validated websocket endpoint used to isolate stored launch intents.
final String endpoint;
final List<String> legacyEndpoints;
/// True under the application shell, which owns the single navigation bar.
final bool embedded;
/// Whether the Pipelines tab is currently visible. The screen stays mounted
/// to preserve tab state, but its pushed detail must not cover another tab.
final bool active;
/// 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<int>? onRunStarted;
final ValueChanged<int>? onViewPipelineRuns;
/// Reports the open pipeline (null for the list) so the route, and with it
/// Back, follows what is shown.
final ValueChanged<int?>? onSelectionChanged;
/// Opens every schedule; shown in the bar while the list is visible.
final VoidCallback? onOpenSchedules;
/// Opens Settings as a page; a gear in the bar while the list is visible.
final VoidCallback? onOpenSettings;
/// 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<PipelinesScreen> createState() => _PipelinesScreenState();
}
class _PipelinesScreenState extends State<PipelinesScreen> {
late PipelinesController _controller;
/// The open pipeline is its own page over the tabs on a phone.
final DetailPageHost _page = DetailPageHost();
/// 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,
legacyEndpoints: widget.legacyEndpoints,
),
)..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);
}
_reported = _controller.selectedId;
_controller.addListener(_reportSelection);
}
int? _reported;
void _reportSelection() {
final id = _controller.selectedId;
if (id == _reported) return;
_reported = id;
widget.onSelectionChanged?.call(id);
}
@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,
legacyEndpoints: widget.legacyEndpoints,
),
)
..load()
..addListener(_reportSelection);
} 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 (!(_controller.selectedId == id &&
// The route caught up with a pipeline opened from the list.
((oldWidget.initialPipelineId != id &&
widget.initialVersion <= 0) ||
_controller.selectedVersion == widget.initialVersion)) &&
!_isRoutePin(_controller.detail)) {
_controller.openPipeline(id, version: widget.initialVersion);
}
}
}
@override
void dispose() {
_page.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 = showsListBesideDetail(context);
// The open pipeline publishes its name, identity and Run/Runs to
// its own bar: a page over the tabs on a phone, a pane beside the
// list (on the shell bar's row) when wide.
Widget detail() => PipelineDetailPanel(
controller: _controller,
gateway: widget.gateway,
showBack: false,
onRunStarted: widget.onRunStarted,
onViewPipelineRuns: widget.onViewPipelineRuns,
stageKey: stageKey,
stagePipelineId: widget.initialPipelineId,
stageVersion: widget.initialVersion,
stageRequest: _stageRequest,
);
_page.sync(
context,
selected: widget.active && _controller.hasSelection && !wide,
destination: AppDestination.pipelines,
onClosed: _controller.clearSelection,
builder: (context) => AnimatedBuilder(
animation: _controller,
builder: (context, _) => detail(),
),
);
if (!wide) {
return ContentWidth(
child: PipelineListPanel(controller: _controller),
);
}
return ListDetailPanes(
list: ContentWidth(
child: PipelineListPanel(controller: _controller),
),
detailIdentity: _controller.selectedId,
detail: _controller.hasSelection
? DetailPane(
key: const ValueKey('pipeline-detail-pane'),
destination: AppDestination.pipelines,
implyBack: false,
builder: (_) => detail(),
)
: detail(),
);
},
);
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 = showsListBesideDetail(context);
final listShown = wide || !_controller.hasSelection;
return ShellBar(
actions: [
if (listShown) ...pipelineListActions(context, _controller),
if (listShown && widget.onOpenSchedules != null)
_schedulesButton(context),
_refreshButton(context),
if (listShown && widget.onOpenSettings != null)
_settingsButton(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),
if (widget.onOpenSettings != null) _settingsButton(context),
],
),
),
),
Expanded(child: body),
],
);
}
Widget _schedulesButton(BuildContext context) =>
pipelineSchedulesButton(context, widget.onOpenSchedules);
Widget _settingsButton(BuildContext context) =>
pipelineSettingsButton(context, widget.onOpenSettings);
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 _materialShell(BuildContext context, Widget body) {
return Scaffold(
extendBodyBehindAppBar: true,
appBar: SegmentedBar(
cupertino: false,
title: 'Pipelines',
actions: [
...pipelineListActions(context, _controller),
if (widget.onOpenSchedules != null) _schedulesButton(context),
_refreshButton(context),
if (widget.onOpenSettings != null) _settingsButton(context),
],
),
body: SafeArea(top: false, child: body),
);
}
Widget _cupertinoShell(BuildContext context, Widget body) {
return CupertinoPageScaffold(
navigationBar: SegmentedBar(
cupertino: true,
title: 'Pipelines',
actions: [
...pipelineListActions(context, _controller),
if (widget.onOpenSchedules != null) _schedulesButton(context),
_refreshButton(context),
if (widget.onOpenSettings != null) _settingsButton(context),
],
),
child: SafeArea(top: false, child: body),
);
}
}