2020-11-20 15:38:08 +01:00
|
|
|
package com.cappielloantonio.play.viewmodel;
|
|
|
|
|
|
|
|
|
|
import android.app.Application;
|
|
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
import androidx.lifecycle.AndroidViewModel;
|
2021-08-10 15:42:39 +02:00
|
|
|
import androidx.lifecycle.LifecycleOwner;
|
2020-11-20 15:38:08 +01:00
|
|
|
import androidx.lifecycle.LiveData;
|
2021-08-10 15:42:39 +02:00
|
|
|
import androidx.lifecycle.MutableLiveData;
|
2020-11-20 15:38:08 +01:00
|
|
|
|
|
|
|
|
import com.cappielloantonio.play.model.Album;
|
|
|
|
|
import com.cappielloantonio.play.model.Artist;
|
|
|
|
|
import com.cappielloantonio.play.model.Genre;
|
|
|
|
|
import com.cappielloantonio.play.model.Playlist;
|
2020-11-21 18:41:35 +01:00
|
|
|
import com.cappielloantonio.play.repository.AlbumRepository;
|
|
|
|
|
import com.cappielloantonio.play.repository.ArtistRepository;
|
2020-11-20 15:38:08 +01:00
|
|
|
import com.cappielloantonio.play.repository.GenreRepository;
|
2020-11-22 19:11:38 +01:00
|
|
|
import com.cappielloantonio.play.repository.PlaylistRepository;
|
2020-11-20 15:38:08 +01:00
|
|
|
|
2021-09-01 16:53:10 +02:00
|
|
|
import java.util.Calendar;
|
|
|
|
|
import java.util.Comparator;
|
2020-11-20 15:38:08 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
public class LibraryViewModel extends AndroidViewModel {
|
2021-09-01 16:53:10 +02:00
|
|
|
private static final String TAG = "LibraryViewModel";
|
|
|
|
|
|
2021-08-17 14:46:10 +02:00
|
|
|
private final AlbumRepository albumRepository;
|
|
|
|
|
private final ArtistRepository artistRepository;
|
|
|
|
|
private final GenreRepository genreRepository;
|
|
|
|
|
private final PlaylistRepository playlistRepository;
|
2020-11-21 18:41:35 +01:00
|
|
|
|
2021-08-17 14:46:10 +02:00
|
|
|
private final MutableLiveData<List<Playlist>> playlistSample = new MutableLiveData<>(null);
|
|
|
|
|
private final MutableLiveData<List<Album>> sampleAlbum = new MutableLiveData<>(null);
|
2021-09-01 16:53:10 +02:00
|
|
|
private final MutableLiveData<List<Album>> newReleasedAlbum = new MutableLiveData<>(null);
|
2021-08-17 14:46:10 +02:00
|
|
|
private final MutableLiveData<List<Artist>> sampleArtist = new MutableLiveData<>(null);
|
|
|
|
|
private final MutableLiveData<List<Genre>> sampleGenres = new MutableLiveData<>(null);
|
2020-11-20 15:38:08 +01:00
|
|
|
|
|
|
|
|
public LibraryViewModel(@NonNull Application application) {
|
|
|
|
|
super(application);
|
|
|
|
|
|
2020-11-21 18:41:35 +01:00
|
|
|
albumRepository = new AlbumRepository(application);
|
|
|
|
|
artistRepository = new ArtistRepository(application);
|
2020-11-20 15:38:08 +01:00
|
|
|
genreRepository = new GenreRepository(application);
|
2020-11-22 19:11:38 +01:00
|
|
|
playlistRepository = new PlaylistRepository(application);
|
2020-11-21 18:41:35 +01:00
|
|
|
|
|
|
|
|
// Inizializzate all'interno del costruttore, in modo da rimanere immutabili per tutto il
|
|
|
|
|
// ciclo di vita dell'applicazione
|
2021-09-01 16:53:10 +02:00
|
|
|
albumRepository.getAlbums("random", 20, null, null).observeForever(sampleAlbum::postValue);
|
2021-08-17 14:46:10 +02:00
|
|
|
artistRepository.getArtists(true, 20).observeForever(sampleArtist::postValue);
|
|
|
|
|
genreRepository.getGenres(true, 15).observeForever(sampleGenres::postValue);
|
|
|
|
|
playlistRepository.getPlaylists(true, 10).observeForever(playlistSample::postValue);
|
2020-11-20 15:38:08 +01:00
|
|
|
}
|
|
|
|
|
|
2021-09-01 16:53:10 +02:00
|
|
|
public LiveData<List<Album>> getRecentlyReleasedAlbums(LifecycleOwner owner) {
|
|
|
|
|
int currentYear = Calendar.getInstance().get(Calendar.YEAR);
|
|
|
|
|
|
|
|
|
|
albumRepository.getAlbums("byYear", 500, currentYear, currentYear).observe(owner, albums -> {
|
|
|
|
|
albums.sort(Comparator.comparing(Album::getCreated).reversed());
|
|
|
|
|
newReleasedAlbum.postValue(albums.subList(0, Math.min(20, albums.size())));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return newReleasedAlbum;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-21 18:41:35 +01:00
|
|
|
public LiveData<List<Album>> getAlbumSample() {
|
|
|
|
|
return sampleAlbum;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public LiveData<List<Artist>> getArtistSample() {
|
|
|
|
|
return sampleArtist;
|
2020-11-20 15:38:08 +01:00
|
|
|
}
|
|
|
|
|
|
2020-11-21 18:41:35 +01:00
|
|
|
public LiveData<List<Genre>> getGenreSample() {
|
|
|
|
|
return sampleGenres;
|
2020-11-20 15:38:08 +01:00
|
|
|
}
|
2021-08-10 15:42:39 +02:00
|
|
|
|
|
|
|
|
public LiveData<List<Playlist>> getPlaylistSample() {
|
|
|
|
|
return playlistSample;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void refreshAlbumSample(LifecycleOwner owner) {
|
2021-09-01 16:53:10 +02:00
|
|
|
albumRepository.getAlbums("random", 20, null, null).observe(owner, sampleAlbum::postValue);
|
2021-08-10 15:42:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void refreshArtistSample(LifecycleOwner owner) {
|
2021-08-17 14:46:10 +02:00
|
|
|
artistRepository.getArtists(true, 20).observe(owner, sampleArtist::postValue);
|
2021-08-10 15:42:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void refreshGenreSample(LifecycleOwner owner) {
|
2021-08-17 14:46:10 +02:00
|
|
|
genreRepository.getGenres(true, 15).observe(owner, sampleGenres::postValue);
|
2021-08-10 15:42:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void refreshPlaylistSample(LifecycleOwner owner) {
|
2021-08-17 14:46:10 +02:00
|
|
|
playlistRepository.getPlaylists(true, 10).observe(owner, playlistSample::postValue);
|
2021-08-10 15:42:39 +02:00
|
|
|
}
|
2020-11-20 15:38:08 +01:00
|
|
|
}
|