2023-06-17 15:30:23 +02:00
|
|
|
package com.cappielloantonio.tempo.viewmodel;
|
2020-11-20 15:38:08 +01:00
|
|
|
|
|
|
|
|
import android.app.Application;
|
|
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
import androidx.lifecycle.AndroidViewModel;
|
2021-08-10 15:42:39 +02:00
|
|
|
import androidx.lifecycle.LifecycleOwner;
|
2020-11-20 15:38:08 +01:00
|
|
|
import androidx.lifecycle.LiveData;
|
2021-08-10 15:42:39 +02:00
|
|
|
import androidx.lifecycle.MutableLiveData;
|
2020-11-20 15:38:08 +01:00
|
|
|
|
2023-06-17 15:30:23 +02:00
|
|
|
import com.cappielloantonio.tempo.repository.AlbumRepository;
|
|
|
|
|
import com.cappielloantonio.tempo.repository.ArtistRepository;
|
|
|
|
|
import com.cappielloantonio.tempo.repository.DirectoryRepository;
|
|
|
|
|
import com.cappielloantonio.tempo.repository.GenreRepository;
|
|
|
|
|
import com.cappielloantonio.tempo.repository.PlaylistRepository;
|
|
|
|
|
import com.cappielloantonio.tempo.subsonic.models.AlbumID3;
|
|
|
|
|
import com.cappielloantonio.tempo.subsonic.models.ArtistID3;
|
|
|
|
|
import com.cappielloantonio.tempo.subsonic.models.Genre;
|
|
|
|
|
import com.cappielloantonio.tempo.subsonic.models.Indexes;
|
|
|
|
|
import com.cappielloantonio.tempo.subsonic.models.MusicFolder;
|
|
|
|
|
import com.cappielloantonio.tempo.subsonic.models.Playlist;
|
2020-11-20 15:38:08 +01:00
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
public class LibraryViewModel extends AndroidViewModel {
|
2021-09-01 16:53:10 +02:00
|
|
|
private static final String TAG = "LibraryViewModel";
|
|
|
|
|
|
2023-05-27 11:57:59 +02:00
|
|
|
private final DirectoryRepository directoryRepository;
|
2021-08-17 14:46:10 +02:00
|
|
|
private final AlbumRepository albumRepository;
|
|
|
|
|
private final ArtistRepository artistRepository;
|
|
|
|
|
private final GenreRepository genreRepository;
|
|
|
|
|
private final PlaylistRepository playlistRepository;
|
2020-11-21 18:41:35 +01:00
|
|
|
|
2023-05-27 11:57:59 +02:00
|
|
|
private final MutableLiveData<List<MusicFolder>> musicFolders = new MutableLiveData<>(null);
|
|
|
|
|
private final MutableLiveData<Indexes> indexes = new MutableLiveData<>(null);
|
2021-08-17 14:46:10 +02:00
|
|
|
private final MutableLiveData<List<Playlist>> playlistSample = new MutableLiveData<>(null);
|
2023-03-06 21:59:10 +01:00
|
|
|
private final MutableLiveData<List<AlbumID3>> sampleAlbum = new MutableLiveData<>(null);
|
|
|
|
|
private final MutableLiveData<List<ArtistID3>> sampleArtist = new MutableLiveData<>(null);
|
2021-08-17 14:46:10 +02:00
|
|
|
private final MutableLiveData<List<Genre>> sampleGenres = new MutableLiveData<>(null);
|
2020-11-20 15:38:08 +01:00
|
|
|
|
|
|
|
|
public LibraryViewModel(@NonNull Application application) {
|
|
|
|
|
super(application);
|
|
|
|
|
|
2023-05-27 11:57:59 +02:00
|
|
|
directoryRepository = new DirectoryRepository();
|
2023-03-10 15:21:02 +01:00
|
|
|
albumRepository = new AlbumRepository();
|
|
|
|
|
artistRepository = new ArtistRepository();
|
|
|
|
|
genreRepository = new GenreRepository();
|
|
|
|
|
playlistRepository = new PlaylistRepository();
|
2020-11-20 15:38:08 +01:00
|
|
|
}
|
|
|
|
|
|
2023-05-27 11:57:59 +02:00
|
|
|
public LiveData<List<MusicFolder>> getMusicFolders(LifecycleOwner owner) {
|
|
|
|
|
if (musicFolders.getValue() == null) {
|
|
|
|
|
directoryRepository.getMusicFolders().observe(owner, musicFolders::postValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return musicFolders;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public LiveData<Indexes> getIndexes(LifecycleOwner owner) {
|
|
|
|
|
if (indexes.getValue() == null) {
|
|
|
|
|
directoryRepository.getIndexes("0", null).observe(owner, indexes::postValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return indexes;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-06 21:59:10 +01:00
|
|
|
public LiveData<List<AlbumID3>> getAlbumSample(LifecycleOwner owner) {
|
2022-09-05 08:21:01 +02:00
|
|
|
if (sampleAlbum.getValue() == null) {
|
|
|
|
|
albumRepository.getAlbums("random", 10, null, null).observe(owner, sampleAlbum::postValue);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-21 18:41:35 +01:00
|
|
|
return sampleAlbum;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-06 21:59:10 +01:00
|
|
|
public LiveData<List<ArtistID3>> getArtistSample(LifecycleOwner owner) {
|
2022-09-05 08:21:01 +02:00
|
|
|
if (sampleArtist.getValue() == null) {
|
|
|
|
|
artistRepository.getArtists(true, 10).observe(owner, sampleArtist::postValue);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-21 18:41:35 +01:00
|
|
|
return sampleArtist;
|
2020-11-20 15:38:08 +01:00
|
|
|
}
|
|
|
|
|
|
2022-09-05 08:21:01 +02:00
|
|
|
public LiveData<List<Genre>> getGenreSample(LifecycleOwner owner) {
|
|
|
|
|
if (sampleGenres.getValue() == null) {
|
|
|
|
|
genreRepository.getGenres(true, 15).observe(owner, sampleGenres::postValue);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-21 18:41:35 +01:00
|
|
|
return sampleGenres;
|
2020-11-20 15:38:08 +01:00
|
|
|
}
|
2021-08-10 15:42:39 +02:00
|
|
|
|
2022-09-05 08:21:01 +02:00
|
|
|
public LiveData<List<Playlist>> getPlaylistSample(LifecycleOwner owner) {
|
|
|
|
|
if (playlistSample.getValue() == null) {
|
|
|
|
|
playlistRepository.getPlaylists(true, 10).observe(owner, playlistSample::postValue);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-10 15:42:39 +02:00
|
|
|
return playlistSample;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void refreshAlbumSample(LifecycleOwner owner) {
|
2022-03-31 15:18:27 +02:00
|
|
|
albumRepository.getAlbums("random", 10, null, null).observe(owner, sampleAlbum::postValue);
|
2021-08-10 15:42:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void refreshArtistSample(LifecycleOwner owner) {
|
2022-03-31 15:18:27 +02:00
|
|
|
artistRepository.getArtists(true, 10).observe(owner, sampleArtist::postValue);
|
2021-08-10 15:42:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void refreshGenreSample(LifecycleOwner owner) {
|
2021-08-17 14:46:10 +02:00
|
|
|
genreRepository.getGenres(true, 15).observe(owner, sampleGenres::postValue);
|
2021-08-10 15:42:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void refreshPlaylistSample(LifecycleOwner owner) {
|
2021-08-17 14:46:10 +02:00
|
|
|
playlistRepository.getPlaylists(true, 10).observe(owner, playlistSample::postValue);
|
2021-08-10 15:42:39 +02:00
|
|
|
}
|
2020-11-20 15:38:08 +01:00
|
|
|
}
|