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

177 lines
7.9 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.App;
import com.cappielloantonio.play.model.Album;
import com.cappielloantonio.play.model.Artist;
2022-02-07 09:47:46 +01:00
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.PlaylistRepository;
import com.cappielloantonio.play.repository.PodcastRepository;
2020-11-21 18:41:35 +01:00
import com.cappielloantonio.play.repository.SongRepository;
import com.cappielloantonio.play.util.PreferenceUtil;
2020-11-20 15:38:08 +01:00
import java.util.Calendar;
import java.util.Collections;
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";
private final SongRepository songRepository;
private final AlbumRepository albumRepository;
private final ArtistRepository artistRepository;
private final PlaylistRepository playlistRepository;
private final PodcastRepository podcastRepository;
2022-02-07 09:47:46 +01:00
private final MutableLiveData<List<Media>> dicoverSongSample = new MutableLiveData<>(null);
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);
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);
private final MutableLiveData<List<Album>> starredAlbums = new MutableLiveData<>(null);
private final MutableLiveData<List<Artist>> starredArtists = 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<Album>> recentlyAddedAlbumSample = new MutableLiveData<>(null);
private final MutableLiveData<List<Playlist>> pinnedPlaylists = new MutableLiveData<>(null);
private final MutableLiveData<List<Media>> newestPodcastEpisodes = 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);
playlistRepository = new PlaylistRepository(application);
podcastRepository = new PodcastRepository(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);
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() {
return dicoverSongSample;
2020-11-21 18:41:35 +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;
}
public LiveData<List<Artist>> getStarredArtistsSample() {
return starredArtistsSample;
}
2022-02-07 09:47:46 +01:00
public LiveData<List<Media>> getStarredTracks(LifecycleOwner owner) {
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;
}
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<Album>> getMostPlayedAlbums(LifecycleOwner owner) {
albumRepository.getAlbums("frequent", 20, null, null).observe(owner, mostPlayedAlbumSample::postValue);
return mostPlayedAlbumSample;
}
public LiveData<List<Album>> getMostRecentlyAddedAlbums(LifecycleOwner owner) {
albumRepository.getAlbums("newest", 20, null, null).observe(owner, recentlyAddedAlbumSample::postValue);
return recentlyAddedAlbumSample;
}
public LiveData<List<Album>> getRecentlyPlayedAlbumList(LifecycleOwner owner) {
albumRepository.getAlbums("recent", 20, null, null).observe(owner, recentlyPlayedAlbumSample::postValue);
return recentlyPlayedAlbumSample;
}
public LiveData<List<Playlist>> getPinnedPlaylistList(LifecycleOwner owner, int maxNumber, boolean random) {
playlistRepository.getPinnedPlaylists(PreferenceUtil.getInstance(App.getInstance()).getServerId()).observe(owner, playlists -> {
if (random) Collections.shuffle(playlists);
List<Playlist> subPlaylist = playlists.subList(0, Math.min(maxNumber, playlists.size()));
pinnedPlaylists.postValue(subPlaylist);
});
return pinnedPlaylists;
}
2022-02-07 09:47:46 +01:00
public LiveData<List<Media>> getPlaylistSongLiveList(String playlistId) {
return playlistRepository.getPlaylistSongs(playlistId);
}
public LiveData<List<Media>> getNewestPodcastEpisodes(LifecycleOwner owner) {
podcastRepository.getNewestPodcastEpisodes(20).observe(owner, newestPodcastEpisodes::postValue);
return newestPodcastEpisodes;
}
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 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);
}
2020-11-20 15:38:08 +01:00
}