tempus/app/src/main/java/com/cappielloantonio/play/viewmodel/HomeViewModel.java

126 lines
5.2 KiB
Java
Raw Normal View History

2020-11-20 15:38:08 +01:00
package com.cappielloantonio.play.viewmodel;
2020-11-21 18:41:35 +01:00
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LifecycleOwner;
2020-11-21 18:41:35 +01:00
import androidx.lifecycle.LiveData;
2021-07-28 15:28:32 +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;
2021-07-29 17:12:55 +02:00
import com.cappielloantonio.play.model.Download;
2020-11-20 15:38:08 +01:00
import com.cappielloantonio.play.model.Song;
import com.cappielloantonio.play.repository.AlbumRepository;
import com.cappielloantonio.play.repository.ArtistRepository;
2021-07-29 17:12:55 +02:00
import com.cappielloantonio.play.repository.DownloadRepository;
2020-11-21 18:41:35 +01:00
import com.cappielloantonio.play.repository.SongRepository;
2020-11-20 15:38:08 +01:00
import java.util.List;
2020-11-21 18:41:35 +01:00
public class HomeViewModel extends AndroidViewModel {
2020-11-22 19:11:38 +01:00
private static final String TAG = "HomeViewModel";
private final SongRepository songRepository;
private final AlbumRepository albumRepository;
private final ArtistRepository artistRepository;
private final MutableLiveData<List<Song>> dicoverSongSample = new MutableLiveData<>(null);
2021-08-17 16:17:41 +02:00
private final MutableLiveData<List<Song>> starredTracksSample = new MutableLiveData<>(null);
private final MutableLiveData<List<Album>> mostPlayedAlbumSample = new MutableLiveData<>(null);
private final MutableLiveData<List<Album>> recentlyPlayedAlbumSample = new MutableLiveData<>(null);
private final MutableLiveData<List<Integer>> years = new MutableLiveData<>(null);
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);
private final MutableLiveData<List<Album>> recentlyAddedAlbumSample = new MutableLiveData<>(null);
2020-11-21 18:41:35 +01:00
public HomeViewModel(@NonNull Application application) {
super(application);
2020-11-20 15:38:08 +01:00
2020-11-21 18:41:35 +01:00
songRepository = new SongRepository(application);
albumRepository = new AlbumRepository(application);
artistRepository = new ArtistRepository(application);
2020-11-20 15:38:08 +01:00
songRepository.getRandomSample(10, null, null).observeForever(dicoverSongSample::postValue);
2021-08-17 16:17:41 +02:00
songRepository.getStarredSongs(true, 10).observeForever(starredTracksSample::postValue);
2020-11-20 15:38:08 +01:00
}
public LiveData<List<Song>> getDiscoverSongSample() {
return dicoverSongSample;
2020-11-21 18:41:35 +01:00
}
2021-08-17 16:17:41 +02:00
public LiveData<List<Song>> getStarredTracksSample() {
return starredTracksSample;
}
public LiveData<List<Integer>> getYearList(LifecycleOwner owner) {
albumRepository.getDecades().observe(owner, years::postValue);
return years;
2020-11-21 18:41:35 +01:00
}
2020-11-20 15:38:08 +01:00
public LiveData<List<Song>> getStarredTracks(LifecycleOwner owner) {
2021-08-17 16:17:41 +02:00
songRepository.getStarredSongs(false, -1).observe(owner, starredTracks::postValue);
return starredTracks;
2020-11-20 15:38:08 +01:00
}
2020-11-28 14:50:15 +01:00
public LiveData<List<Album>> getStarredAlbums(LifecycleOwner owner) {
albumRepository.getStarredAlbums().observe(owner, starredAlbums::postValue);
return starredAlbums;
2020-11-28 14:50:15 +01:00
}
2020-11-30 20:54:05 +01:00
public LiveData<List<Artist>> getStarredArtists(LifecycleOwner owner) {
artistRepository.getStarredArtists().observe(owner, starredArtists::postValue);
return starredArtists;
2020-11-30 20:54:05 +01:00
}
2021-04-26 19:37:05 +02:00
public LiveData<List<Album>> getMostPlayedAlbums(LifecycleOwner owner) {
albumRepository.getAlbums("frequent", 20).observe(owner, mostPlayedAlbumSample::postValue);
return mostPlayedAlbumSample;
}
public LiveData<List<Album>> getMostRecentlyAddedAlbums(LifecycleOwner owner) {
albumRepository.getAlbums("newest", 20).observe(owner, recentlyAddedAlbumSample::postValue);
return recentlyAddedAlbumSample;
}
public LiveData<List<Album>> getRecentlyPlayedAlbumList(LifecycleOwner owner) {
albumRepository.getAlbums("recent", 20).observe(owner, recentlyPlayedAlbumSample::postValue);
return recentlyPlayedAlbumSample;
}
public void refreshDiscoverySongSample(LifecycleOwner owner) {
songRepository.getRandomSample(10, null, null).observe(owner, dicoverSongSample::postValue);
}
2021-08-17 16:17:41 +02:00
public void refreshSimilarSongSample(LifecycleOwner owner) {
songRepository.getStarredSongs(true, 10).observe(owner, starredTracksSample::postValue);
}
public void refreshStarredTracks(LifecycleOwner owner) {
2021-08-17 16:17:41 +02:00
songRepository.getStarredSongs(false, -1).observe(owner, starredTracks::postValue);
}
public void refreshStarredAlbums(LifecycleOwner owner) {
albumRepository.getStarredAlbums().observe(owner, starredAlbums::postValue);
}
public void refreshStarredArtists(LifecycleOwner owner) {
artistRepository.getStarredArtists().observe(owner, starredArtists::postValue);
}
public void refreshMostPlayedAlbums(LifecycleOwner owner) {
albumRepository.getAlbums("frequent", 20).observe(owner, mostPlayedAlbumSample::postValue);
}
public void refreshMostRecentlyAddedAlbums(LifecycleOwner owner) {
albumRepository.getAlbums("newest", 20).observe(owner, recentlyAddedAlbumSample::postValue);
}
public void refreshRecentlyPlayedAlbumList(LifecycleOwner owner) {
albumRepository.getAlbums("recent", 20).observe(owner, recentlyPlayedAlbumSample::postValue);
}
2020-11-20 15:38:08 +01:00
}