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.App; import com.cappielloantonio.play.model.Album; import com.cappielloantonio.play.model.Artist; import com.cappielloantonio.play.model.Chronology; import com.cappielloantonio.play.model.Media; import com.cappielloantonio.play.model.Playlist; import com.cappielloantonio.play.repository.AlbumRepository; import com.cappielloantonio.play.repository.ArtistRepository; import com.cappielloantonio.play.repository.ChronologyRepository; import com.cappielloantonio.play.repository.PlaylistRepository; import com.cappielloantonio.play.repository.PodcastRepository; import com.cappielloantonio.play.repository.SongRepository; import com.cappielloantonio.play.util.PreferenceUtil; import java.util.Calendar; import java.util.Collections; import java.util.Comparator; import java.util.List; public class HomeViewModel extends AndroidViewModel { private static final String TAG = "HomeViewModel"; private final SongRepository songRepository; private final AlbumRepository albumRepository; private final ArtistRepository artistRepository; private final PlaylistRepository playlistRepository; private final PodcastRepository podcastRepository; private final ChronologyRepository chronologyRepository; private final MutableLiveData> dicoverSongSample = new MutableLiveData<>(null); private final MutableLiveData> newReleasedAlbum = new MutableLiveData<>(null); private final MutableLiveData> starredTracksSample = new MutableLiveData<>(null); private final MutableLiveData> starredArtistsSample = new MutableLiveData<>(null); private final MutableLiveData> starredTracks = new MutableLiveData<>(null); private final MutableLiveData> starredAlbums = new MutableLiveData<>(null); private final MutableLiveData> starredArtists = new MutableLiveData<>(null); private final MutableLiveData> mostPlayedAlbumSample = new MutableLiveData<>(null); private final MutableLiveData> recentlyPlayedAlbumSample = new MutableLiveData<>(null); private final MutableLiveData> years = new MutableLiveData<>(null); private final MutableLiveData> recentlyAddedAlbumSample = new MutableLiveData<>(null); private final MutableLiveData> pinnedPlaylists = new MutableLiveData<>(null); private final MutableLiveData> newestPodcastEpisodes = new MutableLiveData<>(null); private final MutableLiveData> thisGridTopSong = new MutableLiveData<>(null); public HomeViewModel(@NonNull Application application) { super(application); songRepository = new SongRepository(application); albumRepository = new AlbumRepository(application); artistRepository = new ArtistRepository(application); playlistRepository = new PlaylistRepository(application); podcastRepository = new PodcastRepository(application); chronologyRepository = new ChronologyRepository(application); } public LiveData> getDiscoverSongSample(LifecycleOwner owner) { if (dicoverSongSample.getValue() == null) { songRepository.getRandomSample(10, null, null).observe(owner, dicoverSongSample::postValue); } return dicoverSongSample; } public LiveData> getGridSongSample(LifecycleOwner owner) { Calendar cal = Calendar.getInstance(); int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH); if (thisGridTopSong.getValue() == null) { if (dayOfMonth >= 7) { chronologyRepository.getThisWeek().observe(owner, thisGridTopSong::postValue); } else { chronologyRepository.getLastWeek().observe(owner, thisGridTopSong::postValue); } } return thisGridTopSong; } public LiveData> getRecentlyReleasedAlbums(LifecycleOwner owner) { if (newReleasedAlbum.getValue() == null) { 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; } public LiveData> getStarredTracksSample(LifecycleOwner owner) { if (starredTracksSample.getValue() == null) { songRepository.getStarredSongs(true, 10).observe(owner, starredTracksSample::postValue); } return starredTracksSample; } public LiveData> getStarredArtistsSample(LifecycleOwner owner) { if (starredArtistsSample.getValue() == null) { artistRepository.getStarredArtists(true, 10).observe(owner, starredArtistsSample::postValue); } return starredArtistsSample; } public LiveData> getStarredTracks(LifecycleOwner owner) { if (starredTracks.getValue() == null) { songRepository.getStarredSongs(true, 20).observe(owner, starredTracks::postValue); } return starredTracks; } public LiveData> getStarredAlbums(LifecycleOwner owner) { if (starredAlbums.getValue() == null) { albumRepository.getStarredAlbums(true, 20).observe(owner, starredAlbums::postValue); } return starredAlbums; } public LiveData> getStarredArtists(LifecycleOwner owner) { if (starredArtists.getValue() == null) { artistRepository.getStarredArtists(true, 20).observe(owner, starredArtists::postValue); } return starredArtists; } public LiveData> getYearList(LifecycleOwner owner) { if (years.getValue() == null) { albumRepository.getDecades().observe(owner, years::postValue); } return years; } public LiveData> getMostPlayedAlbums(LifecycleOwner owner) { if (mostPlayedAlbumSample.getValue() == null) { albumRepository.getAlbums("frequent", 20, null, null).observe(owner, mostPlayedAlbumSample::postValue); } return mostPlayedAlbumSample; } public LiveData> getMostRecentlyAddedAlbums(LifecycleOwner owner) { if (recentlyAddedAlbumSample.getValue() == null) { albumRepository.getAlbums("newest", 20, null, null).observe(owner, recentlyAddedAlbumSample::postValue); } return recentlyAddedAlbumSample; } public LiveData> getRecentlyPlayedAlbumList(LifecycleOwner owner) { if (recentlyPlayedAlbumSample.getValue() == null) { albumRepository.getAlbums("recent", 20, null, null).observe(owner, recentlyPlayedAlbumSample::postValue); } return recentlyPlayedAlbumSample; } public LiveData> getPinnedPlaylistList(LifecycleOwner owner, int maxNumber, boolean random) { playlistRepository.getPinnedPlaylists(PreferenceUtil.getInstance(App.getInstance()).getServerId()).observe(owner, playlists -> { if (random) Collections.shuffle(playlists); List subPlaylist = playlists.subList(0, Math.min(maxNumber, playlists.size())); pinnedPlaylists.postValue(subPlaylist); }); return pinnedPlaylists; } public LiveData> getPlaylistSongLiveList(String playlistId) { return playlistRepository.getPlaylistSongs(playlistId); } public LiveData> getNewestPodcastEpisodes(LifecycleOwner owner) { if (newestPodcastEpisodes.getValue() == null) { podcastRepository.getNewestPodcastEpisodes(20).observe(owner, newestPodcastEpisodes::postValue); } return newestPodcastEpisodes; } public void refreshDiscoverySongSample(LifecycleOwner owner) { songRepository.getRandomSample(10, null, null).observe(owner, dicoverSongSample::postValue); } public void refreshSimilarSongSample(LifecycleOwner owner) { songRepository.getStarredSongs(true, 10).observe(owner, starredTracksSample::postValue); } 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); } public void refreshMostPlayedAlbums(LifecycleOwner owner) { albumRepository.getAlbums("frequent", 20, null, null).observe(owner, mostPlayedAlbumSample::postValue); } public void refreshMostRecentlyAddedAlbums(LifecycleOwner owner) { albumRepository.getAlbums("newest", 20, null, null).observe(owner, recentlyAddedAlbumSample::postValue); } public void refreshRecentlyPlayedAlbumList(LifecycleOwner owner) { albumRepository.getAlbums("recent", 20, null, null).observe(owner, recentlyPlayedAlbumSample::postValue); } }