diff --git a/mobile/lib/shared/widgets/adaptive_conversation_pane.dart b/mobile/lib/shared/widgets/adaptive_conversation_pane.dart index 25ca737a78..5f23341e3c 100644 --- a/mobile/lib/shared/widgets/adaptive_conversation_pane.dart +++ b/mobile/lib/shared/widgets/adaptive_conversation_pane.dart @@ -195,6 +195,11 @@ class AdaptiveConversationPane extends HookConsumerWidget { // The compact detail route that fully covers the list. While a detail // pushes, pops or follows a back swipe, the list stays painted beneath it. final coveringDetail = useValueNotifier?>(null); + // Compact detail routes still mounted. A pop clears the selection as it + // starts, so the detail stays onstage until its reverse transition ends. + // Not disposed with the pane: a detail unmounting with the pane reports + // after the frame, once the pane is gone. + final mountedDetails = useMemoized(() => ValueNotifier(0)); return ConversationPaneScope( controller: controller, active: active && (ModalRoute.isCurrentOf(context) ?? true), @@ -291,118 +296,122 @@ class AdaptiveConversationPane extends HookConsumerWidget { top: 0, bottom: 0, width: detailWidth, - child: Offstage( - offstage: !wide && selected == null, - child: TickerMode( - enabled: wide || selected != null, - child: MediaQueryOverride( - transform: (media) => media.copyWith( - size: Size(detailWidth, constraints.maxHeight), + child: ValueListenableBuilder( + valueListenable: mountedDetails, + builder: (context, mounted, detail) { + final shown = wide || selected != null || mounted > 0; + return Offstage( + offstage: !shown, + child: TickerMode(enabled: shown, child: detail!), + ); + }, + child: MediaQueryOverride( + transform: (media) => media.copyWith( + size: Size(detailWidth, constraints.maxHeight), + ), + child: DuoToolbarScope( + strip: strip, + leadingSlots: wide ? listToolbarSlots : 0, + contentInsets: EdgeInsets.only( + left: wide ? (book?.width ?? 0) / 2 : leftInset, + right: rightInset, ), - child: DuoToolbarScope( - strip: strip, - leadingSlots: wide ? listToolbarSlots : 0, - contentInsets: EdgeInsets.only( - left: wide ? (book?.width ?? 0) / 2 : leftInset, - right: rightInset, - ), - active: - active && - selected != null && - (ModalRoute.isCurrentOf(context) ?? true), - child: Semantics( - container: true, - explicitChildNodes: true, - child: PopScope( - canPop: - !active || - selected == null || - (wide && persistentSelection), - onPopInvokedWithResult: (didPop, result) { - if (!didPop && - active && - selected != null && - !(wide && persistentSelection)) { - navigator.currentState!.maybePop(result); - } - }, - child: ExcludeFocus( - excluding: selected == null, - child: NavigatorPopHandler( - enabled: wide && persistentSelection, - onPopWithResult: (result) => - navigator.currentState!.pop(result), - child: Navigator( - requestFocus: selected != null, - key: navigator, - observers: [routeObserver], - pages: [ - if (!(wide && - persistentSelection && - selected != null)) - MaterialPage( - key: const ValueKey( - 'conversation-placeholder', - ), - // Compact panes show the list - // through this page while a - // detail pushes or pops. - child: !wide - ? const SizedBox.shrink() - : Material( - color: context - .colors - .surface, - child: Center( - child: - emptyContent ?? - Text( - emptyLabel, - textAlign: - TextAlign - .center, - style: Theme.of(context) - .textTheme - .bodyLarge - ?.copyWith( - color: Theme.of( - context, - ).colorScheme.onSurfaceVariant, - decoration: - TextDecoration - .none, - ), - ), - ), - ), + active: + active && + selected != null && + (ModalRoute.isCurrentOf(context) ?? true), + child: Semantics( + container: true, + explicitChildNodes: true, + child: PopScope( + canPop: + !active || + selected == null || + (wide && persistentSelection), + onPopInvokedWithResult: (didPop, result) { + if (!didPop && + active && + selected != null && + !(wide && persistentSelection)) { + navigator.currentState!.maybePop(result); + } + }, + child: ExcludeFocus( + excluding: selected == null, + child: NavigatorPopHandler( + enabled: wide && persistentSelection, + onPopWithResult: (result) => + navigator.currentState!.pop(result), + child: Navigator( + requestFocus: selected != null, + key: navigator, + observers: [routeObserver], + pages: [ + if (!(wide && + persistentSelection && + selected != null)) + MaterialPage( + key: const ValueKey( + 'conversation-placeholder', ), - if (selected != null) - if (controller.animateSelection) - MaterialPage( - key: controller.selectionKey, - child: _CoveringDetail( - covering: coveringDetail, - builder: - controller.detailBuilder!, - ), - ) - else - _ImmediateDetailPage( - key: controller.selectionKey, - builder: (_) => _CoveringDetail( - covering: coveringDetail, - builder: - controller.detailBuilder!, - ), + // Compact panes show the list + // through this page while a + // detail pushes or pops. + child: !wide + ? const SizedBox.shrink() + : Material( + color: + context.colors.surface, + child: Center( + child: + emptyContent ?? + Text( + emptyLabel, + textAlign: TextAlign + .center, + style: Theme.of(context) + .textTheme + .bodyLarge + ?.copyWith( + color: Theme.of( + context, + ).colorScheme.onSurfaceVariant, + decoration: + TextDecoration + .none, + ), + ), + ), + ), + ), + if (selected != null) + if (controller.animateSelection) + MaterialPage( + key: controller.selectionKey, + child: _CoveringDetail( + covering: coveringDetail, + mounted: mountedDetails, + builder: + controller.detailBuilder!, ), - ], - onDidRemovePage: (page) { - if (page.key == - controller.selectionKey) { - controller.clear(); - } - }, - ), + ) + else + _ImmediateDetailPage( + key: controller.selectionKey, + builder: (_) => _CoveringDetail( + covering: coveringDetail, + mounted: mountedDetails, + builder: + controller.detailBuilder!, + ), + ), + ], + onDidRemovePage: (page) { + if (page.key == + controller.selectionKey) { + controller.clear(); + } + }, ), ), ), @@ -483,15 +492,30 @@ class MediaQueryOverride extends StatelessWidget { /// Reports its route as [covering] once the route fully covers the compact /// list, and withdraws it while the route pushes, pops or follows a back swipe. class _CoveringDetail extends HookWidget { - const _CoveringDetail({required this.covering, required this.builder}); + const _CoveringDetail({ + required this.covering, + required this.mounted, + required this.builder, + }); final ValueNotifier?> covering; + + /// Counts mounted details. A popped route unmounts its content only once + /// its reverse transition has finished. + final ValueNotifier mounted; final WidgetBuilder builder; @override Widget build(BuildContext context) { final route = ModalRoute.of(context); final navigator = Navigator.of(context); + useEffect(() { + // The count rebuilds an ancestor, which cannot rebuild while this + // mounts or unmounts. + WidgetsBinding.instance.addPostFrameCallback((_) => mounted.value++); + return () => + WidgetsBinding.instance.addPostFrameCallback((_) => mounted.value--); + }, [mounted]); useEffect(() { if (route == null) return null; void sync() { diff --git a/mobile/test/shared/widgets/adaptive_conversation_pane_test.dart b/mobile/test/shared/widgets/adaptive_conversation_pane_test.dart index 428e9658fb..cbc9543009 100644 --- a/mobile/test/shared/widgets/adaptive_conversation_pane_test.dart +++ b/mobile/test/shared/widgets/adaptive_conversation_pane_test.dart @@ -513,15 +513,47 @@ void main() { expect(controller.selectedId, 'chat'); expect(list, findsNothing); + final chat = find.byType(_Conversation); await tester.tap(find.byType(BackButton)); await tester.pump(); await tester.pump(const Duration(milliseconds: 100)); expect(list, findsOneWidget, reason: 'the list stays beneath a pop'); expect(placeholder, findsNothing); + expect(chat, findsOneWidget, reason: 'the chat slides away, not vanishes'); await tester.pumpAndSettle(); expect(controller.selectedId, isNull); + expect(chat, findsNothing); await tester.tap(list); expect(listTaps, 1); + + // A completed back swipe also slides the chat away. + controller.open('chat', (_) => const _Conversation()); + await tester.pumpAndSettle(); + final release = await tester.startGesture(const Offset(5, 400)); + await release.moveBy(const Offset(150, 0)); + await tester.pump(); + await release.moveBy(const Offset(150, 0)); + await tester.pump(); + await release.up(); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 50)); + expect(chat, findsOneWidget, reason: 'the swiped chat finishes sliding'); + await tester.pumpAndSettle(); + expect(controller.selectedId, isNull); + expect(chat, findsNothing); + + // Reopening while the chat slides away shows the reopened chat. + controller.open('chat', (_) => const _Conversation()); + await tester.pumpAndSettle(); + await tester.tap(find.byType(BackButton)); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 100)); + controller.open('chat', (_) => const _Conversation()); + await tester.pumpAndSettle(); + expect(controller.selectedId, 'chat'); + expect(chat, findsOneWidget); + expect(list, findsNothing); + expect(tester.takeException(), isNull); debugDefaultTargetPlatformOverride = null; });