feat(app): compose live Runs with deep links and reconnect retention
This commit is contained in:
@@ -10,6 +10,11 @@ const runStates = [
|
||||
'interrupted',
|
||||
];
|
||||
|
||||
/// The largest millisecond value [DateTime] supports, and also exactly
|
||||
/// representable by the web's JavaScript numbers. The previous
|
||||
/// `0x7fffffffffffffff` literals failed dart2js compilation.
|
||||
const int _maxMillis = 8640000000000000;
|
||||
|
||||
({String label, String? pipeline}) runIdentity(dynamic title, dynamic name) {
|
||||
final stored = '${title ?? ''}'.trim();
|
||||
return (
|
||||
@@ -116,25 +121,22 @@ List<JsonMap> sortRuns(List<JsonMap> runs, {String sort = 'newest_first'}) {
|
||||
int primary;
|
||||
switch (sort) {
|
||||
case 'oldest_first':
|
||||
primary = cmp(at ?? 0x7fffffffffffffff, bt ?? 0x7fffffffffffffff);
|
||||
primary = cmp(at ?? _maxMillis, bt ?? _maxMillis);
|
||||
break;
|
||||
case 'longest_duration':
|
||||
primary = cmp(duration(a) == null ? 1 : 0, duration(b) == null ? 1 : 0);
|
||||
if (primary == 0) primary = cmp(duration(b) ?? -1, duration(a) ?? -1);
|
||||
if (primary == 0) {
|
||||
primary = cmp(bt ?? -0x7fffffffffffffff, at ?? -0x7fffffffffffffff);
|
||||
primary = cmp(bt ?? -_maxMillis, at ?? -_maxMillis);
|
||||
}
|
||||
break;
|
||||
case 'shortest_duration':
|
||||
primary = cmp(duration(a) == null ? 1 : 0, duration(b) == null ? 1 : 0);
|
||||
if (primary == 0) {
|
||||
primary = cmp(
|
||||
duration(a) ?? 0x7fffffffffffffff,
|
||||
duration(b) ?? 0x7fffffffffffffff,
|
||||
);
|
||||
primary = cmp(duration(a) ?? _maxMillis, duration(b) ?? _maxMillis);
|
||||
}
|
||||
if (primary == 0) {
|
||||
primary = cmp(bt ?? -0x7fffffffffffffff, at ?? -0x7fffffffffffffff);
|
||||
primary = cmp(bt ?? -_maxMillis, at ?? -_maxMillis);
|
||||
}
|
||||
break;
|
||||
case 'pipeline_name':
|
||||
@@ -142,7 +144,7 @@ List<JsonMap> sortRuns(List<JsonMap> runs, {String sort = 'newest_first'}) {
|
||||
'${b['name'] ?? ''}'.toLowerCase(),
|
||||
);
|
||||
if (primary == 0) {
|
||||
primary = cmp(bt ?? -0x7fffffffffffffff, at ?? -0x7fffffffffffffff);
|
||||
primary = cmp(bt ?? -_maxMillis, at ?? -_maxMillis);
|
||||
}
|
||||
break;
|
||||
case 'status':
|
||||
@@ -150,11 +152,11 @@ List<JsonMap> sortRuns(List<JsonMap> runs, {String sort = 'newest_first'}) {
|
||||
'${b['status'] ?? ''}'.toLowerCase(),
|
||||
);
|
||||
if (primary == 0) {
|
||||
primary = cmp(bt ?? -0x7fffffffffffffff, at ?? -0x7fffffffffffffff);
|
||||
primary = cmp(bt ?? -_maxMillis, at ?? -_maxMillis);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
primary = cmp(bt ?? -0x7fffffffffffffff, at ?? -0x7fffffffffffffff);
|
||||
primary = cmp(bt ?? -_maxMillis, at ?? -_maxMillis);
|
||||
}
|
||||
return primary == 0
|
||||
? ((b['id'] as num?) ?? 0).compareTo((a['id'] as num?) ?? 0)
|
||||
|
||||
@@ -1,80 +1,113 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
import '../../core/api_gateway.dart';
|
||||
import 'data/runs_repository.dart';
|
||||
import 'ui/run_detail_view.dart';
|
||||
import 'ui/runs_controller.dart';
|
||||
import 'ui/runs_list.dart';
|
||||
import 'ui/run_detail_view.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});
|
||||
const RunsScreen({
|
||||
super.key,
|
||||
required this.gateway,
|
||||
this.initialRunId,
|
||||
this.syncToken = 0,
|
||||
this.onRunSelected,
|
||||
this.onBack,
|
||||
});
|
||||
|
||||
final ApiGateway gateway;
|
||||
|
||||
/// The run the current route names, or null for the list.
|
||||
final int? initialRunId;
|
||||
|
||||
/// Advances when the shared socket is replaced. The loaded screen refreshes
|
||||
/// in place instead of being remounted, so its selection survives.
|
||||
final int syncToken;
|
||||
|
||||
final ValueChanged<int>? onRunSelected;
|
||||
final VoidCallback? onBack;
|
||||
|
||||
@override
|
||||
State<RunsScreen> createState() => _RunsScreenState();
|
||||
}
|
||||
|
||||
class _RunsScreenState extends State<RunsScreen> {
|
||||
late RunsController controller;
|
||||
num? _selectedRunId;
|
||||
late final RunsController controller;
|
||||
num? _shownRunId;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
controller = RunsController(RunsRepository(widget.gateway));
|
||||
controller.listen(widget.gateway.frames);
|
||||
controller.addListener(_changed);
|
||||
controller.addListener(_selectionChanged);
|
||||
controller.load(widget.initialRunId);
|
||||
}
|
||||
|
||||
void _changed() {
|
||||
@override
|
||||
void didUpdateWidget(covariant RunsScreen oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (widget.syncToken != oldWidget.syncToken) {
|
||||
controller.resync();
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 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 _select(num id) {
|
||||
controller.select(id);
|
||||
widget.onRunSelected?.call(id.toInt());
|
||||
}
|
||||
|
||||
void _back() {
|
||||
controller.back();
|
||||
widget.onBack?.call();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
controller.removeListener(_changed);
|
||||
controller.removeListener(_selectionChanged);
|
||||
controller.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final isCupertino = Theme.of(context).platform == TargetPlatform.iOS;
|
||||
final selected = _selectedRunId != null;
|
||||
final content = Stack(
|
||||
final selected = controller.selectedRunId != null;
|
||||
return Stack(
|
||||
children: [
|
||||
Offstage(
|
||||
offstage: selected,
|
||||
child: RunsList(controller: controller, onSelected: (id) {
|
||||
setState(() => _selectedRunId = id);
|
||||
controller.select(id);
|
||||
}),
|
||||
),
|
||||
if (selected)
|
||||
RunDetailView(
|
||||
controller: controller,
|
||||
onBack: () {
|
||||
controller.back();
|
||||
setState(() => _selectedRunId = null);
|
||||
},
|
||||
AnimatedBuilder(
|
||||
animation: controller,
|
||||
builder: (context, _) => Offstage(
|
||||
offstage: selected,
|
||||
child: RunsList(controller: controller, onSelected: _select),
|
||||
),
|
||||
),
|
||||
if (selected) RunDetailView(controller: controller, onBack: _back),
|
||||
],
|
||||
);
|
||||
if (isCupertino) {
|
||||
return CupertinoPageScaffold(
|
||||
navigationBar: CupertinoNavigationBar(
|
||||
middle: Text(selected ? 'Run ${controller.detail?['id'] ?? _selectedRunId}' : 'Runs'),
|
||||
leading: selected ? CupertinoButton(
|
||||
padding: EdgeInsets.zero,
|
||||
onPressed: () { controller.back(); setState(() => _selectedRunId = null); },
|
||||
child: const Text('Runs'),
|
||||
) : null,
|
||||
),
|
||||
child: SafeArea(child: content),
|
||||
);
|
||||
}
|
||||
return content;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ Widget buildCupertinoRunDetail({
|
||||
children: [
|
||||
const Icon(CupertinoIcons.back),
|
||||
const SizedBox(width: 4),
|
||||
const Text('Runs'),
|
||||
const Text('Back'),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
@@ -28,7 +28,7 @@ Widget buildMaterialRunDetail({
|
||||
child: TextButton.icon(
|
||||
onPressed: onBack,
|
||||
icon: const Icon(Icons.arrow_back),
|
||||
label: const Text('Runs'),
|
||||
label: const Text('Back to list'),
|
||||
),
|
||||
),
|
||||
Text(detailTitle(run), style: theme.textTheme.headlineSmall),
|
||||
|
||||
@@ -2,6 +2,7 @@ import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
import '../../../ui/adaptive/host.dart';
|
||||
import 'run_detail_content.dart';
|
||||
import 'run_detail_cupertino.dart';
|
||||
import 'run_detail_material.dart';
|
||||
@@ -29,7 +30,7 @@ class _RunDetailViewState extends State<RunDetailView> {
|
||||
Widget build(BuildContext context) => AnimatedBuilder(
|
||||
animation: widget.controller,
|
||||
builder: (context, _) {
|
||||
final cupertino = _usesCupertino(context);
|
||||
final cupertino = usesCupertinoHost(context);
|
||||
final run = widget.controller.detail;
|
||||
if (run == null) return _placeholder(context, cupertino: cupertino);
|
||||
final prompt = detailPrompt(run);
|
||||
@@ -57,34 +58,24 @@ class _RunDetailViewState extends State<RunDetailView> {
|
||||
},
|
||||
);
|
||||
|
||||
/// CupertinoApp supplies CupertinoLocalizations only, while MaterialApp
|
||||
/// supplies MaterialLocalizations. This distinguishes the two hosts without
|
||||
/// assuming a desktop browser's target platform.
|
||||
bool _usesCupertino(BuildContext context) =>
|
||||
Localizations.of<MaterialLocalizations>(context, MaterialLocalizations) ==
|
||||
null;
|
||||
|
||||
Widget _placeholder(BuildContext context, {required bool cupertino}) {
|
||||
if (widget.controller.detailLoading) {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
final loading = widget.controller.detailLoading;
|
||||
final message = loading
|
||||
? 'Loading run details…'
|
||||
: widget.controller.error ?? 'Run details unavailable.';
|
||||
// Back stays available while the detail is pending as well as after a
|
||||
// failure, so a slow or refused request can never trap the destination.
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
if (loading) ...[
|
||||
if (cupertino)
|
||||
const CupertinoActivityIndicator()
|
||||
else
|
||||
const CircularProgressIndicator(),
|
||||
const SizedBox(height: 12),
|
||||
const Text('Loading run details…'),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
final message = widget.controller.error ?? 'Run details unavailable.';
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 24),
|
||||
child: Text(message, textAlign: TextAlign.center),
|
||||
@@ -93,12 +84,12 @@ class _RunDetailViewState extends State<RunDetailView> {
|
||||
if (cupertino)
|
||||
CupertinoButton(
|
||||
onPressed: widget.onBack,
|
||||
child: const Text('Back to runs'),
|
||||
child: const Text('Back to list'),
|
||||
)
|
||||
else
|
||||
FilledButton(
|
||||
onPressed: widget.onBack,
|
||||
child: const Text('Back to runs'),
|
||||
child: const Text('Back to list'),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
@@ -61,6 +61,16 @@ class RunsController extends ChangeNotifier {
|
||||
if (initialId != null) await select(initialId);
|
||||
}
|
||||
|
||||
/// Re-reads the list and the open detail after the socket was replaced.
|
||||
///
|
||||
/// The selection is kept, so a reconnect refreshes in place instead of
|
||||
/// discarding the detail the operator had open.
|
||||
Future<void> resync() async {
|
||||
final selected = _selectedId;
|
||||
await load();
|
||||
if (selected != null && _selectedId == selected) await select(selected);
|
||||
}
|
||||
|
||||
Future<void> select(num id) async {
|
||||
final token = ++_detailGeneration;
|
||||
_cancelDetailRefresh();
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../core/api_gateway.dart';
|
||||
import '../../../ui/adaptive/host.dart';
|
||||
import '../domain/run_models.dart';
|
||||
import 'runs_controller.dart';
|
||||
|
||||
@@ -16,15 +16,11 @@ class RunsList extends StatelessWidget {
|
||||
final RunsController controller;
|
||||
final ValueChanged<num> onSelected;
|
||||
|
||||
bool get _isCupertino =>
|
||||
defaultTargetPlatform == TargetPlatform.iOS ||
|
||||
defaultTargetPlatform == TargetPlatform.macOS;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (controller.loading && controller.runs.isEmpty) {
|
||||
return Center(
|
||||
child: _isCupertino
|
||||
child: usesCupertinoHost(context)
|
||||
? const CupertinoActivityIndicator()
|
||||
: const CircularProgressIndicator(),
|
||||
);
|
||||
@@ -43,7 +39,7 @@ class RunsList extends StatelessWidget {
|
||||
if (controller.error != null) _inlineError(context),
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: _isCupertino
|
||||
child: usesCupertinoHost(context)
|
||||
? CupertinoSearchTextField(
|
||||
placeholder: 'Filter runs',
|
||||
onChanged: controller.setQuery,
|
||||
@@ -76,7 +72,7 @@ class RunsList extends StatelessWidget {
|
||||
children: [
|
||||
Text(controller.error!),
|
||||
const SizedBox(height: 8),
|
||||
_isCupertino
|
||||
usesCupertinoHost(context)
|
||||
? CupertinoButton(
|
||||
onPressed: controller.load,
|
||||
child: const Text('Retry'),
|
||||
@@ -94,7 +90,7 @@ class RunsList extends StatelessWidget {
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(child: Text(controller.error!)),
|
||||
if (_isCupertino)
|
||||
if (usesCupertinoHost(context))
|
||||
CupertinoButton(
|
||||
onPressed: controller.load,
|
||||
child: const Text('Retry'),
|
||||
@@ -106,7 +102,7 @@ class RunsList extends StatelessWidget {
|
||||
);
|
||||
|
||||
Widget _selectors(BuildContext context) {
|
||||
if (_isCupertino) {
|
||||
if (usesCupertinoHost(context)) {
|
||||
return Wrap(
|
||||
spacing: 8,
|
||||
children: [
|
||||
@@ -206,7 +202,7 @@ class RunsList extends StatelessWidget {
|
||||
final title = identity.label.isEmpty ? 'Run $id' : identity.label;
|
||||
final subtitle =
|
||||
'${run['name'] ?? 'Pipeline unavailable'} · ${run['created_at'] ?? 'Time unavailable'}${run['scheduled'] == true ? ' · Scheduled' : ''}';
|
||||
if (_isCupertino) {
|
||||
if (usesCupertinoHost(context)) {
|
||||
return CupertinoListTile(
|
||||
title: Text(title),
|
||||
subtitle: Text(subtitle),
|
||||
|
||||
Reference in New Issue
Block a user