-
Notifications
You must be signed in to change notification settings - Fork 68
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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), | ||
); | ||
// 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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); There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} |
There was a problem hiding this comment.
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. 😊