From 246f145dab5c7fc552d0ad54a80ec86afd0cb03e Mon Sep 17 00:00:00 2001 From: Abhishek Shroff Date: Thu, 5 Sep 2024 00:48:30 +0530 Subject: [PATCH] [client] Delete shortcut, fix delete/rename optimistic updateS --- client/lib/app_shortcuts.dart | 9 +++++-- .../actions/action_resource_delete.dart | 4 +-- .../actions/action_resource_rename.dart | 4 +-- .../lib/ui/folder/folder_contents_view.dart | 27 +++++++++++++++++-- 4 files changed, 36 insertions(+), 8 deletions(-) diff --git a/client/lib/app_shortcuts.dart b/client/lib/app_shortcuts.dart index da88edbd..5c5b693b 100644 --- a/client/lib/app_shortcuts.dart +++ b/client/lib/app_shortcuts.dart @@ -33,6 +33,10 @@ class OpenIntent extends Intent { const OpenIntent(); } +class DeleteIntent extends Intent { + const DeleteIntent(); +} + class NavUpIntent extends Intent { const NavUpIntent(); } @@ -46,9 +50,10 @@ Map getAppShortcuts() { SingleActivator(LogicalKeyboardKey.arrowDown, shift: true): FocusDownIntent(SelectionMode.range), SingleActivator(LogicalKeyboardKey.arrowDown, control: true): FocusDownIntent(SelectionMode.multi), SingleActivator(LogicalKeyboardKey.space, control: true): ToggleSelectionIntent(), - SingleActivator(LogicalKeyboardKey.enter): OpenIntent(), - SingleActivator(LogicalKeyboardKey.arrowUp, alt: true): NavUpIntent(), SingleActivator(LogicalKeyboardKey.escape): ClearSelectionIntent(), SingleActivator(LogicalKeyboardKey.keyA, control: true): SelectAllIntent(), + SingleActivator(LogicalKeyboardKey.arrowUp, alt: true): NavUpIntent(), + SingleActivator(LogicalKeyboardKey.enter): OpenIntent(), + SingleActivator(LogicalKeyboardKey.delete): DeleteIntent(), }; } diff --git a/client/lib/libphylum/actions/action_resource_delete.dart b/client/lib/libphylum/actions/action_resource_delete.dart index 83ee1807..b29ad102 100644 --- a/client/lib/libphylum/actions/action_resource_delete.dart +++ b/client/lib/libphylum/actions/action_resource_delete.dart @@ -47,12 +47,12 @@ class ResourceDeleteAction extends ApiAction with JsonApiAction { @override FutureOr applyOptimisticUpdate(PhylumAccount account) { - account.datastore.db.managers.resources.update((o) => o(deleted: const Value(true))); + account.datastore.db.managers.resources.filter((f) => f.id.equals(id)).update((o) => o(deleted: const Value(true))); } @override FutureOr revertOptimisticUpdate(PhylumAccount account) { - account.datastore.db.managers.resources.update((o) => o(deleted: const Value(false))); + account.datastore.db.managers.resources.filter((f) => f.id.equals(id)).update((o) => o(deleted: const Value(false))); } @override diff --git a/client/lib/libphylum/actions/action_resource_rename.dart b/client/lib/libphylum/actions/action_resource_rename.dart index fc8e99d4..886c10ac 100644 --- a/client/lib/libphylum/actions/action_resource_rename.dart +++ b/client/lib/libphylum/actions/action_resource_rename.dart @@ -53,12 +53,12 @@ class ResourceRenameAction extends ApiAction with JsonApiAction { @override FutureOr applyOptimisticUpdate(PhylumAccount account) { - account.datastore.db.managers.resources.update((o) => o(name: Value(newName))); + account.datastore.db.managers.resources.filter((f) => f.id.equals(id)).update((o) => o(name: Value(newName))); } @override FutureOr revertOptimisticUpdate(PhylumAccount account) { - account.datastore.db.managers.resources.update((o) => o(name: Value(oldName))); + account.datastore.db.managers.resources.filter((f) => f.id.equals(id)).update((o) => o(name: Value(oldName))); } @override diff --git a/client/lib/ui/folder/folder_contents_view.dart b/client/lib/ui/folder/folder_contents_view.dart index 6014ac10..1f2d0316 100644 --- a/client/lib/ui/folder/folder_contents_view.dart +++ b/client/lib/ui/folder/folder_contents_view.dart @@ -2,11 +2,15 @@ import 'dart:math'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; +import 'package:phylum/app.dart'; import 'package:phylum/app_shortcuts.dart'; +import 'package:phylum/libphylum/actions/action_resource_delete.dart'; import 'package:phylum/libphylum/db/db.dart'; +import 'package:phylum/libphylum/phylum_account.dart'; import 'package:phylum/ui/folder/folder_navigator_stack.dart'; import 'package:phylum/ui/folder/folder_selection_manager.dart'; import 'package:phylum/ui/folder/resource_details_row.dart'; +import 'package:phylum/util/dialogs.dart'; import 'package:provider/provider.dart'; class FolderContentsView extends StatefulWidget { @@ -76,12 +80,16 @@ class _FolderContentsViewState extends State { context.read().setSelected(const {}); return null; }), + NavUpIntent: CallbackAction(onInvoke: (i) { + context.read().pop(); + return null; + }), OpenIntent: CallbackAction(onInvoke: (i) { open(resources[focusIndex]); return null; }), - NavUpIntent: CallbackAction(onInvoke: (i) { - context.read().pop(); + DeleteIntent: CallbackAction(onInvoke: (i) { + deleteSelected(); return null; }), }, @@ -142,6 +150,21 @@ class _FolderContentsViewState extends State { if (r.dir) context.read().push(r.id, r.name); } + void deleteSelected() async { + final selectedIds = context.read().selected; + if (selectedIds.isEmpty) return; + final account = context.read(); + final selected = resources.where((r) => selectedIds.contains(r.id)).toList(growable: false); + final confirm = await showAlertDialog(context, + title: 'Delete ${selectedIds.length} files?', barrierDismissible: true, positiveText: 'YES', negativeText: 'NO') ?? + false; + + if (!confirm) return; + for (final r in selected) { + account.addAction(ResourceDeleteAction(r: r)); + } + } + FocusNode getFocusNode(Resource r) { return nodes.putIfAbsent(r.id, () => FocusNode(descendantsAreTraversable: false)); }