mirror of
https://github.com/antebudimir/tempus.git
synced 2025-12-31 17:43:32 +00:00
The viewmodels have been modified to not repeat calls to the backend during the lifecycle of the fragment
This commit is contained in:
parent
6817fa739e
commit
8b883c88fc
6 changed files with 87 additions and 46 deletions
|
|
@ -82,7 +82,7 @@ public class PlaylistChooserDialog extends DialogFragment {
|
|||
playlistDialogHorizontalAdapter = new PlaylistDialogHorizontalAdapter(requireContext(), playlistChooserViewModel, this);
|
||||
bind.playlistDialogRecyclerView.setAdapter(playlistDialogHorizontalAdapter);
|
||||
|
||||
playlistChooserViewModel.getPlaylistList().observe(getViewLifecycleOwner(), playlists -> {
|
||||
playlistChooserViewModel.getPlaylistList(getViewLifecycleOwner()).observe(getViewLifecycleOwner(), playlists -> {
|
||||
if (playlists != null) {
|
||||
if (playlists.size() > 0) {
|
||||
if (bind != null) bind.noPlaylistsCreatedTextView.setVisibility(View.GONE);
|
||||
|
|
|
|||
|
|
@ -256,7 +256,7 @@ public class HomeFragment extends Fragment {
|
|||
discoverSongAdapter = new DiscoverSongAdapter(activity, requireContext());
|
||||
bind.discoverSongViewPager.setAdapter(discoverSongAdapter);
|
||||
bind.discoverSongViewPager.setOffscreenPageLimit(1);
|
||||
homeViewModel.getDiscoverSongSample().observe(getViewLifecycleOwner(), songs -> {
|
||||
homeViewModel.getDiscoverSongSample(getViewLifecycleOwner()).observe(getViewLifecycleOwner(), songs -> {
|
||||
if (songs == null) {
|
||||
if (bind != null) bind.homeDiscoveryPlaceholder.placeholder.setVisibility(View.VISIBLE);
|
||||
if (bind != null) bind.homeDiscoverSector.setVisibility(View.GONE);
|
||||
|
|
@ -277,7 +277,7 @@ public class HomeFragment extends Fragment {
|
|||
|
||||
similarMusicAdapter = new SimilarTrackAdapter(activity, requireContext());
|
||||
bind.similarTracksRecyclerView.setAdapter(similarMusicAdapter);
|
||||
homeViewModel.getStarredTracksSample().observe(getViewLifecycleOwner(), songs -> {
|
||||
homeViewModel.getStarredTracksSample(getViewLifecycleOwner()).observe(getViewLifecycleOwner(), songs -> {
|
||||
if (songs == null) {
|
||||
if (bind != null) bind.homeSimilarTracksPlaceholder.placeholder.setVisibility(View.VISIBLE);
|
||||
if (bind != null) bind.homeSimilarTracksSector.setVisibility(View.GONE);
|
||||
|
|
@ -299,7 +299,7 @@ public class HomeFragment extends Fragment {
|
|||
|
||||
radioArtistAdapter = new ArtistAdapter((MainActivity) requireActivity(), requireContext());
|
||||
bind.radioArtistRecyclerView.setAdapter(radioArtistAdapter);
|
||||
homeViewModel.getStarredArtistsSample().observe(getViewLifecycleOwner(), artists -> {
|
||||
homeViewModel.getStarredArtistsSample(getViewLifecycleOwner()).observe(getViewLifecycleOwner(), artists -> {
|
||||
if (artists == null) {
|
||||
if (bind != null) bind.homeRadioArtistPlaceholder.placeholder.setVisibility(View.VISIBLE);
|
||||
if (bind != null) bind.homeRadioArtistSector.setVisibility(View.GONE);
|
||||
|
|
|
|||
|
|
@ -145,7 +145,7 @@ public class LibraryFragment extends Fragment {
|
|||
|
||||
albumAdapter = new AlbumAdapter(requireContext());
|
||||
bind.albumRecyclerView.setAdapter(albumAdapter);
|
||||
libraryViewModel.getAlbumSample().observe(getViewLifecycleOwner(), albums -> {
|
||||
libraryViewModel.getAlbumSample(getViewLifecycleOwner()).observe(getViewLifecycleOwner(), albums -> {
|
||||
if (albums == null) {
|
||||
if (bind != null) bind.libraryAlbumPlaceholder.placeholder.setVisibility(View.VISIBLE);
|
||||
if (bind != null) bind.libraryAlbumSector.setVisibility(View.GONE);
|
||||
|
|
@ -167,7 +167,7 @@ public class LibraryFragment extends Fragment {
|
|||
|
||||
artistAdapter = new ArtistAdapter((MainActivity) requireActivity(), requireContext());
|
||||
bind.artistRecyclerView.setAdapter(artistAdapter);
|
||||
libraryViewModel.getArtistSample().observe(getViewLifecycleOwner(), artists -> {
|
||||
libraryViewModel.getArtistSample(getViewLifecycleOwner()).observe(getViewLifecycleOwner(), artists -> {
|
||||
if (artists == null) {
|
||||
if (bind != null) bind.libraryArtistPlaceholder.placeholder.setVisibility(View.VISIBLE);
|
||||
if (bind != null) bind.libraryArtistSector.setVisibility(View.GONE);
|
||||
|
|
@ -195,7 +195,7 @@ public class LibraryFragment extends Fragment {
|
|||
activity.navController.navigate(R.id.action_libraryFragment_to_songListPageFragment, bundle);
|
||||
});
|
||||
bind.genreRecyclerView.setAdapter(genreAdapter);
|
||||
libraryViewModel.getGenreSample().observe(getViewLifecycleOwner(), genres -> {
|
||||
libraryViewModel.getGenreSample(getViewLifecycleOwner()).observe(getViewLifecycleOwner(), genres -> {
|
||||
if (genres == null) {
|
||||
if (bind != null) bind.libraryGenrePlaceholder.placeholder.setVisibility(View.VISIBLE);
|
||||
if (bind != null) bind.libraryGenresSector.setVisibility(View.GONE);
|
||||
|
|
@ -217,7 +217,7 @@ public class LibraryFragment extends Fragment {
|
|||
|
||||
playlistHorizontalAdapter = new PlaylistHorizontalAdapter(activity, requireContext());
|
||||
bind.playlistRecyclerView.setAdapter(playlistHorizontalAdapter);
|
||||
libraryViewModel.getPlaylistSample().observe(getViewLifecycleOwner(), playlists -> {
|
||||
libraryViewModel.getPlaylistSample(getViewLifecycleOwner()).observe(getViewLifecycleOwner(), playlists -> {
|
||||
if (playlists == null) {
|
||||
if (bind != null) bind.libraryPlaylistPlaceholder.placeholder.setVisibility(View.VISIBLE);
|
||||
if (bind != null) bind.libraryPlaylistSector.setVisibility(View.GONE);
|
||||
|
|
|
|||
|
|
@ -56,67 +56,98 @@ public class HomeViewModel extends AndroidViewModel {
|
|||
artistRepository = new ArtistRepository(application);
|
||||
playlistRepository = new PlaylistRepository(application);
|
||||
podcastRepository = new PodcastRepository(application);
|
||||
|
||||
songRepository.getRandomSample(10, null, null).observeForever(dicoverSongSample::postValue);
|
||||
songRepository.getStarredSongs(true, 10).observeForever(starredTracksSample::postValue);
|
||||
artistRepository.getStarredArtists(true, 10).observeForever(starredArtistsSample::postValue);
|
||||
}
|
||||
|
||||
public LiveData<List<Media>> getDiscoverSongSample() {
|
||||
public LiveData<List<Media>> getDiscoverSongSample(LifecycleOwner owner) {
|
||||
if (dicoverSongSample.getValue() == null) {
|
||||
songRepository.getRandomSample(10, null, null).observe(owner, dicoverSongSample::postValue);
|
||||
}
|
||||
|
||||
return dicoverSongSample;
|
||||
}
|
||||
|
||||
public LiveData<List<Album>> getRecentlyReleasedAlbums(LifecycleOwner owner) {
|
||||
int currentYear = Calendar.getInstance().get(Calendar.YEAR);
|
||||
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())));
|
||||
});
|
||||
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<List<Media>> getStarredTracksSample() {
|
||||
public LiveData<List<Media>> getStarredTracksSample(LifecycleOwner owner) {
|
||||
if (starredTracksSample.getValue() == null) {
|
||||
songRepository.getStarredSongs(true, 10).observe(owner, starredTracksSample::postValue);
|
||||
}
|
||||
|
||||
return starredTracksSample;
|
||||
}
|
||||
|
||||
public LiveData<List<Artist>> getStarredArtistsSample() {
|
||||
public LiveData<List<Artist>> getStarredArtistsSample(LifecycleOwner owner) {
|
||||
if (starredArtistsSample.getValue() == null) {
|
||||
artistRepository.getStarredArtists(true, 10).observe(owner, starredArtistsSample::postValue);
|
||||
}
|
||||
|
||||
return starredArtistsSample;
|
||||
}
|
||||
|
||||
public LiveData<List<Media>> getStarredTracks(LifecycleOwner owner) {
|
||||
songRepository.getStarredSongs(true, 20).observe(owner, starredTracks::postValue);
|
||||
if (starredTracks.getValue() == null) {
|
||||
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);
|
||||
if (starredAlbums.getValue() == null) {
|
||||
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);
|
||||
if (starredArtists.getValue() == null) {
|
||||
artistRepository.getStarredArtists(true, 20).observe(owner, starredArtists::postValue);
|
||||
}
|
||||
|
||||
return starredArtists;
|
||||
}
|
||||
|
||||
public LiveData<List<Integer>> getYearList(LifecycleOwner owner) {
|
||||
albumRepository.getDecades().observe(owner, years::postValue);
|
||||
if (years.getValue() == null) {
|
||||
albumRepository.getDecades().observe(owner, years::postValue);
|
||||
}
|
||||
|
||||
return years;
|
||||
}
|
||||
|
||||
public LiveData<List<Album>> getMostPlayedAlbums(LifecycleOwner owner) {
|
||||
albumRepository.getAlbums("frequent", 20, null, null).observe(owner, mostPlayedAlbumSample::postValue);
|
||||
if (mostPlayedAlbumSample.getValue() == null) {
|
||||
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);
|
||||
if (recentlyAddedAlbumSample.getValue() == null) {
|
||||
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);
|
||||
if (recentlyPlayedAlbumSample.getValue() == null) {
|
||||
albumRepository.getAlbums("recent", 20, null, null).observe(owner, recentlyPlayedAlbumSample::postValue);
|
||||
}
|
||||
|
||||
return recentlyPlayedAlbumSample;
|
||||
}
|
||||
|
||||
|
|
@ -126,6 +157,7 @@ public class HomeViewModel extends AndroidViewModel {
|
|||
List<Playlist> subPlaylist = playlists.subList(0, Math.min(maxNumber, playlists.size()));
|
||||
pinnedPlaylists.postValue(subPlaylist);
|
||||
});
|
||||
|
||||
return pinnedPlaylists;
|
||||
}
|
||||
|
||||
|
|
@ -134,7 +166,10 @@ public class HomeViewModel extends AndroidViewModel {
|
|||
}
|
||||
|
||||
public LiveData<List<Media>> getNewestPodcastEpisodes(LifecycleOwner owner) {
|
||||
podcastRepository.getNewestPodcastEpisodes(20).observe(owner, newestPodcastEpisodes::postValue);
|
||||
if (newestPodcastEpisodes.getValue() == null) {
|
||||
podcastRepository.getNewestPodcastEpisodes(20).observe(owner, newestPodcastEpisodes::postValue);
|
||||
}
|
||||
|
||||
return newestPodcastEpisodes;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -17,8 +17,6 @@ import com.cappielloantonio.play.repository.ArtistRepository;
|
|||
import com.cappielloantonio.play.repository.GenreRepository;
|
||||
import com.cappielloantonio.play.repository.PlaylistRepository;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
public class LibraryViewModel extends AndroidViewModel {
|
||||
|
|
@ -41,28 +39,37 @@ public class LibraryViewModel extends AndroidViewModel {
|
|||
artistRepository = new ArtistRepository(application);
|
||||
genreRepository = new GenreRepository(application);
|
||||
playlistRepository = new PlaylistRepository(application);
|
||||
|
||||
// Inizializzate all'interno del costruttore, in modo da rimanere immutabili per tutto il
|
||||
// ciclo di vita dell'applicazione
|
||||
albumRepository.getAlbums("random", 10, null, null).observeForever(sampleAlbum::postValue);
|
||||
artistRepository.getArtists(true, 10).observeForever(sampleArtist::postValue);
|
||||
genreRepository.getGenres(true, 15).observeForever(sampleGenres::postValue);
|
||||
playlistRepository.getPlaylists(true, 10).observeForever(playlistSample::postValue);
|
||||
}
|
||||
|
||||
public LiveData<List<Album>> getAlbumSample() {
|
||||
public LiveData<List<Album>> getAlbumSample(LifecycleOwner owner) {
|
||||
if (sampleAlbum.getValue() == null) {
|
||||
albumRepository.getAlbums("random", 10, null, null).observe(owner, sampleAlbum::postValue);
|
||||
}
|
||||
|
||||
return sampleAlbum;
|
||||
}
|
||||
|
||||
public LiveData<List<Artist>> getArtistSample() {
|
||||
public LiveData<List<Artist>> getArtistSample(LifecycleOwner owner) {
|
||||
if (sampleArtist.getValue() == null) {
|
||||
artistRepository.getArtists(true, 10).observe(owner, sampleArtist::postValue);
|
||||
}
|
||||
|
||||
return sampleArtist;
|
||||
}
|
||||
|
||||
public LiveData<List<Genre>> getGenreSample() {
|
||||
public LiveData<List<Genre>> getGenreSample(LifecycleOwner owner) {
|
||||
if (sampleGenres.getValue() == null) {
|
||||
genreRepository.getGenres(true, 15).observe(owner, sampleGenres::postValue);
|
||||
}
|
||||
|
||||
return sampleGenres;
|
||||
}
|
||||
|
||||
public LiveData<List<Playlist>> getPlaylistSample() {
|
||||
public LiveData<List<Playlist>> getPlaylistSample(LifecycleOwner owner) {
|
||||
if (playlistSample.getValue() == null) {
|
||||
playlistRepository.getPlaylists(true, 10).observe(owner, playlistSample::postValue);
|
||||
}
|
||||
|
||||
return playlistSample;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,11 +4,12 @@ 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.model.Playlist;
|
||||
import com.cappielloantonio.play.model.Media;
|
||||
import com.cappielloantonio.play.model.Playlist;
|
||||
import com.cappielloantonio.play.repository.PlaylistRepository;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
|
@ -25,12 +26,10 @@ public class PlaylistChooserViewModel extends AndroidViewModel {
|
|||
super(application);
|
||||
|
||||
playlistRepository = new PlaylistRepository(application);
|
||||
|
||||
playlistRepository.getPlaylists(false, -1).observeForever(playlists::postValue);
|
||||
|
||||
}
|
||||
|
||||
public LiveData<List<Playlist>> getPlaylistList() {
|
||||
public LiveData<List<Playlist>> getPlaylistList(LifecycleOwner owner) {
|
||||
playlistRepository.getPlaylists(false, -1).observe(owner, playlists::postValue);
|
||||
return playlists;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue