Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Fix trash restore collection selection and delete confirmation

AmanRajSinghMouryab8f7f05dd573
diff --git a/mobile/apps/locker/lib/ui/pages/trash_page.dart b/mobile/apps/locker/lib/ui/pages/trash_page.dart
index 1175a3c935..dbde2412d0 100644
--- a/mobile/apps/locker/lib/ui/pages/trash_page.dart
+++ b/mobile/apps/locker/lib/ui/pages/trash_page.dart
@@ -13,8 +13,10 @@ import 'package:locker/services/files/sync/models/file.dart';
import 'package:locker/services/trash/models/trash_file.dart';
import 'package:locker/services/trash/trash_service.dart';
import "package:locker/ui/components/delete_confirmation_dialog.dart";
+import "package:locker/ui/components/empty_state_widget.dart";
import 'package:locker/ui/components/file_restore_dialog.dart';
import 'package:locker/ui/components/item_list_view.dart';
+import 'package:locker/utils/collection_list_util.dart';
import 'package:locker/utils/snack_bar_utils.dart';
class TrashPage extends StatefulWidget {
@@ -74,9 +76,9 @@ class _TrashPageState extends State<TrashPage> {
void _restoreFile(BuildContext context, EnteFile file) async {
final collections = await CollectionService.instance.getCollections();
- final availableCollections = collections
- .where((c) => !c.isDeleted && c.type != CollectionType.uncategorized)
- .toList();
+ final availableCollections = uniqueCollectionsById(
+ collections.where((c) => !c.isDeleted).toList(),
+ );
if (availableCollections.isEmpty) {
SnackBarUtils.showWarningSnackBar(
@@ -227,32 +229,6 @@ class _TrashPageState extends State<TrashPage> {
}
Widget _buildBody(EnteColorScheme colorScheme, EnteTextTheme textTheme) {
- if (_sortedTrashFiles.isEmpty) {
- return Center(
- child: Padding(
- padding: const EdgeInsets.all(32.0),
- child: Column(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- const Icon(
- Icons.delete_outline,
- size: 64,
- color: Colors.grey,
- ),
- const SizedBox(height: 16),
- Text(
- context.l10n.trashIsEmpty,
- style: textTheme.large.copyWith(
- color: Colors.grey,
- ),
- textAlign: TextAlign.center,
- ),
- ],
- ),
- ),
- );
- }
-
return Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
@@ -281,25 +257,31 @@ class _TrashPageState extends State<TrashPage> {
],
),
const SizedBox(height: 24),
- Expanded(
- child: LayoutBuilder(
- builder: (context, constraints) {
- return SingleChildScrollView(
- physics: const BouncingScrollPhysics(),
- padding: EdgeInsets.zero,
- child: ConstrainedBox(
- constraints: BoxConstraints(
- minHeight: constraints.maxHeight,
- ),
- child: ItemListView(
- files: _sortedTrashFiles.cast<EnteFile>(),
- fileOverflowActions: _getFileOverflowActions(),
- ),
+ _sortedTrashFiles.isEmpty
+ ? const EmptyStateWidget(
+ assetPath: 'assets/empty_state.png',
+ title: "Nothing to see here",
+ showBorder: false,
+ )
+ : Expanded(
+ child: LayoutBuilder(
+ builder: (context, constraints) {
+ return SingleChildScrollView(
+ physics: const BouncingScrollPhysics(),
+ padding: EdgeInsets.zero,
+ child: ConstrainedBox(
+ constraints: BoxConstraints(
+ minHeight: constraints.maxHeight,
+ ),
+ child: ItemListView(
+ files: _sortedTrashFiles.cast<EnteFile>(),
+ fileOverflowActions: _getFileOverflowActions(),
+ ),
+ ),
+ );
+ },
),
- );
- },
- ),
- ),
+ ),
],
),
);
diff --git a/mobile/apps/locker/lib/ui/viewer/actions/file_selection_overlay_bar.dart b/mobile/apps/locker/lib/ui/viewer/actions/file_selection_overlay_bar.dart
index 6c7daac8be..ca2f158b51 100644
--- a/mobile/apps/locker/lib/ui/viewer/actions/file_selection_overlay_bar.dart
+++ b/mobile/apps/locker/lib/ui/viewer/actions/file_selection_overlay_bar.dart
@@ -1,3 +1,4 @@
+import "package:ente_ui/components/buttons/models/button_result.dart";
import "package:ente_ui/theme/ente_theme.dart";
import "package:ente_ui/utils/dialog_util.dart";
import "package:flutter/material.dart";
@@ -10,9 +11,11 @@ import "package:locker/services/files/links/links_service.dart";
import "package:locker/services/files/sync/metadata_updater_service.dart";
import "package:locker/services/files/sync/models/file.dart";
import "package:locker/ui/components/add_to_collection_dialog.dart";
+import "package:locker/ui/components/delete_confirmation_dialog.dart";
import "package:locker/ui/components/file_edit_dialog.dart";
import "package:locker/ui/components/selection_action_button_widget.dart";
import "package:locker/ui/components/share_link_dialog.dart";
+import "package:locker/utils/collection_list_util.dart";
import "package:locker/utils/file_util.dart";
import "package:locker/utils/snack_bar_utils.dart";
import "package:logging/logging.dart";
@@ -451,14 +454,14 @@ class _FileSelectionOverlayBarState extends State<FileSelectionOverlayBar> {
Future<void> _showEditDialog(BuildContext context, EnteFile file) async {
final allCollections = await CollectionService.instance.getCollections();
- allCollections.removeWhere(
- (c) => c.type == CollectionType.uncategorized,
- );
+ final dedupedCollections =
+ uniqueCollectionsById(allCollections, logger: _logger);
final result = await showFileEditDialog(
context,
file: file,
- collections: allCollections,
+ collections: dedupedCollections,
+ snackBarContext: context,
);
if (result != null && context.mounted) {
@@ -471,21 +474,17 @@ class _FileSelectionOverlayBarState extends State<FileSelectionOverlayBar> {
}
final currentCollectionsSet = currentCollections.toSet();
-
- final newCollectionsSet = result.selectedCollections.toSet();
-
+ final selectedCollectionsSet = result.selectedCollections.toSet();
final collectionsToAdd =
- newCollectionsSet.difference(currentCollectionsSet).toList();
-
- final collectionsToRemove =
- currentCollectionsSet.difference(newCollectionsSet).toList();
+ selectedCollectionsSet.difference(currentCollectionsSet).toList();
+ final hasCollectionAdds = collectionsToAdd.isNotEmpty;
final currentTitle = file.displayName;
final currentCaption = file.caption ?? '';
final hasMetadataChanged =
result.title != currentTitle || result.caption != currentCaption;
- if (hasMetadataChanged || currentCollectionsSet != newCollectionsSet) {
+ if (hasMetadataChanged || hasCollectionAdds) {
final dialog = createProgressDialog(
context,
context.l10n.pleaseWait,
@@ -510,12 +509,6 @@ class _FileSelectionOverlayBarState extends State<FileSelectionOverlayBar> {
final List<Future<void>> apiCalls = [];
- for (final collection in collectionsToRemove) {
- apiCalls.add(
- CollectionService.instance
- .move(file, collection, newCollectionsSet.first),
- );
- }
if (hasMetadataChanged) {
apiCalls.add(
MetadataUpdaterService.instance
@@ -555,14 +548,28 @@ class _FileSelectionOverlayBarState extends State<FileSelectionOverlayBar> {
return;
}
+ _logger.info(
+ 'Opening add-to dialog for ${files.length} file(s); fetching collections.',
+ );
final allCollections = await CollectionService.instance.getCollections();
+ final dedupedCollections =
+ uniqueCollectionsById(allCollections, logger: _logger);
+ _logger.info(
+ 'Presenting ${dedupedCollections.length} unique collection option(s) '
+ 'to add files to.',
+ );
final result = await showAddToCollectionDialog(
context,
- collections: allCollections,
+ collections: dedupedCollections,
+ snackBarContext: context,
);
if (result != null && context.mounted) {
+ _logger.info(
+ 'Add-to dialog submitted with '
+ '${result.selectedCollections.length} selected collection(s).',
+ );
final dialog = createProgressDialog(
context,
context.l10n.pleaseWait,
@@ -575,11 +582,18 @@ class _FileSelectionOverlayBarState extends State<FileSelectionOverlayBar> {
final addFutures = <Future<void>>[];
for (final file in files) {
+ _logger.fine(
+ 'Processing file ${file.uploadedFileID} (${file.displayName}) '
+ 'for add-to operation.',
+ );
List<Collection> currentCollections;
try {
currentCollections =
await CollectionService.instance.getCollectionsForFile(file);
} catch (_) {
+ _logger.warning(
+ 'Failed to fetch existing collections for file ${file.uploadedFileID}',
+ );
currentCollections = <Collection>[];
}
@@ -591,6 +605,9 @@ class _FileSelectionOverlayBarState extends State<FileSelectionOverlayBar> {
);
for (final collection in collectionsToAdd) {
+ _logger.fine(
+ 'Adding file ${file.uploadedFileID} to collection ${collection.id}.',
+ );
addFutures.add(
CollectionService.instance.addToCollection(
collection,
@@ -612,6 +629,9 @@ class _FileSelectionOverlayBarState extends State<FileSelectionOverlayBar> {
await Future.wait(addFutures);
await CollectionService.instance.sync();
+ _logger.info(
+ 'Completed add-to operation for ${files.length} file(s).',
+ );
await dialog.hide();
@@ -623,6 +643,9 @@ class _FileSelectionOverlayBarState extends State<FileSelectionOverlayBar> {
);
} catch (e) {
await dialog.hide();
+ _logger.severe(
+ 'Failed add-to operation: $e',
+ );
SnackBarUtils.showWarningSnackBar(
context,
@@ -633,6 +656,18 @@ class _FileSelectionOverlayBarState extends State<FileSelectionOverlayBar> {
}
Future<void> _deleteFile(BuildContext context, EnteFile file) async {
+ final confirmation = await showDeleteConfirmationDialog(
+ context,
+ title: context.l10n.areYouSure,
+ body: context.l10n.deleteMultipleFilesDialogBody(1),
+ deleteButtonLabel: context.l10n.yesDeleteFiles(1),
+ assetPath: "assets/file_delete_icon.png",
+ );
+
+ if (confirmation?.action != ButtonAction.first) {
+ return;
+ }
+
final dialog = createProgressDialog(
context,
context.l10n.deletingFile,
@@ -650,17 +685,24 @@ class _FileSelectionOverlayBarState extends State<FileSelectionOverlayBar> {
await dialog.hide();
+ widget.selectedFiles.clearAll();
+
SnackBarUtils.showInfoSnackBar(
context,
context.l10n.fileDeletedSuccessfully,
);
- } catch (e) {
+ } catch (e, stackTrace) {
await dialog.hide();
- SnackBarUtils.showWarningSnackBar(
- context,
- context.l10n.failedToDeleteFile(e.toString()),
+ _logger.severe(
+ 'Failed to delete file ${file.uploadedFileID}: $e',
+ e,
+ stackTrace,
);
+ if (!context.mounted) {
+ return;
+ }
+ await showGenericErrorDialog(context: context, error: e);
}
}
@@ -668,6 +710,22 @@ class _FileSelectionOverlayBarState extends State<FileSelectionOverlayBar> {
BuildContext context,
List<EnteFile> files,
) async {
+ if (files.isEmpty) {
+ return;
+ }
+
+ final confirmation = await showDeleteConfirmationDialog(
+ context,
+ title: context.l10n.areYouSure,
+ body: context.l10n.deleteMultipleFilesDialogBody(files.length),
+ deleteButtonLabel: context.l10n.yesDeleteFiles(files.length),
+ assetPath: "assets/file_delete_icon.png",
+ );
+
+ if (confirmation?.action != ButtonAction.first) {
+ return;
+ }
+
final dialog = createProgressDialog(
context,
context.l10n.deletingFile,
@@ -688,24 +746,33 @@ class _FileSelectionOverlayBarState extends State<FileSelectionOverlayBar> {
await dialog.hide();
+ widget.selectedFiles.clearAll();
+
SnackBarUtils.showInfoSnackBar(
context,
context.l10n.fileDeletedSuccessfully,
);
- } catch (e) {
+ } catch (e, stackTrace) {
await dialog.hide();
- SnackBarUtils.showWarningSnackBar(
- context,
- context.l10n.failedToDeleteFile(e.toString()),
+ _logger.severe(
+ 'Failed to delete files via selection bar: $e',
+ e,
+ stackTrace,
+ );
+ if (!context.mounted) {
+ return;
+ }
+ await showGenericErrorDialog(
+ context: context,
+ error: e,
);
}
}
Future<void> _downloadFile(BuildContext context, EnteFile file) async {
try {
- final success =
- await FileUtil.downloadFilesToDownloads(context, [file]);
+ final success = await FileUtil.downloadFilesToDownloads(context, [file]);
if (success) {
widget.selectedFiles.clearAll();
}
diff --git a/mobile/apps/locker/lib/utils/collection_list_util.dart b/mobile/apps/locker/lib/utils/collection_list_util.dart
new file mode 100644
index 0000000000..cef1ed9fc2
--- /dev/null
+++ b/mobile/apps/locker/lib/utils/collection_list_util.dart
@@ -0,0 +1,44 @@
+import 'package:locker/services/collections/models/collection.dart';
+import 'package:logging/logging.dart';
+
+/// Returns a list of collections with duplicate IDs removed while preserving
+/// order. Stops at the first uncategorized collection encountered.
+List<Collection> uniqueCollectionsById(
+ List<Collection> collections, {
+ Logger? logger,
+}) {
+ final seenIds = <int>{};
+ final unique = <Collection>[];
+
+ bool uncategorizedSeen = false;
+
+ for (final collection in collections) {
+ final isUncategorizedCollection = _isUncategorized(collection);
+
+ if (!seenIds.add(collection.id)) {
+ logger?.fine(
+ 'Skipping duplicate collection with id ${collection.id} '
+ '(${collection.name ?? "Unnamed"})',
+ );
+ continue;
+ }
+
+ if (isUncategorizedCollection) {
+ if (uncategorizedSeen) {
+ logger?.finer(
+ 'Skipping duplicate uncategorized collection with id ${collection.id}',
+ );
+ continue;
+ }
+ uncategorizedSeen = true;
+ }
+
+ unique.add(collection);
+ }
+
+ return unique;
+}
+
+bool _isUncategorized(Collection collection) {
+ return collection.type == CollectionType.uncategorized;
+}