[client] Android back handling

This commit is contained in:
Abhishek Shroff
2025-05-01 08:02:13 +05:30
parent 4efec731c4
commit 8d2f10fd72
3 changed files with 27 additions and 0 deletions

View File

@@ -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<PhylumRouterDelegate>().canGoBack || context.watch<ExplorerState>().selectedIds.isNotEmpty;
return PopScope(
canPop: !canHandlePop,
onPopInvokedWithResult: (didPop, result) {
if (didPop) return;
if (!context.read<ExplorerController>().clearSelection()) {
context.read<PhylumRouterDelegate>().goBack();
}
},
child: child!);
},
child: const AppLayout._(),
);
}

View File

@@ -18,6 +18,8 @@ class PhylumRouterDelegate extends RouterDelegate<ExplorerPage> with ChangeNotif
PhylumRouterDelegate({ExplorerPage? page}) : _current = page;
bool get canGoBack => _back.isNotEmpty;
void go(ExplorerPage page) {
if (_current != page) {
_back = [..._back, _current!];

View File

@@ -135,6 +135,17 @@ class ExplorerController extends StateNotifier<ExplorerState> {
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<String>? selectedIds;
int index = indexFn?.call(state.focusIndex) ?? state.focusIndex;