import 'package:flutter/cupertino.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'app_route.dart'; /// Parses the browser (or platform) location into the app's routes. class AppRouteParser extends RouteInformationParser { const AppRouteParser(); @override Future parseRouteInformation( RouteInformation routeInformation, ) async => AppRoute.parse(routeLocation(routeInformation.uri)); @override RouteInformation restoreRouteInformation(AppRoute configuration) => // The route's path already carries its query (`?visit=N`, `?action=…`). // Parsing it keeps that query a query instead of percent-encoding the // `?` into the path, which would make a reload read one bogus key. RouteInformation(uri: Uri.parse(configuration.path)); } /// The app-relative location of [uri]: its path plus query, without origin. /// /// [AppRoute.parse] understands this form, so a browser location such as /// `/runs/7/stage/plan?visit=1` keeps the query instead of losing it to /// `Uri.path`. A fragment is not part of an app route. /// /// The native notification link `pipelines://run/` (the URL the server /// publishes as the notification's click target, as the Android client /// registers it) is the run route `/runs/`. String routeLocation(Uri uri) { final path = uri.scheme == 'pipelines' && uri.host == 'run' ? '/runs${uri.path}' : uri.path; return uri.hasQuery ? '$path?${uri.query}' : path; } /// Owns the app's current route and builds the shell for it. /// /// Route changes made by the operator go through [goTo]; the Router reports /// them as browser history entries. Browser Back/Forward and deep links arrive /// through [setNewRoutePath] and update the same route. class AppRouterDelegate extends RouterDelegate with ChangeNotifier { AppRouterDelegate({ required this.builder, required this.onPopToRuns, required this.onPopToChat, required this.cupertino, this.searchBuilder, this.templatesBuilder, this.schedulesBuilder, AppRoute initial = AppRoute.pipelines, }) : _route = initial, _searchReturn = initial.kind == AppRouteKind.search ? AppRoute.pipelines : initial, _templatesReturn = initial.kind == AppRouteKind.templates || initial.kind == AppRouteKind.schedules ? AppRoute.settings : initial; final Widget Function(BuildContext context, AppRoute route) builder; /// The Templates page above the shell. Templates belongs to Settings, so the /// shell keeps the Settings destination mounted underneath and its state is /// restored when this page closes. final Widget Function(BuildContext context)? templatesBuilder; final Widget Function(BuildContext context)? schedulesBuilder; /// The Search page above the shell. Search is transient chrome, not a fifth /// tab: the shell stays mounted underneath so closing restores its loaded /// state and selection, and the standard navigation bar supplies Back. final Widget Function(BuildContext context)? searchBuilder; /// Called when a system Back belongs to the Runs detail, not the app stack. final VoidCallback onPopToRuns; /// Called when a system Back belongs to one chat session. final VoidCallback onPopToChat; final bool cupertino; final _navigatorKey = GlobalKey(); static const _pageKey = ValueKey('pipelines-shell'); static const _searchPageKey = ValueKey('app-search'); static const _templatesPageKey = ValueKey('app-templates'); static const _schedulesPageKey = ValueKey('app-schedules'); AppRoute _route; /// The destination that was open before Search, restored on close. AppRoute _searchReturn; /// The Settings route the Templates page closes back to. AppRoute _templatesReturn; AppRoute get route => _route; /// Whether a dialog, popup, or other route is covering the shell page. bool get hasTransientRoute => _navigatorKey.currentState?.canPop() ?? false; bool get searchOpen => _route.kind == AppRouteKind.search; bool get templatesOpen => _route.kind == AppRouteKind.templates; /// The saved-schedule list page, opened above Settings. bool get schedulesOpen => _route.kind == AppRouteKind.schedules; /// The route the shell renders underneath the transient Search, Templates /// and Schedules pages. Those pages keep the shell mounted, so the route the /// operator last opened there is still the shell's own destination. AppRoute get shellRoute => searchOpen && searchBuilder != null ? _searchReturn : templatesOpen || schedulesOpen ? _templatesReturn : _route; @override AppRoute get currentConfiguration => _route; Page _page(LocalKey key, Widget child, {bool dialog = false}) => cupertino ? CupertinoPage(key: key, child: child, fullscreenDialog: dialog) : MaterialPage(key: key, child: child, fullscreenDialog: dialog); /// A page-based Navigator supplies the Overlay, overlay portals and route /// context that native controls (action sheets, tooltips) require. The shell /// page key is stable, so changing the route updates it in place instead of /// remounting it and losing the open run. Search is the only extra page, so /// its native Close action pops just that page. @override Widget build(BuildContext context) { final searching = searchOpen && searchBuilder != null; final templates = templatesOpen && templatesBuilder != null; final schedules = schedulesOpen && schedulesBuilder != null; final shellRoute = searching ? _searchReturn : templates || schedules ? _templatesReturn : _route; return Navigator( key: _navigatorKey, pages: [ _page(_pageKey, builder(context, shellRoute)), // Templates is a Settings sub-page: the shell stays mounted underneath, // so closing restores Settings exactly as it was left. if (templates) _page(_templatesPageKey, templatesBuilder!(context)), if (schedules) _page(_schedulesPageKey, schedulesBuilder!(context)), // Search is a transient full-screen mode, so a full-screen dialog gives // the standard Close/Cancel action instead of a Back chevron. if (searching) _page(_searchPageKey, searchBuilder!(context), dialog: true), ], onDidRemovePage: (page) { if (page.key == _searchPageKey) closeSearch(); if (page.key == _templatesPageKey) closeTemplates(); if (page.key == _schedulesPageKey) closeSchedules(); }, ); } /// Open the Templates gallery above Settings. The browser history entry is /// `/templates`; Back returns to the same Settings screen instance. void openTemplates() { if (templatesOpen) return; _templatesReturn = AppRoute.settings; goTo(AppRoute.templates); } void closeTemplates() { if (!templatesOpen) return; goTo(AppRoute.settings); } /// Open the saved-schedule list above Settings. Back restores the same /// Settings screen instance, like Templates. void openSchedules() { if (schedulesOpen) return; _templatesReturn = AppRoute.settings; goTo(AppRoute.schedules); } void closeSchedules() { if (!schedulesOpen) return; goTo(AppRoute.settings); } /// Open Search above the current destination; its route carries the browser /// history entry, and close/back returns to what was open before. void openSearch() { if (searchOpen) return; _searchReturn = _route; goTo(AppRoute.search); } void closeSearch() { if (!searchOpen) return; goTo(_searchReturn); } @override Future setInitialRoutePath(AppRoute configuration) async { _route = configuration; } @override Future setNewRoutePath(AppRoute configuration) async { if (configuration == _route) return; _route = configuration; notifyListeners(); } /// Back (Android's button, the browser's): close whatever page sits on top /// first, then step up one level — a stage to its run or pipeline, a run, /// pipeline or chat to its list — and only leave the app from a list. @override Future popRoute() async { final navigator = _navigatorKey.currentState; if (navigator != null && await navigator.maybePop()) return true; if (searchOpen) { closeSearch(); return true; } if (schedulesOpen) { closeSchedules(); return true; } if (templatesOpen) { closeTemplates(); return true; } final runId = _route.runId; if (runId != null && _route.stageKey != null) { goTo(AppRoute.run(runId)); return true; } if (runId != null) { onPopToRuns(); return true; } final pipelineId = _route.pipelineId; if (_route.kind == AppRouteKind.pipelines && pipelineId != null) { goTo( _route.stageKey != null ? AppRoute.pipeline(pipelineId, version: _route.version) : AppRoute.pipelines, ); return true; } if (_route.chatSessionId != null) { onPopToChat(); return true; } return false; } /// Records a user move; the Router turns the new configuration into a /// history entry (or a replacement when it already matches the location). void goTo(AppRoute route) { if (route == _route) return; _route = route; notifyListeners(); } } /// The location a launch starts at: the browser location on the web, the /// deep link a native app was opened with, or the Pipelines list. /// /// The origin stays out of route configuration so a serialised route is always /// app-relative, but the path and its query are both part of the route: a /// launch at `/runs/7/stage/plan?visit=1` must open exactly that visit. RouteInformation initialRouteInformation() { if (!kIsWeb) { // A cold start from a deep link carries the full link as the platform's // initial route; an ordinary launch opens the Pipelines list. final launched = WidgetsBinding.instance.platformDispatcher.defaultRouteName; return RouteInformation( uri: launched.isEmpty || launched == '/' ? Uri(path: '/pipelines') : Uri.parse(launched), ); } return routeInformationForLocation(Uri.base); } /// The app-relative route information for a browser [base] location. /// /// Exposed for the boot test: [Uri.base] cannot be changed inside a widget /// test, so the web launch seam is exercised through this function. @visibleForTesting RouteInformation routeInformationForLocation(Uri base) { final path = base.path.isEmpty ? '/' : base.path; final query = base.hasQuery ? '?${base.query}' : ''; return RouteInformation(uri: Uri.parse('$path$query')); }