Removed almost all hardcoded constants and deleted offline playlist model

This commit is contained in:
antonio 2023-03-10 17:46:03 +01:00
parent e98429503b
commit ff1a1350f9
24 changed files with 64 additions and 380 deletions

View file

@ -12,13 +12,11 @@ import com.cappielloantonio.play.model.Chronology;
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.subsonic.models.AlbumID3;
import com.cappielloantonio.play.subsonic.models.ArtistID3;
import com.cappielloantonio.play.subsonic.models.Child;
import com.cappielloantonio.play.subsonic.models.Playlist;
import com.cappielloantonio.play.subsonic.models.PodcastEpisode;
import com.cappielloantonio.play.util.Preferences;
@ -33,7 +31,6 @@ public class HomeViewModel extends AndroidViewModel {
private final SongRepository songRepository;
private final AlbumRepository albumRepository;
private final ArtistRepository artistRepository;
private final PlaylistRepository playlistRepository;
private final PodcastRepository podcastRepository;
private final ChronologyRepository chronologyRepository;
@ -49,7 +46,6 @@ public class HomeViewModel extends AndroidViewModel {
private final MutableLiveData<List<AlbumID3>> recentlyPlayedAlbumSample = new MutableLiveData<>(null);
private final MutableLiveData<List<Integer>> years = new MutableLiveData<>(null);
private final MutableLiveData<List<AlbumID3>> recentlyAddedAlbumSample = new MutableLiveData<>(null);
private final MutableLiveData<List<Playlist>> pinnedPlaylists = new MutableLiveData<>(null);
private final MutableLiveData<List<PodcastEpisode>> newestPodcastEpisodes = new MutableLiveData<>(null);
private final MutableLiveData<List<Chronology>> thisGridTopSong = new MutableLiveData<>(null);
@ -63,7 +59,6 @@ public class HomeViewModel extends AndroidViewModel {
songRepository = new SongRepository();
albumRepository = new AlbumRepository();
artistRepository = new ArtistRepository();
playlistRepository = new PlaylistRepository();
podcastRepository = new PodcastRepository();
chronologyRepository = new ChronologyRepository();
}
@ -185,20 +180,6 @@ public class HomeViewModel extends AndroidViewModel {
return recentlyPlayedAlbumSample;
}
public LiveData<List<Playlist>> getPinnedPlaylistList(LifecycleOwner owner, int maxNumber, boolean random) {
playlistRepository.getPinnedPlaylists(Preferences.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;
}
public LiveData<List<Child>> getPlaylistSongLiveList(String playlistId) {
return playlistRepository.getPlaylistSongs(playlistId);
}
public LiveData<List<PodcastEpisode>> getNewestPodcastEpisodes(LifecycleOwner owner) {
if (newestPodcastEpisodes.getValue() == null) {
podcastRepository.getNewestPodcastEpisodes(20).observe(owner, newestPodcastEpisodes::postValue);
@ -215,12 +196,20 @@ public class HomeViewModel extends AndroidViewModel {
return mediaInstantMix;
}
public LiveData<List<Child>> getArtistInstantMix(ArtistID3 artist) {
return artistRepository.getInstantMix(artist, 20);
public LiveData<List<Child>> getArtistInstantMix(LifecycleOwner owner, ArtistID3 artist) {
if (artistInstantMix.getValue() == null) {
artistRepository.getTopSongs(artist.getName(), 10).observe(owner, artistInstantMix::postValue);
}
return artistInstantMix;
}
public LiveData<List<Child>> getArtistBestOf(ArtistID3 artist) {
return artistRepository.getTopSongs(artist.getName(), 10);
public LiveData<List<Child>> getArtistBestOf(LifecycleOwner owner, ArtistID3 artist) {
if (bestOfArtists.getValue() == null) {
artistRepository.getTopSongs(artist.getName(), 10).observe(owner, artistBestOf::postValue);
}
return artistBestOf;
}
public void refreshDiscoverySongSample(LifecycleOwner owner) {

View file

@ -8,63 +8,32 @@ import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import com.cappielloantonio.play.repository.DownloadRepository;
import com.cappielloantonio.play.repository.PlaylistRepository;
import com.cappielloantonio.play.subsonic.models.Playlist;
import com.cappielloantonio.play.util.Constants;
import com.cappielloantonio.play.util.Preferences;
import java.util.ArrayList;
import java.util.List;
public class PlaylistCatalogueViewModel extends AndroidViewModel {
private final PlaylistRepository playlistRepository;
private final DownloadRepository downloadRepository;
private String type;
private MutableLiveData<List<Playlist>> playlistList;
private MutableLiveData<List<Playlist>> pinnedPlaylistList;
private final MutableLiveData<List<Playlist>> playlistList = new MutableLiveData<>(null);
public PlaylistCatalogueViewModel(@NonNull Application application) {
super(application);
playlistRepository = new PlaylistRepository();
downloadRepository = new DownloadRepository();
}
public LiveData<List<Playlist>> getPlaylistList(LifecycleOwner owner) {
playlistList = new MutableLiveData<>(new ArrayList<>());
switch (type) {
case Constants.PLAYLIST_ALL:
playlistRepository.getPlaylists(false, -1).observe(owner, playlists -> playlistList.postValue(playlists));
break;
case Constants.PLAYLIST_DOWNLOADED:
// TODO
//downloadRepository.getLivePlaylist().observe(owner, downloads -> playlistList.setValue(MappingUtil.mapDownloadToPlaylist(downloads)));
break;
if (playlistList.getValue() == null) {
playlistRepository.getPlaylists(false, -1).observe(owner, playlistList::postValue);
}
playlistRepository.getPlaylists(false, -1);
return playlistList;
}
public LiveData<List<Playlist>> getPinnedPlaylistList(LifecycleOwner owner) {
pinnedPlaylistList = new MutableLiveData<>(new ArrayList<>());
playlistRepository.getPinnedPlaylists(Preferences.getServerId()).observe(owner, playlists -> pinnedPlaylistList.postValue(playlists));
return pinnedPlaylistList;
}
public void unpinPlaylist(List<Playlist> playlists) {
if (type.equals(Constants.PLAYLIST_ALL)) {
for (Playlist playlist : playlists) {
playlistRepository.delete(playlist);
}
}
}
public void setType(String type) {
this.type = type;
}

View file

@ -4,23 +4,16 @@ 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.repository.DownloadRepository;
import com.cappielloantonio.play.repository.PlaylistRepository;
import com.cappielloantonio.play.subsonic.models.Child;
import com.cappielloantonio.play.subsonic.models.Playlist;
import com.cappielloantonio.play.util.Preferences;
import java.util.List;
public class PlaylistPageViewModel extends AndroidViewModel {
private final PlaylistRepository playlistRepository;
private final DownloadRepository downloadRepository;
private MutableLiveData<List<Child>> playlistSongLiveList = new MutableLiveData<>();
private Playlist playlist;
private boolean isOffline;
@ -29,18 +22,10 @@ public class PlaylistPageViewModel extends AndroidViewModel {
super(application);
playlistRepository = new PlaylistRepository();
downloadRepository = new DownloadRepository();
}
public LiveData<List<Child>> getPlaylistSongLiveList(LifecycleOwner owner) {
if (isOffline) {
// TODO
// downloadRepository.getLiveDownloadFromPlaylist(playlist.getId()).observe(owner, downloads -> playlistSongLiveList.postValue(MappingUtil.mapDownloadToMedia(downloads)));
} else {
playlistSongLiveList = playlistRepository.getPlaylistSongs(playlist.getId());
}
return playlistSongLiveList;
public LiveData<List<Child>> getPlaylistSongLiveList() {
return playlistRepository.getPlaylistSongs(playlist.getId());
}
public Playlist getPlaylist() {
@ -49,29 +34,5 @@ public class PlaylistPageViewModel extends AndroidViewModel {
public void setPlaylist(Playlist playlist) {
this.playlist = playlist;
// TODO
// this.playlist.setServer(Preferences.getServerId());
}
public void setOffline(boolean offline) {
isOffline = offline;
}
public boolean isOffline() {
return isOffline;
}
public LiveData<Boolean> isPinned(LifecycleOwner owner) {
MutableLiveData<Boolean> isPinnedLive = new MutableLiveData<>();
playlistRepository.getPinnedPlaylists(Preferences.getServerId()).observe(owner, playlists -> isPinnedLive.postValue(playlists.contains(playlist)));
return isPinnedLive;
}
public void setPinned(boolean isNowPinned) {
if (isNowPinned) {
playlistRepository.insert(playlist);
} else {
playlistRepository.delete(playlist);
}
}
}

View file

@ -5,12 +5,10 @@ import android.text.TextUtils;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import com.cappielloantonio.play.repository.ArtistRepository;
import com.cappielloantonio.play.repository.DownloadRepository;
import com.cappielloantonio.play.repository.SongRepository;
import com.cappielloantonio.play.subsonic.models.AlbumID3;
import com.cappielloantonio.play.subsonic.models.ArtistID3;
@ -24,7 +22,6 @@ import java.util.List;
public class SongListPageViewModel extends AndroidViewModel {
private final SongRepository songRepository;
private final ArtistRepository artistRepository;
private final DownloadRepository downloadRepository;
public String title;
public String toolbarTitle;
@ -44,10 +41,9 @@ public class SongListPageViewModel extends AndroidViewModel {
songRepository = new SongRepository();
artistRepository = new ArtistRepository();
downloadRepository = new DownloadRepository();
}
public LiveData<List<Child>> getSongList(LifecycleOwner owner) {
public LiveData<List<Child>> getSongList() {
songList = new MutableLiveData<>(new ArrayList<>());
switch (title) {