Skip to content

armgit / ente

Public Imported from GitHub
0

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

Add likes bottom sheet on long press of heart icon

ashilknd3dc0ad2fd99
diff --git a/mobile/apps/photos/lib/db/social_db.dart b/mobile/apps/photos/lib/db/social_db.dart
index 174b29abea..baa71206a5 100644
--- a/mobile/apps/photos/lib/db/social_db.dart
+++ b/mobile/apps/photos/lib/db/social_db.dart
@@ -325,6 +325,21 @@ class SocialDB {
return rows.map(_rowToReaction).toList();
}
+ /// Get reactions for a file within a specific collection (file-level likes only)
+ Future<List<Reaction>> getReactionsForFileInCollection(
+ int fileID,
+ int collectionID,
+ ) async {
+ final db = await database;
+ final rows = await db.query(
+ reactionsTable,
+ where:
+ 'file_id = ? AND collection_id = ? AND comment_id IS NULL AND is_deleted = 0',
+ whereArgs: [fileID, collectionID],
+ );
+ return rows.map(_rowToReaction).toList();
+ }
+
Future<List<Reaction>> getReactionsForComment(String commentID) async {
final db = await database;
final rows = await db.query(
diff --git a/mobile/apps/photos/lib/models/social/social_data_provider.dart b/mobile/apps/photos/lib/models/social/social_data_provider.dart
index f7aae46a9a..2ee9eb815a 100644
--- a/mobile/apps/photos/lib/models/social/social_data_provider.dart
+++ b/mobile/apps/photos/lib/models/social/social_data_provider.dart
@@ -26,6 +26,13 @@ class SocialDataProvider {
return _db.getReactionsForFile(fileID);
}
+ Future<List<Reaction>> getReactionsForFileInCollection(
+ int fileID,
+ int collectionID,
+ ) {
+ return _db.getReactionsForFileInCollection(fileID, collectionID);
+ }
+
Future<List<Comment>> getRepliesForComment(String commentID) {
return _db.getRepliesForComment(commentID);
}
diff --git a/mobile/apps/photos/lib/ui/social/likes_bottom_sheet.dart b/mobile/apps/photos/lib/ui/social/likes_bottom_sheet.dart
new file mode 100644
index 0000000000..18b25a111e
--- /dev/null
+++ b/mobile/apps/photos/lib/ui/social/likes_bottom_sheet.dart
@@ -0,0 +1,298 @@
+import "dart:async";
+
+import "package:flutter/material.dart";
+import "package:photos/core/configuration.dart";
+import "package:photos/db/files_db.dart";
+import "package:photos/extensions/user_extension.dart";
+import "package:photos/models/api/collection/user.dart";
+import "package:photos/models/collection/collection.dart";
+import "package:photos/models/social/reaction.dart";
+import "package:photos/models/social/social_data_provider.dart";
+import "package:photos/services/collections_service.dart";
+import "package:photos/theme/ente_theme.dart";
+import "package:photos/ui/components/buttons/icon_button_widget.dart";
+import "package:photos/ui/sharing/user_avator_widget.dart";
+import "package:photos/ui/social/widgets/collection_selector_widget.dart";
+
+/// Shows the likes bottom sheet for a file
+Future<void> showLikesBottomSheet(
+ BuildContext context, {
+ required int fileID,
+ required int initialCollectionID,
+}) {
+ return showModalBottomSheet(
+ context: context,
+ isScrollControlled: true,
+ backgroundColor: Colors.transparent,
+ builder: (_) => LikesBottomSheet(
+ fileID: fileID,
+ initialCollectionID: initialCollectionID,
+ ),
+ );
+}
+
+class LikesBottomSheet extends StatefulWidget {
+ final int fileID;
+ final int initialCollectionID;
+
+ const LikesBottomSheet({
+ required this.fileID,
+ required this.initialCollectionID,
+ super.key,
+ });
+
+ @override
+ State<LikesBottomSheet> createState() => _LikesBottomSheetState();
+}
+
+class _LikesBottomSheetState extends State<LikesBottomSheet> {
+ List<Reaction> _likes = [];
+ bool _isLoading = true;
+ List<CollectionLikeInfo> _sharedCollections = [];
+ late int _selectedCollectionID;
+ late final int _currentUserID;
+ final Map<int, User> _userCache = {};
+
+ @override
+ void initState() {
+ super.initState();
+ _currentUserID = Configuration.instance.getUserID()!;
+ _selectedCollectionID = widget.initialCollectionID;
+ _loadSharedCollections();
+ }
+
+ Future<void> _loadSharedCollections() async {
+ // Get all collections containing this file
+ final collectionIDs = await FilesDB.instance.getAllCollectionIDsOfFile(
+ widget.fileID,
+ );
+
+ // Filter to shared collections only
+ final sharedCollectionsList = collectionIDs
+ .map((id) => CollectionsService.instance.getCollectionByID(id))
+ .whereType<Collection>()
+ .where(
+ (c) => c.hasSharees || c.hasLink || !c.isOwner(_currentUserID),
+ )
+ .toList();
+
+ // Fetch like counts and thumbnails in parallel
+ final sharedCollections = await Future.wait(
+ sharedCollectionsList.map((collection) async {
+ final likes = await SocialDataProvider.instance
+ .getReactionsForFileInCollection(widget.fileID, collection.id);
+ final thumbnail =
+ await CollectionsService.instance.getCover(collection);
+ return CollectionLikeInfo(
+ collection: collection,
+ likeCount: likes.length,
+ thumbnail: thumbnail,
+ );
+ }),
+ );
+
+ if (mounted) {
+ // If no shared collections, close the sheet
+ if (sharedCollections.isEmpty) {
+ Navigator.of(context).pop();
+ return;
+ }
+
+ // Validate selected collection is in the shared list
+ final isSelectedInShared = sharedCollections.any(
+ (info) => info.collection.id == _selectedCollectionID,
+ );
+
+ setState(() {
+ _sharedCollections = sharedCollections;
+ if (!isSelectedInShared) {
+ _selectedCollectionID = sharedCollections.first.collection.id;
+ }
+ });
+
+ unawaited(_loadLikes());
+ }
+ }
+
+ Future<void> _loadLikes() async {
+ setState(() => _isLoading = true);
+
+ final likes = await SocialDataProvider.instance
+ .getReactionsForFileInCollection(widget.fileID, _selectedCollectionID);
+
+ if (mounted) {
+ setState(() {
+ _likes = likes;
+ _isLoading = false;
+ });
+ }
+ }
+
+ Future<void> _onCollectionSelected(int collectionID) async {
+ // Load likes for new collection first to avoid flash/jump
+ final likes = await SocialDataProvider.instance
+ .getReactionsForFileInCollection(widget.fileID, collectionID);
+
+ if (mounted) {
+ setState(() {
+ _selectedCollectionID = collectionID;
+ _likes = likes;
+ _userCache.clear();
+ });
+ }
+ }
+
+ User _getUserForReaction(Reaction reaction) {
+ if (_userCache.containsKey(reaction.userID)) {
+ return _userCache[reaction.userID]!;
+ }
+
+ if (reaction.isAnonymous) {
+ final user = User(
+ id: reaction.userID,
+ email: "${reaction.anonUserID ?? "anonymous"}@unknown.com",
+ name: reaction.anonUserID ?? "Anonymous",
+ );
+ _userCache[reaction.userID] = user;
+ return user;
+ }
+
+ final user = CollectionsService.instance
+ .getFileOwner(reaction.userID, _selectedCollectionID);
+ _userCache[reaction.userID] = user;
+ return user;
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ final colorScheme = getEnteColorScheme(context);
+ final mediaQuery = MediaQuery.of(context);
+
+ return Container(
+ constraints: BoxConstraints(
+ maxHeight: mediaQuery.size.height * 0.7,
+ ),
+ decoration: BoxDecoration(
+ color: colorScheme.backgroundBase,
+ borderRadius: const BorderRadius.vertical(
+ top: Radius.circular(24),
+ ),
+ ),
+ child: SafeArea(
+ top: false,
+ child: Column(
+ mainAxisSize: MainAxisSize.min,
+ children: [
+ _buildHeader(),
+ Flexible(
+ child: _isLoading
+ ? const Center(child: CircularProgressIndicator())
+ : _likes.isEmpty
+ ? _buildEmptyState()
+ : _buildLikesList(),
+ ),
+ ],
+ ),
+ ),
+ );
+ }
+
+ Widget _buildHeader() {
+ final textTheme = getEnteTextTheme(context);
+
+ return Padding(
+ padding: const EdgeInsets.fromLTRB(16, 16, 12, 8),
+ child: Row(
+ children: [
+ Expanded(
+ child: _sharedCollections.length > 1
+ ? LikesCollectionSelectorWidget(
+ sharedCollections: _sharedCollections,
+ selectedCollectionID: _selectedCollectionID,
+ onCollectionSelected: _onCollectionSelected,
+ )
+ : Text(
+ "${_likes.length} ${_likes.length == 1 ? 'like' : 'likes'}",
+ style: textTheme.bodyBold,
+ ),
+ ),
+ IconButtonWidget(
+ iconButtonType: IconButtonType.rounded,
+ icon: Icons.close_rounded,
+ onTap: () => Navigator.of(context).pop(),
+ ),
+ ],
+ ),
+ );
+ }
+
+ Widget _buildEmptyState() {
+ final textTheme = getEnteTextTheme(context);
+
+ return Padding(
+ padding: const EdgeInsets.symmetric(vertical: 48, horizontal: 24),
+ child: Text(
+ "No likes yet",
+ style: textTheme.smallMuted,
+ textAlign: TextAlign.center,
+ ),
+ );
+ }
+
+ Widget _buildLikesList() {
+ return ListView.builder(
+ shrinkWrap: _likes.length <= 30,
+ padding: const EdgeInsets.fromLTRB(16, 8, 16, 16),
+ itemCount: _likes.length,
+ itemBuilder: (context, index) {
+ final reaction = _likes[index];
+ final user = _getUserForReaction(reaction);
+ return _LikeListItem(
+ user: user,
+ currentUserID: _currentUserID,
+ );
+ },
+ );
+ }
+}
+
+class _LikeListItem extends StatelessWidget {
+ final User user;
+ final int currentUserID;
+
+ const _LikeListItem({
+ required this.user,
+ required this.currentUserID,
+ });
+
+ @override
+ Widget build(BuildContext context) {
+ final textTheme = getEnteTextTheme(context);
+
+ return Padding(
+ padding: const EdgeInsets.symmetric(vertical: 8),
+ child: Row(
+ children: [
+ UserAvatarWidget(
+ user,
+ currentUserID: currentUserID,
+ type: AvatarType.lg,
+ ),
+ const SizedBox(width: 12),
+ Expanded(
+ child: Text(
+ user.displayName ?? user.email,
+ style: textTheme.body,
+ overflow: TextOverflow.ellipsis,
+ ),
+ ),
+ const Icon(
+ Icons.favorite,
+ color: Color(0xFF08C225),
+ size: 20,
+ ),
+ ],
+ ),
+ );
+ }
+}
diff --git a/mobile/apps/photos/lib/ui/social/widgets/collection_selector_widget.dart b/mobile/apps/photos/lib/ui/social/widgets/collection_selector_widget.dart
index 148f29fc5b..f82702b7ad 100644
--- a/mobile/apps/photos/lib/ui/social/widgets/collection_selector_widget.dart
+++ b/mobile/apps/photos/lib/ui/social/widgets/collection_selector_widget.dart
@@ -17,6 +17,19 @@ class CollectionCommentInfo {
});
}
+/// Holds collection info with like count for the likes bottom sheet
+class CollectionLikeInfo {
+ final Collection collection;
+ final int likeCount;
+ final EnteFile? thumbnail;
+
+ const CollectionLikeInfo({
+ required this.collection,
+ required this.likeCount,
+ this.thumbnail,
+ });
+}
+
class CollectionSelectorWidget extends StatefulWidget {
final List<CollectionCommentInfo> sharedCollections;
final int selectedCollectionID;
@@ -72,7 +85,7 @@ class _CollectionSelectorWidgetState extends State<CollectionSelectorWidget> {
children: [
_ThumbnailWithBadge(
thumbnail: _selectedCollection.thumbnail,
- commentCount: _selectedCollection.commentCount,
+ badgeCount: _selectedCollection.commentCount,
),
const SizedBox(width: 12),
Flexible(
@@ -134,7 +147,7 @@ class _CollectionSelectorWidgetState extends State<CollectionSelectorWidget> {
children: [
_ThumbnailWithBadge(
thumbnail: info.thumbnail,
- commentCount: info.commentCount,
+ badgeCount: info.commentCount,
),
const SizedBox(width: 12),
Expanded(
@@ -159,11 +172,11 @@ class _CollectionSelectorWidgetState extends State<CollectionSelectorWidget> {
class _ThumbnailWithBadge extends StatelessWidget {
final EnteFile? thumbnail;
- final int commentCount;
+ final int badgeCount;
const _ThumbnailWithBadge({
required this.thumbnail,
- required this.commentCount,
+ required this.badgeCount,
});
@override
@@ -192,7 +205,7 @@ class _ThumbnailWithBadge extends StatelessWidget {
borderRadius: const BorderRadius.all(Radius.circular(16)),
),
child: Text(
- commentCount > 99 ? '99+' : '$commentCount',
+ badgeCount > 99 ? '99+' : '$badgeCount',
style: TextStyle(
color: colorScheme.textBase,
fontSize: 8,
@@ -207,3 +220,144 @@ class _ThumbnailWithBadge extends StatelessWidget {
);
}
}
+
+class LikesCollectionSelectorWidget extends StatefulWidget {
+ final List<CollectionLikeInfo> sharedCollections;
+ final int selectedCollectionID;
+ final ValueChanged<int> onCollectionSelected;
+
+ const LikesCollectionSelectorWidget({
+ required this.sharedCollections,
+ required this.selectedCollectionID,
+ required this.onCollectionSelected,
+ super.key,
+ });
+
+ @override
+ State<LikesCollectionSelectorWidget> createState() =>
+ _LikesCollectionSelectorWidgetState();
+}
+
+class _LikesCollectionSelectorWidgetState
+ extends State<LikesCollectionSelectorWidget> {
+ final GlobalKey _widgetKey = GlobalKey();
+ bool _isMenuOpen = false;
+
+ CollectionLikeInfo get _selectedCollection =>
+ widget.sharedCollections.firstWhere(
+ (c) => c.collection.id == widget.selectedCollectionID,
+ orElse: () => widget.sharedCollections.first,
+ );
+
+ @override
+ Widget build(BuildContext context) {
+ final colorScheme = getEnteColorScheme(context);
+ final textTheme = getEnteTextTheme(context);
+ final isDarkMode = Theme.of(context).brightness == Brightness.dark;
+
+ final containerColor =
+ isDarkMode ? const Color(0xFF212121) : const Color(0xFFF0F0F0);
+ final borderColor =
+ isDarkMode ? const Color(0x14FFFFFF) : const Color(0x14000000);
+
+ return GestureDetector(
+ onTap: () => _showCollectionMenu(context, containerColor, borderColor),
+ child: Container(
+ key: _widgetKey,
+ padding: const EdgeInsets.all(6),
+ decoration: BoxDecoration(
+ color: containerColor,
+ borderRadius: BorderRadius.circular(12),
+ border: Border.all(
+ color: _isMenuOpen ? borderColor : Colors.transparent,
+ ),
+ ),
+ child: Row(
+ mainAxisSize: MainAxisSize.min,
+ children: [
+ _ThumbnailWithBadge(
+ thumbnail: _selectedCollection.thumbnail,
+ badgeCount: _selectedCollection.likeCount,
+ ),
+ const SizedBox(width: 12),
+ Flexible(
+ child: Text(
+ _selectedCollection.collection.displayName,
+ style: textTheme.smallBold.copyWith(height: 20 / 14.0),
+ overflow: TextOverflow.ellipsis,
+ ),
+ ),
+ const SizedBox(width: 12),
+ Icon(
+ Icons.keyboard_arrow_down_rounded,
+ color: colorScheme.textBase,
+ size: 16,
+ ),
+ const SizedBox(width: 2),
+ ],
+ ),
+ ),
+ );
+ }
+
+ void _showCollectionMenu(
+ BuildContext context,
+ Color bgColor,
+ Color borderColor,
+ ) {
+ setState(() => _isMenuOpen = true);
+
+ final textTheme = getEnteTextTheme(context);
+ final renderBox =
+ _widgetKey.currentContext!.findRenderObject()! as RenderBox;
+ final widgetPosition = renderBox.localToGlobal(Offset.zero);
+ final widgetSize = renderBox.size;
+ final screenSize = MediaQuery.sizeOf(context);
+
+ showMenu<int>(
+ context: context,
+ position: RelativeRect.fromLTRB(
+ widgetPosition.dx,
+ widgetPosition.dy + widgetSize.height + 12,
+ screenSize.width - widgetPosition.dx - widgetSize.width,
+ 0,
+ ),
+ shape: RoundedRectangleBorder(
+ borderRadius: BorderRadius.circular(12),
+ side: BorderSide(color: borderColor),
+ ),
+ color: bgColor,
+ elevation: 0,
+ constraints: const BoxConstraints(minWidth: 184),
+ menuPadding: const EdgeInsets.symmetric(horizontal: 6),
+ items: widget.sharedCollections.map((info) {
+ return PopupMenuItem<int>(
+ height: 40,
+ padding: EdgeInsets.zero,
+ value: info.collection.id,
+ child: Row(
+ children: [
+ _ThumbnailWithBadge(
+ thumbnail: info.thumbnail,
+ badgeCount: info.likeCount,
+ ),
+ const SizedBox(width: 12),
+ Expanded(
+ child: Text(
+ info.collection.displayName,
+ style: textTheme.miniBold.copyWith(height: 20 / 12.0),
+ overflow: TextOverflow.ellipsis,
+ ),
+ ),
+ ],
+ ),
+ );
+ }).toList(),
+ ).then((selectedID) {
+ setState(() => _isMenuOpen = false);
+ if (selectedID != null && selectedID != widget.selectedCollectionID) {
+ widget.onCollectionSelected(selectedID);
+ }
+ });
+ }
+}
diff --git a/mobile/apps/photos/lib/ui/viewer/file/file_bottom_bar.dart b/mobile/apps/photos/lib/ui/viewer/file/file_bottom_bar.dart
index 24868517af..d2eee3c787 100644
--- a/mobile/apps/photos/lib/ui/viewer/file/file_bottom_bar.dart
+++ b/mobile/apps/photos/lib/ui/viewer/file/file_bottom_bar.dart
@@ -17,6 +17,7 @@ import "package:photos/services/collections_service.dart";
import "package:photos/ui/actions/file/file_actions.dart";
import "package:photos/ui/collections/collection_action_sheet.dart";
import "package:photos/ui/social/comments_screen.dart";
+import "package:photos/ui/social/likes_bottom_sheet.dart";
import "package:photos/utils/delete_file_util.dart";
import "package:photos/utils/navigation_util.dart";
import "package:photos/utils/panorama_util.dart";
@@ -305,6 +306,7 @@ class FileBottomBarState extends State<FileBottomBar> {
padding: const EdgeInsets.only(top: 12),
child: GestureDetector(
onTap: _toggleReaction,
+ onLongPress: _showLikesBottomSheet,
child: Icon(
_hasLiked
? (Platform.isAndroid
@@ -339,6 +341,17 @@ class FileBottomBarState extends State<FileBottomBar> {
safeRefresh();
}
+ void _showLikesBottomSheet() {
+ final file = widget.file;
+ if (file.uploadedFileID == null || file.collectionID == null) return;
+
+ showLikesBottomSheet(
+ context,
+ fileID: file.uploadedFileID!,
+ initialCollectionID: file.collectionID!,
+ );
+ }
+
Widget _buildCommentIcon() {
if (_isFileInSharedCollectionFuture == null) {
return const SizedBox.shrink();