From d19a14b9f9d060fe82b3d4b87f0667b6447e5d48 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Thu, 12 Nov 2026 20:19:10 +0000 Subject: [PATCH] Hide the phone chat as soon as its Back is pressed _back marked the open chat hidden without rebuilding and relied on the owner's route change to redraw. When the chat was shown while the app was already on /chat (a restored layout, the Chat tab re-selected) nothing changed, so the chat stayed on screen for seconds while the list behind it took the taps. Found on the device: closeChat logged route=null and the entrance never animated out. _back now rebuilds itself. --- app/lib/features/chat/screen.dart | 25 +++++-- .../features/chat/chat_back_rebuild_test.dart | 73 +++++++++++++++++++ 2 files changed, 90 insertions(+), 8 deletions(-) create mode 100644 app/test/features/chat/chat_back_rebuild_test.dart diff --git a/app/lib/features/chat/screen.dart b/app/lib/features/chat/screen.dart index da02893a..91741c65 100644 --- a/app/lib/features/chat/screen.dart +++ b/app/lib/features/chat/screen.dart @@ -333,17 +333,26 @@ class _ChatScreenState extends State @override void _back() { - _workspaceVisible = false; - _publishedWorkspaceSession = null; + // Rebuild here rather than wait for the owner's route change: when the + // chat was shown without its own route (the Chat tab re-selected, a + // restored layout) the owner is already on /chat, nothing else rebuilds + // this screen, and the chat would stay on screen while the list behind + // it took the taps. + void hide() { + _workspaceVisible = false; + _publishedWorkspaceSession = null; + if (widget.onBack == null) _standaloneSelection = null; + } + + if (mounted) { + setState(hide); + } else { + hide(); + } _runtime.startNotice = null; _composerFocus.cancel(); if (_workspace.paneCount == 0) widget.onWorkspaceEmpty?.call(); - final back = widget.onBack; - if (back != null) { - back(); - } else { - setState(() => _standaloneSelection = null); - } + widget.onBack?.call(); } /// On a phone the open chat is a real page above the main screen. diff --git a/app/test/features/chat/chat_back_rebuild_test.dart b/app/test/features/chat/chat_back_rebuild_test.dart new file mode 100644 index 00000000..ba153a24 --- /dev/null +++ b/app/test/features/chat/chat_back_rebuild_test.dart @@ -0,0 +1,73 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:shared_preferences/shared_preferences.dart'; +import 'package:pipelines_app/core/api_gateway.dart'; +import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart'; +import 'package:pipelines_app/features/chat/screen.dart'; +import 'package:pipelines_app/features/chat/workspace/chat_workspace.dart'; +import 'package:pipelines_app/features/chat/workspace/chat_workspace_store.dart'; + +import 'conversation/chat_thread_fixtures.dart'; + +class _Gateway extends FakeChatGateway { + @override + Future call(String type, [JsonMap fields = const {}]) { + if (type == 'chat_sessions') { + return Future.value({ + 'chat_sessions': [ + for (final id in ['a', 'b']) + {'id': id, 'title': 'Chat $id', 'directory': '/home/$id'}, + ], + }); + } + if (type == 'chat_shells') return Future.value({'chat_shells': []}); + return super.call(type, fields); + } +} + +Finder _thread(String id) => find.byWidgetPredicate( + (widget) => widget is ChatThreadView && widget.sessionId == id, +); + +void main() { + testWidgets('phone Back hides a restored chat at once when the owner is ' + 'already on the chat list route', (tester) async { + // A restored layout shows chat a while the app is already on /chat: Back + // asks the owner to go to /chat, which changes nothing, so the screen must + // hide the chat itself instead of waiting for an unrelated rebuild. + final layout = ChatWorkspaceController()..open('a'); + SharedPreferences.setMockInitialValues({ + chatWorkspacePreferenceKey: layout.snapshot()!.encode(), + }); + tester.view.physicalSize = const Size(412, 915); + tester.view.devicePixelRatio = 1; + addTearDown(tester.view.reset); + final gateway = _Gateway()..history = (_) async => chatPage([]); + addTearDown(gateway.close); + var backs = 0; + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: ChatScreen( + gateway: gateway, + persistWorkspace: true, + onSessionSelected: (_) {}, + onBack: () => backs++, + ), + ), + ), + ); + await tester.pumpAndSettle(); + expect(_thread('a'), findsOneWidget); + + await tester.tap(find.byType(BackButton).hitTestable()); + // Only the screen's own frames: nothing else rebuilds it. + for (var i = 0; i < 30; i++) { + await tester.pump(const Duration(milliseconds: 16)); + } + expect(backs, 1); + expect(_thread('a'), findsNothing); + expect(find.text('Chat a').hitTestable(), findsWidgets); + expect(tester.takeException(), isNull); + }); +}