Skip to content

armgit / ente

Public Imported from GitHub
0

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

Fix bugs around

AmanRajSinghMouryad22acbf2248e
diff --git a/mobile/apps/locker/lib/ui/components/add_to_collection_dialog.dart b/mobile/apps/locker/lib/ui/components/add_to_collection_dialog.dart
index 76d7058943..3b94c6bdd1 100644
--- a/mobile/apps/locker/lib/ui/components/add_to_collection_dialog.dart
+++ b/mobile/apps/locker/lib/ui/components/add_to_collection_dialog.dart
@@ -5,6 +5,7 @@ import "package:flutter/material.dart";
import "package:locker/l10n/l10n.dart";
import "package:locker/services/collections/models/collection.dart";
import "package:locker/ui/components/collection_selection_widget.dart";
+import "package:locker/utils/collection_list_util.dart";
import "package:locker/utils/snack_bar_utils.dart";
class AddToCollectionDialogResult {
@@ -17,10 +18,12 @@ class AddToCollectionDialogResult {
class AddToCollectionDialog extends StatefulWidget {
final List<Collection> collections;
+ final BuildContext snackBarContext;
const AddToCollectionDialog({
super.key,
required this.collections,
+ required this.snackBarContext,
});
@override
@@ -34,7 +37,7 @@ class _AddToCollectionDialogState extends State<AddToCollectionDialog> {
@override
void initState() {
super.initState();
- _availableCollections = List.from(widget.collections);
+ _availableCollections = uniqueCollectionsById(widget.collections);
}
void _toggleCollection(int collectionId) {
@@ -49,7 +52,7 @@ class _AddToCollectionDialogState extends State<AddToCollectionDialog> {
void _onCollectionsUpdated(List<Collection> updatedCollections) {
setState(() {
- _availableCollections = updatedCollections;
+ _availableCollections = uniqueCollectionsById(updatedCollections);
});
}
@@ -64,8 +67,8 @@ class _AddToCollectionDialogState extends State<AddToCollectionDialog> {
if (selectedCollections.isEmpty) {
SnackBarUtils.showWarningSnackBar(
- context,
- context.l10n.pleaseSelectAtLeastOneCollection,
+ widget.snackBarContext,
+ widget.snackBarContext.l10n.pleaseSelectAtLeastOneCollection,
);
return;
}
@@ -149,11 +152,14 @@ class _AddToCollectionDialogState extends State<AddToCollectionDialog> {
Future<AddToCollectionDialogResult?> showAddToCollectionDialog(
BuildContext context, {
required List<Collection> collections,
+ BuildContext? snackBarContext,
}) async {
+ final messengerContext = snackBarContext ?? context;
return showDialog<AddToCollectionDialogResult>(
context: context,
- builder: (context) => AddToCollectionDialog(
+ builder: (dialogContext) => AddToCollectionDialog(
collections: collections,
+ snackBarContext: messengerContext,
),
);
}
diff --git a/mobile/apps/locker/lib/ui/components/collection_selection_widget.dart b/mobile/apps/locker/lib/ui/components/collection_selection_widget.dart
index 26d910d35a..1bb953d7df 100644
--- a/mobile/apps/locker/lib/ui/components/collection_selection_widget.dart
+++ b/mobile/apps/locker/lib/ui/components/collection_selection_widget.dart
@@ -1,10 +1,12 @@
-import "package:dotted_border/dotted_border.dart";
-import "package:ente_ui/components/title_bar_title_widget.dart";
+import 'package:collection/collection.dart' show IterableExtension;
+import 'package:dotted_border/dotted_border.dart';
+import 'package:ente_ui/components/title_bar_title_widget.dart';
import 'package:ente_ui/theme/ente_theme.dart';
import 'package:flutter/material.dart';
import 'package:locker/l10n/l10n.dart';
import 'package:locker/services/collections/models/collection.dart';
import 'package:locker/utils/collection_actions.dart';
+import 'package:locker/utils/collection_list_util.dart';
class CollectionSelectionWidget extends StatefulWidget {
final List<Collection> collections;
@@ -31,18 +33,31 @@ class CollectionSelectionWidget extends StatefulWidget {
class _CollectionSelectionWidgetState extends State<CollectionSelectionWidget> {
List<Collection> _availableCollections = [];
+ Collection? _uncategorizedCollection;
@override
void initState() {
super.initState();
- _availableCollections = List.from(widget.collections);
+ _availableCollections = uniqueCollectionsById(widget.collections);
+ _uncategorizedCollection = _availableCollections.firstWhereOrNull(
+ (collection) => collection.type == CollectionType.uncategorized,
+ );
+ _availableCollections.removeWhere(
+ (collection) => collection.type == CollectionType.uncategorized,
+ );
}
@override
void didUpdateWidget(CollectionSelectionWidget oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.collections != widget.collections) {
- _availableCollections = List.from(widget.collections);
+ _availableCollections = uniqueCollectionsById(widget.collections);
+ _uncategorizedCollection = _availableCollections.firstWhereOrNull(
+ (collection) => collection.type == CollectionType.uncategorized,
+ );
+ _availableCollections.removeWhere(
+ (collection) => collection.type == CollectionType.uncategorized,
+ );
}
}
@@ -68,14 +83,6 @@ class _CollectionSelectionWidgetState extends State<CollectionSelectionWidget> {
}
}
- void _onUncategorizedSelected() {
- // Clear all selected collections when uncategorized is selected
- final collectionIdsCopy = Set<int>.from(widget.selectedCollectionIds);
- for (final id in collectionIdsCopy) {
- widget.onToggleCollection(id);
- }
- }
-
void _onCollectionTap(int collectionId) {
if (widget.singleSelectionMode) {
// In single selection mode, clear other selections first
@@ -93,6 +100,7 @@ class _CollectionSelectionWidgetState extends State<CollectionSelectionWidget> {
Widget build(BuildContext context) {
final colorScheme = getEnteColorScheme(context);
final textTheme = getEnteTextTheme(context);
+ final containsUncategorized = _uncategorizedCollection != null;
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
@@ -106,13 +114,19 @@ class _CollectionSelectionWidgetState extends State<CollectionSelectionWidget> {
spacing: 8,
runSpacing: 12,
children: [
- _buildUncategorizedChip(
- context.l10n.uncategorized,
- widget.selectedCollectionIds.isEmpty,
- _onUncategorizedSelected,
- colorScheme,
- textTheme,
- ),
+ if (containsUncategorized)
+ _buildUncategorizedChip(
+ name: context.l10n.uncategorized,
+ isSelected: widget.selectedCollectionIds
+ .contains(_uncategorizedCollection?.id ?? -1),
+ onTap: () {
+ if (_uncategorizedCollection != null) {
+ _onCollectionTap(_uncategorizedCollection!.id);
+ }
+ },
+ colorScheme: colorScheme,
+ textTheme: textTheme,
+ ),
for (final collection in _availableCollections)
_buildCollectionChip(
collection: collection,
@@ -166,13 +180,13 @@ class _CollectionSelectionWidgetState extends State<CollectionSelectionWidget> {
);
}
- Widget _buildUncategorizedChip(
- String name,
- bool isSelected,
- VoidCallback onTap,
- colorScheme,
- textTheme,
- ) {
+ Widget _buildUncategorizedChip({
+ required String name,
+ required bool isSelected,
+ required VoidCallback onTap,
+ required colorScheme,
+ required textTheme,
+ }) {
return GestureDetector(
onTap: onTap,
child: AnimatedContainer(
diff --git a/mobile/apps/locker/lib/ui/components/file_edit_dialog.dart b/mobile/apps/locker/lib/ui/components/file_edit_dialog.dart
index 38dfc45582..3e1f54329d 100644
--- a/mobile/apps/locker/lib/ui/components/file_edit_dialog.dart
+++ b/mobile/apps/locker/lib/ui/components/file_edit_dialog.dart
@@ -5,6 +5,7 @@ import 'package:locker/l10n/l10n.dart';
import 'package:locker/services/collections/models/collection.dart';
import 'package:locker/services/files/sync/models/file.dart';
import 'package:locker/ui/components/collection_selection_widget.dart';
+import 'package:locker/utils/collection_list_util.dart';
import 'package:locker/utils/snack_bar_utils.dart';
class FileEditDialogResult {
@@ -22,11 +23,13 @@ class FileEditDialogResult {
class FileEditDialog extends StatefulWidget {
final EnteFile file;
final List<Collection> collections;
+ final BuildContext snackBarContext;
const FileEditDialog({
super.key,
required this.file,
required this.collections,
+ required this.snackBarContext,
});
@override
@@ -43,7 +46,7 @@ class _FileEditDialogState extends State<FileEditDialog> {
void initState() {
super.initState();
- _availableCollections = List.from(widget.collections);
+ _availableCollections = uniqueCollectionsById(widget.collections);
_titleController.text = widget.file.displayName;
@@ -68,7 +71,7 @@ class _FileEditDialogState extends State<FileEditDialog> {
void _onCollectionsUpdated(List<Collection> updatedCollections) {
setState(() {
- _availableCollections = updatedCollections;
+ _availableCollections = uniqueCollectionsById(updatedCollections);
});
}
@@ -83,8 +86,8 @@ class _FileEditDialogState extends State<FileEditDialog> {
if (selectedCollections.isEmpty) {
SnackBarUtils.showWarningSnackBar(
- context,
- context.l10n.pleaseSelectAtLeastOneCollection,
+ widget.snackBarContext,
+ widget.snackBarContext.l10n.pleaseSelectAtLeastOneCollection,
);
return;
}
@@ -203,9 +206,8 @@ class _FileEditDialogState extends State<FileEditDialog> {
selectedCollectionIds: _selectedCollectionIds,
onToggleCollection: _toggleCollection,
onCollectionsUpdated: _onCollectionsUpdated,
- singleSelectionMode: true,
titleWidget: Text(
- "Move to collection",
+ "Add to collection",
style: textTheme.largeBold,
),
),
@@ -230,12 +232,14 @@ Future<FileEditDialogResult?> showFileEditDialog(
BuildContext context, {
required EnteFile file,
required List<Collection> collections,
+ BuildContext? snackBarContext,
}) async {
return showDialog<FileEditDialogResult>(
context: context,
- builder: (context) => FileEditDialog(
+ builder: (dialogContext) => FileEditDialog(
file: file,
collections: collections,
+ snackBarContext: snackBarContext ?? context,
),
);
}
diff --git a/mobile/apps/locker/lib/ui/components/file_popup_menu_widget.dart b/mobile/apps/locker/lib/ui/components/file_popup_menu_widget.dart
index 91df9e2704..ca0d874403 100644
--- a/mobile/apps/locker/lib/ui/components/file_popup_menu_widget.dart
+++ b/mobile/apps/locker/lib/ui/components/file_popup_menu_widget.dart
@@ -14,13 +14,13 @@ import "package:locker/ui/components/file_edit_dialog.dart";
import "package:locker/ui/components/item_list_view.dart";
import "package:locker/ui/components/menu_item_widget.dart";
import "package:locker/ui/components/share_link_dialog.dart";
+import "package:locker/utils/collection_list_util.dart";
import "package:locker/utils/snack_bar_utils.dart";
class FilePopupMenuWidget extends StatelessWidget {
final EnteFile file;
final List<OverflowMenuAction>? overflowActions;
final Widget? child;
-
const FilePopupMenuWidget({
super.key,
required this.file,
@@ -239,14 +239,13 @@ class FilePopupMenuWidget extends StatelessWidget {
Future<void> _showEditDialog(BuildContext context) async {
final allCollections = await CollectionService.instance.getCollections();
- allCollections.removeWhere(
- (c) => c.type == CollectionType.uncategorized,
- );
+ final dedupedCollections = uniqueCollectionsById(allCollections);
final result = await showFileEditDialog(
context,
file: file,
- collections: allCollections,
+ collections: dedupedCollections,
+ snackBarContext: context,
);
if (result != null && context.mounted) {
@@ -259,18 +258,17 @@ class FilePopupMenuWidget extends StatelessWidget {
}
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,
@@ -294,13 +292,6 @@ class FilePopupMenuWidget extends StatelessWidget {
}
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
@@ -311,12 +302,18 @@ class FilePopupMenuWidget extends StatelessWidget {
await dialog.hide();
+ if (!context.mounted) {
+ return;
+ }
SnackBarUtils.showInfoSnackBar(
context,
context.l10n.fileUpdatedSuccessfully,
);
} catch (e) {
await dialog.hide();
+ if (!context.mounted) {
+ return;
+ }
SnackBarUtils.showWarningSnackBar(
context,
@@ -324,6 +321,9 @@ class FilePopupMenuWidget extends StatelessWidget {
);
}
} else {
+ if (!context.mounted) {
+ return;
+ }
SnackBarUtils.showWarningSnackBar(
context,
context.l10n.noChangesWereMade,
diff --git a/mobile/apps/locker/lib/ui/components/recents_section_widget.dart b/mobile/apps/locker/lib/ui/components/recents_section_widget.dart
index 5f12f6f88b..09d6477080 100644
--- a/mobile/apps/locker/lib/ui/components/recents_section_widget.dart
+++ b/mobile/apps/locker/lib/ui/components/recents_section_widget.dart
@@ -60,6 +60,8 @@ class _RecentsSectionWidgetState extends State<RecentsSectionWidget> {
if (oldWidget.recentFiles != widget.recentFiles ||
oldWidget.collections != widget.collections) {
_handleCollectionUpdates();
+ _fileCollectionsCache.clear();
+ _fileCollectionsRequests.clear();
setState(() {
_originalCollectionOrder = List.from(widget.collections);
if (!_hasActiveFilters) {
@@ -276,8 +278,10 @@ class _RecentsSectionWidgetState extends State<RecentsSectionWidget> {
return;
}
- final fileCollections =
- await _ensureCollectionsForFiles(widget.recentFiles);
+ final fileCollections = await _ensureCollectionsForFiles(
+ widget.recentFiles,
+ refresh: hasCollectionFilters,
+ );
if (!mounted || computationId != _filtersComputationId) {
return;
@@ -335,8 +339,9 @@ class _RecentsSectionWidgetState extends State<RecentsSectionWidget> {
}
Future<Map<int, List<Collection>>> _ensureCollectionsForFiles(
- List<EnteFile> files,
- ) async {
+ List<EnteFile> files, {
+ bool refresh = false,
+ }) async {
final List<Future<void>> pending = [];
for (final file in files) {
@@ -345,6 +350,12 @@ class _RecentsSectionWidgetState extends State<RecentsSectionWidget> {
continue;
}
+ if (refresh) {
+ _fileCollectionsCache.remove(fileId);
+ // Drop any in-flight request so a fresh fetch is triggered.
+ final _ = _fileCollectionsRequests.remove(fileId);
+ }
+
if (_fileCollectionsCache.containsKey(fileId)) {
continue;
}
diff --git a/mobile/apps/locker/lib/utils/collection_list_util.dart b/mobile/apps/locker/lib/utils/collection_list_util.dart
index cef1ed9fc2..e2c05e8f06 100644
--- a/mobile/apps/locker/lib/utils/collection_list_util.dart
+++ b/mobile/apps/locker/lib/utils/collection_list_util.dart
@@ -1,12 +1,8 @@
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,
-}) {
+List<Collection> uniqueCollectionsById(List<Collection> collections) {
final seenIds = <int>{};
final unique = <Collection>[];
@@ -15,25 +11,15 @@ List<Collection> uniqueCollectionsById(
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;
+ if (seenIds.add(collection.id)) {
+ if (isUncategorizedCollection) {
+ if (uncategorizedSeen) {
+ continue;
+ }
+ uncategorizedSeen = true;
}
- uncategorizedSeen = true;
+ unique.add(collection);
}
-
- unique.add(collection);
}
return unique;