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 1 commit
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
40 changes: 31 additions & 9 deletions lib/feed/utils/community.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,36 @@ 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. 😊

);
// This is the list we will return. If we don't do any prioritization,
// it will just be the initial list we received.
List<CommunityView>? prioritizedCommunities = communities;
Copy link
Member

@hjiangsu hjiangsu Jan 5, 2024

Choose a reason for hiding this comment

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

Hmm, I believe there's a less verbose way to do this, but feel free to correct me if I'm wrong!

Instead of having to loop through the list in reverse, we can just perform a filter on the original communities list to get favorited and non-favorited communities. Once we have those, we can combine them into one. I feel like this makes it a bit easier to understand what's happening.

I wrote up some code to explain what I mean (its untested so not sure if it'll work properly) Let me know what you think!

// 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((community) => favoriteCommunityIds.contains(community.id)).toList();

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

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds good! I had to make a few teaks, but I agree it's much more readable! 😊


// Only attempt to prioritize favorites if the list of communities and favorites is not empty
if (communities?.isNotEmpty == true && favoriteCommunities?.isNotEmpty == true) {
prioritizedCommunities = [];
final List<CommunityView> sortedFavorites = [];

// Iterate through the list in reverse so that we can insert things at index 0 while preserving the order.
for (int i = communities!.length - 1; i >= 0; --i) {
// Grab the community at this index
final CommunityView community = communities[i];

if (favoriteCommunities!.any((c) => c.community.id == communities[i].community.id)) {
// If this is a favorite, add it to a separate list which only holds favorites
sortedFavorites.insert(0, community);
} else {
// If this is not a favorite, add it to our final list.
prioritizedCommunities.insert(0, community);
}
}

// At this point, we have a list of communities in the same order they were passed in but with no favorites.
// Now insert all the favorites at the beginning.
prioritizedCommunities.insertAll(0, sortedFavorites);
}

// Return the finalized list. If we were not gives communities/favorites, this may be null.
return prioritizedCommunities;
}
Loading