Files
pipelines/app/lib/application/pipelines_app.dart
T

266 lines
9.8 KiB
Dart

import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import '../core/api_gateway.dart';
import '../core/transport/pipelines_client.dart';
import '../shell/adaptive_shell.dart';
import '../shell/destination.dart';
import '../features/attention/notify/notification_controller.dart';
import '../features/attention/notify/notification_store.dart';
import '../features/search/domain/search_target.dart';
import '../features/settings/settings_screen.dart';
import 'app.dart';
import 'app_route.dart';
import 'app_router.dart';
import 'app_session.dart';
import 'chat_destination.dart';
import 'pipelines_destination.dart';
import 'runs_destination.dart';
import 'schedules_destination.dart';
import 'search_destination.dart';
import 'server_endpoint.dart';
import 'templates_destination.dart';
/// Composes the live application around one [AppSession] and one [Router].
///
/// Tests may inject the client and route-information provider; the session
/// owns the socket either way, so there is never a second app-level connection.
class PipelinesApp extends StatefulWidget {
const PipelinesApp({
super.key,
required this.server,
this.client,
this.cupertino,
this.routeInformationProvider,
});
final ServerEndpoint server;
final PipelinesClient? client;
final bool? cupertino;
/// Test seam: the Router's initial and subsequent route information.
final RouteInformationProvider? routeInformationProvider;
@override
State<PipelinesApp> createState() => _PipelinesAppState();
}
class _PipelinesAppState extends State<PipelinesApp> {
late final AppSession _session;
late final AppRouterDelegate _delegate;
late final RouteInformationProvider _provider;
late final bool _ownsProvider;
late final RouterConfig<AppRoute> _config;
late final bool _cupertino;
/// Advances when a template create is accepted. The Pipelines destination
/// uses it to re-read through its mounted controller; no row is inserted
/// locally, so the next list reply is the only source of the new pipeline.
int _pipelinesRefresh = 0;
/// Advances when a launch was accepted. The Runs destination re-reads its
/// list once; the run itself is then opened from its route.
int _runsRefresh = 0;
/// One notification capability for the app: the Runs attention surface
/// publishes live transitions and Settings renders the same permission.
late final NotificationController _notifications = NotificationController(
store: const SharedPreferencesNotificationStore(),
);
@override
void initState() {
super.initState();
_session = AppSession(endpoint: widget.server.uri, client: widget.client);
_ownsProvider = widget.routeInformationProvider == null;
_provider =
widget.routeInformationProvider ??
PlatformRouteInformationProvider(
initialRouteInformation: initialRouteInformation(),
);
// A malformed or unknown boot path renders Pipelines, so make the address
// bar agree with the rendered destination instead of showing a dead link.
final initial = AppRoute.parse(_provider.value.uri.path);
if (_provider.value.uri.path != initial.path) {
_provider.routerReportsNewRouteInformation(
RouteInformation(uri: Uri(path: initial.path)),
type: RouteInformationReportingType.neglect,
);
}
_cupertino = widget.cupertino ?? _platformIsCupertino;
_notifications.onOpenRun = (id) => _delegate.goTo(AppRoute.run(id));
unawaited(_notifications.load());
_delegate = AppRouterDelegate(
initial: initial,
cupertino: _cupertino,
onPopToRuns: () => _delegate.goTo(AppRoute.runs),
onPopToChat: () => _delegate.goTo(AppRoute.chat),
searchBuilder: _buildSearch,
templatesBuilder: _buildTemplates,
schedulesBuilder: _buildSchedules,
builder: _buildShell,
);
_config = RouterConfig<AppRoute>(
routeInformationParser: const AppRouteParser(),
routerDelegate: _delegate,
routeInformationProvider: _provider,
);
unawaited(_session.start());
}
@override
void dispose() {
_notifications.dispose();
_delegate.dispose();
if (_ownsProvider) {
(_provider as PlatformRouteInformationProvider).dispose();
}
_session.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) =>
MyApp.router(cupertino: _cupertino, routerConfig: _config);
/// The transient Search page. Escape matches the browser's panel shortcut;
/// the native Back button and browser Back come from the router and page.
Widget _buildSearch(BuildContext context) => CallbackShortcuts(
bindings: {
const SingleActivator(LogicalKeyboardKey.escape): _delegate.closeSearch,
},
child: SearchDestination(
session: _session,
server: widget.server,
onOpen: _openSearchResult,
),
);
/// Open a result through the exact identity the hit records. An unresolvable
/// hit navigates nowhere: the screen already presents it read-only rather than
/// guessing a destination.
void _openSearchResult(JsonMap hit) {
final resolved = searchTargetFor(hit);
if (resolved is! ResolvedSearchTarget) return;
final route = switch (resolved.target) {
SearchPipelineTarget(:final pipelineId) => AppRoute.pipeline(pipelineId),
SearchVersionTarget(:final pipelineId, :final version) =>
AppRoute.pipeline(pipelineId, version: version),
SearchRunTarget(:final runId) => AppRoute.run(runId),
SearchStepTarget(:final runId, :final stage, :final visit) =>
stage == null
? AppRoute.run(runId)
: AppRoute.run.stage(runId, stage, visit: visit),
// A field or artifact names a visit inside the run; the run detail owns
// that pane, so open the run rather than inventing a stage route.
SearchFieldTarget(:final runId) => AppRoute.run(runId),
SearchArtifactTarget(:final runId) =>
runId == null ? null : AppRoute.run(runId),
};
if (route != null) _delegate.goTo(route);
}
/// The Templates page above Settings. Closing it restores the same Settings
/// screen instance, so an edited field or an open connection state survives.
Widget _buildSchedules(BuildContext context) => SchedulesDestination(
session: _session,
server: widget.server,
onBack: _delegate.closeSchedules,
onOpenPipeline: (id) => _delegate.goTo(AppRoute.pipeline(id)),
);
Widget _buildTemplates(BuildContext context) => TemplatesDestination(
session: _session,
server: widget.server,
onBack: _delegate.closeTemplates,
onCreated: _templateCreated,
initialKey: _delegate.route.templateKey,
);
/// An accepted create only invalidates Pipelines; the list is re-read and the
/// new row appears from the server's reply, never from an optimistic insert.
void _templateCreated(JsonMap _) {
if (!mounted) return;
setState(() => _pipelinesRefresh++);
}
/// Opens the run id an accepted launch named and invalidates the Runs list.
///
/// The order is deliberate: the list token is already advanced when the Runs
/// screen mounts for the first time, so it performs one fresh `list_runs`
/// read and then opens the route's run. No row is inserted locally.
void _runStarted(int id) {
if (!mounted) return;
setState(() => _runsRefresh++);
_delegate.goTo(AppRoute.run(id));
}
Widget _buildShell(BuildContext context, AppRoute route) => AdaptiveShell(
cupertino: _cupertino,
destination: _destinationOf(route.kind),
onDestinationSelected: (destination) =>
_delegate.goTo(_routeOf(destination)),
onSearch: _delegate.openSearch,
destinationBodies: {
AppDestination.pipelines: (_) => PipelinesDestination(
session: _session,
server: widget.server,
refreshToken: _pipelinesRefresh,
onRunStarted: _runStarted,
initialPipelineId: route.pipelineId,
initialVersion: route.version,
),
AppDestination.runs: (_) => RunsDestination(
session: _session,
server: widget.server,
initialRunId: route.runId,
listRefreshToken: _runsRefresh,
notifications: _notifications,
onRunSelected: (id) => _delegate.goTo(AppRoute.run(id)),
onBack: () => _delegate.goTo(AppRoute.runs),
),
AppDestination.chat: (_) => ChatDestination(
session: _session,
server: widget.server,
initialSessionId: route.chatSessionId,
onSessionSelected: (id) => _delegate.goTo(AppRoute.chatSession(id)),
onBack: () => _delegate.goTo(AppRoute.chat),
),
AppDestination.settings: (_) => SettingsScreen(
client: _session.client,
initialEndpoint: _session.endpoint,
embedded: true,
onOpenTemplates: _delegate.openTemplates,
onOpenSchedules: _delegate.openSchedules,
notifications: _notifications,
),
},
);
AppDestination _destinationOf(AppRouteKind kind) => switch (kind) {
AppRouteKind.pipelines => AppDestination.pipelines,
AppRouteKind.runs => AppDestination.runs,
AppRouteKind.chat => AppDestination.chat,
// Search is a transient page above the shell; it never becomes a tab.
AppRouteKind.search => AppDestination.pipelines,
AppRouteKind.settings => AppDestination.settings,
// Templates and Schedules are Settings sub-pages, not a fifth tab.
AppRouteKind.templates => AppDestination.settings,
AppRouteKind.schedules => AppDestination.settings,
};
AppRoute _routeOf(AppDestination destination) => switch (destination) {
AppDestination.pipelines => AppRoute.pipelines,
AppDestination.runs => AppRoute.runs,
AppDestination.chat => AppRoute.chat,
AppDestination.settings => AppRoute.settings,
};
bool get _platformIsCupertino =>
defaultTargetPlatform == TargetPlatform.iOS ||
defaultTargetPlatform == TargetPlatform.macOS;
}