mirror of
https://codeberg.org/shroff/phylum.git
synced 2026-01-02 01:29:42 -06:00
[client] Android back handling
This commit is contained in:
@@ -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._(),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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!];
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user