feat: sort artists by album count (#206)

This commit is contained in:
eddyizm 2025-11-02 07:58:46 -08:00 committed by GitHub
commit 911acc3c2d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 35 additions and 1 deletions

View file

@ -151,6 +151,9 @@ public class ArtistCatalogueAdapter extends RecyclerView.Adapter<ArtistCatalogue
case Constants.ARTIST_ORDER_BY_RANDOM:
Collections.shuffle(artists);
break;
case Constants.ARTIST_ORDER_BY_ALBUM_COUNT:
artists.sort(Comparator.comparing(ArtistID3::getAlbumCount).reversed());
break;
}
notifyDataSetChanged();

View file

@ -34,6 +34,7 @@ import com.cappielloantonio.tempo.interfaces.ClickCallback;
import com.cappielloantonio.tempo.ui.activity.MainActivity;
import com.cappielloantonio.tempo.ui.adapter.ArtistCatalogueAdapter;
import com.cappielloantonio.tempo.util.Constants;
import com.cappielloantonio.tempo.util.Preferences;
import com.cappielloantonio.tempo.viewmodel.ArtistCatalogueViewModel;
import com.cappielloantonio.tempo.subsonic.models.ArtistID3;
@ -114,7 +115,10 @@ public class ArtistCatalogueFragment extends Fragment implements ClickCallback {
artistAdapter = new ArtistCatalogueAdapter(this);
artistAdapter.setStateRestorationPolicy(RecyclerView.Adapter.StateRestorationPolicy.PREVENT_WHEN_EMPTY);
bind.artistCatalogueRecyclerView.setAdapter(artistAdapter);
artistCatalogueViewModel.getArtistList().observe(getViewLifecycleOwner(), artistList -> artistAdapter.setItems(artistList));
artistCatalogueViewModel.getArtistList().observe(getViewLifecycleOwner(), artistList -> {
artistAdapter.setItems(artistList);
artistAdapter.sort(Preferences.getArtistSortOrder());
});
bind.artistCatalogueRecyclerView.setOnTouchListener((v, event) -> {
hideKeyboard(v);
@ -192,6 +196,9 @@ public class ArtistCatalogueFragment extends Fragment implements ClickCallback {
} else if (menuItem.getItemId() == R.id.menu_artist_sort_random) {
artistAdapter.sort(Constants.ARTIST_ORDER_BY_RANDOM);
return true;
} else if (menuItem.getItemId() == R.id.menu_artist_sort_album_count) {
artistAdapter.sort(Constants.ARTIST_ORDER_BY_ALBUM_COUNT);
return true;
}
return false;

View file

@ -40,6 +40,7 @@ object Constants {
const val ARTIST_STARRED = "ARTIST_STARRED"
const val ARTIST_ORDER_BY_NAME = "ARTIST_ORDER_BY_NAME"
const val ARTIST_ORDER_BY_RANDOM = "ARTIST_ORDER_BY_RANDOM"
const val ARTIST_ORDER_BY_ALBUM_COUNT = "ARTIST_ORDER_BY_ALBUM_COUNT"
const val ARTIST_ORDER_BY_MOST_RECENTLY_STARRED = "ARTIST_ORDER_BY_MOST_RECENTLY_STARRED"
const val ARTIST_ORDER_BY_LEAST_RECENTLY_STARRED = "ARTIST_ORDER_BY_LEAST_RECENTLY_STARRED"

View file

@ -79,6 +79,7 @@ object Preferences {
private const val ALBUM_DETAIL = "album_detail"
private const val ALBUM_SORT_ORDER = "album_sort_order"
private const val DEFAULT_ALBUM_SORT_ORDER = Constants.ALBUM_ORDER_BY_NAME
private const val ARTIST_SORT_BY_ALBUM_COUNT= "artist_sort_by_album_count"
@JvmStatic
fun getServer(): String? {
@ -656,4 +657,14 @@ object Preferences {
fun setAlbumSortOrder(sortOrder: String) {
App.getInstance().preferences.edit().putString(ALBUM_SORT_ORDER, sortOrder).apply()
}
@JvmStatic
fun getArtistSortOrder(): String {
val sort_by_album_count = App.getInstance().preferences.getBoolean(ARTIST_SORT_BY_ALBUM_COUNT, false)
Log.d("Preferences", "getSortOrder")
if (sort_by_album_count)
return Constants.ARTIST_ORDER_BY_ALBUM_COUNT
else
return Constants.ARTIST_ORDER_BY_NAME
}
}

View file

@ -6,4 +6,7 @@
<item
android:id="@+id/menu_artist_sort_random"
android:title="@string/menu_sort_random" />
<item
android:id="@+id/menu_artist_sort_album_count"
android:title="@string/menu_sort_album_count" />
</menu>

View file

@ -200,6 +200,7 @@
<string name="menu_sort_artist">Artist</string>
<string name="menu_sort_name">Name</string>
<string name="menu_sort_random">Random</string>
<string name="menu_sort_album_count">Album Count</string>
<string name="menu_sort_recently_added">Recently added</string>
<string name="menu_sort_recently_played">Recently played</string>
<string name="menu_sort_most_played">Most played</string>
@ -527,4 +528,6 @@
<string name="settings_album_detail">Show album detail</string>
<string name="settings_album_detail_summary">If enabled, show the album details like genre, song count etc. on the album page</string>
<string name="settings_artist_sort_by_album_count">Sort artists by album count</string>
<string name="settings_artist_sort_by_album_count_summary">If enabled, sort the artists by album count. Sort by name if disabled.</string>
</resources>

View file

@ -116,6 +116,12 @@
android:summary="@string/settings_album_detail_summary"
android:key="album_detail" />
<SwitchPreference
android:title="@string/settings_artist_sort_by_album_count"
android:defaultValue="false"
android:summary="@string/settings_artist_sort_by_album_count_summary"
android:key="artist_sort_by_album_count" />
</PreferenceCategory>
<PreferenceCategory app:title="@string/settings_title_playlist">