[client] Maintain focus/selection across underlying data changes

This commit is contained in:
Abhishek Shroff
2024-09-05 23:11:08 +05:30
parent ee4de99cf1
commit 178b510968
2 changed files with 20 additions and 12 deletions
+18 -7
View File
@@ -24,6 +24,7 @@ class FolderContentsView extends StatefulWidget {
class _FolderContentsViewState extends State<FolderContentsView> {
final Map<String, FocusNode> nodes = {};
late List<Resource> resources;
int focusIndex = -1;
int selectionStartIndex = -1;
@override
@@ -35,6 +36,20 @@ class _FolderContentsViewState extends State<FolderContentsView> {
@override
void didUpdateWidget(covariant FolderContentsView oldWidget) {
super.didUpdateWidget(oldWidget);
final selection = context.read<FolderSelectionState>();
final extras = Set.of(selection.selected);
extras.removeAll(widget.resources);
final selected = Set.of(selection.selected);
selected.removeAll(extras);
// selection.
focusIndex = widget.resources.indexWhere((r) => r.id == selection.focusId);
context.read<FolderSelectionManager>().update(
focusId: focusIndex == -1 ? "" : null,
selected: selected,
);
setState(() => resources = widget.resources);
}
@@ -54,7 +69,6 @@ class _FolderContentsViewState extends State<FolderContentsView> {
PreviousFocusIntent: CallbackAction<PreviousFocusIntent>(onInvoke: (i) => null),
FocusUpIntent: CallbackAction<FocusUpIntent>(onInvoke: (i) {
if (resources.isEmpty) return;
int focusIndex = context.read<FolderSelectionState>().focusIndex;
final index = focusIndex == -1
? resources.length
: focusIndex > 0
@@ -65,13 +79,11 @@ class _FolderContentsViewState extends State<FolderContentsView> {
}),
FocusDownIntent: CallbackAction<FocusDownIntent>(onInvoke: (i) {
if (resources.isEmpty) return;
int focusIndex = context.read<FolderSelectionState>().focusIndex;
final index = min(focusIndex + 1, resources.length - 1);
updateSelection(index, i.mode, true);
return null;
}),
ToggleSelectionIntent: CallbackAction<ToggleSelectionIntent>(onInvoke: (i) {
final focusIndex = context.read<FolderSelectionState>().focusIndex;
updateSelection(focusIndex, SelectionMode.toggle, true);
return null;
}),
@@ -80,7 +92,6 @@ class _FolderContentsViewState extends State<FolderContentsView> {
return null;
}),
DismissIntent: CallbackAction<DismissIntent>(onInvoke: (i) {
int focusIndex = context.read<FolderSelectionState>().focusIndex;
if (focusIndex >= 0) {
updateSelection(focusIndex, SelectionMode.single, true);
} else {
@@ -93,7 +104,6 @@ class _FolderContentsViewState extends State<FolderContentsView> {
return null;
}),
ActivateIntent: CallbackAction<ActivateIntent>(onInvoke: (i) {
int focusIndex = context.read<FolderSelectionState>().focusIndex;
open(resources[focusIndex]);
return null;
}),
@@ -120,7 +130,7 @@ class _FolderContentsViewState extends State<FolderContentsView> {
: SelectionMode.single;
if (context.read<FolderSelectionState>().isSelected(r.id) && mode == SelectionMode.single) {
deferHandling = true;
context.read<FolderSelectionManager>().update(focusId: r.id, focusIndex: index, showFocus: false);
context.read<FolderSelectionManager>().update(focusId: r.id, showFocus: false);
} else {
deferHandling = false;
updateSelection(index, mode, false);
@@ -161,9 +171,10 @@ class _FolderContentsViewState extends State<FolderContentsView> {
case SelectionMode.multi:
break;
}
focusIndex = index;
context
.read<FolderSelectionManager>()
.update(selected: selected, focusId: index < resources.length ? resources[index].id : null, focusIndex: index, showFocus: highlight);
.update(selected: selected, focusId: index < resources.length ? resources[index].id : null, showFocus: highlight);
}
void open(Resource r) {
@@ -3,11 +3,10 @@ import 'package:state_notifier/state_notifier.dart';
class FolderSelectionState {
final Set<String> selected;
final String focusId;
final int focusIndex;
final bool showFocus;
final bool dragging;
FolderSelectionState({required this.selected, required this.focusId, required this.focusIndex, required this.showFocus, required this.dragging});
FolderSelectionState({required this.selected, required this.focusId, required this.showFocus, required this.dragging});
bool isSelected(String id) {
return selected.contains(id);
@@ -15,19 +14,17 @@ class FolderSelectionState {
}
class FolderSelectionManager extends StateNotifier<FolderSelectionState> {
FolderSelectionManager() : super(FolderSelectionState(selected: {}, focusId: "", focusIndex: -1, showFocus: false, dragging: false));
FolderSelectionManager() : super(FolderSelectionState(selected: {}, focusId: "", showFocus: false, dragging: false));
void update({
Set<String>? selected,
String? focusId,
int? focusIndex,
bool? showFocus,
bool? dragging,
}) {
state = FolderSelectionState(
selected: selected ?? state.selected,
focusId: focusId ?? state.focusId,
focusIndex: focusIndex ?? state.focusIndex,
showFocus: showFocus ?? state.showFocus,
dragging: dragging ?? state.dragging);
}