π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Add favorite functionality for files with UI updates and localization
AmanRajSinghMourya3b129782c1fe
diff --git a/mobile/apps/locker/lib/l10n/app_en.arb b/mobile/apps/locker/lib/l10n/app_en.arbindex c43e6156fa..b462635826 100644--- a/mobile/apps/locker/lib/l10n/app_en.arb+++ b/mobile/apps/locker/lib/l10n/app_en.arb@@ -814,5 +814,43 @@"example": "1,000"}}+ },+ "favorite": "Favorite",+ "unfavorite": "Unfavorite",+ "addingToFavorites": "Adding to Favorites...",+ "removingFromFavorites": "Removing from Favorites...",+ "markingAsFavorites": "Marking as Favorites...",+ "fileMarkedAsFavorite": "Item marked as favorite",+ "fileRemovedFromFavorites": "Item removed from favorites",+ "filesMarkedAsFavorites": "{count, plural, =1{1 item marked as favorite} other{{count} items marked as favorites}}",+ "@filesMarkedAsFavorites": {+ "description": "Success message when items are marked as favorites",+ "placeholders": {+ "count": {+ "type": "int",+ "example": "3"+ }+ }+ },+ "allFilesAlreadyMarkedAsFavorites": "All items are already marked as favorites",+ "failedToMarkFilesAsFavorites": "Failed to mark items as favorites: {error}",+ "@failedToMarkFilesAsFavorites": {+ "description": "Error message when marking items as favorites fails",+ "placeholders": {+ "error": {+ "type": "String",+ "example": "Network error"+ }+ }+ },+ "failedToUpdateFavoriteStatus": "Failed to update favorite status: {error}",+ "@failedToUpdateFavoriteStatus": {+ "description": "Error message when updating favorite status fails",+ "placeholders": {+ "error": {+ "type": "String",+ "example": "Network error"+ }+ }}}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.dartindex c077635f6a..70e9e9be75 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@@ -89,6 +89,29 @@ class FilePopupMenuWidget extends StatelessWidget {isLast: false,),),+ PopupMenuItem<String>(+ value: 'toggle_favorite',+ padding: EdgeInsets.zero,+ height: 0,+ child: FutureBuilder<bool>(+ future: FileActions.isFavorite(file),+ initialData: false,+ builder: (context, snapshot) {+ final isFavorite = snapshot.data ?? false;+ return PopupMenuItemWidget(+ icon: Icon(+ isFavorite ? Icons.star_rounded : Icons.star_border_rounded,+ color: colorScheme.textBase,+ size: 20,+ ),+ label:+ isFavorite ? context.l10n.unfavorite : context.l10n.favorite,+ isFirst: false,+ isLast: false,+ );+ },+ ),+ ),PopupMenuItem<String>(value: 'share_link',padding: EdgeInsets.zero,@@ -137,6 +160,9 @@ class FilePopupMenuWidget extends StatelessWidget {case 'edit':_editFile(context);break;+ case 'toggle_favorite':+ _toggleFavorite(context);+ break;case 'share_link':_shareFileLink(context);break;@@ -163,4 +189,11 @@ class FilePopupMenuWidget extends StatelessWidget {Future<void> _editFile(BuildContext context) async {await FileActions.editFile(context, file);}++ Future<void> _toggleFavorite(BuildContext context) async {+ await FileActions.toggleFavorite(+ context,+ file,+ );+ }}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.dartindex ea332259e6..789b002bf7 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@@ -9,7 +9,6 @@ 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/sync/models/file.dart";import "package:locker/ui/components/add_to_collection_dialog.dart";import "package:locker/ui/components/delete_confirmation_dialog.dart";@@ -36,29 +35,24 @@ class FileSelectionOverlayBar extends StatefulWidget {class _FileSelectionOverlayBarState extends State<FileSelectionOverlayBar> {static final Logger _logger = Logger("FileSelectionOverlayBar");- bool _isImportant = false;+ bool _isFavorite = false;@overridevoid initState() {super.initState();widget.selectedFiles.addListener(_onSelectionChanged);- _checkIfImportant();+ _checkIfFavorite();}- Future<void> _checkIfImportant() async {+ Future<void> _checkIfFavorite() async {if (widget.selectedFiles.files.length == 1) {final file = widget.selectedFiles.files.first;+ final isFav = await FileActions.isFavorite(file);- try {- final isFav = await FavoritesService.instance.isFavorite(file);-- if (mounted) {- setState(() {- _isImportant = isFav;- });- }- } catch (e) {- _logger.severe("Error checking favorite status: $e");+ if (mounted) {+ setState(() {+ _isFavorite = isFav;+ });}}}@@ -72,7 +66,7 @@ class _FileSelectionOverlayBarState extends State<FileSelectionOverlayBar> {void _onSelectionChanged() {if (mounted) {setState(() {});- _checkIfImportant();+ _checkIfFavorite();}}@@ -374,8 +368,8 @@ class _FileSelectionOverlayBarState extends State<FileSelectionOverlayBar> {hugeIcon: const HugeIcon(icon: HugeIcons.strokeRoundedStar,),- label: _isImportant ? "Unmark" : "Important",- onTap: () => _toggleImportant(context, file!),+ label: _isFavorite ? context.l10n.unfavorite : context.l10n.favorite,+ onTap: () => _toggleFavorite(context, file!),),);} else {@@ -384,8 +378,8 @@ class _FileSelectionOverlayBarState extends State<FileSelectionOverlayBar> {hugeIcon: const HugeIcon(icon: HugeIcons.strokeRoundedStar,),- label: "Important",- onTap: () => _markMultipleAsImportant(context, files),+ label: context.l10n.favorite,+ onTap: () => _markMultipleAsFavorites(context, files),),);}@@ -644,108 +638,31 @@ class _FileSelectionOverlayBarState extends State<FileSelectionOverlayBar> {}}- Future<void> _toggleImportant(BuildContext context, EnteFile file) async {- final dialog = createProgressDialog(+ Future<void> _toggleFavorite(BuildContext context, EnteFile file) async {+ await FileActions.toggleFavorite(context,- _isImportant ? "Removing from Important..." : "Adding to Important...",- isDismissible: false,+ file,+ onSuccess: () {+ widget.selectedFiles.clearAll();+ if (mounted) {+ setState(() {+ _isFavorite = !_isFavorite;+ });+ }+ },);-- 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(+ Future<void> _markMultipleAsFavorites(BuildContext context,List<EnteFile> files,) async {- final dialog = createProgressDialog(+ await FileActions.markMultipleAsImportant(context,- "Marking as Important...",- isDismissible: false,+ files,+ onSuccess: () {+ widget.selectedFiles.clearAll();+ },);-- 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);- }- }}}diff --git a/mobile/apps/locker/lib/utils/file_actions.dart b/mobile/apps/locker/lib/utils/file_actions.dartindex 9ad385f7cf..f26c9aab6c 100644--- a/mobile/apps/locker/lib/utils/file_actions.dart+++ b/mobile/apps/locker/lib/utils/file_actions.dart@@ -4,6 +4,7 @@ import "package:flutter/material.dart";import "package:locker/l10n/l10n.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";@@ -15,7 +16,7 @@ import "package:locker/utils/collection_list_util.dart";import "package:locker/utils/snack_bar_utils.dart";import "package:logging/logging.dart";-/// Utility class for common file actions like edit, share, and delete+/// Utility class for common file actions like edit, share, delete, and favoritesclass FileActions {static final _logger = Logger("FileActions");@@ -290,4 +291,128 @@ class FileActions {);}}++ /// Checks if a file is marked as favorite/important+ static Future<bool> isFavorite(EnteFile file) async {+ try {+ return await FavoritesService.instance.isFavorite(file);+ } catch (e) {+ _logger.severe("Error checking favorite status: $e");+ return false;+ }+ }++ /// Toggles favorite/important status of a single file+ static Future<void> toggleFavorite(+ BuildContext context,+ EnteFile file, {+ VoidCallback? onSuccess,+ }) async {+ final isCurrentlyFavorite = await isFavorite(file);+ final dialog = createProgressDialog(+ context,+ isCurrentlyFavorite+ ? context.l10n.removingFromFavorites+ : context.l10n.addingToFavorites,+ isDismissible: false,+ );++ try {+ await dialog.show();++ if (isCurrentlyFavorite) {+ await FavoritesService.instance.removeFromFavorites(context, file);+ } else {+ await FavoritesService.instance.addToFavorites(context, file);+ }++ await dialog.hide();++ if (context.mounted) {+ final message = !isCurrentlyFavorite+ ? context.l10n.fileMarkedAsFavorite+ : context.l10n.fileRemovedFromFavorites;+ SnackBarUtils.showInfoSnackBar(context, message);+ }++ onSuccess?.call();+ } catch (e, stackTrace) {+ _logger.severe("Failed to toggle favorite status: $e", e, stackTrace);+ await dialog.hide();++ if (context.mounted) {+ SnackBarUtils.showWarningSnackBar(+ context,+ context.l10n.failedToUpdateFavoriteStatus(e.toString()),+ );+ }+ }+ }++ /// Marks multiple files as important/favorite+ static Future<void> markMultipleAsImportant(+ BuildContext context,+ List<EnteFile> files, {+ VoidCallback? onSuccess,+ }) async {+ final dialog = createProgressDialog(+ context,+ context.l10n.markingAsFavorites,+ 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,+ context.l10n.allFilesAlreadyMarkedAsFavorites,+ );+ }+ return;+ }++ await FavoritesService.instance.updateFavorites(+ context,+ filesToMark,+ true,+ );++ await dialog.hide();++ if (context.mounted) {+ SnackBarUtils.showInfoSnackBar(+ context,+ context.l10n.filesMarkedAsFavorites(filesToMark.length),+ );+ }++ onSuccess?.call();+ } catch (e, stackTrace) {+ _logger.severe(+ "Failed to mark multiple files as favorites: $e",+ e,+ stackTrace,+ );+ await dialog.hide();++ if (context.mounted) {+ SnackBarUtils.showWarningSnackBar(+ context,+ context.l10n.failedToMarkFilesAsFavorites(e.toString()),+ );+ }+ }+ }}