- Removed middle layer of abstraction for subsonic classes

- Used kotlin for classes
This commit is contained in:
antonio 2023-03-06 21:59:10 +01:00
parent 917c0839de
commit ca15f51c85
168 changed files with 2026 additions and 6588 deletions

View file

@ -7,19 +7,20 @@ import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import com.cappielloantonio.play.model.Album;
import com.cappielloantonio.play.model.Artist;
import com.cappielloantonio.play.model.Media;
import com.cappielloantonio.play.repository.AlbumRepository;
import com.cappielloantonio.play.repository.ArtistRepository;
import com.cappielloantonio.play.subsonic.models.AlbumID3;
import com.cappielloantonio.play.subsonic.models.ArtistID3;
import com.cappielloantonio.play.subsonic.models.Child;
import java.util.Date;
import java.util.List;
public class AlbumBottomSheetViewModel extends AndroidViewModel {
private final AlbumRepository albumRepository;
private final ArtistRepository artistRepository;
private Album album;
private AlbumID3 album;
public AlbumBottomSheetViewModel(@NonNull Application application) {
super(application);
@ -28,29 +29,29 @@ public class AlbumBottomSheetViewModel extends AndroidViewModel {
artistRepository = new ArtistRepository(application);
}
public Album getAlbum() {
public AlbumID3 getAlbum() {
return album;
}
public void setAlbum(Album album) {
public void setAlbum(AlbumID3 album) {
this.album = album;
}
public LiveData<Artist> getArtist() {
public LiveData<ArtistID3> getArtist() {
return artistRepository.getArtist(album.getArtistId());
}
public MutableLiveData<List<Media>> getAlbumTracks() {
public MutableLiveData<List<Child>> getAlbumTracks() {
return albumRepository.getAlbumTracks(album.getId());
}
public void setFavorite() {
if (Boolean.TRUE.equals(album.getStarred())) {
if (album.getStarred() != null) {
artistRepository.unstar(album.getId());
album.setStarred(false);
album.setStarred(null);
} else {
artistRepository.star(album.getId());
album.setStarred(true);
album.setStarred(new Date());
}
}
}

View file

@ -10,10 +10,8 @@ import androidx.lifecycle.MutableLiveData;
import com.cappielloantonio.play.App;
import com.cappielloantonio.play.interfaces.MediaCallback;
import com.cappielloantonio.play.model.Album;
import com.cappielloantonio.play.subsonic.models.AlbumID3;
import com.cappielloantonio.play.subsonic.models.SubsonicResponse;
import com.cappielloantonio.play.util.MappingUtil;
import java.util.ArrayList;
import java.util.List;
@ -22,7 +20,7 @@ import retrofit2.Call;
import retrofit2.Callback;
public class AlbumCatalogueViewModel extends AndroidViewModel {
private final MutableLiveData<List<Album>> albumList = new MutableLiveData<>(new ArrayList<>());
private final MutableLiveData<List<AlbumID3>> albumList = new MutableLiveData<>(new ArrayList<>());
private int page = 0;
@ -30,7 +28,7 @@ public class AlbumCatalogueViewModel extends AndroidViewModel {
super(application);
}
public LiveData<List<Album>> getAlbumList() {
public LiveData<List<AlbumID3>> getAlbumList() {
return albumList;
}
@ -42,9 +40,11 @@ public class AlbumCatalogueViewModel extends AndroidViewModel {
@Override
public void onLoadMedia(List<?> media) {
List<Album> liveAlbum = albumList.getValue();
List<AlbumID3> liveAlbum = albumList.getValue();
if (liveAlbum == null) liveAlbum = new ArrayList<>();
liveAlbum.addAll(MappingUtil.mapAlbum((List<AlbumID3>) media));
liveAlbum.addAll((List<AlbumID3>) media);
albumList.setValue(liveAlbum);
if (media.size() == size) {

View file

@ -3,17 +3,16 @@ package com.cappielloantonio.play.viewmodel;
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import com.cappielloantonio.play.model.Album;
import com.cappielloantonio.play.model.Artist;
import com.cappielloantonio.play.repository.AlbumRepository;
import com.cappielloantonio.play.repository.DownloadRepository;
import com.cappielloantonio.play.util.MappingUtil;
import com.cappielloantonio.play.subsonic.models.AlbumID3;
import com.cappielloantonio.play.subsonic.models.ArtistID3;
import java.util.ArrayList;
import java.util.Calendar;
@ -25,9 +24,9 @@ public class AlbumListPageViewModel extends AndroidViewModel {
private final DownloadRepository downloadRepository;
public String title;
public Artist artist;
public ArtistID3 artist;
private MutableLiveData<List<Album>> albumList;
private MutableLiveData<List<AlbumID3>> albumList;
public AlbumListPageViewModel(@NonNull Application application) {
super(application);
@ -36,7 +35,7 @@ public class AlbumListPageViewModel extends AndroidViewModel {
downloadRepository = new DownloadRepository(application);
}
public LiveData<List<Album>> getAlbumList(LifecycleOwner owner) {
public LiveData<List<AlbumID3>> getAlbumList(LifecycleOwner owner) {
albumList = new MutableLiveData<>(new ArrayList<>());
switch (title) {
@ -55,15 +54,17 @@ public class AlbumListPageViewModel extends AndroidViewModel {
case Album.NEW_RELEASES:
int currentYear = Calendar.getInstance().get(Calendar.YEAR);
albumRepository.getAlbums("byYear", 500, currentYear, currentYear).observe(owner, albums -> {
albums.sort(Comparator.comparing(Album::getCreated).reversed());
albums.sort(Comparator.comparing(AlbumID3::getCreated).reversed());
albumList.postValue(albums.subList(0, Math.min(20, albums.size())));
});
break;
case Album.DOWNLOADED:
downloadRepository.getLiveDownload().observe(owner, downloads -> albumList.setValue(MappingUtil.mapDownloadToAlbum(downloads)));
// TODO
// downloadRepository.getLiveDownload().observe(owner, downloads -> albumList.setValue(MappingUtil.mapDownloadToAlbum(downloads)));
break;
case Album.FROM_ARTIST:
downloadRepository.getLiveDownloadFromArtist(artist.getId()).observe(owner, downloads -> albumList.setValue(MappingUtil.mapDownloadToAlbum(downloads)));
// TODO
// downloadRepository.getLiveDownloadFromArtist(artist.getId()).observe(owner, downloads -> albumList.setValue(MappingUtil.mapDownloadToAlbum(downloads)));
break;
}

View file

@ -8,13 +8,12 @@ import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import com.cappielloantonio.play.model.Album;
import com.cappielloantonio.play.model.Artist;
import com.cappielloantonio.play.model.Media;
import com.cappielloantonio.play.repository.AlbumRepository;
import com.cappielloantonio.play.repository.ArtistRepository;
import com.cappielloantonio.play.repository.DownloadRepository;
import com.cappielloantonio.play.util.MappingUtil;
import com.cappielloantonio.play.subsonic.models.AlbumID3;
import com.cappielloantonio.play.subsonic.models.ArtistID3;
import com.cappielloantonio.play.subsonic.models.Child;
import java.util.List;
@ -23,9 +22,9 @@ public class AlbumPageViewModel extends AndroidViewModel {
private final ArtistRepository artistRepository;
private final DownloadRepository downloadRepository;
private MutableLiveData<List<Media>> songLiveList = new MutableLiveData<>();
private MutableLiveData<List<Child>> songLiveList = new MutableLiveData<>();
private Album album;
private AlbumID3 album;
private boolean isOffline;
public AlbumPageViewModel(@NonNull Application application) {
@ -36,9 +35,10 @@ public class AlbumPageViewModel extends AndroidViewModel {
downloadRepository = new DownloadRepository(application);
}
public LiveData<List<Media>> getAlbumSongLiveList(LifecycleOwner owner) {
public LiveData<List<Child>> getAlbumSongLiveList(LifecycleOwner owner) {
if (isOffline) {
downloadRepository.getLiveDownloadFromAlbum(album.getId()).observe(owner, downloads -> songLiveList.postValue(MappingUtil.mapDownloadToMedia(downloads)));
// TODO
//downloadRepository.getLiveDownloadFromAlbum(album.getId()).observe(owner, downloads -> songLiveList.postValue(MappingUtil.mapDownloadToMedia(downloads)));
} else {
songLiveList = albumRepository.getAlbumTracks(album.getId());
}
@ -46,11 +46,11 @@ public class AlbumPageViewModel extends AndroidViewModel {
return songLiveList;
}
public Album getAlbum() {
public AlbumID3 getAlbum() {
return album;
}
public void setAlbum(Album album) {
public void setAlbum(AlbumID3 album) {
this.album = album;
}
@ -58,7 +58,7 @@ public class AlbumPageViewModel extends AndroidViewModel {
isOffline = offline;
}
public LiveData<Artist> getArtist() {
public LiveData<ArtistID3> getArtist() {
return artistRepository.getArtistInfo(album.getArtistId());
}
}

View file

@ -5,13 +5,15 @@ import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import com.cappielloantonio.play.model.Artist;
import com.cappielloantonio.play.repository.AlbumRepository;
import com.cappielloantonio.play.subsonic.models.ArtistID3;
import java.util.Date;
public class ArtistBottomSheetViewModel extends AndroidViewModel {
private final AlbumRepository albumRepository;
private Artist artist;
private ArtistID3 artist;
public ArtistBottomSheetViewModel(@NonNull Application application) {
super(application);
@ -19,21 +21,21 @@ public class ArtistBottomSheetViewModel extends AndroidViewModel {
albumRepository = new AlbumRepository(application);
}
public Artist getArtist() {
public ArtistID3 getArtist() {
return artist;
}
public void setArtist(Artist artist) {
public void setArtist(ArtistID3 artist) {
this.artist = artist;
}
public void setFavorite() {
if (Boolean.TRUE.equals(artist.getStarred())) {
if (artist.getStarred() != null) {
albumRepository.unstar(artist.getId());
artist.setStarred(false);
artist.setStarred(null);
} else {
albumRepository.star(artist.getId());
artist.setStarred(true);
artist.setStarred(new Date());
}
}
}

View file

@ -9,11 +9,9 @@ import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import com.cappielloantonio.play.App;
import com.cappielloantonio.play.model.Artist;
import com.cappielloantonio.play.subsonic.models.ArtistID3;
import com.cappielloantonio.play.subsonic.models.IndexID3;
import com.cappielloantonio.play.subsonic.models.SubsonicResponse;
import com.cappielloantonio.play.util.MappingUtil;
import java.util.ArrayList;
import java.util.List;
@ -22,13 +20,13 @@ import retrofit2.Call;
import retrofit2.Callback;
public class ArtistCatalogueViewModel extends AndroidViewModel {
private final MutableLiveData<List<Artist>> artistList = new MutableLiveData<>(new ArrayList<>());
private final MutableLiveData<List<ArtistID3>> artistList = new MutableLiveData<>(new ArrayList<>());
public ArtistCatalogueViewModel(@NonNull Application application) {
super(application);
}
public LiveData<List<Artist>> getArtistList() {
public LiveData<List<ArtistID3>> getArtistList() {
return artistList;
}
@ -46,7 +44,7 @@ public class ArtistCatalogueViewModel extends AndroidViewModel {
artists.addAll(index.getArtists());
}
artistList.setValue(MappingUtil.mapArtist(artists));
artistList.setValue(artists);
}
}

View file

@ -3,7 +3,6 @@ package com.cappielloantonio.play.viewmodel;
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LiveData;
@ -13,7 +12,7 @@ import com.cappielloantonio.play.model.Artist;
import com.cappielloantonio.play.model.Download;
import com.cappielloantonio.play.repository.ArtistRepository;
import com.cappielloantonio.play.repository.DownloadRepository;
import com.cappielloantonio.play.util.MappingUtil;
import com.cappielloantonio.play.subsonic.models.ArtistID3;
import java.util.ArrayList;
import java.util.Comparator;
@ -27,7 +26,7 @@ public class ArtistListPageViewModel extends AndroidViewModel {
public String title;
private MutableLiveData<List<Artist>> artistList;
private MutableLiveData<List<ArtistID3>> artistList;
public ArtistListPageViewModel(@NonNull Application application) {
super(application);
@ -36,7 +35,7 @@ public class ArtistListPageViewModel extends AndroidViewModel {
downloadRepository = new DownloadRepository(application);
}
public LiveData<List<Artist>> getArtistList(LifecycleOwner owner) {
public LiveData<List<ArtistID3>> getArtistList(LifecycleOwner owner) {
artistList = new MutableLiveData<>(new ArrayList<>());
switch (title) {
@ -48,10 +47,11 @@ public class ArtistListPageViewModel extends AndroidViewModel {
List<Download> unique = downloads
.stream()
.collect(Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Download::getArtistName))), ArrayList::new)
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Download::getArtist))), ArrayList::new)
);
artistList.setValue(MappingUtil.mapDownloadToArtist(unique));
// TODO
// artistList.setValue(MappingUtil.mapDownloadToArtist(unique));
});
break;
}

View file

@ -6,11 +6,12 @@ import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import com.cappielloantonio.play.model.Album;
import com.cappielloantonio.play.model.Artist;
import com.cappielloantonio.play.model.Media;
import com.cappielloantonio.play.repository.AlbumRepository;
import com.cappielloantonio.play.repository.ArtistRepository;
import com.cappielloantonio.play.subsonic.models.AlbumID3;
import com.cappielloantonio.play.subsonic.models.ArtistID3;
import com.cappielloantonio.play.subsonic.models.ArtistInfo2;
import com.cappielloantonio.play.subsonic.models.Child;
import java.util.List;
@ -18,7 +19,7 @@ public class ArtistPageViewModel extends AndroidViewModel {
private final AlbumRepository albumRepository;
private final ArtistRepository artistRepository;
private Artist artist;
private ArtistID3 artist;
public ArtistPageViewModel(@NonNull Application application) {
super(application);
@ -27,23 +28,23 @@ public class ArtistPageViewModel extends AndroidViewModel {
artistRepository = new ArtistRepository(application);
}
public LiveData<List<Album>> getAlbumList() {
public LiveData<List<AlbumID3>> getAlbumList() {
return albumRepository.getArtistAlbums(artist.getId());
}
public LiveData<Artist> getArtistInfo(String id) {
public LiveData<ArtistInfo2> getArtistInfo(String id) {
return artistRepository.getArtistFullInfo(id);
}
public LiveData<List<Media>> getArtistTopSongList(int count) {
public LiveData<List<Child>> getArtistTopSongList(int count) {
return artistRepository.getTopSongs(artist.getName(), count);
}
public Artist getArtist() {
public ArtistID3 getArtist() {
return artist;
}
public void setArtist(Artist artist) {
public void setArtist(ArtistID3 artist) {
this.artist = artist;
}
}

View file

@ -11,11 +11,12 @@ import androidx.lifecycle.MutableLiveData;
import com.cappielloantonio.play.model.Album;
import com.cappielloantonio.play.model.Artist;
import com.cappielloantonio.play.model.Playlist;
import com.cappielloantonio.play.model.Media;
import com.cappielloantonio.play.repository.DownloadRepository;
import com.cappielloantonio.play.subsonic.models.Child;
import com.cappielloantonio.play.util.MappingUtil;
import java.util.List;
import java.util.stream.Collectors;
public class DownloadViewModel extends AndroidViewModel {
private static final String TAG = "HomeViewModel";
@ -24,7 +25,7 @@ public class DownloadViewModel extends AndroidViewModel {
private final MutableLiveData<List<Artist>> downloadedArtistSample = new MutableLiveData<>(null);
private final MutableLiveData<List<Album>> downloadedAlbumSample = new MutableLiveData<>(null);
private final MutableLiveData<List<Media>> downloadedTrackSample = new MutableLiveData<>(null);
private final MutableLiveData<List<Child>> downloadedTrackSample = new MutableLiveData<>(null);
private final MutableLiveData<List<Playlist>> downloadedPlaylistSample = new MutableLiveData<>(null);
public DownloadViewModel(@NonNull Application application) {
@ -43,8 +44,8 @@ public class DownloadViewModel extends AndroidViewModel {
return downloadedAlbumSample;
}
public LiveData<List<Media>> getDownloadedTracks(LifecycleOwner owner, int size) {
downloadRepository.getLiveDownloadSample(size, false, false, true, false).observe(owner, downloads -> downloadedTrackSample.postValue(MappingUtil.mapDownloadToMedia(downloads)));
public LiveData<List<Child>> getDownloadedTracks(LifecycleOwner owner, int size) {
downloadRepository.getLiveDownloadSample(size, false, false, true, false).observe(owner, downloads -> downloadedTrackSample.postValue(downloads.stream().map(download -> (Child) download).collect(Collectors.toList())));
return downloadedTrackSample;
}

View file

@ -6,8 +6,8 @@ import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import com.cappielloantonio.play.model.Genre;
import com.cappielloantonio.play.repository.GenreRepository;
import com.cappielloantonio.play.subsonic.models.Genre;
import java.util.ArrayList;
import java.util.List;

View file

@ -6,8 +6,8 @@ import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import com.cappielloantonio.play.model.Genre;
import com.cappielloantonio.play.repository.GenreRepository;
import com.cappielloantonio.play.subsonic.models.Genre;
import java.util.List;

View file

@ -8,21 +8,20 @@ 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 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;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
@ -38,25 +37,25 @@ public class HomeViewModel extends AndroidViewModel {
private final PodcastRepository podcastRepository;
private final ChronologyRepository chronologyRepository;
private final MutableLiveData<List<Media>> dicoverSongSample = new MutableLiveData<>(null);
private final MutableLiveData<List<Album>> newReleasedAlbum = new MutableLiveData<>(null);
private final MutableLiveData<List<Media>> starredTracksSample = new MutableLiveData<>(null);
private final MutableLiveData<List<Artist>> starredArtistsSample = new MutableLiveData<>(null);
private final MutableLiveData<List<Artist>> bestOfArtists = new MutableLiveData<>(null);
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<Child>> dicoverSongSample = new MutableLiveData<>(null);
private final MutableLiveData<List<AlbumID3>> newReleasedAlbum = new MutableLiveData<>(null);
private final MutableLiveData<List<Child>> starredTracksSample = new MutableLiveData<>(null);
private final MutableLiveData<List<ArtistID3>> starredArtistsSample = new MutableLiveData<>(null);
private final MutableLiveData<List<ArtistID3>> bestOfArtists = new MutableLiveData<>(null);
private final MutableLiveData<List<Child>> starredTracks = new MutableLiveData<>(null);
private final MutableLiveData<List<AlbumID3>> starredAlbums = new MutableLiveData<>(null);
private final MutableLiveData<List<ArtistID3>> starredArtists = new MutableLiveData<>(null);
private final MutableLiveData<List<AlbumID3>> mostPlayedAlbumSample = new MutableLiveData<>(null);
private final MutableLiveData<List<AlbumID3>> 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<AlbumID3>> recentlyAddedAlbumSample = new MutableLiveData<>(null);
private final MutableLiveData<List<Playlist>> pinnedPlaylists = new MutableLiveData<>(null);
private final MutableLiveData<List<Media>> newestPodcastEpisodes = new MutableLiveData<>(null);
private final MutableLiveData<List<PodcastEpisode>> newestPodcastEpisodes = new MutableLiveData<>(null);
private final MutableLiveData<List<Chronology>> thisGridTopSong = new MutableLiveData<>(null);
private final MutableLiveData<List<Media>> mediaInstantMix = new MutableLiveData<>(null);
private final MutableLiveData<List<Media>> artistInstantMix = new MutableLiveData<>(null);
private final MutableLiveData<List<Media>> artistBestOf = new MutableLiveData<>(null);
private final MutableLiveData<List<Child>> mediaInstantMix = new MutableLiveData<>(null);
private final MutableLiveData<List<Child>> artistInstantMix = new MutableLiveData<>(null);
private final MutableLiveData<List<Child>> artistBestOf = new MutableLiveData<>(null);
public HomeViewModel(@NonNull Application application) {
super(application);
@ -69,7 +68,7 @@ public class HomeViewModel extends AndroidViewModel {
chronologyRepository = new ChronologyRepository(application);
}
public LiveData<List<Media>> getDiscoverSongSample(LifecycleOwner owner) {
public LiveData<List<Child>> getDiscoverSongSample(LifecycleOwner owner) {
if (dicoverSongSample.getValue() == null) {
songRepository.getRandomSample(10, null, null).observe(owner, dicoverSongSample::postValue);
}
@ -80,7 +79,7 @@ public class HomeViewModel extends AndroidViewModel {
public LiveData<List<Chronology>> getGridSongSample(LifecycleOwner owner) {
Calendar cal = Calendar.getInstance();
int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
String server = PreferenceUtil.getInstance(App.getInstance()).getServerId();
String server = Preferences.getServerId();
if (thisGridTopSong.getValue() == null) {
if (dayOfMonth >= 7) {
@ -93,12 +92,12 @@ public class HomeViewModel extends AndroidViewModel {
return thisGridTopSong;
}
public LiveData<List<Album>> getRecentlyReleasedAlbums(LifecycleOwner owner) {
public LiveData<List<AlbumID3>> 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());
albums.sort(Comparator.comparing(AlbumID3::getCreated).reversed());
newReleasedAlbum.postValue(albums.subList(0, Math.min(20, albums.size())));
});
}
@ -106,7 +105,7 @@ public class HomeViewModel extends AndroidViewModel {
return newReleasedAlbum;
}
public LiveData<List<Media>> getStarredTracksSample(LifecycleOwner owner) {
public LiveData<List<Child>> getStarredTracksSample(LifecycleOwner owner) {
if (starredTracksSample.getValue() == null) {
songRepository.getStarredSongs(true, 10).observe(owner, starredTracksSample::postValue);
}
@ -114,7 +113,7 @@ public class HomeViewModel extends AndroidViewModel {
return starredTracksSample;
}
public LiveData<List<Artist>> getStarredArtistsSample(LifecycleOwner owner) {
public LiveData<List<ArtistID3>> getStarredArtistsSample(LifecycleOwner owner) {
if (starredArtistsSample.getValue() == null) {
artistRepository.getStarredArtists(true, 10).observe(owner, starredArtistsSample::postValue);
}
@ -122,7 +121,7 @@ public class HomeViewModel extends AndroidViewModel {
return starredArtistsSample;
}
public LiveData<List<Artist>> getBestOfArtists(LifecycleOwner owner) {
public LiveData<List<ArtistID3>> getBestOfArtists(LifecycleOwner owner) {
if (bestOfArtists.getValue() == null) {
artistRepository.getStarredArtists(true, 20).observe(owner, bestOfArtists::postValue);
}
@ -130,7 +129,7 @@ public class HomeViewModel extends AndroidViewModel {
return bestOfArtists;
}
public LiveData<List<Media>> getStarredTracks(LifecycleOwner owner) {
public LiveData<List<Child>> getStarredTracks(LifecycleOwner owner) {
if (starredTracks.getValue() == null) {
songRepository.getStarredSongs(true, 20).observe(owner, starredTracks::postValue);
}
@ -138,7 +137,7 @@ public class HomeViewModel extends AndroidViewModel {
return starredTracks;
}
public LiveData<List<Album>> getStarredAlbums(LifecycleOwner owner) {
public LiveData<List<AlbumID3>> getStarredAlbums(LifecycleOwner owner) {
if (starredAlbums.getValue() == null) {
albumRepository.getStarredAlbums(true, 20).observe(owner, starredAlbums::postValue);
}
@ -146,7 +145,7 @@ public class HomeViewModel extends AndroidViewModel {
return starredAlbums;
}
public LiveData<List<Artist>> getStarredArtists(LifecycleOwner owner) {
public LiveData<List<ArtistID3>> getStarredArtists(LifecycleOwner owner) {
if (starredArtists.getValue() == null) {
artistRepository.getStarredArtists(true, 20).observe(owner, starredArtists::postValue);
}
@ -162,7 +161,7 @@ public class HomeViewModel extends AndroidViewModel {
return years;
}
public LiveData<List<Album>> getMostPlayedAlbums(LifecycleOwner owner) {
public LiveData<List<AlbumID3>> getMostPlayedAlbums(LifecycleOwner owner) {
if (mostPlayedAlbumSample.getValue() == null) {
albumRepository.getAlbums("frequent", 20, null, null).observe(owner, mostPlayedAlbumSample::postValue);
}
@ -170,7 +169,7 @@ public class HomeViewModel extends AndroidViewModel {
return mostPlayedAlbumSample;
}
public LiveData<List<Album>> getMostRecentlyAddedAlbums(LifecycleOwner owner) {
public LiveData<List<AlbumID3>> getMostRecentlyAddedAlbums(LifecycleOwner owner) {
if (recentlyAddedAlbumSample.getValue() == null) {
albumRepository.getAlbums("newest", 20, null, null).observe(owner, recentlyAddedAlbumSample::postValue);
}
@ -178,7 +177,7 @@ public class HomeViewModel extends AndroidViewModel {
return recentlyAddedAlbumSample;
}
public LiveData<List<Album>> getRecentlyPlayedAlbumList(LifecycleOwner owner) {
public LiveData<List<AlbumID3>> getRecentlyPlayedAlbumList(LifecycleOwner owner) {
if (recentlyPlayedAlbumSample.getValue() == null) {
albumRepository.getAlbums("recent", 20, null, null).observe(owner, recentlyPlayedAlbumSample::postValue);
}
@ -187,7 +186,7 @@ public class HomeViewModel extends AndroidViewModel {
}
public LiveData<List<Playlist>> getPinnedPlaylistList(LifecycleOwner owner, int maxNumber, boolean random) {
playlistRepository.getPinnedPlaylists(PreferenceUtil.getInstance(App.getInstance()).getServerId()).observe(owner, playlists -> {
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);
@ -196,11 +195,11 @@ public class HomeViewModel extends AndroidViewModel {
return pinnedPlaylists;
}
public LiveData<List<Media>> getPlaylistSongLiveList(String playlistId) {
public LiveData<List<Child>> getPlaylistSongLiveList(String playlistId) {
return playlistRepository.getPlaylistSongs(playlistId);
}
public LiveData<List<Media>> getNewestPodcastEpisodes(LifecycleOwner owner) {
public LiveData<List<PodcastEpisode>> getNewestPodcastEpisodes(LifecycleOwner owner) {
if (newestPodcastEpisodes.getValue() == null) {
podcastRepository.getNewestPodcastEpisodes(20).observe(owner, newestPodcastEpisodes::postValue);
}
@ -208,7 +207,7 @@ public class HomeViewModel extends AndroidViewModel {
return newestPodcastEpisodes;
}
public LiveData<List<Media>> getMediaInstantMix(LifecycleOwner owner, Media media) {
public LiveData<List<Child>> getMediaInstantMix(LifecycleOwner owner, Child media) {
mediaInstantMix.setValue(Collections.emptyList());
songRepository.getInstantMix(media, 20).observe(owner, mediaInstantMix::postValue);
@ -216,11 +215,11 @@ public class HomeViewModel extends AndroidViewModel {
return mediaInstantMix;
}
public LiveData<ArrayList<Media>> getArtistInstantMix(Artist artist) {
public LiveData<List<Child>> getArtistInstantMix(ArtistID3 artist) {
return artistRepository.getInstantMix(artist, 20);
}
public LiveData<List<Media>> getArtistBestOf(Artist artist) {
public LiveData<List<Child>> getArtistBestOf(ArtistID3 artist) {
return artistRepository.getTopSongs(artist.getName(), 10);
}

View file

@ -8,14 +8,14 @@ import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import com.cappielloantonio.play.model.Album;
import com.cappielloantonio.play.model.Artist;
import com.cappielloantonio.play.model.Genre;
import com.cappielloantonio.play.model.Playlist;
import com.cappielloantonio.play.repository.AlbumRepository;
import com.cappielloantonio.play.repository.ArtistRepository;
import com.cappielloantonio.play.repository.GenreRepository;
import com.cappielloantonio.play.repository.PlaylistRepository;
import com.cappielloantonio.play.subsonic.models.AlbumID3;
import com.cappielloantonio.play.subsonic.models.ArtistID3;
import com.cappielloantonio.play.subsonic.models.Genre;
import com.cappielloantonio.play.subsonic.models.Playlist;
import java.util.List;
@ -28,8 +28,8 @@ public class LibraryViewModel extends AndroidViewModel {
private final PlaylistRepository playlistRepository;
private final MutableLiveData<List<Playlist>> playlistSample = new MutableLiveData<>(null);
private final MutableLiveData<List<Album>> sampleAlbum = new MutableLiveData<>(null);
private final MutableLiveData<List<Artist>> sampleArtist = new MutableLiveData<>(null);
private final MutableLiveData<List<AlbumID3>> sampleAlbum = new MutableLiveData<>(null);
private final MutableLiveData<List<ArtistID3>> sampleArtist = new MutableLiveData<>(null);
private final MutableLiveData<List<Genre>> sampleGenres = new MutableLiveData<>(null);
public LibraryViewModel(@NonNull Application application) {
@ -41,7 +41,7 @@ public class LibraryViewModel extends AndroidViewModel {
playlistRepository = new PlaylistRepository(application);
}
public LiveData<List<Album>> getAlbumSample(LifecycleOwner owner) {
public LiveData<List<AlbumID3>> getAlbumSample(LifecycleOwner owner) {
if (sampleAlbum.getValue() == null) {
albumRepository.getAlbums("random", 10, null, null).observe(owner, sampleAlbum::postValue);
}
@ -49,7 +49,7 @@ public class LibraryViewModel extends AndroidViewModel {
return sampleAlbum;
}
public LiveData<List<Artist>> getArtistSample(LifecycleOwner owner) {
public LiveData<List<ArtistID3>> getArtistSample(LifecycleOwner owner) {
if (sampleArtist.getValue() == null) {
artistRepository.getArtists(true, 10).observe(owner, sampleArtist::postValue);
}

View file

@ -11,17 +11,16 @@ import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.media3.common.util.UnstableApi;
import com.cappielloantonio.play.model.Artist;
import com.cappielloantonio.play.model.Queue;
import com.cappielloantonio.play.model.Media;
import com.cappielloantonio.play.model.Queue;
import com.cappielloantonio.play.repository.ArtistRepository;
import com.cappielloantonio.play.repository.QueueRepository;
import com.cappielloantonio.play.repository.SongRepository;
import com.cappielloantonio.play.util.DownloadUtil;
import com.cappielloantonio.play.util.MappingUtil;
import com.cappielloantonio.play.util.PreferenceUtil;
import com.cappielloantonio.play.subsonic.models.ArtistID3;
import com.cappielloantonio.play.subsonic.models.Child;
import java.util.Collections;
import java.util.Date;
import java.util.List;
@OptIn(markerClass = UnstableApi.class)
@ -34,9 +33,9 @@ public class PlayerBottomSheetViewModel extends AndroidViewModel {
private final MutableLiveData<String> lyricsLiveData = new MutableLiveData<>(null);
private final MutableLiveData<Media> liveMedia = new MutableLiveData<>(null);
private final MutableLiveData<Artist> liveArtist = new MutableLiveData<>(null);
private final MutableLiveData<List<Media>> instantMix = new MutableLiveData<>(null);
private final MutableLiveData<Child> liveMedia = new MutableLiveData<>(null);
private final MutableLiveData<ArtistID3> liveArtist = new MutableLiveData<>(null);
private final MutableLiveData<List<Child>> instantMix = new MutableLiveData<>(null);
public PlayerBottomSheetViewModel(@NonNull Application application) {
@ -51,21 +50,22 @@ public class PlayerBottomSheetViewModel extends AndroidViewModel {
return queueRepository.getLiveQueue();
}
public void setFavorite(Context context, Media media) {
public void setFavorite(Context context, Child media) {
if (media != null) {
if (Boolean.TRUE.equals(media.getStarred())) {
if (media.getStarred() != null) {
songRepository.unstar(media.getId());
media.setStarred(false);
media.setStarred(null);
} else {
songRepository.star(media.getId());
media.setStarred(true);
media.setStarred(new Date());
if (PreferenceUtil.getInstance(context).isStarredSyncEnabled()) {
// TODO
/* if (Preferences.isStarredSyncEnabled()) {
DownloadUtil.getDownloadTracker(context).download(
MappingUtil.mapMediaItem(context, media, false),
MappingUtil.mapDownload(media, null, null)
);
}
} */
}
}
}
@ -74,16 +74,16 @@ public class PlayerBottomSheetViewModel extends AndroidViewModel {
return lyricsLiveData;
}
public void refreshMediaInfo(LifecycleOwner owner, Media media) {
public void refreshMediaInfo(LifecycleOwner owner, Child media) {
songRepository.getSongLyrics(media).observe(owner, lyricsLiveData::postValue);
}
public LiveData<Media> getLiveMedia() {
public LiveData<Child> getLiveMedia() {
return liveMedia;
}
public void setLiveMedia(LifecycleOwner owner, String mediaType, String mediaId) {
if(mediaType != null) {
if (mediaType != null) {
switch (mediaType) {
case Media.MEDIA_TYPE_MUSIC:
songRepository.getSong(mediaId).observe(owner, liveMedia::postValue);
@ -95,12 +95,12 @@ public class PlayerBottomSheetViewModel extends AndroidViewModel {
}
}
public LiveData<Artist> getLiveArtist() {
public LiveData<ArtistID3> getLiveArtist() {
return liveArtist;
}
public void setLiveArtist(LifecycleOwner owner, String mediaType, String ArtistId) {
if(mediaType != null) {
if (mediaType != null) {
switch (mediaType) {
case Media.MEDIA_TYPE_MUSIC:
artistRepository.getArtist(ArtistId).observe(owner, liveArtist::postValue);
@ -112,7 +112,7 @@ public class PlayerBottomSheetViewModel extends AndroidViewModel {
}
}
public LiveData<List<Media>> getMediaInstantMix(LifecycleOwner owner, Media media) {
public LiveData<List<Child>> getMediaInstantMix(LifecycleOwner owner, Child media) {
instantMix.setValue(Collections.emptyList());
songRepository.getInstantMix(media, 20).observe(owner, instantMix::postValue);

View file

@ -3,18 +3,15 @@ package com.cappielloantonio.play.viewmodel;
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
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.Playlist;
import com.cappielloantonio.play.repository.DownloadRepository;
import com.cappielloantonio.play.repository.PlaylistRepository;
import com.cappielloantonio.play.util.MappingUtil;
import com.cappielloantonio.play.util.PreferenceUtil;
import com.cappielloantonio.play.subsonic.models.Playlist;
import com.cappielloantonio.play.util.Preferences;
import java.util.ArrayList;
import java.util.List;
@ -39,11 +36,12 @@ public class PlaylistCatalogueViewModel extends AndroidViewModel {
playlistList = new MutableLiveData<>(new ArrayList<>());
switch (type) {
case Playlist.ALL:
case com.cappielloantonio.play.model.Playlist.ALL:
playlistRepository.getPlaylists(false, -1).observe(owner, playlists -> playlistList.postValue(playlists));
break;
case Playlist.DOWNLOADED:
downloadRepository.getLivePlaylist().observe(owner, downloads -> playlistList.setValue(MappingUtil.mapDownloadToPlaylist(downloads)));
case com.cappielloantonio.play.model.Playlist.DOWNLOADED:
// TODO
//downloadRepository.getLivePlaylist().observe(owner, downloads -> playlistList.setValue(MappingUtil.mapDownloadToPlaylist(downloads)));
break;
}
@ -54,13 +52,13 @@ public class PlaylistCatalogueViewModel extends AndroidViewModel {
public LiveData<List<Playlist>> getPinnedPlaylistList(LifecycleOwner owner) {
pinnedPlaylistList = new MutableLiveData<>(new ArrayList<>());
playlistRepository.getPinnedPlaylists(PreferenceUtil.getInstance(App.getInstance()).getServerId()).observe(owner, playlists -> pinnedPlaylistList.postValue(playlists));
playlistRepository.getPinnedPlaylists(Preferences.getServerId()).observe(owner, playlists -> pinnedPlaylistList.postValue(playlists));
return pinnedPlaylistList;
}
public void unpinPlaylist(List<Playlist> playlists) {
if(type.equals(Playlist.ALL)) {
for(Playlist playlist: playlists) {
if (type.equals(com.cappielloantonio.play.model.Playlist.ALL)) {
for (Playlist playlist : playlists) {
playlistRepository.delete(playlist);
}
}

View file

@ -8,9 +8,9 @@ import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import com.cappielloantonio.play.model.Media;
import com.cappielloantonio.play.model.Playlist;
import com.cappielloantonio.play.repository.PlaylistRepository;
import com.cappielloantonio.play.subsonic.models.Child;
import com.cappielloantonio.play.subsonic.models.Playlist;
import java.util.ArrayList;
import java.util.Collections;
@ -20,7 +20,7 @@ public class PlaylistChooserViewModel extends AndroidViewModel {
private final PlaylistRepository playlistRepository;
private final MutableLiveData<List<Playlist>> playlists = new MutableLiveData<>(null);
private Media toAdd;
private Child toAdd;
public PlaylistChooserViewModel(@NonNull Application application) {
super(application);
@ -37,11 +37,11 @@ public class PlaylistChooserViewModel extends AndroidViewModel {
playlistRepository.addSongToPlaylist(playlistId, new ArrayList(Collections.singletonList(toAdd.getId())));
}
public void setSongToAdd(Media song) {
public void setSongToAdd(Child song) {
toAdd = song;
}
public Media getSongToAdd() {
public Child getSongToAdd() {
return toAdd;
}
}

View file

@ -7,9 +7,9 @@ import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import com.cappielloantonio.play.model.Playlist;
import com.cappielloantonio.play.model.Media;
import com.cappielloantonio.play.repository.PlaylistRepository;
import com.cappielloantonio.play.subsonic.models.Child;
import com.cappielloantonio.play.subsonic.models.Playlist;
import java.util.ArrayList;
import java.util.Collections;
@ -21,10 +21,10 @@ public class PlaylistEditorViewModel extends AndroidViewModel {
private final PlaylistRepository playlistRepository;
private Media toAdd;
private Child toAdd;
private Playlist toEdit;
private MutableLiveData<List<Media>> songLiveList = new MutableLiveData<>();
private MutableLiveData<List<Child>> songLiveList = new MutableLiveData<>();
public PlaylistEditorViewModel(@NonNull Application application) {
super(application);
@ -45,11 +45,11 @@ public class PlaylistEditorViewModel extends AndroidViewModel {
if (toEdit != null) playlistRepository.deletePlaylist(toEdit.getId());
}
public Media getSongToAdd() {
public Child getSongToAdd() {
return toAdd;
}
public void setSongToAdd(Media song) {
public void setSongToAdd(Child song) {
this.toAdd = song;
}
@ -67,26 +67,26 @@ public class PlaylistEditorViewModel extends AndroidViewModel {
}
}
public LiveData<List<Media>> getPlaylistSongLiveList() {
public LiveData<List<Child>> getPlaylistSongLiveList() {
return songLiveList;
}
public void removeFromPlaylistSongLiveList(int position) {
List<Media> songs = songLiveList.getValue();
List<Child> songs = songLiveList.getValue();
Objects.requireNonNull(songs).remove(position);
songLiveList.postValue(songs);
}
public void orderPlaylistSongLiveListAfterSwap(List<Media> songs) {
public void orderPlaylistSongLiveListAfterSwap(List<Child> songs) {
songLiveList.postValue(songs);
}
private ArrayList<String> getPlaylistSongIds() {
List<Media> songs = songLiveList.getValue();
List<Child> songs = songLiveList.getValue();
ArrayList<String> ids = new ArrayList<>();
if (songs != null && !songs.isEmpty()) {
for (Media song : songs) {
for (Child song : songs) {
ids.add(song.getId());
}
}

View file

@ -8,13 +8,11 @@ import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import com.cappielloantonio.play.App;
import com.cappielloantonio.play.model.Playlist;
import com.cappielloantonio.play.model.Media;
import com.cappielloantonio.play.repository.DownloadRepository;
import com.cappielloantonio.play.repository.PlaylistRepository;
import com.cappielloantonio.play.util.MappingUtil;
import com.cappielloantonio.play.util.PreferenceUtil;
import com.cappielloantonio.play.subsonic.models.Child;
import com.cappielloantonio.play.subsonic.models.Playlist;
import com.cappielloantonio.play.util.Preferences;
import java.util.List;
@ -22,7 +20,7 @@ public class PlaylistPageViewModel extends AndroidViewModel {
private final PlaylistRepository playlistRepository;
private final DownloadRepository downloadRepository;
private MutableLiveData<List<Media>> playlistSongLiveList = new MutableLiveData<>();
private MutableLiveData<List<Child>> playlistSongLiveList = new MutableLiveData<>();
private Playlist playlist;
private boolean isOffline;
@ -34,9 +32,10 @@ public class PlaylistPageViewModel extends AndroidViewModel {
downloadRepository = new DownloadRepository(application);
}
public LiveData<List<Media>> getPlaylistSongLiveList(LifecycleOwner owner) {
public LiveData<List<Child>> getPlaylistSongLiveList(LifecycleOwner owner) {
if (isOffline) {
downloadRepository.getLiveDownloadFromPlaylist(playlist.getId()).observe(owner, downloads -> playlistSongLiveList.postValue(MappingUtil.mapDownloadToMedia(downloads)));
// TODO
// downloadRepository.getLiveDownloadFromPlaylist(playlist.getId()).observe(owner, downloads -> playlistSongLiveList.postValue(MappingUtil.mapDownloadToMedia(downloads)));
} else {
playlistSongLiveList = playlistRepository.getPlaylistSongs(playlist.getId());
}
@ -50,7 +49,8 @@ public class PlaylistPageViewModel extends AndroidViewModel {
public void setPlaylist(Playlist playlist) {
this.playlist = playlist;
this.playlist.setServer(PreferenceUtil.getInstance(App.getInstance()).getServerId());
// TODO
// this.playlist.setServer(Preferences.getServerId());
}
public void setOffline(boolean offline) {
@ -63,12 +63,12 @@ public class PlaylistPageViewModel extends AndroidViewModel {
public LiveData<Boolean> isPinned(LifecycleOwner owner) {
MutableLiveData<Boolean> isPinnedLive = new MutableLiveData<>();
playlistRepository.getPinnedPlaylists(PreferenceUtil.getInstance(App.getInstance()).getServerId()).observe(owner, playlists -> isPinnedLive.postValue(playlists.contains(playlist)));
playlistRepository.getPinnedPlaylists(Preferences.getServerId()).observe(owner, playlists -> isPinnedLive.postValue(playlists.contains(playlist)));
return isPinnedLive;
}
public void setPinned(boolean isNowPinned) {
if(isNowPinned) {
if (isNowPinned) {
playlistRepository.insert(playlist);
} else {
playlistRepository.delete(playlist);

View file

@ -1,21 +1,17 @@
package com.cappielloantonio.play.viewmodel;
import android.app.Application;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import com.cappielloantonio.play.model.Media;
import com.cappielloantonio.play.repository.PodcastRepository;
import com.cappielloantonio.play.util.DownloadUtil;
import com.cappielloantonio.play.util.MappingUtil;
import com.cappielloantonio.play.util.PreferenceUtil;
import com.cappielloantonio.play.subsonic.models.PodcastEpisode;
public class PodcastBottomSheetViewModel extends AndroidViewModel {
private final PodcastRepository podcastRepository;
private Media podcast;
private PodcastEpisode podcast;
public PodcastBottomSheetViewModel(@NonNull Application application) {
super(application);
@ -23,11 +19,11 @@ public class PodcastBottomSheetViewModel extends AndroidViewModel {
podcastRepository = new PodcastRepository(application);
}
public Media getPodcast() {
public PodcastEpisode getPodcast() {
return podcast;
}
public void setPodcast(Media podcast) {
public void setPodcast(PodcastEpisode podcast) {
this.podcast = podcast;
}
}

View file

@ -6,21 +6,21 @@ import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import com.cappielloantonio.play.model.Album;
import com.cappielloantonio.play.model.Artist;
import com.cappielloantonio.play.model.Media;
import com.cappielloantonio.play.repository.AlbumRepository;
import com.cappielloantonio.play.repository.ArtistRepository;
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;
public class RatingViewModel extends AndroidViewModel {
private final SongRepository songRepository;
private final AlbumRepository albumRepository;
private final ArtistRepository artistRepository;
private Media song;
private Album album;
private Artist artist;
private Child song;
private AlbumID3 album;
private ArtistID3 artist;
public RatingViewModel(@NonNull Application application) {
super(application);
@ -30,43 +30,43 @@ public class RatingViewModel extends AndroidViewModel {
artistRepository = new ArtistRepository(application);
}
public Media getSong() {
public Child getSong() {
return song;
}
public LiveData<Media> getLiveSong() {
public LiveData<Child> getLiveSong() {
return songRepository.getSong(song.getId());
}
public void setSong(Media song) {
public void setSong(Child song) {
this.song = song;
this.album = null;
this.artist = null;
}
public Album getAlbum() {
public AlbumID3 getAlbum() {
return album;
}
public LiveData<Album> getLiveAlbum() {
public LiveData<AlbumID3> getLiveAlbum() {
return albumRepository.getAlbum(album.getId());
}
public void setAlbum(Album album) {
public void setAlbum(AlbumID3 album) {
this.song = null;
this.album = album;
this.artist = null;
}
public Artist getArtist() {
public ArtistID3 getArtist() {
return artist;
}
public LiveData<Artist> getLiveArtist() {
public LiveData<ArtistID3> getLiveArtist() {
return artistRepository.getArtist(artist.getId());
}
public void setArtist(Artist artist) {
public void setArtist(ArtistID3 artist) {
this.song = null;
this.album = null;
this.artist = artist;

View file

@ -6,11 +6,9 @@ import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import com.cappielloantonio.play.model.Album;
import com.cappielloantonio.play.model.Artist;
import com.cappielloantonio.play.model.RecentSearch;
import com.cappielloantonio.play.model.Media;
import com.cappielloantonio.play.repository.SearchingRepository;
import com.cappielloantonio.play.subsonic.models.SearchResult3;
import java.util.ArrayList;
import java.util.List;
@ -40,16 +38,8 @@ public class SearchViewModel extends AndroidViewModel {
}
}
public LiveData<List<Media>> searchSong(String title) {
return searchingRepository.getSearchedSongs(title);
}
public LiveData<List<Album>> searchAlbum(String name) {
return searchingRepository.getSearchedAlbums(name);
}
public LiveData<List<Artist>> searchArtist(String name) {
return searchingRepository.getSearchedArtists(name);
public LiveData<SearchResult3> search(String title) {
return searchingRepository.search(title);
}
public void insertNewSearch(String search) {

View file

@ -10,17 +10,15 @@ import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.media3.common.util.UnstableApi;
import com.cappielloantonio.play.model.Album;
import com.cappielloantonio.play.model.Artist;
import com.cappielloantonio.play.model.Media;
import com.cappielloantonio.play.repository.AlbumRepository;
import com.cappielloantonio.play.repository.ArtistRepository;
import com.cappielloantonio.play.repository.SongRepository;
import com.cappielloantonio.play.util.DownloadUtil;
import com.cappielloantonio.play.util.MappingUtil;
import com.cappielloantonio.play.util.PreferenceUtil;
import com.cappielloantonio.play.subsonic.models.AlbumID3;
import com.cappielloantonio.play.subsonic.models.ArtistID3;
import com.cappielloantonio.play.subsonic.models.Child;
import java.util.Collections;
import java.util.Date;
import java.util.List;
@UnstableApi
@ -29,9 +27,9 @@ public class SongBottomSheetViewModel extends AndroidViewModel {
private final AlbumRepository albumRepository;
private final ArtistRepository artistRepository;
private Media song;
private Child song;
private final MutableLiveData<List<Media>> instantMix = new MutableLiveData<>(null);
private final MutableLiveData<List<Child>> instantMix = new MutableLiveData<>(null);
public SongBottomSheetViewModel(@NonNull Application application) {
super(application);
@ -41,40 +39,41 @@ public class SongBottomSheetViewModel extends AndroidViewModel {
artistRepository = new ArtistRepository(application);
}
public Media getSong() {
public Child getSong() {
return song;
}
public void setSong(Media song) {
public void setSong(Child song) {
this.song = song;
}
public void setFavorite(Context context) {
if (Boolean.TRUE.equals(song.getStarred())) {
if (song.getStarred() != null) {
songRepository.unstar(song.getId());
song.setStarred(false);
song.setStarred(null);
} else {
songRepository.star(song.getId());
song.setStarred(true);
song.setStarred(new Date());
if (PreferenceUtil.getInstance(context).isStarredSyncEnabled()) {
// TODO
/* if (Preferences.isStarredSyncEnabled()) {
DownloadUtil.getDownloadTracker(context).download(
MappingUtil.mapMediaItem(context, song, false),
MappingUtil.mapDownload(song, null, null)
);
}
} */
}
}
public LiveData<Album> getAlbum() {
public LiveData<AlbumID3> getAlbum() {
return albumRepository.getAlbum(song.getAlbumId());
}
public LiveData<Artist> getArtist() {
public LiveData<ArtistID3> getArtist() {
return artistRepository.getArtist(song.getArtistId());
}
public LiveData<List<Media>> getInstantMix(LifecycleOwner owner, Media media) {
public LiveData<List<Child>> getInstantMix(LifecycleOwner owner, Child media) {
instantMix.setValue(Collections.emptyList());
songRepository.getInstantMix(media, 20).observe(owner, instantMix::postValue);

View file

@ -9,17 +9,18 @@ import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import com.cappielloantonio.play.model.Album;
import com.cappielloantonio.play.model.Artist;
import com.cappielloantonio.play.model.Genre;
import com.cappielloantonio.play.model.Media;
import com.cappielloantonio.play.repository.ArtistRepository;
import com.cappielloantonio.play.repository.DownloadRepository;
import com.cappielloantonio.play.repository.SongRepository;
import com.cappielloantonio.play.util.MappingUtil;
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.Genre;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class SongListPageViewModel extends AndroidViewModel {
private final SongRepository songRepository;
@ -29,10 +30,10 @@ public class SongListPageViewModel extends AndroidViewModel {
public String title;
public String toolbarTitle;
public Genre genre;
public Artist artist;
public Album album;
public ArtistID3 artist;
public AlbumID3 album;
private MutableLiveData<List<Media>> songList;
private MutableLiveData<List<Child>> songList;
public ArrayList<String> filters = new ArrayList<>();
public ArrayList<String> filterNames = new ArrayList<>();
@ -47,12 +48,12 @@ public class SongListPageViewModel extends AndroidViewModel {
downloadRepository = new DownloadRepository(application);
}
public LiveData<List<Media>> getSongList(LifecycleOwner owner) {
public LiveData<List<Child>> getSongList(LifecycleOwner owner) {
songList = new MutableLiveData<>(new ArrayList<>());
switch (title) {
case Media.BY_GENRE:
songList = songRepository.getSongsByGenre(genre.getId());
songList = songRepository.getSongsByGenre(genre.getGenre());
break;
case Media.BY_ARTIST:
songList = artistRepository.getTopSongs(artist.getName(), 50);
@ -67,10 +68,10 @@ public class SongListPageViewModel extends AndroidViewModel {
songList = songRepository.getStarredSongs(false, -1);
break;
case Media.DOWNLOADED:
downloadRepository.getLiveDownload().observe(owner, downloads -> songList.setValue(MappingUtil.mapDownloadToMedia(downloads)));
downloadRepository.getLiveDownload().observe(owner, downloads -> songList.setValue(downloads.stream().map(download -> (Child) download).collect(Collectors.toList())));
break;
case Media.FROM_ALBUM:
downloadRepository.getLiveDownloadFromAlbum(album.getId()).observe(owner, downloads -> songList.setValue(MappingUtil.mapDownloadToMedia(downloads)));
downloadRepository.getLiveDownloadFromAlbum(album.getId()).observe(owner, downloads -> songList.setValue(downloads.stream().map(download -> (Child) download).collect(Collectors.toList())));
break;
}

View file

@ -8,15 +8,15 @@ import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import com.cappielloantonio.play.model.Media;
import com.cappielloantonio.play.repository.SongRepository;
import com.cappielloantonio.play.subsonic.models.Child;
import java.util.List;
public class StarredSyncViewModel extends AndroidViewModel {
private final SongRepository songRepository;
private final MutableLiveData<List<Media>> starredTracks = new MutableLiveData<>(null);
private final MutableLiveData<List<Child>> starredTracks = new MutableLiveData<>(null);
public StarredSyncViewModel(@NonNull Application application) {
super(application);
@ -24,7 +24,7 @@ public class StarredSyncViewModel extends AndroidViewModel {
songRepository = new SongRepository(application);
}
public LiveData<List<Media>> getStarredTracks(LifecycleOwner owner) {
public LiveData<List<Child>> getStarredTracks(LifecycleOwner owner) {
songRepository.getStarredSongs(false, -1).observe(owner, starredTracks::postValue);
return starredTracks;
}