Skip to content

armgit / ente

Public Imported from GitHub
0

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

refactor(photos): defer memory social collection resolution

ashilkn11ce8cee18cc
diff --git a/mobile/apps/photos/lib/services/collections_service.dart b/mobile/apps/photos/lib/services/collections_service.dart
index f65902652b..51d1d7f375 100644
--- a/mobile/apps/photos/lib/services/collections_service.dart
+++ b/mobile/apps/photos/lib/services/collections_service.dart
@@ -312,26 +312,62 @@ class CollectionsService {
return [];
}
- final hiddenCollectionIDs = includeHidden
- ? const <int>{}
- : getHiddenCollectionIds();
- final sharedCollections =
- collectionIDs
- .map((collectionID) => _collectionIDToCollections[collectionID])
- .whereType<Collection>()
- .where(
- (collection) =>
- !collection.isDeleted &&
- !hiddenCollectionIDs.contains(collection.id) &&
- (collection.hasSharees ||
- collection.hasLink ||
- !collection.isOwner(currentUserID)),
- )
- .toList()
- ..sort((first, second) => first.id.compareTo(second.id));
+ final sharedCollections = <Collection>[];
+ for (final collectionID in collectionIDs) {
+ final collection = _collectionIDToCollections[collectionID];
+ if (_isEligibleSharedCollection(
+ collection,
+ currentUserID,
+ includeHidden: includeHidden,
+ )) {
+ sharedCollections.add(collection!);
+ }
+ }
+ sharedCollections.sort((first, second) => first.id.compareTo(second.id));
return sharedCollections;
}
+ /// Returns IDs of visible shared collections containing [uploadedFileID].
+ ///
+ /// Use this to scope social content that may be rendered in the UI without
+ /// constructing a list of collection objects on the view path.
+ Future<List<int>> getVisibleSharedCollectionIDsForFile(
+ int uploadedFileID,
+ ) async {
+ final collectionIDs = await _filesDB.getAllCollectionIDsOfFile(
+ uploadedFileID,
+ );
+ final currentUserID = _config.getUserID();
+ if (collectionIDs.isEmpty || currentUserID == null) {
+ return [];
+ }
+
+ final sharedCollectionIDs = <int>[];
+ for (final collectionID in collectionIDs) {
+ if (_isEligibleSharedCollection(
+ _collectionIDToCollections[collectionID],
+ currentUserID,
+ includeHidden: false,
+ )) {
+ sharedCollectionIDs.add(collectionID);
+ }
+ }
+ return sharedCollectionIDs;
+ }
+
+ bool _isEligibleSharedCollection(
+ Collection? collection,
+ int currentUserID, {
+ required bool includeHidden,
+ }) {
+ return collection != null &&
+ !collection.isDeleted &&
+ (includeHidden || !collection.isHidden()) &&
+ (collection.hasSharees ||
+ collection.hasLink ||
+ !collection.isOwner(currentUserID));
+ }
+
Future<List<Collection>> getArchivedCollection() async {
final allCollections = getCollectionsForUI();
return allCollections
diff --git a/mobile/apps/photos/lib/ui/social/like_collection_selector_sheet.dart b/mobile/apps/photos/lib/ui/social/like_collection_selector_sheet.dart
index d234f402f2..af352082be 100644
--- a/mobile/apps/photos/lib/ui/social/like_collection_selector_sheet.dart
+++ b/mobile/apps/photos/lib/ui/social/like_collection_selector_sheet.dart
@@ -8,7 +8,6 @@ import "package:photos/models/collection/collection.dart";
import "package:photos/models/file/extensions/file_props.dart";
import "package:photos/models/file/file.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";
@@ -32,13 +31,14 @@ class _CollectionLikeState {
/// Parameters:
/// - [fileID]: The uploaded file ID to like
/// - [currentUserID]: Current user's ID for checking existing likes
+/// - [collections]: Shared collections eligible for this action
/// - [file]: The EnteFile for displaying thumbnail (optional, will fetch if null)
Future<void> showLikeCollectionSelectorSheet(
BuildContext context, {
required int fileID,
required int currentUserID,
+ required List<Collection> collections,
EnteFile? file,
- Set<int>? allowedCollectionIDs,
}) {
return showModalBottomSheet(
context: context,
@@ -47,8 +47,8 @@ Future<void> showLikeCollectionSelectorSheet(
builder: (_) => LikeCollectionSelectorSheet(
fileID: fileID,
currentUserID: currentUserID,
+ collections: collections,
file: file,
- allowedCollectionIDs: allowedCollectionIDs,
),
);
}
@@ -56,14 +56,14 @@ Future<void> showLikeCollectionSelectorSheet(
class LikeCollectionSelectorSheet extends StatefulWidget {
final int fileID;
final int currentUserID;
+ final List<Collection> collections;
final EnteFile? file;
- final Set<int>? allowedCollectionIDs;
const LikeCollectionSelectorSheet({
required this.fileID,
required this.currentUserID,
+ required this.collections,
this.file,
- this.allowedCollectionIDs,
super.key,
});
@@ -93,27 +93,9 @@ class _LikeCollectionSelectorSheetState
// Load file if not provided (for thumbnail)
_file ??= await FilesDB.instance.getAnyUploadedFile(widget.fileID);
- final sharedCollections =
- (await CollectionsService.instance.getSharedCollectionsForFile(
- widget.fileID,
- includeHidden: true,
- ))
- .where(
- (c) =>
- (widget.allowedCollectionIDs == null ||
- widget.allowedCollectionIDs!.contains(c.id)),
- )
- .toList();
-
- // If no shared collections, close the sheet
- if (sharedCollections.isEmpty) {
- if (mounted) Navigator.of(context).pop();
- return;
- }
-
// Fetch like states in parallel
final collectionStates = await Future.wait(
- sharedCollections.map((collection) async {
+ widget.collections.map((collection) async {
final reactions = await SocialDataProvider.instance
.getReactionsForFileInCollection(widget.fileID, collection.id);
final isLiked = reactions.any(
diff --git a/mobile/apps/photos/lib/ui/social/widgets/file_social_overlay.dart b/mobile/apps/photos/lib/ui/social/widgets/file_social_overlay.dart
index f3b6321d0f..41cb2dd46d 100644
--- a/mobile/apps/photos/lib/ui/social/widgets/file_social_overlay.dart
+++ b/mobile/apps/photos/lib/ui/social/widgets/file_social_overlay.dart
@@ -10,7 +10,6 @@ import "package:photos/core/event_bus.dart";
import "package:photos/events/social_data_updated_event.dart";
import "package:photos/generated/l10n.dart";
import "package:photos/models/api/collection/user.dart";
-import "package:photos/models/collection/collection.dart";
import "package:photos/models/file/file.dart";
import "package:photos/models/social/comment.dart";
import "package:photos/models/social/comment_author_utils.dart";
@@ -55,7 +54,7 @@ class _FileSocialOverlayState extends State<FileSocialOverlay> {
late final StreamSubscription<SocialDataUpdatedEvent>
_socialDataUpdatedSubscription;
- List<Collection> _sharedCollections = const [];
+ List<int> _visibleSharedCollectionIDs = const [];
bool _hasLiked = false;
int _commentCount = 0;
Comment? _latestComment;
@@ -89,7 +88,7 @@ class _FileSocialOverlayState extends State<FileSocialOverlay> {
}
void _clearSocialState() {
- _sharedCollections = const [];
+ _visibleSharedCollectionIDs = const [];
_hasLiked = false;
_commentCount = 0;
_latestComment = null;
@@ -123,20 +122,17 @@ class _FileSocialOverlayState extends State<FileSocialOverlay> {
}
try {
- final sharedCollections = await CollectionsService.instance
- .getSharedCollectionsForFile(fileID);
+ final collectionIDs = await CollectionsService.instance
+ .getVisibleSharedCollectionIDsForFile(fileID);
if (!_isCurrentLoad(fileID, generation)) return;
- if (sharedCollections.isEmpty) {
+ if (collectionIDs.isEmpty) {
setState(_clearSocialState);
widget.onVisibilityChanged(false);
return;
}
final provider = SocialDataProvider.instance;
- final collectionIDs = sharedCollections
- .map((collection) => collection.id)
- .toList();
final results = await Future.wait<Object?>([
provider.getReactionsForFile(fileID),
provider.getCommentCountForFile(fileID),
@@ -169,7 +165,7 @@ class _FileSocialOverlayState extends State<FileSocialOverlay> {
if (!_isCurrentLoad(fileID, generation)) return;
setState(() {
- _sharedCollections = sharedCollections;
+ _visibleSharedCollectionIDs = collectionIDs;
_hasLiked = reactions.any(
(reaction) => reaction.userID == currentUserID,
);
@@ -194,7 +190,7 @@ class _FileSocialOverlayState extends State<FileSocialOverlay> {
final currentUserID = widget.currentUserID;
if (fileID == null ||
currentUserID == null ||
- _sharedCollections.isEmpty ||
+ _visibleSharedCollectionIDs.isEmpty ||
_reactionUpdateFileIDs.contains(fileID)) {
return;
}
@@ -206,13 +202,22 @@ class _FileSocialOverlayState extends State<FileSocialOverlay> {
return;
}
- if (_sharedCollections.length == 1) {
+ final sharedCollections = await CollectionsService.instance
+ .getSharedCollectionsForFile(fileID);
+ if (!mounted ||
+ widget.file.uploadedFileID != fileID ||
+ widget.currentUserID != currentUserID ||
+ sharedCollections.isEmpty) {
+ return;
+ }
+
+ if (sharedCollections.length == 1) {
final previousState = _hasLiked;
setState(() => _hasLiked = true);
try {
await SocialDataProvider.instance.toggleReaction(
userID: currentUserID,
- collectionID: _sharedCollections.single.id,
+ collectionID: sharedCollections.single.id,
fileID: fileID,
);
} catch (error, stackTrace) {
@@ -233,10 +238,8 @@ class _FileSocialOverlayState extends State<FileSocialOverlay> {
context,
fileID: fileID,
currentUserID: currentUserID,
+ collections: sharedCollections,
file: file,
- allowedCollectionIDs: _sharedCollections
- .map((collection) => collection.id)
- .toSet(),
),
);
} finally {
@@ -301,31 +304,48 @@ class _FileSocialOverlayState extends State<FileSocialOverlay> {
Future<void> _showLikes() async {
final fileID = widget.file.uploadedFileID;
- if (fileID == null || _sharedCollections.isEmpty) return;
- final initialCollectionID = _sharedCollections.first.id;
- await _runSheetAndRefresh(
- () => showLikesBottomSheet(
+ if (fileID == null || _visibleSharedCollectionIDs.isEmpty) return;
+ await _runSheetAndRefresh(() async {
+ final sharedCollections = await CollectionsService.instance
+ .getSharedCollectionsForFile(fileID);
+ if (!mounted ||
+ widget.file.uploadedFileID != fileID ||
+ sharedCollections.isEmpty) {
+ return;
+ }
+ await showLikesBottomSheet(
context,
fileID: fileID,
- initialCollectionID: initialCollectionID,
- ),
- );
+ initialCollectionID: sharedCollections.first.id,
+ );
+ });
}
Future<void> _openComments({Comment? comment}) async {
final fileID = widget.file.uploadedFileID;
- if (fileID == null || _sharedCollections.isEmpty) return;
- await _runSheetAndRefresh(
- () => showFileCommentsBottomSheet(
+ if (fileID == null || _visibleSharedCollectionIDs.isEmpty) return;
+ await _runSheetAndRefresh(() async {
+ final sharedCollections = await CollectionsService.instance
+ .getSharedCollectionsForFile(fileID);
+ if (!mounted ||
+ widget.file.uploadedFileID != fileID ||
+ sharedCollections.isEmpty) {
+ return;
+ }
+
+ final preferredCollectionID =
+ comment?.collectionID ?? _latestComment?.collectionID;
+ final preferredCollection = sharedCollections.firstWhereOrNull(
+ (collection) => collection.id == preferredCollectionID,
+ );
+
+ await showFileCommentsBottomSheet(
context,
- collectionID:
- comment?.collectionID ??
- _latestComment?.collectionID ??
- _sharedCollections.first.id,
+ collectionID: preferredCollection?.id ?? sharedCollections.first.id,
fileID: fileID,
- highlightCommentID: comment?.id,
- ),
- );
+ highlightCommentID: preferredCollection == null ? null : comment?.id,
+ );
+ });
}
Future<void> _runSheetAndRefresh(Future<void> Function() showSheet) async {
@@ -340,7 +360,7 @@ class _FileSocialOverlayState extends State<FileSocialOverlay> {
@override
Widget build(BuildContext context) {
- if (_sharedCollections.isEmpty) {
+ if (_visibleSharedCollectionIDs.isEmpty) {
return const SizedBox.shrink();
}
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 524fa4de1e..728284292d 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
@@ -441,6 +441,7 @@ class FileBottomBarState extends State<FileBottomBar> {
context,
fileID: file.uploadedFileID!,
currentUserID: widget.userID!,
+ collections: sharedCollections,
file: file,
);
// Refresh state after sheet closes to sync heart icon