Skip to content

armgit / ente

Public Imported from GitHub
0

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

Show like count on comment bubbles with tappable likes sheet

ashilknb1c17d496edc
diff --git a/mobile/apps/photos/lib/ui/social/comment_likes_bottom_sheet.dart b/mobile/apps/photos/lib/ui/social/comment_likes_bottom_sheet.dart
new file mode 100644
index 0000000000..eb68e40174
--- /dev/null
+++ b/mobile/apps/photos/lib/ui/social/comment_likes_bottom_sheet.dart
@@ -0,0 +1,222 @@
+import "package:flutter/material.dart";
+import "package:photos/extensions/user_extension.dart";
+import "package:photos/models/api/collection/user.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/common/loading_widget.dart";
+import "package:photos/ui/components/buttons/icon_button_widget.dart";
+import "package:photos/ui/sharing/user_avator_widget.dart";
+
+const _shrinkWrapThreshold = 30;
+
+/// Shows the likes bottom sheet for a comment
+Future<void> showCommentLikesBottomSheet(
+ BuildContext context, {
+ required List<Reaction> reactions,
+ required int collectionID,
+ required int currentUserID,
+}) {
+ return showModalBottomSheet(
+ context: context,
+ isScrollControlled: true,
+ backgroundColor: Colors.transparent,
+ builder: (_) => CommentLikesBottomSheet(
+ reactions: reactions,
+ collectionID: collectionID,
+ currentUserID: currentUserID,
+ ),
+ );
+}
+
+class CommentLikesBottomSheet extends StatefulWidget {
+ final List<Reaction> reactions;
+ final int collectionID;
+ final int currentUserID;
+
+ const CommentLikesBottomSheet({
+ required this.reactions,
+ required this.collectionID,
+ required this.currentUserID,
+ super.key,
+ });
+
+ @override
+ State<CommentLikesBottomSheet> createState() =>
+ _CommentLikesBottomSheetState();
+}
+
+class _CommentLikesBottomSheetState extends State<CommentLikesBottomSheet> {
+ static const _maxHeightFraction = 0.7;
+
+ Map<String, String> _anonDisplayNames = {};
+ bool _isLoading = true;
+
+ List<Reaction> get _likes =>
+ widget.reactions.where((r) => !r.isDeleted).toList();
+
+ @override
+ void initState() {
+ super.initState();
+ _loadAnonDisplayNames();
+ }
+
+ Future<void> _loadAnonDisplayNames() async {
+ try {
+ final anonNames = await SocialDataProvider.instance
+ .getAnonDisplayNamesForCollection(widget.collectionID);
+ if (mounted) {
+ setState(() {
+ _anonDisplayNames = anonNames;
+ _isLoading = false;
+ });
+ }
+ } catch (_) {
+ if (mounted) {
+ setState(() => _isLoading = false);
+ }
+ }
+ }
+
+ User _getUserForReaction(Reaction reaction) {
+ if (reaction.isAnonymous) {
+ final anonID = reaction.anonUserID;
+ final displayName =
+ anonID != null ? (_anonDisplayNames[anonID] ?? anonID) : "Anonymous";
+ return User(
+ id: reaction.userID,
+ email: "${anonID ?? "anonymous"}@unknown.com",
+ name: displayName,
+ );
+ }
+
+ return CollectionsService.instance
+ .getFileOwner(reaction.userID, widget.collectionID);
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ final colorScheme = getEnteColorScheme(context);
+ final textTheme = getEnteTextTheme(context);
+ final mediaQuery = MediaQuery.of(context);
+
+ return Container(
+ constraints: BoxConstraints(
+ maxHeight: mediaQuery.size.height * _maxHeightFraction,
+ ),
+ decoration: BoxDecoration(
+ color: colorScheme.backgroundBase,
+ borderRadius: const BorderRadius.vertical(
+ top: Radius.circular(24),
+ ),
+ ),
+ child: SafeArea(
+ top: false,
+ child: Column(
+ mainAxisSize: MainAxisSize.min,
+ children: [
+ Padding(
+ padding: const EdgeInsets.fromLTRB(16, 16, 12, 8),
+ child: Row(
+ mainAxisAlignment: MainAxisAlignment.spaceBetween,
+ children: [
+ Text(
+ "${_likes.length} ${_likes.length == 1 ? 'like' : 'likes'}",
+ style: textTheme.bodyBold,
+ ),
+ IconButtonWidget(
+ iconButtonType: IconButtonType.rounded,
+ icon: Icons.close_rounded,
+ onTap: () => Navigator.of(context).pop(),
+ ),
+ ],
+ ),
+ ),
+ if (_isLoading)
+ const Padding(
+ padding: EdgeInsets.symmetric(vertical: 48),
+ child: EnteLoadingWidget(size: 24),
+ )
+ else if (_likes.isEmpty)
+ Padding(
+ padding:
+ const EdgeInsets.symmetric(vertical: 48, horizontal: 24),
+ child: Text(
+ "No likes yet",
+ style: textTheme.smallMuted,
+ textAlign: TextAlign.center,
+ ),
+ )
+ else
+ Flexible(
+ child: ListView.builder(
+ shrinkWrap: _likes.length <= _shrinkWrapThreshold,
+ padding: const EdgeInsets.fromLTRB(16, 8, 16, 16),
+ itemCount: _likes.length,
+ itemBuilder: (context, index) {
+ final reaction = _likes[index];
+ final user = _getUserForReaction(reaction);
+ return _CommentLikeListItem(
+ user: user,
+ currentUserID: widget.currentUserID,
+ );
+ },
+ ),
+ ),
+ ],
+ ),
+ ),
+ );
+ }
+}
+
+class _CommentLikeListItem extends StatelessWidget {
+ final User user;
+ final int currentUserID;
+
+ const _CommentLikeListItem({
+ required this.user,
+ required this.currentUserID,
+ });
+
+ @override
+ Widget build(BuildContext context) {
+ final colorScheme = getEnteColorScheme(context);
+
+ return Padding(
+ padding: const EdgeInsets.symmetric(vertical: 8),
+ child: Row(
+ children: [
+ UserAvatarWidget(
+ user,
+ currentUserID: currentUserID,
+ type: AvatarType.lg,
+ addStroke: false,
+ ),
+ const SizedBox(width: 12),
+ Expanded(
+ child: Text(
+ user.id == currentUserID
+ ? "You"
+ : (user.displayName ?? user.email),
+ style: TextStyle(
+ fontSize: 14,
+ fontWeight: FontWeight.w500,
+ height: 20 / 14,
+ letterSpacing: 14 * -0.02,
+ color: colorScheme.textMuted,
+ ),
+ overflow: TextOverflow.ellipsis,
+ ),
+ ),
+ const Icon(
+ Icons.favorite,
+ color: Color(0xFF08C225),
+ size: 20,
+ ),
+ ],
+ ),
+ );
+ }
+}
diff --git a/mobile/apps/photos/lib/ui/social/widgets/comment_actions_capsule.dart b/mobile/apps/photos/lib/ui/social/widgets/comment_actions_capsule.dart
index 14b5c5c3b3..0a026d9ebc 100644
--- a/mobile/apps/photos/lib/ui/social/widgets/comment_actions_capsule.dart
+++ b/mobile/apps/photos/lib/ui/social/widgets/comment_actions_capsule.dart
@@ -2,14 +2,12 @@ import "package:flutter/material.dart";
import "package:photos/theme/ente_theme.dart";
class CommentActionsCapsule extends StatelessWidget {
- final bool isLiked;
- final VoidCallback onLikeTap;
- final VoidCallback onReplyTap;
+ final int likeCount;
+ final VoidCallback? onTap;
const CommentActionsCapsule({
- required this.isLiked,
- required this.onLikeTap,
- required this.onReplyTap,
+ required this.likeCount,
+ this.onTap,
super.key,
});
@@ -18,41 +16,41 @@ class CommentActionsCapsule extends StatelessWidget {
final colorScheme = getEnteColorScheme(context);
final isDark = Theme.of(context).brightness == Brightness.dark;
final background =
- isDark ? const Color(0xFF161616) : const Color(0xFFF5F5F7);
- final iconColor =
- isDark ? const Color(0xFFFFFFFF) : const Color(0xFF131313);
+ isDark ? const Color(0xFF161616) : const Color(0xFFF0F0F0);
- return Container(
- padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 6),
- decoration: BoxDecoration(
- color: background,
- border: Border.all(
- color: colorScheme.backgroundBase,
- width: 2,
+ return GestureDetector(
+ onTap: onTap,
+ child: Container(
+ padding: const EdgeInsets.only(left: 6, right: 8, top: 6, bottom: 6),
+ decoration: BoxDecoration(
+ color: background,
+ border: Border.all(
+ color: colorScheme.backgroundBase,
+ width: 2,
+ ),
+ borderRadius: BorderRadius.circular(42),
),
- borderRadius: BorderRadius.circular(42),
- ),
- child: Row(
- mainAxisSize: MainAxisSize.min,
- children: [
- GestureDetector(
- onTap: onLikeTap,
- child: Icon(
- isLiked ? Icons.favorite : Icons.favorite_border,
- size: 18,
- color: isLiked ? const Color(0xFF08C225) : iconColor,
+ child: Row(
+ mainAxisSize: MainAxisSize.min,
+ children: [
+ const Icon(
+ Icons.favorite,
+ size: 14,
+ color: Color(0xFF08C225),
),
- ),
- const SizedBox(width: 8),
- GestureDetector(
- onTap: onReplyTap,
- child: Icon(
- Icons.reply,
- size: 18,
- color: iconColor,
+ const SizedBox(width: 2),
+ Text(
+ likeCount.toString(),
+ style: const TextStyle(
+ fontSize: 10,
+ fontWeight: FontWeight.w500,
+ color: Color(0xFF666666),
+ height: 14 / 10,
+ letterSpacing: -0.3,
+ ),
),
- ),
- ],
+ ],
+ ),
),
);
}
diff --git a/mobile/apps/photos/lib/ui/social/widgets/comment_bubble_widget.dart b/mobile/apps/photos/lib/ui/social/widgets/comment_bubble_widget.dart
index 6ea9ebc1af..195ef0b0f3 100644
--- a/mobile/apps/photos/lib/ui/social/widgets/comment_bubble_widget.dart
+++ b/mobile/apps/photos/lib/ui/social/widgets/comment_bubble_widget.dart
@@ -15,6 +15,7 @@ import "package:photos/theme/ente_theme.dart";
import "package:photos/ui/common/loading_widget.dart";
import "package:photos/ui/notification/toast.dart";
import "package:photos/ui/sharing/user_avator_widget.dart";
+import "package:photos/ui/social/comment_likes_bottom_sheet.dart";
import "package:photos/ui/social/widgets/comment_actions_capsule.dart";
import "package:photos/ui/social/widgets/comment_actions_popup.dart";
import "package:photos/ui/social/widgets/delete_comment_confirmation_dialog.dart";
@@ -71,6 +72,7 @@ class _CommentBubbleWidgetState extends State<CommentBubbleWidget>
Comment? _parentComment;
List<Reaction> _reactions = [];
bool _isLiked = false;
+ int _optimisticLikeDelta = 0;
bool _isLoadingParent = false;
bool _isLoadingReactions = false;
double _dragOffset = 0.0;
@@ -128,6 +130,7 @@ class _CommentBubbleWidgetState extends State<CommentBubbleWidget>
_parentComment = null;
_reactions = [];
_isLiked = false;
+ _optimisticLikeDelta = 0;
_contentSize = null;
_loadData();
_measureContent();
@@ -168,6 +171,7 @@ class _CommentBubbleWidgetState extends State<CommentBubbleWidget>
_isLiked = _reactions.any(
(r) => r.userID == widget.currentUserID && !r.isDeleted,
);
+ _optimisticLikeDelta = 0;
if (mounted) setState(() => _isLoadingReactions = false);
}
@@ -193,7 +197,12 @@ class _CommentBubbleWidgetState extends State<CommentBubbleWidget>
Future<void> _toggleLike() async {
final previousState = _isLiked;
- setState(() => _isLiked = !_isLiked);
+ final previousDelta = _optimisticLikeDelta;
+
+ setState(() {
+ _isLiked = !_isLiked;
+ _optimisticLikeDelta = _isLiked ? 1 : -1;
+ });
try {
await SocialDataProvider.instance.toggleReaction(
@@ -205,7 +214,10 @@ class _CommentBubbleWidgetState extends State<CommentBubbleWidget>
} catch (e) {
_logger.severe('Failed to toggle comment like', e);
if (mounted) {
- setState(() => _isLiked = previousState);
+ setState(() {
+ _isLiked = previousState;
+ _optimisticLikeDelta = previousDelta;
+ });
showShortToast(context, "Failed to like comment");
}
return;
@@ -213,7 +225,11 @@ class _CommentBubbleWidgetState extends State<CommentBubbleWidget>
// Refresh reactions after successful toggle (best-effort, no rollback if fails)
_reactions = await widget.onFetchReactions();
- if (mounted) setState(() {});
+ if (mounted) {
+ setState(() {
+ _optimisticLikeDelta = 0;
+ });
+ }
}
void _showHighlight() {
@@ -299,6 +315,15 @@ class _CommentBubbleWidgetState extends State<CommentBubbleWidget>
}
}
+ void _showCommentLikesSheet() {
+ showCommentLikesBottomSheet(
+ context,
+ reactions: _reactions,
+ collectionID: widget.collectionID,
+ currentUserID: widget.currentUserID,
+ );
+ }
+
@override
Widget build(BuildContext context) {
final colorScheme = getEnteColorScheme(context);
@@ -432,6 +457,11 @@ class _CommentBubbleWidgetState extends State<CommentBubbleWidget>
final canDeleteComment = widget.isOwnComment ||
(widget.canModerateAnonComments && widget.comment.isAnonymous);
+ final actualCount = _reactions.where((r) => !r.isDeleted).length;
+ final likeCount = (actualCount + _optimisticLikeDelta).clamp(0, 999999);
+ final showCapsule =
+ !_isLoadingReactions && showActionsCapsule && likeCount > 0;
+
return Padding(
padding: EdgeInsets.only(
right: widget.isOwnComment ? 6 : 0,
@@ -471,9 +501,7 @@ class _CommentBubbleWidgetState extends State<CommentBubbleWidget>
Stack(
children: [
Padding(
- padding: showActionsCapsule
- ? const EdgeInsets.only(right: 16, bottom: 17)
- : const EdgeInsets.only(right: 16),
+ padding: const EdgeInsets.only(right: 16, bottom: 12),
child: _CommentBubble(
comment: widget.comment,
isOwnComment: widget.isOwnComment,
@@ -483,16 +511,15 @@ class _CommentBubbleWidgetState extends State<CommentBubbleWidget>
userResolver: widget.userResolver,
),
),
- if (!_isLoadingReactions && showActionsCapsule)
+ if (showCapsule)
Positioned(
right: 0,
bottom: 0,
child: Opacity(
opacity: capsuleOpacity,
child: CommentActionsCapsule(
- isLiked: _isLiked,
- onLikeTap: _toggleLike,
- onReplyTap: widget.onReplyTap,
+ likeCount: likeCount,
+ onTap: _showCommentLikesSheet,
),
),
),