Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix prioritized community sort #1015

Merged
merged 3 commits into from
Jan 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions lib/feed/utils/community.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,22 @@ Future<void> toggleFavoriteCommunity(BuildContext context, Community community,
}

/// Takes a list of [communities] and returns the list with any [favoriteCommunities] at the beginning of the list
/// Note that you may need to call [toList] on any lists that are marked as readonly.
/// Note that you may need to call [toList] when passing in lists that are marked as readonly.
List<CommunityView>? prioritizeFavorites(List<CommunityView>? communities, List<CommunityView>? favoriteCommunities) {
return communities
?..sort(
(a, b) => favoriteCommunities?.any((c) => c.community.id == a.community.id) == true
? -1
: favoriteCommunities?.any((c) => c.community.id == b.community.id) == true
? 1
: b.counts.subscribers.compareTo(a.counts.subscribers),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the problematic line from before. 😊

);
// If either communities or favorites are empty, no reason to prioritize.
if (communities?.isNotEmpty != true || favoriteCommunities?.isNotEmpty != true) {
return communities;
}

// Create a set of the favorited community ids for filtering later
Set<int> favoriteCommunityIds = Set<int>.from(favoriteCommunities!.map((c) => c.community.id));

// Filters out communities that are part of the favorites, and keeps the same order
List<CommunityView>? sortedFavorites = communities!.where((c) => favoriteCommunityIds.contains(c.community.id)).toList();

// Filters out communities that are not a part of the favorites, and keeps the same order
List<CommunityView>? sortedNonFavorites = communities.where((c) => !favoriteCommunityIds.contains(c.community.id)).toList();

// Combine them together, with favorites at the top
return List<CommunityView>.from(sortedFavorites)..addAll(sortedNonFavorites);
}
Loading