Randomized and limited the elements shown in the home in the starred sections

This commit is contained in:
CappielloAntonio 2021-12-06 11:43:52 +01:00
parent d9ce19c265
commit b92310f40f
5 changed files with 26 additions and 16 deletions

View file

@ -15,6 +15,7 @@ import com.cappielloantonio.play.util.MappingUtil;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.Collections;
import java.util.List; import java.util.List;
import retrofit2.Call; import retrofit2.Call;
@ -57,7 +58,7 @@ public class AlbumRepository {
return listLiveAlbums; return listLiveAlbums;
} }
public MutableLiveData<List<Album>> getStarredAlbums() { public MutableLiveData<List<Album>> getStarredAlbums(boolean random, int size) {
MutableLiveData<List<Album>> starredAlbums = new MutableLiveData<>(); MutableLiveData<List<Album>> starredAlbums = new MutableLiveData<>();
App.getSubsonicClientInstance(application, false) App.getSubsonicClientInstance(application, false)
@ -66,13 +67,16 @@ public class AlbumRepository {
.enqueue(new Callback<SubsonicResponse>() { .enqueue(new Callback<SubsonicResponse>() {
@Override @Override
public void onResponse(@NonNull Call<SubsonicResponse> call, @NonNull Response<SubsonicResponse> response) { public void onResponse(@NonNull Call<SubsonicResponse> call, @NonNull Response<SubsonicResponse> response) {
List<Album> albums = new ArrayList<>();
if (response.isSuccessful() && response.body() != null && response.body().getStarred2() != null) { if (response.isSuccessful() && response.body() != null && response.body().getStarred2() != null) {
albums.addAll(MappingUtil.mapAlbum(response.body().getStarred2().getAlbums())); List<Album> albums = new ArrayList<>(MappingUtil.mapAlbum(response.body().getStarred2().getAlbums()));
}
if (!random) {
starredAlbums.setValue(albums); starredAlbums.setValue(albums);
} else {
Collections.shuffle(albums);
starredAlbums.setValue(albums.subList(0, Math.min(size, albums.size())));
}
}
} }
@Override @Override

View file

@ -30,7 +30,7 @@ public class ArtistRepository {
this.application = application; this.application = application;
} }
public MutableLiveData<List<Artist>> getStarredArtists() { public MutableLiveData<List<Artist>> getStarredArtists(boolean random, int size) {
MutableLiveData<List<Artist>> starredArtists = new MutableLiveData<>(); MutableLiveData<List<Artist>> starredArtists = new MutableLiveData<>();
App.getSubsonicClientInstance(application, false) App.getSubsonicClientInstance(application, false)
@ -41,7 +41,13 @@ public class ArtistRepository {
public void onResponse(@NonNull Call<SubsonicResponse> call, @NonNull Response<SubsonicResponse> response) { public void onResponse(@NonNull Call<SubsonicResponse> call, @NonNull Response<SubsonicResponse> response) {
if (response.isSuccessful() && response.body() != null && response.body().getStarred2() != null) { if (response.isSuccessful() && response.body() != null && response.body().getStarred2() != null) {
List<Artist> artists = new ArrayList<>(MappingUtil.mapArtist(response.body().getStarred2().getArtists())); List<Artist> artists = new ArrayList<>(MappingUtil.mapArtist(response.body().getStarred2().getArtists()));
if (!random) {
getArtistInfo(artists, starredArtists); getArtistInfo(artists, starredArtists);
} else {
Collections.shuffle(artists);
getArtistInfo(artists.subList(0, Math.min(size, artists.size())), starredArtists);
}
} }
} }

View file

@ -47,7 +47,7 @@ public class AlbumListPageViewModel extends AndroidViewModel {
albumRepository.getAlbums("newest", 500, null, null).observe(activity, albums -> albumList.setValue(albums)); albumRepository.getAlbums("newest", 500, null, null).observe(activity, albums -> albumList.setValue(albums));
break; break;
case Album.STARRED: case Album.STARRED:
albumList = albumRepository.getStarredAlbums(); albumList = albumRepository.getStarredAlbums(false, -1);
break; break;
case Album.DOWNLOADED: case Album.DOWNLOADED:
downloadRepository.getLiveDownload().observe(activity, downloads -> albumList.setValue(MappingUtil.mapDownloadToAlbum(downloads))); downloadRepository.getLiveDownload().observe(activity, downloads -> albumList.setValue(MappingUtil.mapDownloadToAlbum(downloads)));

View file

@ -40,7 +40,7 @@ public class ArtistListPageViewModel extends AndroidViewModel {
switch (title) { switch (title) {
case Artist.STARRED: case Artist.STARRED:
artistList = artistRepository.getStarredArtists(); artistList = artistRepository.getStarredArtists(false, -1);
break; break;
case Artist.DOWNLOADED: case Artist.DOWNLOADED:
downloadRepository.getLiveDownload().observe(activity, downloads -> { downloadRepository.getLiveDownload().observe(activity, downloads -> {

View file

@ -68,17 +68,17 @@ public class HomeViewModel extends AndroidViewModel {
} }
public LiveData<List<Song>> getStarredTracks(LifecycleOwner owner) { public LiveData<List<Song>> getStarredTracks(LifecycleOwner owner) {
songRepository.getStarredSongs(false, -1).observe(owner, starredTracks::postValue); songRepository.getStarredSongs(true, 20).observe(owner, starredTracks::postValue);
return starredTracks; return starredTracks;
} }
public LiveData<List<Album>> getStarredAlbums(LifecycleOwner owner) { public LiveData<List<Album>> getStarredAlbums(LifecycleOwner owner) {
albumRepository.getStarredAlbums().observe(owner, starredAlbums::postValue); albumRepository.getStarredAlbums(true, 20).observe(owner, starredAlbums::postValue);
return starredAlbums; return starredAlbums;
} }
public LiveData<List<Artist>> getStarredArtists(LifecycleOwner owner) { public LiveData<List<Artist>> getStarredArtists(LifecycleOwner owner) {
artistRepository.getStarredArtists().observe(owner, starredArtists::postValue); artistRepository.getStarredArtists(true, 20).observe(owner, starredArtists::postValue);
return starredArtists; return starredArtists;
} }
@ -119,15 +119,15 @@ public class HomeViewModel extends AndroidViewModel {
} }
public void refreshStarredTracks(LifecycleOwner owner) { public void refreshStarredTracks(LifecycleOwner owner) {
songRepository.getStarredSongs(false, -1).observe(owner, starredTracks::postValue); songRepository.getStarredSongs(true, 20).observe(owner, starredTracks::postValue);
} }
public void refreshStarredAlbums(LifecycleOwner owner) { public void refreshStarredAlbums(LifecycleOwner owner) {
albumRepository.getStarredAlbums().observe(owner, starredAlbums::postValue); albumRepository.getStarredAlbums(true, 20).observe(owner, starredAlbums::postValue);
} }
public void refreshStarredArtists(LifecycleOwner owner) { public void refreshStarredArtists(LifecycleOwner owner) {
artistRepository.getStarredArtists().observe(owner, starredArtists::postValue); artistRepository.getStarredArtists(true, 20).observe(owner, starredArtists::postValue);
} }
public void refreshMostPlayedAlbums(LifecycleOwner owner) { public void refreshMostPlayedAlbums(LifecycleOwner owner) {