Skip to content

armgit / ente

Public Imported from GitHub
0

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

Setting up favourite service

AmanRajSinghMouryafdce4e0dfa0f
diff --git a/mobile/apps/locker/lib/services/collections/collections_service.dart b/mobile/apps/locker/lib/services/collections/collections_service.dart
index e2d00ee261..3ea6be77b7 100644
--- a/mobile/apps/locker/lib/services/collections/collections_service.dart
+++ b/mobile/apps/locker/lib/services/collections/collections_service.dart
@@ -102,7 +102,7 @@ class CollectionService {
diff.latestUpdatedAtTime,
);
}).catchError((e) {
- _logger.warning(
+ _logger.severe(
"Failed to fetch files for collection ${collection.id}: $e",
);
}),
@@ -129,6 +129,14 @@ class CollectionService {
// Cache in memory
_collectionIDToCollections[collection.id] = collection;
+
+ // Add to local database immediately
+ await _db.updateCollections([collection]);
+
+ // Fire event to update UI
+ Bus.instance.fire(CollectionsUpdatedEvent('collection_created'));
+
+ // Sync to ensure we have the latest state
await sync();
return collection;
@@ -243,8 +251,8 @@ class CollectionService {
// Also sync to ensure we have the latest state from server
await sync();
}
- } catch (e) {
- _logger.severe("Failed to add file to collection: $e");
+ } catch (e, stackTrace) {
+ _logger.severe("Failed to add file to collection: $e", e, stackTrace);
rethrow;
}
}
@@ -280,7 +288,7 @@ class CollectionService {
// Let sync update the local state
await sync();
} catch (e, s) {
- _logger.warning("failed to rename collection", e, s);
+ _logger.severe("failed to rename collection", e, s);
rethrow;
}
}
@@ -319,18 +327,32 @@ class CollectionService {
return collection;
}
}
- _logger.info("No collections found, creating important collection.");
- return await createCollection("Important", type: CollectionType.favorites);
+ _logger
+ .info("No favorites collection found, creating important collection.");
+ final collection =
+ await createCollection("Important", type: CollectionType.favorites);
+ return collection;
}
Future<void> move(EnteFile file, Collection from, Collection to) async {
try {
await _apiClient.move(file, from, to);
- _logger.info("Moved file ${file.title} from ${from.name} to ${to.name}");
- // Let sync update the local state
+
+ // Update local database immediately for both collections
+ // Remove from source collection
+ await _db.deleteFilesFromCollection(from, [file]);
+
+ // Add to target collection with updated collectionID
+ file.collectionID = to.id;
+ await _db.addFilesToCollection(to, [file]);
+
+ // Fire event to update UI
+ Bus.instance.fire(CollectionsUpdatedEvent('file_moved'));
+
+ // Let sync update the local state to ensure consistency
await sync();
- } catch (e) {
- _logger.severe("Failed to move file: $e");
+ } catch (e, stackTrace) {
+ _logger.severe("Failed to move file: $e", e, stackTrace);
rethrow;
}
}
diff --git a/mobile/apps/locker/lib/services/favorites_service.dart b/mobile/apps/locker/lib/services/favorites_service.dart
index 021357d575..e5dcea3bca 100644
--- a/mobile/apps/locker/lib/services/favorites_service.dart
+++ b/mobile/apps/locker/lib/services/favorites_service.dart
@@ -33,14 +33,10 @@ class FavoritesService {
_db = CollectionDB.instance;
_collectionUpdatesSubscription =
Bus.instance.on<CollectionsUpdatedEvent>().listen((event) {
- _logger.info(
- "CollectionsUpdatedEvent received in FavoritesService: ${event.source}",
- );
// When collections are updated, refresh our cache
_warmUpCache();
});
await _warmUpCache();
- _logger.info("FavoritesService initialized");
}
void dispose() {
@@ -51,8 +47,10 @@ class FavoritesService {
final favCollection = await getFavoritesCollection();
if (favCollection != null) {
final favoriteFiles = await _db.getFilesInCollection(favCollection);
+
_cachedFavUploadedIDs.clear();
_cachedFavFileHashes.clear();
+
for (final file in favoriteFiles) {
if (file.uploadedFileID != null) {
_cachedFavUploadedIDs.add(file.uploadedFileID!);
@@ -76,17 +74,19 @@ class FavoritesService {
if (file.collectionID != null &&
_cachedFavoritesCollectionID != null &&
file.collectionID == _cachedFavoritesCollectionID) {
- debugPrint("File ${file.uploadedFileID} is part of favorite collection");
return true;
}
if (checkOnlyAlbum) {
return false;
}
if (file.uploadedFileID != null) {
+ bool isFav = false;
if (file.ownerID != _config.getUserID() && file.hash != null) {
- return _cachedFavFileHashes.containsKey(file.hash!);
+ isFav = _cachedFavFileHashes.containsKey(file.hash!);
+ } else {
+ isFav = _cachedFavUploadedIDs.contains(file.uploadedFileID);
}
- return _cachedFavUploadedIDs.contains(file.uploadedFileID);
+ return isFav;
}
return false;
}
@@ -94,10 +94,13 @@ class FavoritesService {
Future<bool> isFavorite(EnteFile file) async {
// Use cache for better performance
if (file.uploadedFileID != null) {
+ bool isFav = false;
if (file.ownerID != _config.getUserID() && file.hash != null) {
- return _cachedFavFileHashes.containsKey(file.hash!);
+ isFav = _cachedFavFileHashes.containsKey(file.hash!);
+ } else {
+ isFav = _cachedFavUploadedIDs.contains(file.uploadedFileID);
}
- return _cachedFavUploadedIDs.contains(file.uploadedFileID);
+ return isFav;
}
return false;
}
@@ -116,6 +119,7 @@ class FavoritesService {
}
}
}
+
if (favFlag) {
_cachedFavUploadedIDs.addAll(updatedIDs);
_cachedFavFileHashes.addAll(hashes);
@@ -129,15 +133,20 @@ class FavoritesService {
Future<void> addToFavorites(BuildContext context, EnteFile file) async {
final collectionID = await _getOrCreateFavoriteCollectionID();
+
final List<EnteFile> files = [file];
if (file.uploadedFileID == null) {
+ _logger.severe("Cannot favorite file without uploadedFileID");
throw AssertionError("Can only favorite uploaded items");
} else {
final collection =
await _collectionService.getCollectionByID(collectionID);
+
await _collectionService.addToCollection(collection!, files[0]);
}
+
_updateFavoriteFilesCache(files, favFlag: true);
+
_collectionService.sync().ignore();
}
@@ -200,6 +209,7 @@ class FavoritesService {
// Do nothing, ignore
} else {
final Collection? favCollection = await getFavoritesCollection();
+
// The file might be part of another collection. For unfav, we need to
// move file from the fav collection.
if (file.ownerID != _config.getUserID() &&
@@ -212,6 +222,7 @@ class FavoritesService {
);
file = favFile;
}
+
if (file.collectionID != favCollection!.id) {
final favFiles = await _db.getFilesInCollection(favCollection);
final favFile = favFiles.firstWhere(
@@ -236,8 +247,10 @@ class FavoritesService {
}
// If no other collection found, move to uncategorized
- targetCollection ??=
- await _collectionService.getOrCreateUncategorizedCollection();
+ if (targetCollection == null) {
+ targetCollection =
+ await _collectionService.getOrCreateUncategorizedCollection();
+ }
await _collectionService.move(
file,
@@ -245,12 +258,14 @@ class FavoritesService {
targetCollection,
);
}
+
_updateFavoriteFilesCache([file], favFlag: false);
}
Future<Collection?> getFavoritesCollection() async {
if (_cachedFavoritesCollectionID == null) {
final collections = await _collectionService.getCollections();
+
for (final collection in collections) {
if (collection.owner.id == _config.getUserID() &&
collection.type == CollectionType.favorites) {
@@ -260,6 +275,7 @@ class FavoritesService {
}
return null;
}
+
return _collectionService.getCollectionByID(_cachedFavoritesCollectionID!);
}
@@ -272,9 +288,11 @@ class FavoritesService {
if (_cachedFavoritesCollectionID != null) {
return _cachedFavoritesCollectionID!;
}
+
final collection =
await _collectionService.getOrCreateImportantCollection();
_cachedFavoritesCollectionID = collection.id;
+
return collection.id;
}
}
diff --git a/mobile/apps/locker/lib/services/trash/trash_service.dart b/mobile/apps/locker/lib/services/trash/trash_service.dart
index 372e020b50..128713eee9 100644
--- a/mobile/apps/locker/lib/services/trash/trash_service.dart
+++ b/mobile/apps/locker/lib/services/trash/trash_service.dart
@@ -256,8 +256,9 @@ class TrashService {
data: params,
);
await _trashDB.delete(files.map((e) => e.uploadedFileID!).toList());
+ // Refresh collections so restored files are immediately available in UI
+ await CollectionService.instance.sync();
Bus.instance.fire(CollectionsUpdatedEvent("file_restore"));
- // Force reload home gallery to pull in the restored files
} catch (e, s) {
_logger.severe("failed to restore files", e, s);
rethrow;
diff --git a/mobile/apps/locker/lib/ui/components/selection_action_widget.dart b/mobile/apps/locker/lib/ui/components/selection_action_widget.dart
new file mode 100644
index 0000000000..594894d252
--- /dev/null
+++ b/mobile/apps/locker/lib/ui/components/selection_action_widget.dart
@@ -0,0 +1,59 @@
+import "package:ente_ui/theme/ente_theme.dart";
+import "package:flutter/material.dart";
+
+class SelectionActionWidget extends StatelessWidget {
+ final IconData icon;
+ final String label;
+ final VoidCallback onTap;
+ final Color? iconColor;
+ final Color? textColor;
+
+ const SelectionActionWidget({
+ required this.icon,
+ required this.label,
+ required this.onTap,
+ this.iconColor,
+ this.textColor,
+ super.key,
+ });
+
+ @override
+ Widget build(BuildContext context) {
+ final colorScheme = getEnteColorScheme(context);
+ final textTheme = getEnteTextTheme(context);
+
+ final effectiveIconColor = iconColor ?? colorScheme.textBase;
+ final effectiveTextColor = textColor ?? colorScheme.textBase;
+
+ return Expanded(
+ child: GestureDetector(
+ onTap: onTap,
+ child: Container(
+ padding: const EdgeInsets.symmetric(
+ vertical: 18.0,
+ horizontal: 12.0,
+ ),
+ child: Column(
+ mainAxisSize: MainAxisSize.min,
+ mainAxisAlignment: MainAxisAlignment.center,
+ children: [
+ Icon(
+ icon,
+ color: effectiveIconColor,
+ size: 24,
+ ),
+ const SizedBox(height: 8),
+ Text(
+ label,
+ style: textTheme.body.copyWith(
+ color: effectiveTextColor,
+ ),
+ textAlign: TextAlign.center,
+ ),
+ ],
+ ),
+ ),
+ ),
+ );
+ }
+}
diff --git a/mobile/apps/locker/lib/ui/pages/collection_page.dart b/mobile/apps/locker/lib/ui/pages/collection_page.dart
index afdf2f3b67..eac24c210b 100644
--- a/mobile/apps/locker/lib/ui/pages/collection_page.dart
+++ b/mobile/apps/locker/lib/ui/pages/collection_page.dart
@@ -53,6 +53,7 @@ class _CollectionPageState extends UploaderPageState<CollectionPage>
List<EnteFile> _filteredFiles = [];
late CollectionViewType collectionViewType;
bool isQuickLink = false;
+ bool isFavorite = false;
final _selectedFiles = SelectedFiles();
@@ -142,6 +143,7 @@ class _CollectionPageState extends UploaderPageState<CollectionPage>
_collection,
Configuration.instance.getUserID()!,
);
+ isFavorite = collectionViewType == CollectionViewType.favorite;
}
Future<void> _initializeData(Collection collection) async {
@@ -238,6 +240,9 @@ class _CollectionPageState extends UploaderPageState<CollectionPage>
}
Widget _buildMenuButton(EnteColorScheme colorScheme) {
+ if (isFavorite) {
+ return SizedBox.fromSize();
+ }
return PopupMenuButton<String>(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
diff --git a/mobile/apps/locker/lib/ui/pages/home_page.dart b/mobile/apps/locker/lib/ui/pages/home_page.dart
index d7d99a8f68..b44323e969 100644
--- a/mobile/apps/locker/lib/ui/pages/home_page.dart
+++ b/mobile/apps/locker/lib/ui/pages/home_page.dart
@@ -440,11 +440,18 @@ class _HomePageState extends UploaderPageState<HomePage>
}
Future<void> _loadCollections() async {
+ final shouldShowLoading =
+ _collections.isEmpty && _recentFiles.isEmpty && !_isLoading;
+
try {
- setState(() {
- _isLoading = true;
- _error = null;
- });
+ if (shouldShowLoading || _error != null) {
+ setState(() {
+ if (shouldShowLoading) {
+ _isLoading = true;
+ }
+ _error = null;
+ });
+ }
final collections = await CollectionService.instance.getCollections();
await _loadRecentFiles(collections);
@@ -639,12 +646,20 @@ class _HomePageState extends UploaderPageState<HomePage>
return LayoutBuilder(
builder: (context, constraints) {
+ final scrollBottomPadding =
+ MediaQuery.of(context).padding.bottom + 120.0;
+
return SingleChildScrollView(
- padding: const EdgeInsets.all(16.0),
+ padding: EdgeInsets.only(
+ left: 16.0,
+ right: 16.0,
+ top: 16.0,
+ bottom: scrollBottomPadding,
+ ),
physics: const AlwaysScrollableScrollPhysics(),
child: ConstrainedBox(
constraints: BoxConstraints(
- minHeight: constraints.maxHeight - 32, // Account for padding
+ minHeight: constraints.maxHeight,
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
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 d4f7d0d296..0563f59d56 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
@@ -5,13 +5,16 @@ import "package:locker/l10n/l10n.dart";
import "package:locker/models/selected_files.dart";
import "package:locker/services/collections/collections_service.dart";
import "package:locker/services/collections/models/collection.dart";
+import "package:locker/services/favorites_service.dart";
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/file_edit_dialog.dart";
import "package:locker/ui/components/selection_action_button_widget.dart";
+import "package:locker/ui/components/selection_action_widget.dart";
import "package:locker/ui/components/share_link_dialog.dart";
import "package:locker/utils/snack_bar_utils.dart";
+import "package:logging/logging.dart";
class FileSelectionOverlayBar extends StatefulWidget {
final SelectedFiles selectedFiles;
@@ -28,10 +31,32 @@ class FileSelectionOverlayBar extends StatefulWidget {
}
class _FileSelectionOverlayBarState extends State<FileSelectionOverlayBar> {
+ static final Logger _logger = Logger("FileSelectionOverlayBar");
+ bool _isImportant = false;
+
@override
void initState() {
super.initState();
widget.selectedFiles.addListener(_onSelectionChanged);
+ _checkIfImportant();
+ }
+
+ Future<void> _checkIfImportant() async {
+ if (widget.selectedFiles.files.length == 1) {
+ final file = widget.selectedFiles.files.first;
+
+ try {
+ final isFav = await FavoritesService.instance.isFavorite(file);
+
+ if (mounted) {
+ setState(() {
+ _isImportant = isFav;
+ });
+ }
+ } catch (e) {
+ _logger.severe("Error checking favorite status: $e");
+ }
+ }
}
@override
@@ -43,6 +68,7 @@ class _FileSelectionOverlayBarState extends State<FileSelectionOverlayBar> {
void _onSelectionChanged() {
if (mounted) {
setState(() {});
+ _checkIfImportant();
}
}
@@ -261,26 +287,12 @@ class _FileSelectionOverlayBarState extends State<FileSelectionOverlayBar> {
_buildContinuousActionRow(file!),
],
)
- : Row(
- key: const ValueKey('multi_selection'),
- children: [
- Expanded(
- child: SelectionActionButton(
- icon: Icons.delete_outline,
- label: context.l10n.delete,
- onTap: () =>
- _deleteMultipleFile(context, selectedFiles.toList()),
- isDestructive: true,
- ),
- ),
- ],
- ),
+ : _buildMultiSelectionActionRow(selectedFiles.toList()),
);
}
Widget _buildContinuousActionRow(EnteFile file) {
final colorScheme = getEnteColorScheme(context);
- final textTheme = getEnteTextTheme(context);
return Container(
decoration: BoxDecoration(
@@ -290,65 +302,45 @@ class _FileSelectionOverlayBarState extends State<FileSelectionOverlayBar> {
child: IntrinsicHeight(
child: Row(
children: [
- Expanded(
- child: GestureDetector(
- onTap: () => _showEditDialog(context, file),
- child: Container(
- padding: const EdgeInsets.symmetric(
- vertical: 18.0,
- horizontal: 12.0,
- ),
- child: Column(
- mainAxisSize: MainAxisSize.min,
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- Icon(
- Icons.edit_outlined,
- color: colorScheme.textBase,
- size: 24,
- ),
- const SizedBox(height: 8),
- Text(
- context.l10n.edit,
- style: textTheme.body.copyWith(
- color: colorScheme.textBase,
- ),
- textAlign: TextAlign.center,
- ),
- ],
- ),
- ),
- ),
+ SelectionActionWidget(
+ icon: Icons.edit_outlined,
+ label: context.l10n.edit,
+ onTap: () => _showEditDialog(context, file),
),
- Expanded(
- child: GestureDetector(
- onTap: () => _toggleImportant(context, file),
- child: Container(
- padding: const EdgeInsets.symmetric(
- vertical: 18.0,
- horizontal: 12.0,
- ),
- child: Column(
- mainAxisSize: MainAxisSize.min,
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- Icon(
- Icons.star_outline,
- color: colorScheme.textBase,
- size: 24,
- ),
- const SizedBox(height: 8),
- Text(
- "Important",
- style: textTheme.body.copyWith(
- color: colorScheme.textBase,
- ),
- textAlign: TextAlign.center,
- ),
- ],
- ),
- ),
- ),
+ SelectionActionWidget(
+ icon: _isImportant ? Icons.star : Icons.star_outline,
+ label: _isImportant ? "Unmark" : "Important",
+ onTap: () => _toggleImportant(context, file),
+ ),
+ ],
+ ),
+ ),
+ );
+ }
+
+ Widget _buildMultiSelectionActionRow(List<EnteFile> files) {
+ final colorScheme = getEnteColorScheme(context);
+
+ return Container(
+ key: const ValueKey('multi_selection'),
+ decoration: BoxDecoration(
+ color: colorScheme.backgroundElevated2,
+ borderRadius: BorderRadius.circular(24.0),
+ ),
+ child: IntrinsicHeight(
+ child: Row(
+ children: [
+ SelectionActionWidget(
+ icon: Icons.star_outline,
+ label: "Important",
+ onTap: () => _markMultipleAsImportant(context, files),
+ ),
+ SelectionActionWidget(
+ icon: Icons.delete_outline,
+ label: context.l10n.delete,
+ onTap: () => _deleteMultipleFile(context, files),
+ iconColor: colorScheme.warning500,
+ textColor: colorScheme.warning500,
),
],
),
@@ -569,10 +561,107 @@ class _FileSelectionOverlayBarState extends State<FileSelectionOverlayBar> {
}
Future<void> _toggleImportant(BuildContext context, EnteFile file) async {
- // TODO: Implement toggle important/star functionality
- SnackBarUtils.showInfoSnackBar(
+ final dialog = createProgressDialog(
+ context,
+ _isImportant ? "Removing from Important..." : "Adding to Important...",
+ isDismissible: false,
+ );
+
+ try {
+ await dialog.show();
+
+ if (_isImportant) {
+ await FavoritesService.instance.removeFromFavorites(context, file);
+ } else {
+ await FavoritesService.instance.addToFavorites(context, file);
+ }
+
+ await dialog.hide();
+ widget.selectedFiles.clearAll();
+
+ setState(() {
+ _isImportant = !_isImportant;
+ });
+
+ if (context.mounted) {
+ final message = _isImportant
+ ? "File marked as important"
+ : "File removed from important";
+ SnackBarUtils.showInfoSnackBar(context, message);
+ }
+ } catch (e, stackTrace) {
+ _logger.severe("Failed to toggle important status: $e", e, stackTrace);
+ await dialog.hide();
+
+ if (context.mounted) {
+ final errorMessage =
+ 'Failed to update important status: ${e.toString()}';
+ SnackBarUtils.showWarningSnackBar(context, errorMessage);
+ }
+ }
+ }
+
+ Future<void> _markMultipleAsImportant(
+ BuildContext context,
+ List<EnteFile> files,
+ ) async {
+ final dialog = createProgressDialog(
context,
- "Mark as important functionality coming soon",
+ "Marking as Important...",
+ isDismissible: false,
);
+
+ try {
+ await dialog.show();
+
+ final List<EnteFile> filesToMark = [];
+ for (final file in files) {
+ final isFav = await FavoritesService.instance.isFavorite(file);
+ if (!isFav) {
+ filesToMark.add(file);
+ }
+ }
+
+ if (filesToMark.isEmpty) {
+ await dialog.hide();
+ if (context.mounted) {
+ SnackBarUtils.showInfoSnackBar(
+ context,
+ "All files are already marked as important",
+ );
+ }
+ return;
+ }
+
+ await FavoritesService.instance.updateFavorites(
+ context,
+ filesToMark,
+ true,
+ );
+
+ await dialog.hide();
+
+ widget.selectedFiles.clearAll();
+
+ if (context.mounted) {
+ final message = filesToMark.length == 1
+ ? "1 file marked as important"
+ : "${filesToMark.length} files marked as important";
+ SnackBarUtils.showInfoSnackBar(context, message);
+ }
+ } catch (e, stackTrace) {
+ _logger.severe(
+ "Failed to mark multiple files as important: $e",
+ e,
+ stackTrace,
+ );
+ await dialog.hide();
+
+ if (context.mounted) {
+ final errorMessage =
+ 'Failed to mark files as important: ${e.toString()}';
+ SnackBarUtils.showWarningSnackBar(context, errorMessage);
+ }
+ }
}
}