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;
|
2021-08-10 15:42:39 +02:00
|
|
|
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
|
|
|
|
2021-07-27 12:10:28 +02:00
|
|
|
import com.cappielloantonio.play.model.Album;
|
2021-07-27 16:58:38 +02:00
|
|
|
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;
|
2021-07-27 12:10:28 +02:00
|
|
|
import com.cappielloantonio.play.repository.AlbumRepository;
|
2021-07-27 16:58:38 +02:00
|
|
|
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";
|
2021-08-17 14:46:10 +02:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
2021-08-17 14:46:10 +02:00
|
|
|
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);
|
2021-07-27 16:58:38 +02:00
|
|
|
|
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);
|
2021-07-27 12:10:28 +02:00
|
|
|
albumRepository = new AlbumRepository(application);
|
2021-07-27 16:58:38 +02:00
|
|
|
artistRepository = new ArtistRepository(application);
|
2020-11-20 15:38:08 +01:00
|
|
|
|
2021-08-17 14:46:10 +02: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
|
|
|
}
|
|
|
|
|
|
2021-07-28 15:46:05 +02: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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-17 14:46:10 +02:00
|
|
|
public LiveData<List<Integer>> getYearList(LifecycleOwner owner) {
|
|
|
|
|
albumRepository.getDecades().observe(owner, years::postValue);
|
2021-07-28 15:46:05 +02:00
|
|
|
return years;
|
2020-11-21 18:41:35 +01:00
|
|
|
}
|
2020-11-20 15:38:08 +01:00
|
|
|
|
2021-08-17 14:46:10 +02:00
|
|
|
public LiveData<List<Song>> getStarredTracks(LifecycleOwner owner) {
|
2021-08-17 16:17:41 +02:00
|
|
|
songRepository.getStarredSongs(false, -1).observe(owner, starredTracks::postValue);
|
2021-07-27 16:58:38 +02:00
|
|
|
return starredTracks;
|
2020-11-20 15:38:08 +01:00
|
|
|
}
|
2020-11-28 14:50:15 +01:00
|
|
|
|
2021-08-17 14:46:10 +02:00
|
|
|
public LiveData<List<Album>> getStarredAlbums(LifecycleOwner owner) {
|
|
|
|
|
albumRepository.getStarredAlbums().observe(owner, starredAlbums::postValue);
|
2021-07-27 16:58:38 +02:00
|
|
|
return starredAlbums;
|
2020-11-28 14:50:15 +01:00
|
|
|
}
|
2020-11-30 20:54:05 +01:00
|
|
|
|
2021-08-17 14:46:10 +02:00
|
|
|
public LiveData<List<Artist>> getStarredArtists(LifecycleOwner owner) {
|
|
|
|
|
artistRepository.getStarredArtists().observe(owner, starredArtists::postValue);
|
2021-07-27 16:58:38 +02:00
|
|
|
return starredArtists;
|
2020-11-30 20:54:05 +01:00
|
|
|
}
|
2021-04-26 19:37:05 +02:00
|
|
|
|
2021-08-17 14:46:10 +02:00
|
|
|
public LiveData<List<Album>> getMostPlayedAlbums(LifecycleOwner owner) {
|
|
|
|
|
albumRepository.getAlbums("frequent", 20).observe(owner, mostPlayedAlbumSample::postValue);
|
2021-07-27 12:10:28 +02:00
|
|
|
return mostPlayedAlbumSample;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-17 14:46:10 +02:00
|
|
|
public LiveData<List<Album>> getMostRecentlyAddedAlbums(LifecycleOwner owner) {
|
|
|
|
|
albumRepository.getAlbums("newest", 20).observe(owner, recentlyAddedAlbumSample::postValue);
|
2021-07-27 12:10:28 +02:00
|
|
|
return recentlyAddedAlbumSample;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-17 14:46:10 +02:00
|
|
|
public LiveData<List<Album>> getRecentlyPlayedAlbumList(LifecycleOwner owner) {
|
|
|
|
|
albumRepository.getAlbums("recent", 20).observe(owner, recentlyPlayedAlbumSample::postValue);
|
2021-07-27 12:10:28 +02:00
|
|
|
return recentlyPlayedAlbumSample;
|
|
|
|
|
}
|
2021-08-10 15:42:39 +02:00
|
|
|
|
|
|
|
|
public void refreshDiscoverySongSample(LifecycleOwner owner) {
|
2021-08-17 14:46:10 +02:00
|
|
|
songRepository.getRandomSample(10, null, null).observe(owner, dicoverSongSample::postValue);
|
2021-08-10 15:42:39 +02:00
|
|
|
}
|
|
|
|
|
|
2021-08-17 16:17:41 +02:00
|
|
|
public void refreshSimilarSongSample(LifecycleOwner owner) {
|
|
|
|
|
songRepository.getStarredSongs(true, 10).observe(owner, starredTracksSample::postValue);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-10 15:42:39 +02:00
|
|
|
public void refreshStarredTracks(LifecycleOwner owner) {
|
2021-08-17 16:17:41 +02:00
|
|
|
songRepository.getStarredSongs(false, -1).observe(owner, starredTracks::postValue);
|
2021-08-10 15:42:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void refreshStarredAlbums(LifecycleOwner owner) {
|
2021-08-17 14:46:10 +02:00
|
|
|
albumRepository.getStarredAlbums().observe(owner, starredAlbums::postValue);
|
2021-08-10 15:42:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void refreshStarredArtists(LifecycleOwner owner) {
|
2021-08-17 14:46:10 +02:00
|
|
|
artistRepository.getStarredArtists().observe(owner, starredArtists::postValue);
|
2021-08-10 15:42:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void refreshMostPlayedAlbums(LifecycleOwner owner) {
|
2021-08-17 14:46:10 +02:00
|
|
|
albumRepository.getAlbums("frequent", 20).observe(owner, mostPlayedAlbumSample::postValue);
|
2021-08-10 15:42:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void refreshMostRecentlyAddedAlbums(LifecycleOwner owner) {
|
2021-08-17 14:46:10 +02:00
|
|
|
albumRepository.getAlbums("newest", 20).observe(owner, recentlyAddedAlbumSample::postValue);
|
2021-08-10 15:42:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void refreshRecentlyPlayedAlbumList(LifecycleOwner owner) {
|
2021-08-17 14:46:10 +02:00
|
|
|
albumRepository.getAlbums("recent", 20).observe(owner, recentlyPlayedAlbumSample::postValue);
|
2021-08-10 15:42:39 +02:00
|
|
|
}
|
2020-11-20 15:38:08 +01:00
|
|
|
}
|