Speed up fetching artist songs for shuffle and radio

This commit is contained in:
antonio 2023-03-10 16:11:10 +01:00
parent c7dc0a8a31
commit cc7775c986
4 changed files with 26 additions and 34 deletions

View file

@ -1,12 +1,10 @@
package com.cappielloantonio.play.repository; package com.cappielloantonio.play.repository;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.MutableLiveData;
import com.cappielloantonio.play.App; import com.cappielloantonio.play.App;
import com.cappielloantonio.play.subsonic.base.ApiResponse; import com.cappielloantonio.play.subsonic.base.ApiResponse;
import com.cappielloantonio.play.subsonic.models.AlbumID3;
import com.cappielloantonio.play.subsonic.models.ArtistID3; import com.cappielloantonio.play.subsonic.models.ArtistID3;
import com.cappielloantonio.play.subsonic.models.ArtistInfo2; import com.cappielloantonio.play.subsonic.models.ArtistInfo2;
import com.cappielloantonio.play.subsonic.models.Child; import com.cappielloantonio.play.subsonic.models.Child;
@ -258,31 +256,23 @@ public class ArtistRepository {
return instantMix; return instantMix;
} }
public MutableLiveData<ArrayList<Child>> getArtistRandomSong(LifecycleOwner owner, ArtistID3 artist, int count) { public MutableLiveData<List<Child>> getRandomSong(ArtistID3 artist, int count) {
MutableLiveData<ArrayList<Child>> randomSongs = new MutableLiveData<>(); MutableLiveData<List<Child>> randomSongs = new MutableLiveData<>();
App.getSubsonicClientInstance(false) App.getSubsonicClientInstance(false)
.getBrowsingClient() .getBrowsingClient()
.getArtist(artist.getId()) .getTopSongs(artist.getName(), count)
.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().getArtist() != null && response.body().getSubsonicResponse().getArtist().getAlbums() != null) { if (response.isSuccessful() && response.body() != null && response.body().getSubsonicResponse().getTopSongs() != null && response.body().getSubsonicResponse().getTopSongs().getSongs() != null) {
List<AlbumID3> albums = response.body().getSubsonicResponse().getArtist().getAlbums(); List<Child> songs = response.body().getSubsonicResponse().getTopSongs().getSongs();
if (albums.size() > 0) { if (songs != null && !songs.isEmpty()) {
AlbumRepository albumRepository = new AlbumRepository(); Collections.shuffle(songs);
for (int index = 0; index < albums.size(); index++) {
albumRepository.getAlbumTracks(albums.get(index).getId()).observe(owner, songs -> {
ArrayList<Child> liveSongs = randomSongs.getValue();
if (liveSongs == null) liveSongs = new ArrayList<>();
Collections.shuffle(liveSongs);
liveSongs.addAll(songs);
randomSongs.setValue(liveSongs);
});
}
} }
randomSongs.setValue(songs);
} }
} }

View file

@ -19,18 +19,17 @@ import androidx.navigation.Navigation;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import com.cappielloantonio.play.R; import com.cappielloantonio.play.R;
import com.cappielloantonio.play.ui.adapter.AlbumArtistPageOrSimilarAdapter;
import com.cappielloantonio.play.ui.adapter.ArtistSimilarAdapter;
import com.cappielloantonio.play.ui.adapter.SongHorizontalAdapter;
import com.cappielloantonio.play.databinding.FragmentArtistPageBinding; import com.cappielloantonio.play.databinding.FragmentArtistPageBinding;
import com.cappielloantonio.play.glide.CustomGlideRequest; import com.cappielloantonio.play.glide.CustomGlideRequest;
import com.cappielloantonio.play.helper.recyclerview.CustomLinearSnapHelper; import com.cappielloantonio.play.helper.recyclerview.CustomLinearSnapHelper;
import com.cappielloantonio.play.interfaces.ClickCallback; import com.cappielloantonio.play.interfaces.ClickCallback;
import com.cappielloantonio.play.model.Media; import com.cappielloantonio.play.model.Media;
import com.cappielloantonio.play.repository.ArtistRepository;
import com.cappielloantonio.play.service.MediaManager; import com.cappielloantonio.play.service.MediaManager;
import com.cappielloantonio.play.service.MediaService; import com.cappielloantonio.play.service.MediaService;
import com.cappielloantonio.play.ui.activity.MainActivity; import com.cappielloantonio.play.ui.activity.MainActivity;
import com.cappielloantonio.play.ui.adapter.AlbumArtistPageOrSimilarAdapter;
import com.cappielloantonio.play.ui.adapter.ArtistSimilarAdapter;
import com.cappielloantonio.play.ui.adapter.SongHorizontalAdapter;
import com.cappielloantonio.play.util.MusicUtil; import com.cappielloantonio.play.util.MusicUtil;
import com.cappielloantonio.play.viewmodel.ArtistPageViewModel; import com.cappielloantonio.play.viewmodel.ArtistPageViewModel;
import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.ListenableFuture;
@ -135,12 +134,9 @@ public class ArtistPageFragment extends Fragment implements ClickCallback {
}); });
} }
// TODO Utilizzare il viewmodel come tramite ed evitare le chiamate dirette
private void initPlayButtons() { private void initPlayButtons() {
bind.artistPageShuffleButton.setOnClickListener(v -> { bind.artistPageShuffleButton.setOnClickListener(v -> {
ArtistRepository artistRepository = new ArtistRepository(); artistPageViewModel.getArtistShuffleList().observe(getViewLifecycleOwner(), songs -> {
artistRepository.getArtistRandomSong(getViewLifecycleOwner(), artistPageViewModel.getArtist(), 20).observe(getViewLifecycleOwner(), songs -> {
if (songs.size() > 0) { if (songs.size() > 0) {
MediaManager.startQueue(mediaBrowserListenableFuture, requireContext(), songs, 0); MediaManager.startQueue(mediaBrowserListenableFuture, requireContext(), songs, 0);
activity.setBottomSheetInPeek(true); activity.setBottomSheetInPeek(true);
@ -151,9 +147,7 @@ public class ArtistPageFragment extends Fragment implements ClickCallback {
}); });
bind.artistPageRadioButton.setOnClickListener(v -> { bind.artistPageRadioButton.setOnClickListener(v -> {
ArtistRepository artistRepository = new ArtistRepository(); artistPageViewModel.getArtistInstantMix().observe(getViewLifecycleOwner(), songs -> {
artistRepository.getInstantMix(artistPageViewModel.getArtist(), 20).observe(getViewLifecycleOwner(), songs -> {
if (songs.size() > 0) { if (songs.size() > 0) {
MediaManager.startQueue(mediaBrowserListenableFuture, requireContext(), songs, 0); MediaManager.startQueue(mediaBrowserListenableFuture, requireContext(), songs, 0);
activity.setBottomSheetInPeek(true); activity.setBottomSheetInPeek(true);
@ -169,7 +163,7 @@ public class ArtistPageFragment extends Fragment implements ClickCallback {
songHorizontalAdapter = new SongHorizontalAdapter(this, true); songHorizontalAdapter = new SongHorizontalAdapter(this, true);
bind.mostStreamedSongRecyclerView.setAdapter(songHorizontalAdapter); bind.mostStreamedSongRecyclerView.setAdapter(songHorizontalAdapter);
artistPageViewModel.getArtistTopSongList(10).observe(getViewLifecycleOwner(), songs -> { artistPageViewModel.getArtistTopSongList().observe(getViewLifecycleOwner(), songs -> {
if (bind != null) if (bind != null)
bind.artistPageTopSongsSector.setVisibility(!songs.isEmpty() ? View.VISIBLE : View.GONE); bind.artistPageTopSongsSector.setVisibility(!songs.isEmpty() ? View.VISIBLE : View.GONE);
songHorizontalAdapter.setItems(songs); songHorizontalAdapter.setItems(songs);

View file

@ -104,7 +104,7 @@ public class ArtistBottomSheetDialog extends BottomSheetDialogFragment implement
TextView playRandom = view.findViewById(R.id.play_random_text_view); TextView playRandom = view.findViewById(R.id.play_random_text_view);
playRandom.setOnClickListener(v -> { playRandom.setOnClickListener(v -> {
ArtistRepository artistRepository = new ArtistRepository(); ArtistRepository artistRepository = new ArtistRepository();
artistRepository.getArtistRandomSong(getViewLifecycleOwner(), artist, 20).observe(getViewLifecycleOwner(), songs -> { artistRepository.getRandomSong(artist, 50).observe(getViewLifecycleOwner(), songs -> {
if (songs.size() > 0) { if (songs.size() > 0) {
MediaManager.startQueue(mediaBrowserListenableFuture, requireContext(), songs, 0); MediaManager.startQueue(mediaBrowserListenableFuture, requireContext(), songs, 0);
((MainActivity) requireActivity()).setBottomSheetInPeek(true); ((MainActivity) requireActivity()).setBottomSheetInPeek(true);

View file

@ -36,8 +36,16 @@ public class ArtistPageViewModel extends AndroidViewModel {
return artistRepository.getArtistFullInfo(id); return artistRepository.getArtistFullInfo(id);
} }
public LiveData<List<Child>> getArtistTopSongList(int count) { public LiveData<List<Child>> getArtistTopSongList() {
return artistRepository.getTopSongs(artist.getName(), count); return artistRepository.getTopSongs(artist.getName(), 20);
}
public LiveData<List<Child>> getArtistShuffleList() {
return artistRepository.getRandomSong(artist, 50);
}
public LiveData<List<Child>> getArtistInstantMix() {
return artistRepository.getInstantMix(artist, 20);
} }
public ArtistID3 getArtist() { public ArtistID3 getArtist() {