Skip to content

armgit / ente

Public Imported from GitHub
0

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

Add collection selector for file comments

ashilknde70f345ee3b
diff --git a/mobile/apps/photos/lib/db/social_db.dart b/mobile/apps/photos/lib/db/social_db.dart
index 07c3a76a0f..174b29abea 100644
--- a/mobile/apps/photos/lib/db/social_db.dart
+++ b/mobile/apps/photos/lib/db/social_db.dart
@@ -142,6 +142,20 @@ class SocialDB {
return comments.length;
}
+ /// Get comment count for a specific file within a specific collection
+ Future<int> getCommentCountForFileInCollection(
+ int fileID,
+ int collectionID,
+ ) async {
+ final db = await database;
+ final result = await db.rawQuery(
+ 'SELECT COUNT(*) as count FROM $commentsTable '
+ 'WHERE file_id = ? AND collection_id = ? AND is_deleted = 0',
+ [fileID, collectionID],
+ );
+ return Sqflite.firstIntValue(result) ?? 0;
+ }
+
Future<List<Comment>> getCommentsForCollection(int collectionID) async {
final db = await database;
final rows = await db.query(
@@ -174,17 +188,18 @@ class SocialDB {
return _rowToComment(rows.first);
}
- /// Paginated fetch for file comments
+ /// Paginated fetch for file comments within a specific collection
Future<List<Comment>> getCommentsForFilePaginated(
int fileID, {
+ required int collectionID,
int limit = 20,
int offset = 0,
}) async {
final db = await database;
final rows = await db.query(
commentsTable,
- where: 'file_id = ? AND is_deleted = 0',
- whereArgs: [fileID],
+ where: 'file_id = ? AND collection_id = ? AND is_deleted = 0',
+ whereArgs: [fileID, collectionID],
orderBy: 'created_at DESC',
limit: limit,
offset: offset,
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 88453f7ae2..f7aae46a9a 100644
--- a/mobile/apps/photos/lib/models/social/social_data_provider.dart
+++ b/mobile/apps/photos/lib/models/social/social_data_provider.dart
@@ -18,6 +18,10 @@ class SocialDataProvider {
return _db.getCommentCountForFile(fileID);
}
+ Future<int> getCommentCountForFileInCollection(int fileID, int collectionID) {
+ return _db.getCommentCountForFileInCollection(fileID, collectionID);
+ }
+
Future<List<Reaction>> getReactionsForFile(int fileID) {
return _db.getReactionsForFile(fileID);
}
@@ -36,11 +40,13 @@ class SocialDataProvider {
Future<List<Comment>> getCommentsForFilePaginated(
int fileID, {
+ required int collectionID,
int limit = 20,
int offset = 0,
}) {
return _db.getCommentsForFilePaginated(
fileID,
+ collectionID: collectionID,
limit: limit,
offset: offset,
);
diff --git a/mobile/apps/photos/lib/ui/social/comments_screen.dart b/mobile/apps/photos/lib/ui/social/comments_screen.dart
index c9368e31ec..8447eeb6c1 100644
--- a/mobile/apps/photos/lib/ui/social/comments_screen.dart
+++ b/mobile/apps/photos/lib/ui/social/comments_screen.dart
@@ -1,14 +1,20 @@
+import "dart:async";
+
import "package:flutter/material.dart";
+import "package:logging/logging.dart";
import "package:photos/core/configuration.dart";
import "package:photos/core/event_bus.dart";
+import "package:photos/db/files_db.dart";
import "package:photos/events/comment_deleted_event.dart";
import "package:photos/models/api/collection/user.dart";
+import "package:photos/models/collection/collection.dart";
import "package:photos/models/social/comment.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/social/widgets/collection_selector_widget.dart";
import "package:photos/ui/social/widgets/comment_bubble_widget.dart";
import "package:photos/ui/social/widgets/comment_input_widget.dart";
import "package:uuid/uuid.dart";
@@ -28,6 +34,8 @@ class FileCommentsScreen extends StatefulWidget {
}
class _FileCommentsScreenState extends State<FileCommentsScreen> {
+ static final _logger = Logger('FileCommentsScreen');
+
final List<Comment> _comments = [];
Comment? _replyingTo;
@@ -37,6 +45,9 @@ class _FileCommentsScreenState extends State<FileCommentsScreen> {
int _offset = 0;
final Map<int, User> _userCache = {};
+ List<CollectionCommentInfo> _sharedCollections = [];
+ late int _selectedCollectionID;
+
late final TextEditingController _textController;
late final FocusNode _inputFocusNode;
late final ScrollController _scrollController;
@@ -51,7 +62,8 @@ class _FileCommentsScreenState extends State<FileCommentsScreen> {
_inputFocusNode = FocusNode();
_scrollController = ScrollController()..addListener(_onScroll);
_currentUserID = Configuration.instance.getUserID()!;
- _loadInitialComments();
+ _selectedCollectionID = widget.collectionID;
+ _loadSharedCollections();
}
@override
@@ -62,12 +74,67 @@ class _FileCommentsScreenState extends State<FileCommentsScreen> {
super.dispose();
}
+ Future<void> _loadSharedCollections() async {
+ final collectionIDs = await FilesDB.instance.getAllCollectionIDsOfFile(
+ widget.fileID,
+ );
+
+ // Filter to shared collections first (sync operation)
+ final sharedCollectionsList = collectionIDs
+ .map((id) => CollectionsService.instance.getCollectionByID(id))
+ .whereType<Collection>()
+ .where(
+ (c) => c.hasSharees || c.hasLink || !c.isOwner(_currentUserID),
+ )
+ .toList();
+
+ // Fetch data in parallel
+ final sharedCollections = await Future.wait(
+ sharedCollectionsList.map((collection) async {
+ final commentCount = await SocialDataProvider.instance
+ .getCommentCountForFileInCollection(widget.fileID, collection.id);
+ final thumbnail =
+ await CollectionsService.instance.getCover(collection);
+ return CollectionCommentInfo(
+ collection: collection,
+ commentCount: commentCount,
+ thumbnail: thumbnail,
+ );
+ }),
+ );
+
+ if (mounted) {
+ if (sharedCollections.isEmpty) {
+ _logger.warning(
+ 'FileCommentsScreen opened for file ${widget.fileID} with no shared collections',
+ );
+ Navigator.of(context).pop();
+ return;
+ }
+
+ final isSelectedInShared = sharedCollections.any(
+ (info) => info.collection.id == _selectedCollectionID,
+ );
+
+ setState(() {
+ _sharedCollections = sharedCollections;
+
+ if (!isSelectedInShared) {
+ _selectedCollectionID = sharedCollections.first.collection.id;
+ }
+ });
+
+ unawaited(_loadInitialComments());
+ }
+ }
+
Future<void> _loadInitialComments() async {
setState(() => _isLoading = true);
final comments =
await SocialDataProvider.instance.getCommentsForFilePaginated(
widget.fileID,
+ collectionID: _selectedCollectionID,
limit: _pageSize,
offset: 0,
);
@@ -85,17 +152,13 @@ class _FileCommentsScreenState extends State<FileCommentsScreen> {
setState(() => _isLoadingMore = true);
- final comments = widget.fileID != null
- ? await SocialDataProvider.instance.getCommentsForFilePaginated(
- widget.fileID!,
- limit: _pageSize,
- offset: _offset,
- )
- : await SocialDataProvider.instance.getCommentsForCollectionPaginated(
- widget.collectionID,
- limit: _pageSize,
- offset: _offset,
- );
+ final comments =
+ await SocialDataProvider.instance.getCommentsForFilePaginated(
+ widget.fileID,
+ collectionID: _selectedCollectionID,
+ limit: _pageSize,
+ offset: _offset,
+ );
setState(() {
_comments.addAll(comments);
@@ -112,6 +175,17 @@ class _FileCommentsScreenState extends State<FileCommentsScreen> {
}
}
+ void _onCollectionSelected(int collectionID) {
+ setState(() {
+ _selectedCollectionID = collectionID;
+ _comments.clear();
+ _offset = 0;
+ _hasMoreComments = true;
+ _userCache.clear();
+ });
+ _loadInitialComments();
+ }
+
Future<Comment?> _getParentComment(String parentCommentId) {
return SocialDataProvider.instance.getCommentById(parentCommentId);
}
@@ -141,7 +215,7 @@ class _FileCommentsScreenState extends State<FileCommentsScreen> {
}
final user = CollectionsService.instance
- .getFileOwner(comment.userID, widget.collectionID);
+ .getFileOwner(comment.userID, _selectedCollectionID);
_userCache[comment.userID] = user;
return user;
}
@@ -157,7 +231,7 @@ class _FileCommentsScreenState extends State<FileCommentsScreen> {
final now = DateTime.now().millisecondsSinceEpoch;
final newComment = Comment(
id: const Uuid().v4(),
- collectionID: widget.collectionID,
+ collectionID: _selectedCollectionID,
fileID: widget.fileID,
data: text,
parentCommentID: _replyingTo?.id,
@@ -166,21 +240,53 @@ class _FileCommentsScreenState extends State<FileCommentsScreen> {
updatedAt: now,
);
- // Optimistic update
- setState(() {
- _comments.insert(0, newComment);
- _replyingTo = null;
- });
- _textController.clear();
+ // Persist first
+ final result = await SocialDataProvider.instance.addComment(newComment);
+ if (result == null) {
+ _logger.warning('Failed to save comment');
+ return;
+ }
+
+ // Update UI only after successful persistence
+ if (mounted) {
+ setState(() {
+ _comments.insert(0, newComment);
+ _replyingTo = null;
- // Persist
- await SocialDataProvider.instance.addComment(newComment);
+ // Update comment count in shared collections list
+ final index = _sharedCollections.indexWhere(
+ (c) => c.collection.id == _selectedCollectionID,
+ );
+ if (index != -1) {
+ final old = _sharedCollections[index];
+ _sharedCollections[index] = CollectionCommentInfo(
+ collection: old.collection,
+ commentCount: old.commentCount + 1,
+ thumbnail: old.thumbnail,
+ );
+ }
+ });
+ _textController.clear();
+ }
}
void _handleCommentDeleted(String commentId) {
Bus.instance.fire(CommentDeletedEvent(commentId));
setState(() {
_comments.removeWhere((c) => c.id == commentId);
+
+ // Decrement comment count in shared collections list
+ final index = _sharedCollections.indexWhere(
+ (c) => c.collection.id == _selectedCollectionID,
+ );
+ if (index != -1) {
+ final old = _sharedCollections[index];
+ _sharedCollections[index] = CollectionCommentInfo(
+ collection: old.collection,
+ commentCount: old.commentCount - 1,
+ thumbnail: old.thumbnail,
+ );
+ }
});
}
@@ -192,13 +298,19 @@ class _FileCommentsScreenState extends State<FileCommentsScreen> {
return Scaffold(
appBar: AppBar(
automaticallyImplyLeading: false,
- titleSpacing: 28,
+ titleSpacing: _sharedCollections.length > 1 ? 16 : 28,
backgroundColor: colorScheme.backgroundBase,
elevation: 0,
- title: Text(
- "${_comments.length} comments",
- style: textTheme.bodyBold,
- ),
+ title: _sharedCollections.length > 1
+ ? CollectionSelectorWidget(
+ sharedCollections: _sharedCollections,
+ selectedCollectionID: _selectedCollectionID,
+ onCollectionSelected: _onCollectionSelected,
+ )
+ : Text(
+ "${_comments.length} comments",
+ style: textTheme.bodyBold,
+ ),
actions: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 12),
@@ -242,7 +354,7 @@ class _FileCommentsScreenState extends State<FileCommentsScreen> {
user: _getUserForComment(comment),
isOwnComment: comment.userID == _currentUserID,
currentUserID: _currentUserID,
- collectionID: widget.collectionID,
+ collectionID: _selectedCollectionID,
onFetchParent: comment.isReply
? () => _getParentComment(comment.parentCommentID!)
: null,
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
new file mode 100644
index 0000000000..148f29fc5b
--- /dev/null
+++ b/mobile/apps/photos/lib/ui/social/widgets/collection_selector_widget.dart
@@ -0,0 +1,209 @@
+import "package:flutter/material.dart";
+import "package:photos/models/collection/collection.dart";
+import "package:photos/models/file/file.dart";
+import "package:photos/theme/ente_theme.dart";
+import "package:photos/ui/viewer/file/thumbnail_widget.dart";
+
+/// Holds collection info with comment count for the collection selector
+class CollectionCommentInfo {
+ final Collection collection;
+ final int commentCount;
+ final EnteFile? thumbnail;
+
+ const CollectionCommentInfo({
+ required this.collection,
+ required this.commentCount,
+ this.thumbnail,
+ });
+}
+
+class CollectionSelectorWidget extends StatefulWidget {
+ final List<CollectionCommentInfo> sharedCollections;
+ final int selectedCollectionID;
+ final ValueChanged<int> onCollectionSelected;
+
+ const CollectionSelectorWidget({
+ required this.sharedCollections,
+ required this.selectedCollectionID,
+ required this.onCollectionSelected,
+ super.key,
+ });
+
+ @override
+ State<CollectionSelectorWidget> createState() =>
+ _CollectionSelectorWidgetState();
+}
+
+class _CollectionSelectorWidgetState extends State<CollectionSelectorWidget> {
+ final GlobalKey _widgetKey = GlobalKey();
+ bool _isMenuOpen = false;
+
+ CollectionCommentInfo 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,
+ commentCount: _selectedCollection.commentCount,
+ ),
+ 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,
+ commentCount: info.commentCount,
+ ),
+ 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);
+ }
+ });
+ }
+}
+
+class _ThumbnailWithBadge extends StatelessWidget {
+ final EnteFile? thumbnail;
+ final int commentCount;
+
+ const _ThumbnailWithBadge({
+ required this.thumbnail,
+ required this.commentCount,
+ });
+
+ @override
+ Widget build(BuildContext context) {
+ final colorScheme = getEnteColorScheme(context);
+
+ return SizedBox(
+ width: 28,
+ height: 28,
+ child: Stack(
+ clipBehavior: Clip.none,
+ children: [
+ ClipRRect(
+ borderRadius: BorderRadius.circular(5),
+ child: thumbnail != null
+ ? ThumbnailWidget(thumbnail!)
+ : Container(color: colorScheme.fillMuted),
+ ),
+ Positioned(
+ bottom: -4,
+ right: -4,
+ child: Container(
+ padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 1),
+ decoration: BoxDecoration(
+ color: colorScheme.backgroundBase,
+ borderRadius: const BorderRadius.all(Radius.circular(16)),
+ ),
+ child: Text(
+ commentCount > 99 ? '99+' : '$commentCount',
+ style: TextStyle(
+ color: colorScheme.textBase,
+ fontSize: 8,
+ fontWeight: FontWeight.w500,
+ ),
+ textAlign: TextAlign.center,
+ ),
+ ),
+ ),
+ ],
+ ),
+ );
+ }
+}