Add preference to shuffle songs on the 'genre' page

This commit is contained in:
Joril
2026-03-17 20:26:07 +00:00
parent 0b61653dcc
commit 1ef7ef7377
7 changed files with 72 additions and 26 deletions

View File

@@ -1008,39 +1008,61 @@ public class AutomotiveRepository {
return listenableFuture;
}
public ListenableFuture<LibraryResult<ImmutableList<MediaItem>>> getSongsByGenre(String genre, int count) {
public ListenableFuture<LibraryResult<ImmutableList<MediaItem>>> getSongsByGenre(String genre, int count, boolean shuffle) {
final SettableFuture<LibraryResult<ImmutableList<MediaItem>>> listenableFuture = SettableFuture.create();
App.getSubsonicClientInstance(false)
.getAlbumSongListClient()
.getSongsByGenre(genre, count, 0)
.enqueue(new Callback<ApiResponse>() {
@Override
public void onResponse(@NonNull Call<ApiResponse> call, @NonNull Response<ApiResponse> response) {
if (response.isSuccessful() && response.body() != null && response.body().getSubsonicResponse().getSongsByGenre() != null && response.body().getSubsonicResponse().getSongsByGenre().getSongs() != null) {
List<com.cappielloantonio.tempo.subsonic.models.Child> songs = response.body().getSubsonicResponse().getSongsByGenre().getSongs();
Call<ApiResponse> call;
if (shuffle) {
call = App.getSubsonicClientInstance(false)
.getAlbumSongListClient()
.getRandomSongs(count, null, null, genre);
} else {
call = App.getSubsonicClientInstance(false)
.getAlbumSongListClient()
.getSongsByGenre(genre, count, 0);
}
setChildrenMetadata(songs);
List<MediaItem> mediaItems = MappingUtil.mapMediaItems(songs);
LibraryResult<ImmutableList<MediaItem>> libraryResult = LibraryResult.ofItemList(ImmutableList.copyOf(mediaItems), null);
listenableFuture.set(libraryResult);
} else {
listenableFuture.set(LibraryResult.ofError(LibraryResult.RESULT_ERROR_BAD_VALUE));
}
call.enqueue(new Callback<ApiResponse>() {
@Override
public void onResponse(@NonNull Call<ApiResponse> call, @NonNull Response<ApiResponse> response) {
if (response.isSuccessful() && response.body() != null) {
List<com.cappielloantonio.tempo.subsonic.models.Child> songs;
if (shuffle) {
songs = response.body().getSubsonicResponse().getRandomSongs() != null
? response.body().getSubsonicResponse().getRandomSongs().getSongs()
: null;
} else {
songs = response.body().getSubsonicResponse().getSongsByGenre() != null
? response.body().getSubsonicResponse().getSongsByGenre().getSongs()
: null;
}
@Override
public void onFailure(@NonNull Call<ApiResponse> call, @NonNull Throwable t) {
listenableFuture.setException(t);
if (songs != null) {
setChildrenMetadata(songs);
List<MediaItem> mediaItems = MappingUtil.mapMediaItems(songs);
LibraryResult<ImmutableList<MediaItem>> libraryResult = LibraryResult.ofItemList(ImmutableList.copyOf(mediaItems), null);
listenableFuture.set(libraryResult);
} else {
listenableFuture.set(LibraryResult.ofError(LibraryResult.RESULT_ERROR_BAD_VALUE));
}
});
} else {
listenableFuture.set(LibraryResult.ofError(LibraryResult.RESULT_ERROR_BAD_VALUE));
}
}
@Override
public void onFailure(@NonNull Call<ApiResponse> call, @NonNull Throwable t) {
listenableFuture.setException(t);
}
});
return listenableFuture;
}
public ListenableFuture<LibraryResult<ImmutableList<MediaItem>>> getSongsByGenre(String genre, int count) {
return getSongsByGenre(genre, count, false);
}
private static class GetMediaItemThreadSafe implements Runnable {
private final SessionMediaItemDao sessionMediaItemDao;
private final String id;

View File

@@ -102,6 +102,7 @@ object Preferences {
private const val AA_SECOND_TAB = "androidauto_second_tab"
private const val AA_THIRD_TAB = "androidauto_third_tab"
private const val AA_FOURTH_TAB = "androidauto_fourth_tab"
private const val AA_SHUFFLE_GENRE_SONGS = "androidauto_shuffle_genre_songs"
@JvmStatic
fun getServer(): String? {
@@ -818,4 +819,14 @@ object Preferences {
return App.getInstance().preferences.getString(AA_FOURTH_TAB, "3")!!.toInt()
}
@JvmStatic
fun isAndroidAutoShuffleGenreSongsEnabled(): Boolean {
return App.getInstance().preferences.getBoolean(AA_SHUFFLE_GENRE_SONGS, false)
}
@JvmStatic
fun setAndroidAutoShuffleGenreSongsEnabled(enabled: Boolean) {
App.getInstance().preferences.edit().putBoolean(AA_SHUFFLE_GENRE_SONGS, enabled).apply()
}
}

View File

@@ -338,6 +338,8 @@
<string name="settings_androidauto_second_tab">Affichage du deuxième onglet</string>
<string name="settings_androidauto_third_tab">Affichage du troisième onglet</string>
<string name="settings_androidauto_fourth_tab">Affichage du quatrième onglet</string>
<string name="settings_androidauto_shuffle_genre_songs">Mélanger les chansons par genre</string>
<string name="settings_androidauto_shuffle_genre_songs_summary">Lire des chansons aléatoires lors de la sélection d\'un genre</string>
<string name="settings_audio_transcode_download_format">Format de transcodage</string>
<string name="settings_audio_transcode_download_priority_summary">Si activé, Tempus ne forcera pas le téléchargement de la piste avec les paramètres de transcodage ci-dessous.</string>
<string name="settings_audio_transcode_download_priority_title">Prioriser les paramètres du serveurs, utilisés pour le streaming, dans les téléchargements</string>
@@ -434,7 +436,6 @@
<string name="settings_theme">Thème</string>
<string name="settings_tile_size">Taille des vignettes</string>
<string name="settings_title_data">Données</string>
<string name="settings_title_data">Données</string>
<string name="settings_title_general">Géneral</string>
<string name="settings_title_playlist">Playlist</string>
<string name="settings_title_rating">Note</string>

View File

@@ -402,6 +402,8 @@
<string name="settings_androidauto_second_tab">Second tab display</string>
<string name="settings_androidauto_third_tab">Third tab display</string>
<string name="settings_androidauto_fourth_tab">Fourth tab display</string>
<string name="settings_androidauto_shuffle_genre_songs">Перемешивать треки по жанру</string>
<string name="settings_androidauto_shuffle_genre_songs_summary">Воспроизводить случайные треки при выборе жанра</string>
<string name="settings_audio_quality">Показывать качество аудио</string>
<string name="settings_audio_quality_summary">Битрейт и формат аудио будут отображаться для каждого трека.</string>
<string name="settings_song_rating">Показывать рейтинг трека</string>

View File

@@ -403,6 +403,8 @@
<string name="settings_androidauto_second_tab">Second tab display</string>
<string name="settings_androidauto_third_tab">Third tab display</string>
<string name="settings_androidauto_fourth_tab">Fourth tab display</string>
<string name="settings_androidauto_shuffle_genre_songs">Shuffle genre songs</string>
<string name="settings_androidauto_shuffle_genre_songs_summary">Play random songs when selecting a genre</string>
<string name="settings_audio_quality">Show audio quality</string>
<string name="settings_audio_quality_summary">The bitrate and audio format will be shown for each audio track.</string>
<string name="settings_song_rating">Show song star rating</string>

View File

@@ -519,7 +519,12 @@
app:entryValues="@array/aa_tab_values"
app:key="androidauto_fourth_tab"
app:title="@string/settings_androidauto_fourth_tab"
app:useSimpleSummaryProvider="true" />
app:useSimpleSummaryProvider="true" />
<SwitchPreference
android:title="@string/settings_androidauto_shuffle_genre_songs"
android:defaultValue="false"
android:key="androidauto_shuffle_genre_songs" />
</PreferenceCategory>
<!-- end Add by MFO -->

View File

@@ -529,7 +529,10 @@ object MediaBrowserTree {
}
if (id.startsWith(GENRES_ID)) {
return automotiveRepository.getSongsByGenre(id.removePrefix(GENRES_ID), 100)
val shuffle = Preferences.isAndroidAutoShuffleGenreSongsEnabled()
// If the user doesn't want random songs, it's likely it's for perusing them, so provide as many as possible
val count = if (shuffle) 100 else 500
return automotiveRepository.getSongsByGenre(id.removePrefix(GENRES_ID), count, shuffle)
}
if (id.startsWith(PLAYLIST_ID)) {