fix(chat): double-back to close with a chat open beside the list

This commit is contained in:
2026-10-24 16:20:56 +01:00
parent 2740bb0673
commit 7bb95d4610
3 changed files with 120 additions and 1 deletions
+9 -1
View File
@@ -1,6 +1,7 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import '../shell/list_detail.dart';
import 'android_root_exit.dart';
import 'app_route.dart';
@@ -134,8 +135,13 @@ class AppRouterDelegate extends RouterDelegate<AppRoute> with ChangeNotifier {
/// 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.
/// Whether the window shows an open chat beside its list rather than as a
/// pushed page. Read in [build], so a resize rebuilds the Back policy.
bool _chatBesideList = false;
@override
Widget build(BuildContext context) {
_chatBesideList = showsListBesideDetail(context);
final searching = searchOpen && searchBuilder != null;
final templates = templatesOpen && templatesBuilder != null;
final schedules = schedulesOpen && schedulesBuilder != null;
@@ -263,7 +269,9 @@ class AppRouterDelegate extends RouterDelegate<AppRoute> with ChangeNotifier {
bool get _isExitRoot =>
_rootExit.enabled &&
_route.chatSessionId == null &&
// A phone's open chat is a pushed page with stock Back. Beside the list
// it is no page at all, so the list root's double-back applies.
(_route.chatSessionId == null || _chatBesideList) &&
!_canStepUp &&
switch (_route.kind) {
AppRouteKind.pipelines ||
+8
View File
@@ -34,3 +34,11 @@ user explicitly asked to remove chat Back interception; needs a decision.
- Browser: `tool/chat_list_detail_probe.mjs` on a loopback fixture,
captures in `build/chat-list-detail-1280/` and `build/chat-list-detail-800/`.
- Not deployed, no APK, no device verification.
## Follow-up: wide Back
User asked to fix the single-press exit. With a chat open beside the list the
router now applies the list root's double-back (`_chatBesideList`, read from the
window width in `AppRouterDelegate.build`); phones keep the pushed chat page and
stock Back with no root interception. No chat-specific Back callback was added.
Regression: `test/application/chat_wide_back_test.dart`; full suite 1,866 pass.
@@ -0,0 +1,103 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:pipelines_app/application/app_route.dart';
import 'package:pipelines_app/application/app_router.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/chat/screen.dart';
import 'package:pipelines_app/shell/adaptive_shell.dart';
import 'package:pipelines_app/shell/destination.dart';
import 'package:pipelines_app/ui/theme/app_theme.dart';
import '../features/chat/conversation/chat_thread_fixtures.dart';
class _Gateway extends FakeChatGateway {
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) {
if (type == 'chat_sessions') {
return Future.value({
'chat_sessions': [
{'id': 's', 'title': 'Beside the list'},
],
});
}
if (type == 'chat_shells') return Future.value({'chat_shells': []});
return super.call(type, fields);
}
}
void main() {
testWidgets('an open chat beside the list takes the root double-back', (
tester,
) async {
SharedPreferences.setMockInitialValues({});
tester.view.physicalSize = const Size(1000, 800);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
final exits = <String>[];
tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
SystemChannels.platform,
(call) async {
if (call.method == 'SystemNavigator.pop') exits.add(call.method);
return null;
},
);
addTearDown(
() => tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
SystemChannels.platform,
null,
),
);
final gateway = _Gateway()..history = (_) async => chatPage(const []);
addTearDown(gateway.close);
late final AppRouterDelegate router;
router = AppRouterDelegate(
initial: AppRoute.chat,
cupertino: false,
onPopToChat: () => fail('Chat Back must not use the router override'),
onPopToRuns: () {},
builder: (_, route) => AdaptiveShell(
destination: AppDestination.chat,
destinationBodies: {
AppDestination.chat: (_) => ChatScreen(
gateway: gateway,
initialSessionId: route.chatSessionId,
onSessionSelected: (id) => router.goTo(AppRoute.chatSession(id)),
onBack: () => router.goTo(AppRoute.chat),
),
},
),
);
final provider = PlatformRouteInformationProvider(
initialRouteInformation: RouteInformation(uri: Uri.parse('/chat')),
);
addTearDown(provider.dispose);
await tester.pumpWidget(
MaterialApp.router(
theme: appLightTheme(),
routerDelegate: router,
routeInformationParser: const AppRouteParser(),
routeInformationProvider: provider,
),
);
await tester.pumpAndSettle();
await tester.tap(find.text('Beside the list'));
await tester.pumpAndSettle();
expect(router.route, AppRoute.chatSession('s'));
expect(find.byKey(const ValueKey('chat-thread-s')), findsOneWidget);
// First Back arms the exit instead of closing the app at once.
await tester.binding.handlePopRoute();
await tester.pumpAndSettle();
expect(find.text('Press back again to close'), findsOneWidget);
expect(exits, isEmpty);
expect(find.byKey(const ValueKey('chat-thread-s')), findsOneWidget);
await tester.binding.handlePopRoute();
await tester.pump();
expect(exits, ['SystemNavigator.pop']);
await tester.pumpWidget(const SizedBox.shrink());
router.dispose();
});
}