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-12-05 16:55:47 +01:00
|
|
|
import com.cappielloantonio.play.App;
|
2021-07-27 12:10:28 +02:00
|
|
|
import com.cappielloantonio.play.model.Album;
|
2022-01-17 17:32:59 +01:00
|
|
|
import com.cappielloantonio.play.model.Artist;
|
2022-02-07 09:47:46 +01:00
|
|
|
import com.cappielloantonio.play.model.Media;
|
2022-02-07 17:34:46 +01:00
|
|
|
import com.cappielloantonio.play.model.Playlist;
|
2021-07-27 12:10:28 +02:00
|
|
|
import com.cappielloantonio.play.repository.AlbumRepository;
|
2022-01-17 17:32:59 +01:00
|
|
|
import com.cappielloantonio.play.repository.ArtistRepository;
|
2021-11-26 15:57:36 +01:00
|
|
|
import com.cappielloantonio.play.repository.PlaylistRepository;
|
2022-02-05 18:39:17 +01:00
|
|
|
import com.cappielloantonio.play.repository.PodcastRepository;
|
2020-11-21 18:41:35 +01:00
|
|
|
import com.cappielloantonio.play.repository.SongRepository;
|
2021-12-05 16:55:47 +01:00
|
|
|
import com.cappielloantonio.play.util.PreferenceUtil;
|
2020-11-20 15:38:08 +01:00
|
|
|
|
2022-01-17 17:32:59 +01:00
|
|
|
import java.util.Calendar;
|
2021-11-26 16:09:49 +01:00
|
|
|
import java.util.Collections;
|
2022-01-17 17:32:59 +01:00
|
|
|
import java.util.Comparator;
|
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;
|
2022-01-17 17:32:59 +01:00
|
|
|
private final ArtistRepository artistRepository;
|
2021-11-26 15:57:36 +01:00
|
|
|
private final PlaylistRepository playlistRepository;
|
2022-02-05 18:39:17 +01:00
|
|
|
private final PodcastRepository podcastRepository;
|
2021-08-17 14:46:10 +02:00
|
|
|
|
2022-02-07 09:47:46 +01:00
|
|
|
private final MutableLiveData<List<Media>> dicoverSongSample = new MutableLiveData<>(null);
|
2022-01-17 17:32:59 +01:00
|
|
|
private final MutableLiveData<List<Album>> newReleasedAlbum = new MutableLiveData<>(null);
|
2022-02-07 09:47:46 +01:00
|
|
|
private final MutableLiveData<List<Media>> starredTracksSample = new MutableLiveData<>(null);
|
2022-01-17 17:32:59 +01:00
|
|
|
private final MutableLiveData<List<Artist>> starredArtistsSample = new MutableLiveData<>(null);
|
2022-02-07 09:47:46 +01:00
|
|
|
private final MutableLiveData<List<Media>> starredTracks = new MutableLiveData<>(null);
|
2022-01-17 17:32:59 +01:00
|
|
|
private final MutableLiveData<List<Album>> starredAlbums = new MutableLiveData<>(null);
|
|
|
|
|
private final MutableLiveData<List<Artist>> starredArtists = 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<Album>> recentlyAddedAlbumSample = new MutableLiveData<>(null);
|
2021-11-26 15:57:36 +01:00
|
|
|
private final MutableLiveData<List<Playlist>> pinnedPlaylists = new MutableLiveData<>(null);
|
2022-02-07 17:34:46 +01:00
|
|
|
private final MutableLiveData<List<Media>> newestPodcastEpisodes = 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);
|
2022-01-17 17:32:59 +01:00
|
|
|
artistRepository = new ArtistRepository(application);
|
2021-11-26 15:57:36 +01:00
|
|
|
playlistRepository = new PlaylistRepository(application);
|
2022-02-05 18:39:17 +01:00
|
|
|
podcastRepository = new PodcastRepository(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);
|
2022-01-17 17:32:59 +01:00
|
|
|
artistRepository.getStarredArtists(true, 10).observeForever(starredArtistsSample::postValue);
|
2020-11-20 15:38:08 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-07 09:47:46 +01:00
|
|
|
public LiveData<List<Media>> getDiscoverSongSample() {
|
2021-07-28 15:46:05 +02:00
|
|
|
return dicoverSongSample;
|
2020-11-21 18:41:35 +01:00
|
|
|
}
|
|
|
|
|
|
2022-01-17 17:32:59 +01: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;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-07 09:47:46 +01:00
|
|
|
public LiveData<List<Media>> getStarredTracksSample() {
|
2021-08-17 16:17:41 +02:00
|
|
|
return starredTracksSample;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-17 17:32:59 +01:00
|
|
|
public LiveData<List<Artist>> getStarredArtistsSample() {
|
|
|
|
|
return starredArtistsSample;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-07 09:47:46 +01:00
|
|
|
public LiveData<List<Media>> getStarredTracks(LifecycleOwner owner) {
|
2022-01-17 17:32:59 +01:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
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<Album>> getMostPlayedAlbums(LifecycleOwner owner) {
|
2021-09-01 16:53:10 +02:00
|
|
|
albumRepository.getAlbums("frequent", 20, null, null).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) {
|
2021-09-01 16:53:10 +02:00
|
|
|
albumRepository.getAlbums("newest", 20, null, null).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) {
|
2021-09-01 16:53:10 +02:00
|
|
|
albumRepository.getAlbums("recent", 20, null, null).observe(owner, recentlyPlayedAlbumSample::postValue);
|
2021-07-27 12:10:28 +02:00
|
|
|
return recentlyPlayedAlbumSample;
|
|
|
|
|
}
|
2021-08-10 15:42:39 +02:00
|
|
|
|
2021-11-26 16:09:49 +01:00
|
|
|
public LiveData<List<Playlist>> getPinnedPlaylistList(LifecycleOwner owner, int maxNumber, boolean random) {
|
2021-12-05 16:55:47 +01:00
|
|
|
playlistRepository.getPinnedPlaylists(PreferenceUtil.getInstance(App.getInstance()).getServerId()).observe(owner, playlists -> {
|
2021-11-26 16:09:49 +01:00
|
|
|
if (random) Collections.shuffle(playlists);
|
|
|
|
|
List<Playlist> subPlaylist = playlists.subList(0, Math.min(maxNumber, playlists.size()));
|
|
|
|
|
pinnedPlaylists.postValue(subPlaylist);
|
|
|
|
|
});
|
2021-11-26 15:57:36 +01:00
|
|
|
return pinnedPlaylists;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-07 09:47:46 +01:00
|
|
|
public LiveData<List<Media>> getPlaylistSongLiveList(String playlistId) {
|
2021-11-26 15:57:36 +01:00
|
|
|
return playlistRepository.getPlaylistSongs(playlistId);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-07 17:34:46 +01:00
|
|
|
public LiveData<List<Media>> getNewestPodcastEpisodes(LifecycleOwner owner) {
|
2022-02-05 18:39:17 +01:00
|
|
|
podcastRepository.getNewestPodcastEpisodes(20).observe(owner, newestPodcastEpisodes::postValue);
|
|
|
|
|
return newestPodcastEpisodes;
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-17 17:32:59 +01:00
|
|
|
public void refreshRadioArtistSample(LifecycleOwner owner) {
|
|
|
|
|
artistRepository.getStarredArtists(true, 10).observe(owner, starredArtistsSample::postValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-10 15:42:39 +02:00
|
|
|
public void refreshMostPlayedAlbums(LifecycleOwner owner) {
|
2021-09-01 16:53:10 +02:00
|
|
|
albumRepository.getAlbums("frequent", 20, null, null).observe(owner, mostPlayedAlbumSample::postValue);
|
2021-08-10 15:42:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void refreshMostRecentlyAddedAlbums(LifecycleOwner owner) {
|
2021-09-01 16:53:10 +02:00
|
|
|
albumRepository.getAlbums("newest", 20, null, null).observe(owner, recentlyAddedAlbumSample::postValue);
|
2021-08-10 15:42:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void refreshRecentlyPlayedAlbumList(LifecycleOwner owner) {
|
2021-09-01 16:53:10 +02:00
|
|
|
albumRepository.getAlbums("recent", 20, null, null).observe(owner, recentlyPlayedAlbumSample::postValue);
|
2021-08-10 15:42:39 +02:00
|
|
|
}
|
2020-11-20 15:38:08 +01:00
|
|
|
}
|