mirror of
https://github.com/antebudimir/tempus.git
synced 2026-01-01 18:03:33 +00:00
fix: shuffle for artists without using getTopSongs
This commit is contained in:
parent
77bdd71d79
commit
4b2e963a81
3 changed files with 30 additions and 9 deletions
|
|
@ -13,9 +13,11 @@ import com.cappielloantonio.tempo.subsonic.models.Child;
|
||||||
import com.cappielloantonio.tempo.subsonic.models.IndexID3;
|
import com.cappielloantonio.tempo.subsonic.models.IndexID3;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import retrofit2.Call;
|
import retrofit2.Call;
|
||||||
import retrofit2.Callback;
|
import retrofit2.Callback;
|
||||||
|
|
@ -312,24 +314,42 @@ public class ArtistRepository {
|
||||||
|
|
||||||
App.getSubsonicClientInstance(false)
|
App.getSubsonicClientInstance(false)
|
||||||
.getBrowsingClient()
|
.getBrowsingClient()
|
||||||
.getTopSongs(artist.getName(), count)
|
.getArtist(artist.getId())
|
||||||
.enqueue(new Callback<ApiResponse>() {
|
.enqueue(new Callback<ApiResponse>() {
|
||||||
@Override
|
@Override
|
||||||
public void onResponse(@NonNull Call<ApiResponse> call, @NonNull Response<ApiResponse> response) {
|
public void onResponse(@NonNull Call<ApiResponse> call, @NonNull Response<ApiResponse> response) {
|
||||||
if (response.isSuccessful() && response.body() != null && response.body().getSubsonicResponse().getTopSongs() != null && response.body().getSubsonicResponse().getTopSongs().getSongs() != null) {
|
if (response.isSuccessful() && response.body() != null &&
|
||||||
List<Child> songs = response.body().getSubsonicResponse().getTopSongs().getSongs();
|
response.body().getSubsonicResponse().getArtist() != null &&
|
||||||
|
response.body().getSubsonicResponse().getArtist().getAlbums() != null) {
|
||||||
|
|
||||||
if (songs != null && !songs.isEmpty()) {
|
List<AlbumID3> albums = response.body().getSubsonicResponse().getArtist().getAlbums();
|
||||||
Collections.shuffle(songs);
|
Log.d("ArtistRepository", "Got albums directly: " + albums.size());
|
||||||
|
if (albums.isEmpty()) {
|
||||||
|
Log.d("ArtistRepository", "No albums found in artist response");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
randomSongs.setValue(songs);
|
Collections.shuffle(albums);
|
||||||
|
int[] counts = albums.stream().mapToInt(AlbumID3::getSongCount).toArray();
|
||||||
|
Arrays.parallelPrefix(counts, Integer::sum);
|
||||||
|
int albumLimit = 0;
|
||||||
|
int multiplier = 4; // get more than the limit so we can shuffle them
|
||||||
|
while (albumLimit < albums.size() && counts[albumLimit] < count * multiplier)
|
||||||
|
albumLimit++;
|
||||||
|
Log.d("ArtistRepository", String.format("Retaining %d/%d albums", albumLimit, albums.size()));
|
||||||
|
|
||||||
|
fetchAllAlbumSongsWithCallback(albums.stream().limit(albumLimit).collect(Collectors.toList()), songs -> {
|
||||||
|
Collections.shuffle(songs);
|
||||||
|
randomSongs.setValue(songs.stream().limit(count).collect(Collectors.toList()));
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
Log.d("ArtistRepository", "Failed to get artist info");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(@NonNull Call<ApiResponse> call, @NonNull Throwable t) {
|
public void onFailure(@NonNull Call<ApiResponse> call, @NonNull Throwable t) {
|
||||||
|
Log.d("ArtistRepository", "Error getting artist info: " + t.getMessage());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -188,8 +188,6 @@ public class ArtistPageFragment extends Fragment implements ClickCallback {
|
||||||
} else {
|
} else {
|
||||||
if (bind != null)
|
if (bind != null)
|
||||||
bind.artistPageTopSongsSector.setVisibility(!songs.isEmpty() ? View.VISIBLE : View.GONE);
|
bind.artistPageTopSongsSector.setVisibility(!songs.isEmpty() ? View.VISIBLE : View.GONE);
|
||||||
if (bind != null)
|
|
||||||
bind.artistPageShuffleButton.setEnabled(!songs.isEmpty());
|
|
||||||
songHorizontalAdapter.setItems(songs);
|
songHorizontalAdapter.setItems(songs);
|
||||||
reapplyPlayback();
|
reapplyPlayback();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -89,6 +89,9 @@ public class ArtistBottomSheetDialog extends BottomSheetDialogFragment implement
|
||||||
ArtistRepository artistRepository = new ArtistRepository();
|
ArtistRepository artistRepository = new ArtistRepository();
|
||||||
|
|
||||||
artistRepository.getInstantMix(artist, 20).observe(getViewLifecycleOwner(), songs -> {
|
artistRepository.getInstantMix(artist, 20).observe(getViewLifecycleOwner(), songs -> {
|
||||||
|
// navidrome may return null for this
|
||||||
|
if (songs == null)
|
||||||
|
return;
|
||||||
MusicUtil.ratingFilter(songs);
|
MusicUtil.ratingFilter(songs);
|
||||||
|
|
||||||
if (!songs.isEmpty()) {
|
if (!songs.isEmpty()) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue