Added a new page dedicated to the favorites of artists, albums and songs

This commit is contained in:
CappielloAntonio 2022-01-15 17:07:29 +01:00
parent fc71341c73
commit 2a103909d1
7 changed files with 542 additions and 2 deletions

View file

@ -16,6 +16,8 @@ import com.cappielloantonio.play.repository.DownloadRepository;
import com.cappielloantonio.play.util.MappingUtil;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Comparator;
import java.util.List;
public class AlbumListPageViewModel extends AndroidViewModel {
@ -50,6 +52,13 @@ public class AlbumListPageViewModel extends AndroidViewModel {
case Album.STARRED:
albumList = albumRepository.getStarredAlbums(false, -1);
break;
case Album.NEW_RELEASES:
int currentYear = Calendar.getInstance().get(Calendar.YEAR);
albumRepository.getAlbums("byYear", 500, currentYear, currentYear).observe(owner, albums -> {
albums.sort(Comparator.comparing(Album::getCreated).reversed());
albumList.postValue(albums.subList(0, Math.min(20, albums.size())));
});
break;
case Album.DOWNLOADED:
downloadRepository.getLiveDownload().observe(owner, downloads -> albumList.setValue(MappingUtil.mapDownloadToAlbum(downloads)));
break;

View file

@ -0,0 +1,64 @@
package com.cappielloantonio.play.viewmodel;
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import com.cappielloantonio.play.model.Album;
import com.cappielloantonio.play.model.Artist;
import com.cappielloantonio.play.model.Song;
import com.cappielloantonio.play.repository.AlbumRepository;
import com.cappielloantonio.play.repository.ArtistRepository;
import com.cappielloantonio.play.repository.SongRepository;
import java.util.List;
public class StarredViewModel extends AndroidViewModel {
private final SongRepository songRepository;
private final AlbumRepository albumRepository;
private final ArtistRepository artistRepository;
private final MutableLiveData<List<Song>> starredTracks = new MutableLiveData<>(null);
private final MutableLiveData<List<Album>> starredAlbums = new MutableLiveData<>(null);
private final MutableLiveData<List<Artist>> starredArtists = new MutableLiveData<>(null);
public StarredViewModel(@NonNull Application application) {
super(application);
songRepository = new SongRepository(application);
albumRepository = new AlbumRepository(application);
artistRepository = new ArtistRepository(application);
}
public LiveData<List<Song>> getStarredTracks(LifecycleOwner owner) {
songRepository.getStarredSongs(true, 20).observe(owner, starredTracks::postValue);
return starredTracks;
}
public LiveData<List<Album>> getStarredAlbums(LifecycleOwner owner) {
albumRepository.getStarredAlbums(true, 20).observe(owner, starredAlbums::postValue);
return starredAlbums;
}
public LiveData<List<Artist>> getStarredArtists(LifecycleOwner owner) {
artistRepository.getStarredArtists(true, 20).observe(owner, starredArtists::postValue);
return starredArtists;
}
public void refreshStarredTracks(LifecycleOwner owner) {
songRepository.getStarredSongs(true, 20).observe(owner, starredTracks::postValue);
}
public void refreshStarredAlbums(LifecycleOwner owner) {
albumRepository.getStarredAlbums(true, 20).observe(owner, starredAlbums::postValue);
}
public void refreshStarredArtists(LifecycleOwner owner) {
artistRepository.getStarredArtists(true, 20).observe(owner, starredArtists::postValue);
}
}