fix(app): Android Back and the back gesture go through the router: they step up a level, and ask before closing the app; an open pipeline is part of the route
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// Asks before Back leaves the app from the top of a tab. True to leave.
|
||||
Future<bool> confirmAppExit(
|
||||
BuildContext context, {
|
||||
required bool cupertino,
|
||||
}) async {
|
||||
const title = Text('Close Pipelines?');
|
||||
final leave = cupertino
|
||||
? await showCupertinoDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => CupertinoAlertDialog(
|
||||
title: title,
|
||||
actions: [
|
||||
CupertinoDialogAction(
|
||||
key: const Key('app-exit-stay'),
|
||||
isDefaultAction: true,
|
||||
onPressed: () => Navigator.of(context).pop(false),
|
||||
child: const Text('Stay'),
|
||||
),
|
||||
CupertinoDialogAction(
|
||||
key: const Key('app-exit-close'),
|
||||
onPressed: () => Navigator.of(context).pop(true),
|
||||
child: const Text('Close'),
|
||||
),
|
||||
],
|
||||
),
|
||||
)
|
||||
: await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: title,
|
||||
actions: [
|
||||
TextButton(
|
||||
key: const Key('app-exit-stay'),
|
||||
onPressed: () => Navigator.of(context).pop(false),
|
||||
child: const Text('Stay'),
|
||||
),
|
||||
FilledButton(
|
||||
key: const Key('app-exit-close'),
|
||||
onPressed: () => Navigator.of(context).pop(true),
|
||||
child: const Text('Close'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
return leave ?? false;
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
import 'app_exit_dialog.dart';
|
||||
import 'app_route.dart';
|
||||
|
||||
/// Parses the browser (or platform) location into the app's routes.
|
||||
@@ -144,7 +146,19 @@ class AppRouterDelegate extends RouterDelegate<AppRoute> with ChangeNotifier {
|
||||
return Navigator(
|
||||
key: _navigatorKey,
|
||||
pages: [
|
||||
_page(_pageKey, builder(context, shellRoute)),
|
||||
// While an item is open inside its tab (a wide screen keeps it
|
||||
// there), Android's Back and back gesture step up a level instead of
|
||||
// leaving the app: the shell tells the system it handles Back.
|
||||
_page(
|
||||
_pageKey,
|
||||
PopScope(
|
||||
canPop: !_canStepUp && !_asksBeforeExit,
|
||||
onPopInvokedWithResult: (didPop, _) {
|
||||
if (!didPop && !_stepUp()) _confirmExit();
|
||||
},
|
||||
child: 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)),
|
||||
@@ -232,6 +246,38 @@ class AppRouterDelegate extends RouterDelegate<AppRoute> with ChangeNotifier {
|
||||
closeTemplates();
|
||||
return true;
|
||||
}
|
||||
if (_stepUp()) return true;
|
||||
return _confirmExit();
|
||||
}
|
||||
|
||||
/// At the top of a tab Android's Back would leave the app; ask first.
|
||||
/// Elsewhere (a browser) Back keeps its usual meaning.
|
||||
bool get _asksBeforeExit =>
|
||||
!kIsWeb && defaultTargetPlatform == TargetPlatform.android;
|
||||
|
||||
bool _confirming = false;
|
||||
|
||||
bool _confirmExit() {
|
||||
final context = _navigatorKey.currentContext;
|
||||
if (!_asksBeforeExit || context == null) return false;
|
||||
if (_confirming) return true;
|
||||
_confirming = true;
|
||||
confirmAppExit(context, cupertino: cupertino).then((leave) {
|
||||
_confirming = false;
|
||||
if (leave) SystemNavigator.pop();
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Whether the open route has a level above it in its own tab (a stage,
|
||||
/// a run, a pipeline, a chat).
|
||||
bool get _canStepUp =>
|
||||
_route.runId != null ||
|
||||
_route.chatSessionId != null ||
|
||||
(_route.kind == AppRouteKind.pipelines && _route.pipelineId != null);
|
||||
|
||||
/// Goes up one level: a stage to its run or pipeline, an item to its list.
|
||||
bool _stepUp() {
|
||||
final runId = _route.runId;
|
||||
if (runId != null && _route.stageKey != null) {
|
||||
goTo(AppRoute.run(runId));
|
||||
|
||||
@@ -178,6 +178,9 @@ class _PipelinesAppState extends State<PipelinesApp>
|
||||
routeInformationParser: const AppRouteParser(),
|
||||
routerDelegate: _delegate,
|
||||
routeInformationProvider: _provider,
|
||||
// Android's Back button and back gesture go through the router, so
|
||||
// they close a page or step up a level instead of leaving the app.
|
||||
backButtonDispatcher: RootBackButtonDispatcher(),
|
||||
);
|
||||
unawaited(_session.start());
|
||||
}
|
||||
@@ -434,6 +437,18 @@ class _PipelinesAppState extends State<PipelinesApp>
|
||||
onViewPipelineRuns: (id) =>
|
||||
_delegate.goTo(AppRoute.runsForPipeline(id)),
|
||||
onOpenSchedules: _delegate.openSchedules,
|
||||
// The open pipeline is part of the route, so Back steps out of it.
|
||||
onSelectionChanged: (id) {
|
||||
final current = _delegate.route;
|
||||
if (current.kind != AppRouteKind.pipelines) return;
|
||||
if (id == null) {
|
||||
if (current.pipelineId != null) {
|
||||
_delegate.goTo(AppRoute.pipelines);
|
||||
}
|
||||
} else if (current.pipelineId != id) {
|
||||
_delegate.goTo(AppRoute.pipeline(id));
|
||||
}
|
||||
},
|
||||
initialPipelineId: pipelinesRoute.pipelineId,
|
||||
initialVersion: pipelinesRoute.version,
|
||||
initialStageKey: pipelinesRoute.stageKey,
|
||||
|
||||
@@ -21,6 +21,7 @@ class PipelinesDestination extends StatelessWidget {
|
||||
this.onRunStarted,
|
||||
this.onViewPipelineRuns,
|
||||
this.onOpenSchedules,
|
||||
this.onSelectionChanged,
|
||||
this.initialPipelineId,
|
||||
this.initialVersion = 0,
|
||||
this.initialStageKey,
|
||||
@@ -38,6 +39,7 @@ class PipelinesDestination extends StatelessWidget {
|
||||
final ValueChanged<int>? onRunStarted;
|
||||
final ValueChanged<int>? onViewPipelineRuns;
|
||||
final VoidCallback? onOpenSchedules;
|
||||
final ValueChanged<int?>? onSelectionChanged;
|
||||
|
||||
/// The pipeline and pinned version a deep link names.
|
||||
final int? initialPipelineId;
|
||||
@@ -84,6 +86,7 @@ class PipelinesDestination extends StatelessWidget {
|
||||
onRunStarted: onRunStarted,
|
||||
onViewPipelineRuns: onViewPipelineRuns,
|
||||
onOpenSchedules: onOpenSchedules,
|
||||
onSelectionChanged: onSelectionChanged,
|
||||
initialPipelineId: initialPipelineId,
|
||||
initialVersion: initialVersion,
|
||||
initialStageKey: initialStageKey,
|
||||
|
||||
Reference in New Issue
Block a user