From 8d2f10fd72069a2829dd13af43553cd5149944d9 Mon Sep 17 00:00:00 2001 From: Abhishek Shroff Date: Thu, 1 May 2025 08:02:13 +0530 Subject: [PATCH] [client] Android back handling --- client/lib/ui/app/app_layout.dart | 14 ++++++++++++++ client/lib/ui/app/router.dart | 2 ++ client/lib/ui/explorer/explorer_controller.dart | 11 +++++++++++ 3 files changed, 27 insertions(+) diff --git a/client/lib/ui/app/app_layout.dart b/client/lib/ui/app/app_layout.dart index 4ffe3ddd..d7a4c595 100644 --- a/client/lib/ui/app/app_layout.dart +++ b/client/lib/ui/app/app_layout.dart @@ -2,6 +2,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_state_notifier/flutter_state_notifier.dart'; import 'package:phylum/ui/app/app_actions.dart'; import 'package:phylum/ui/app/nav_list.dart'; +import 'package:phylum/ui/app/router.dart'; import 'package:phylum/ui/app/search.dart'; import 'package:phylum/ui/explorer/explorer_view.dart'; import 'package:phylum/ui/explorer/explorer_controller.dart'; @@ -23,6 +24,19 @@ class AppLayout extends StatelessWidget { ), ChangeNotifierProvider(create: (context) => PhylumSearchController(context)), ], + builder: (context, child) { + final canHandlePop = + context.watch().canGoBack || context.watch().selectedIds.isNotEmpty; + return PopScope( + canPop: !canHandlePop, + onPopInvokedWithResult: (didPop, result) { + if (didPop) return; + if (!context.read().clearSelection()) { + context.read().goBack(); + } + }, + child: child!); + }, child: const AppLayout._(), ); } diff --git a/client/lib/ui/app/router.dart b/client/lib/ui/app/router.dart index 74bba94d..b631f866 100644 --- a/client/lib/ui/app/router.dart +++ b/client/lib/ui/app/router.dart @@ -18,6 +18,8 @@ class PhylumRouterDelegate extends RouterDelegate with ChangeNotif PhylumRouterDelegate({ExplorerPage? page}) : _current = page; + bool get canGoBack => _back.isNotEmpty; + void go(ExplorerPage page) { if (_current != page) { _back = [..._back, _current!]; diff --git a/client/lib/ui/explorer/explorer_controller.dart b/client/lib/ui/explorer/explorer_controller.dart index 0a45a67b..a59c5a0d 100644 --- a/client/lib/ui/explorer/explorer_controller.dart +++ b/client/lib/ui/explorer/explorer_controller.dart @@ -135,6 +135,17 @@ class ExplorerController extends StateNotifier { state = state.copyWith(dragging: dragging); } + bool clearSelection() { + if (state.selectedIds.isEmpty) return false; + state = state.copyWith( + selectedIds: const {}, + selectionStartIndex: state.selectionStartIndex, + focusIndex: state.focusIndex, + showFocus: false, + ); + return true; + } + void updateSelection(int Function(int)? indexFn, SelectionMode mode, bool highlight) { Set? selectedIds; int index = indexFn?.call(state.focusIndex) ?? state.focusIndex;