build: change of package name

This commit is contained in:
antonio 2023-06-17 15:30:23 +02:00
parent 49afdbe4eb
commit b76a38cb30
274 changed files with 1981 additions and 2161 deletions

View file

@ -0,0 +1,57 @@
package com.cappielloantonio.tempo.viewmodel;
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import com.cappielloantonio.tempo.repository.AlbumRepository;
import com.cappielloantonio.tempo.repository.ArtistRepository;
import com.cappielloantonio.tempo.subsonic.models.AlbumID3;
import com.cappielloantonio.tempo.subsonic.models.ArtistID3;
import com.cappielloantonio.tempo.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 AlbumID3 album;
public AlbumBottomSheetViewModel(@NonNull Application application) {
super(application);
albumRepository = new AlbumRepository();
artistRepository = new ArtistRepository();
}
public AlbumID3 getAlbum() {
return album;
}
public void setAlbum(AlbumID3 album) {
this.album = album;
}
public LiveData<ArtistID3> getArtist() {
return artistRepository.getArtist(album.getArtistId());
}
public MutableLiveData<List<Child>> getAlbumTracks() {
return albumRepository.getAlbumTracks(album.getId());
}
public void setFavorite() {
if (album.getStarred() != null) {
artistRepository.unstar(album.getId());
album.setStarred(null);
} else {
artistRepository.star(album.getId());
album.setStarred(new Date());
}
}
}

View file

@ -0,0 +1,77 @@
package com.cappielloantonio.tempo.viewmodel;
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import com.cappielloantonio.tempo.App;
import com.cappielloantonio.tempo.interfaces.MediaCallback;
import com.cappielloantonio.tempo.subsonic.base.ApiResponse;
import com.cappielloantonio.tempo.subsonic.models.AlbumID3;
import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
public class AlbumCatalogueViewModel extends AndroidViewModel {
private final MutableLiveData<List<AlbumID3>> albumList = new MutableLiveData<>(new ArrayList<>());
private int page = 0;
public AlbumCatalogueViewModel(@NonNull Application application) {
super(application);
}
public LiveData<List<AlbumID3>> getAlbumList() {
return albumList;
}
public void loadAlbums(int size) {
retrieveAlbums(new MediaCallback() {
@Override
public void onError(Exception exception) {
}
@Override
public void onLoadMedia(List<?> media) {
List<AlbumID3> liveAlbum = albumList.getValue();
if (liveAlbum == null) liveAlbum = new ArrayList<>();
liveAlbum.addAll((List<AlbumID3>) media);
albumList.setValue(liveAlbum);
if (media.size() == size) {
loadAlbums(size);
}
}
}, size, size * page++);
}
private void retrieveAlbums(MediaCallback callback, int size, int offset) {
App.getSubsonicClientInstance(false)
.getAlbumSongListClient()
.getAlbumList2("alphabeticalByName", size, offset, null, null)
.enqueue(new Callback<ApiResponse>() {
@Override
public void onResponse(@NonNull Call<ApiResponse> call, @NonNull retrofit2.Response<ApiResponse> response) {
if (response.isSuccessful() && response.body() != null && response.body().getSubsonicResponse().getAlbumList2() != null) {
List<AlbumID3> albumList = new ArrayList<>();
albumList.addAll(response.body().getSubsonicResponse().getAlbumList2().getAlbums());
callback.onLoadMedia(albumList);
}
}
@Override
public void onFailure(@NonNull Call<ApiResponse> call, @NonNull Throwable t) {
callback.onError(new Exception(t.getMessage()));
}
});
}
}

View file

@ -0,0 +1,65 @@
package com.cappielloantonio.tempo.viewmodel;
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.tempo.repository.AlbumRepository;
import com.cappielloantonio.tempo.repository.DownloadRepository;
import com.cappielloantonio.tempo.subsonic.models.AlbumID3;
import com.cappielloantonio.tempo.subsonic.models.ArtistID3;
import com.cappielloantonio.tempo.util.Constants;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Comparator;
import java.util.List;
public class AlbumListPageViewModel extends AndroidViewModel {
private final AlbumRepository albumRepository;
private final DownloadRepository downloadRepository;
public String title;
public ArtistID3 artist;
private MutableLiveData<List<AlbumID3>> albumList;
public AlbumListPageViewModel(@NonNull Application application) {
super(application);
albumRepository = new AlbumRepository();
downloadRepository = new DownloadRepository();
}
public LiveData<List<AlbumID3>> getAlbumList(LifecycleOwner owner) {
albumList = new MutableLiveData<>(new ArrayList<>());
switch (title) {
case Constants.ALBUM_RECENTLY_PLAYED:
albumRepository.getAlbums("recent", 500, null, null).observe(owner, albums -> albumList.setValue(albums));
break;
case Constants.ALBUM_MOST_PLAYED:
albumRepository.getAlbums("frequent", 500, null, null).observe(owner, albums -> albumList.setValue(albums));
break;
case Constants.ALBUM_RECENTLY_ADDED:
albumRepository.getAlbums("newest", 500, null, null).observe(owner, albums -> albumList.setValue(albums));
break;
case Constants.ALBUM_STARRED:
albumList = albumRepository.getStarredAlbums(false, -1);
break;
case Constants.ALBUM_NEW_RELEASES:
int currentYear = Calendar.getInstance().get(Calendar.YEAR);
albumRepository.getAlbums("byYear", 500, currentYear, currentYear).observe(owner, albums -> {
albums.sort(Comparator.comparing(AlbumID3::getCreated).reversed());
albumList.postValue(albums.subList(0, Math.min(20, albums.size())));
});
break;
}
return albumList;
}
}

View file

@ -0,0 +1,45 @@
package com.cappielloantonio.tempo.viewmodel;
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import com.cappielloantonio.tempo.repository.AlbumRepository;
import com.cappielloantonio.tempo.repository.ArtistRepository;
import com.cappielloantonio.tempo.subsonic.models.AlbumID3;
import com.cappielloantonio.tempo.subsonic.models.ArtistID3;
import com.cappielloantonio.tempo.subsonic.models.Child;
import java.util.List;
public class AlbumPageViewModel extends AndroidViewModel {
private final AlbumRepository albumRepository;
private final ArtistRepository artistRepository;
private AlbumID3 album;
public AlbumPageViewModel(@NonNull Application application) {
super(application);
albumRepository = new AlbumRepository();
artistRepository = new ArtistRepository();
}
public LiveData<List<Child>> getAlbumSongLiveList() {
return albumRepository.getAlbumTracks(album.getId());
}
public AlbumID3 getAlbum() {
return album;
}
public void setAlbum(AlbumID3 album) {
this.album = album;
}
public LiveData<ArtistID3> getArtist() {
return artistRepository.getArtistInfo(album.getArtistId());
}
}

View file

@ -0,0 +1,41 @@
package com.cappielloantonio.tempo.viewmodel;
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import com.cappielloantonio.tempo.repository.AlbumRepository;
import com.cappielloantonio.tempo.subsonic.models.ArtistID3;
import java.util.Date;
public class ArtistBottomSheetViewModel extends AndroidViewModel {
private final AlbumRepository albumRepository;
private ArtistID3 artist;
public ArtistBottomSheetViewModel(@NonNull Application application) {
super(application);
albumRepository = new AlbumRepository();
}
public ArtistID3 getArtist() {
return artist;
}
public void setArtist(ArtistID3 artist) {
this.artist = artist;
}
public void setFavorite() {
if (artist.getStarred() != null) {
albumRepository.unstar(artist.getId());
artist.setStarred(null);
} else {
albumRepository.star(artist.getId());
artist.setStarred(new Date());
}
}
}

View file

@ -0,0 +1,56 @@
package com.cappielloantonio.tempo.viewmodel;
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import com.cappielloantonio.tempo.App;
import com.cappielloantonio.tempo.subsonic.base.ApiResponse;
import com.cappielloantonio.tempo.subsonic.models.ArtistID3;
import com.cappielloantonio.tempo.subsonic.models.IndexID3;
import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
public class ArtistCatalogueViewModel extends AndroidViewModel {
private final MutableLiveData<List<ArtistID3>> artistList = new MutableLiveData<>(new ArrayList<>());
public ArtistCatalogueViewModel(@NonNull Application application) {
super(application);
}
public LiveData<List<ArtistID3>> getArtistList() {
return artistList;
}
public void loadArtists() {
App.getSubsonicClientInstance(false)
.getBrowsingClient()
.getArtists()
.enqueue(new Callback<ApiResponse>() {
@Override
public void onResponse(@NonNull Call<ApiResponse> call, @NonNull retrofit2.Response<ApiResponse> response) {
if (response.isSuccessful() && response.body() != null && response.body().getSubsonicResponse().getArtists() != null) {
List<ArtistID3> artists = new ArrayList<>();
for (IndexID3 index : response.body().getSubsonicResponse().getArtists().getIndices()) {
artists.addAll(index.getArtists());
}
artistList.setValue(artists);
}
}
@Override
public void onFailure(@NonNull Call<ApiResponse> call, @NonNull Throwable t) {
}
});
}
}

View file

@ -0,0 +1,61 @@
package com.cappielloantonio.tempo.viewmodel;
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.tempo.model.Download;
import com.cappielloantonio.tempo.repository.ArtistRepository;
import com.cappielloantonio.tempo.repository.DownloadRepository;
import com.cappielloantonio.tempo.subsonic.models.ArtistID3;
import com.cappielloantonio.tempo.util.Constants;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;
public class ArtistListPageViewModel extends AndroidViewModel {
private final ArtistRepository artistRepository;
private final DownloadRepository downloadRepository;
public String title;
private MutableLiveData<List<ArtistID3>> artistList;
public ArtistListPageViewModel(@NonNull Application application) {
super(application);
artistRepository = new ArtistRepository();
downloadRepository = new DownloadRepository();
}
public LiveData<List<ArtistID3>> getArtistList(LifecycleOwner owner) {
artistList = new MutableLiveData<>(new ArrayList<>());
switch (title) {
case Constants.ARTIST_STARRED:
artistList = artistRepository.getStarredArtists(false, -1);
break;
case Constants.ARTIST_DOWNLOADED:
downloadRepository.getLiveDownload().observe(owner, downloads -> {
List<Download> unique = downloads
.stream()
.collect(Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Download::getArtist))), ArrayList::new)
);
// TODO
// artistList.setValue(MappingUtil.mapDownloadToArtist(unique));
});
break;
}
return artistList;
}
}

View file

@ -0,0 +1,58 @@
package com.cappielloantonio.tempo.viewmodel;
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import com.cappielloantonio.tempo.repository.AlbumRepository;
import com.cappielloantonio.tempo.repository.ArtistRepository;
import com.cappielloantonio.tempo.subsonic.models.AlbumID3;
import com.cappielloantonio.tempo.subsonic.models.ArtistID3;
import com.cappielloantonio.tempo.subsonic.models.ArtistInfo2;
import com.cappielloantonio.tempo.subsonic.models.Child;
import java.util.List;
public class ArtistPageViewModel extends AndroidViewModel {
private final AlbumRepository albumRepository;
private final ArtistRepository artistRepository;
private ArtistID3 artist;
public ArtistPageViewModel(@NonNull Application application) {
super(application);
albumRepository = new AlbumRepository();
artistRepository = new ArtistRepository();
}
public LiveData<List<AlbumID3>> getAlbumList() {
return albumRepository.getArtistAlbums(artist.getId());
}
public LiveData<ArtistInfo2> getArtistInfo(String id) {
return artistRepository.getArtistFullInfo(id);
}
public LiveData<List<Child>> getArtistTopSongList() {
return artistRepository.getTopSongs(artist.getName(), 20);
}
public LiveData<List<Child>> getArtistShuffleList() {
return artistRepository.getRandomSong(artist, 50);
}
public LiveData<List<Child>> getArtistInstantMix() {
return artistRepository.getInstantMix(artist, 20);
}
public ArtistID3 getArtist() {
return artist;
}
public void setArtist(ArtistID3 artist) {
this.artist = artist;
}
}

View file

@ -0,0 +1,47 @@
package com.cappielloantonio.tempo.viewmodel;
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.tempo.repository.DirectoryRepository;
import com.cappielloantonio.tempo.subsonic.models.Directory;
public class DirectoryViewModel extends AndroidViewModel {
private final DirectoryRepository directoryRepository;
private MutableLiveData<String> id = new MutableLiveData<>(null);
private MutableLiveData<String> name = new MutableLiveData<>(null);
private MutableLiveData<Directory> directory = new MutableLiveData<>(null);
public DirectoryViewModel(@NonNull Application application) {
super(application);
directoryRepository = new DirectoryRepository();
}
public LiveData<Directory> getDirectory() {
return directory;
}
public void setMusicDirectoryId(String id) {
this.id.setValue(id);
}
public void setMusicDirectoryName(String name) {
this.name.setValue(name);
}
public void loadMusicDirectory(LifecycleOwner owner) {
this.id.observe(owner, id -> directoryRepository.getMusicDirectory(id).observe(owner, directory -> this.directory.setValue(directory)));
}
public void goBack() {
this.id.setValue(this.directory.getValue().getParentId());
}
}

View file

@ -0,0 +1,34 @@
package com.cappielloantonio.tempo.viewmodel;
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.tempo.repository.DownloadRepository;
import com.cappielloantonio.tempo.subsonic.models.Child;
import java.util.List;
import java.util.stream.Collectors;
public class DownloadViewModel extends AndroidViewModel {
private static final String TAG = "HomeViewModel";
private final DownloadRepository downloadRepository;
private final MutableLiveData<List<Child>> downloadedTrackSample = new MutableLiveData<>(null);
public DownloadViewModel(@NonNull Application application) {
super(application);
downloadRepository = new DownloadRepository();
}
public LiveData<List<Child>> getDownloadedTracks(LifecycleOwner owner) {
downloadRepository.getLiveDownload().observe(owner, downloads -> downloadedTrackSample.postValue(downloads.stream().map(download -> (Child) download).collect(Collectors.toList())));
return downloadedTrackSample;
}
}

View file

@ -0,0 +1,48 @@
package com.cappielloantonio.tempo.viewmodel;
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import com.cappielloantonio.tempo.repository.GenreRepository;
import com.cappielloantonio.tempo.subsonic.models.Genre;
import java.util.ArrayList;
import java.util.List;
public class FilterViewModel extends AndroidViewModel {
private final GenreRepository genreRepository;
private final ArrayList<String> selectedFiltersID = new ArrayList<>();
private final ArrayList<String> selectedFilters = new ArrayList<>();
public FilterViewModel(@NonNull Application application) {
super(application);
genreRepository = new GenreRepository();
}
public LiveData<List<Genre>> getGenreList() {
return genreRepository.getGenres(false, -1);
}
public void addFilter(String filterID, String filterName) {
selectedFiltersID.add(filterID);
selectedFilters.add(filterName);
}
public void removeFilter(String filterID, String filterName) {
selectedFiltersID.remove(filterID);
selectedFilters.remove(filterName);
}
public ArrayList<String> getFilters() {
return selectedFiltersID;
}
public ArrayList<String> getFilterNames() {
return selectedFilters;
}
}

View file

@ -0,0 +1,26 @@
package com.cappielloantonio.tempo.viewmodel;
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import com.cappielloantonio.tempo.repository.GenreRepository;
import com.cappielloantonio.tempo.subsonic.models.Genre;
import java.util.List;
public class GenreCatalogueViewModel extends AndroidViewModel {
private final GenreRepository genreRepository;
public GenreCatalogueViewModel(@NonNull Application application) {
super(application);
genreRepository = new GenreRepository();
}
public LiveData<List<Genre>> getGenreList() {
return genreRepository.getGenres(false, -1);
}
}

View file

@ -0,0 +1,240 @@
package com.cappielloantonio.tempo.viewmodel;
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.tempo.model.Chronology;
import com.cappielloantonio.tempo.repository.AlbumRepository;
import com.cappielloantonio.tempo.repository.ArtistRepository;
import com.cappielloantonio.tempo.repository.ChronologyRepository;
import com.cappielloantonio.tempo.repository.PodcastRepository;
import com.cappielloantonio.tempo.repository.SongRepository;
import com.cappielloantonio.tempo.subsonic.models.AlbumID3;
import com.cappielloantonio.tempo.subsonic.models.ArtistID3;
import com.cappielloantonio.tempo.subsonic.models.Child;
import com.cappielloantonio.tempo.util.Preferences;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class HomeViewModel extends AndroidViewModel {
private static final String TAG = "HomeViewModel";
private final SongRepository songRepository;
private final AlbumRepository albumRepository;
private final ArtistRepository artistRepository;
private final PodcastRepository podcastRepository;
private final ChronologyRepository chronologyRepository;
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<AlbumID3>> recentlyAddedAlbumSample = new MutableLiveData<>(null);
private final MutableLiveData<List<Chronology>> thisGridTopSong = 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);
songRepository = new SongRepository();
albumRepository = new AlbumRepository();
artistRepository = new ArtistRepository();
podcastRepository = new PodcastRepository();
chronologyRepository = new ChronologyRepository();
}
public LiveData<List<Child>> getDiscoverSongSample(LifecycleOwner owner) {
if (dicoverSongSample.getValue() == null) {
songRepository.getRandomSample(10, null, null).observe(owner, dicoverSongSample::postValue);
}
return dicoverSongSample;
}
public LiveData<List<Chronology>> getGridSongSample(LifecycleOwner owner) {
String server = Preferences.getServerId();
chronologyRepository.getLastWeek(server).observe(owner, thisGridTopSong::postValue);
return thisGridTopSong;
}
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 -> {
if (albums != null) {
albums.sort(Comparator.comparing(AlbumID3::getCreated).reversed());
newReleasedAlbum.postValue(albums.subList(0, Math.min(20, albums.size())));
}
});
}
return newReleasedAlbum;
}
public LiveData<List<Child>> getStarredTracksSample(LifecycleOwner owner) {
if (starredTracksSample.getValue() == null) {
songRepository.getStarredSongs(true, 10).observe(owner, starredTracksSample::postValue);
}
return starredTracksSample;
}
public LiveData<List<ArtistID3>> getStarredArtistsSample(LifecycleOwner owner) {
if (starredArtistsSample.getValue() == null) {
artistRepository.getStarredArtists(true, 10).observe(owner, starredArtistsSample::postValue);
}
return starredArtistsSample;
}
public LiveData<List<ArtistID3>> getBestOfArtists(LifecycleOwner owner) {
if (bestOfArtists.getValue() == null) {
artistRepository.getStarredArtists(true, 20).observe(owner, bestOfArtists::postValue);
}
return bestOfArtists;
}
public LiveData<List<Child>> getStarredTracks(LifecycleOwner owner) {
if (starredTracks.getValue() == null) {
songRepository.getStarredSongs(true, 20).observe(owner, starredTracks::postValue);
}
return starredTracks;
}
public LiveData<List<AlbumID3>> getStarredAlbums(LifecycleOwner owner) {
if (starredAlbums.getValue() == null) {
albumRepository.getStarredAlbums(true, 20).observe(owner, starredAlbums::postValue);
}
return starredAlbums;
}
public LiveData<List<ArtistID3>> getStarredArtists(LifecycleOwner owner) {
if (starredArtists.getValue() == null) {
artistRepository.getStarredArtists(true, 20).observe(owner, starredArtists::postValue);
}
return starredArtists;
}
public LiveData<List<Integer>> getYearList(LifecycleOwner owner) {
if (years.getValue() == null) {
albumRepository.getDecades().observe(owner, years::postValue);
}
return years;
}
public LiveData<List<AlbumID3>> getMostPlayedAlbums(LifecycleOwner owner) {
if (mostPlayedAlbumSample.getValue() == null) {
albumRepository.getAlbums("frequent", 20, null, null).observe(owner, mostPlayedAlbumSample::postValue);
}
return mostPlayedAlbumSample;
}
public LiveData<List<AlbumID3>> getMostRecentlyAddedAlbums(LifecycleOwner owner) {
if (recentlyAddedAlbumSample.getValue() == null) {
albumRepository.getAlbums("newest", 20, null, null).observe(owner, recentlyAddedAlbumSample::postValue);
}
return recentlyAddedAlbumSample;
}
public LiveData<List<AlbumID3>> getRecentlyPlayedAlbumList(LifecycleOwner owner) {
if (recentlyPlayedAlbumSample.getValue() == null) {
albumRepository.getAlbums("recent", 20, null, null).observe(owner, recentlyPlayedAlbumSample::postValue);
}
return recentlyPlayedAlbumSample;
}
public LiveData<List<Child>> getMediaInstantMix(LifecycleOwner owner, Child media) {
mediaInstantMix.setValue(Collections.emptyList());
songRepository.getInstantMix(media, 20).observe(owner, mediaInstantMix::postValue);
return mediaInstantMix;
}
public LiveData<List<Child>> getArtistInstantMix(LifecycleOwner owner, ArtistID3 artist) {
artistInstantMix.setValue(Collections.emptyList());
artistRepository.getTopSongs(artist.getName(), 10).observe(owner, artistInstantMix::postValue);
return artistInstantMix;
}
public LiveData<List<Child>> getArtistBestOf(LifecycleOwner owner, ArtistID3 artist) {
artistBestOf.setValue(Collections.emptyList());
artistRepository.getTopSongs(artist.getName(), 10).observe(owner, artistBestOf::postValue);
return artistBestOf;
}
public LiveData<List<Child>> getAllStarredTracks() {
return songRepository.getStarredSongs(false, -1);
}
public void refreshDiscoverySongSample(LifecycleOwner owner) {
songRepository.getRandomSample(10, null, null).observe(owner, dicoverSongSample::postValue);
}
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 refreshBestOfArtist(LifecycleOwner owner) {
artistRepository.getStarredArtists(true, 20).observe(owner, bestOfArtists::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);
}
}

View file

@ -0,0 +1,37 @@
package com.cappielloantonio.tempo.viewmodel;
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;
import com.cappielloantonio.tempo.repository.DirectoryRepository;
import com.cappielloantonio.tempo.subsonic.models.Indexes;
import com.cappielloantonio.tempo.subsonic.models.MusicFolder;
public class IndexViewModel extends AndroidViewModel {
private final DirectoryRepository directoryRepository;
private MusicFolder musicFolder;
private MutableLiveData<Indexes> indexes = new MutableLiveData<>(null);
public IndexViewModel(@NonNull Application application) {
super(application);
directoryRepository = new DirectoryRepository();
}
public MutableLiveData<Indexes> getIndexes() {
return directoryRepository.getIndexes(null, null);
}
public String getMusicFolderName() {
return musicFolder != null ? musicFolder.getName() : "";
}
public void setMusicFolder(MusicFolder musicFolder) {
this.musicFolder = musicFolder;
}
}

View file

@ -0,0 +1,114 @@
package com.cappielloantonio.tempo.viewmodel;
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.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;
import java.util.List;
public class LibraryViewModel extends AndroidViewModel {
private static final String TAG = "LibraryViewModel";
private final DirectoryRepository directoryRepository;
private final AlbumRepository albumRepository;
private final ArtistRepository artistRepository;
private final GenreRepository genreRepository;
private final PlaylistRepository playlistRepository;
private final MutableLiveData<List<MusicFolder>> musicFolders = new MutableLiveData<>(null);
private final MutableLiveData<Indexes> indexes = new MutableLiveData<>(null);
private final MutableLiveData<List<Playlist>> playlistSample = 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) {
super(application);
directoryRepository = new DirectoryRepository();
albumRepository = new AlbumRepository();
artistRepository = new ArtistRepository();
genreRepository = new GenreRepository();
playlistRepository = new PlaylistRepository();
}
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;
}
public LiveData<List<AlbumID3>> getAlbumSample(LifecycleOwner owner) {
if (sampleAlbum.getValue() == null) {
albumRepository.getAlbums("random", 10, null, null).observe(owner, sampleAlbum::postValue);
}
return sampleAlbum;
}
public LiveData<List<ArtistID3>> getArtistSample(LifecycleOwner owner) {
if (sampleArtist.getValue() == null) {
artistRepository.getArtists(true, 10).observe(owner, sampleArtist::postValue);
}
return sampleArtist;
}
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(LifecycleOwner owner) {
if (playlistSample.getValue() == null) {
playlistRepository.getPlaylists(true, 10).observe(owner, playlistSample::postValue);
}
return playlistSample;
}
public void refreshAlbumSample(LifecycleOwner owner) {
albumRepository.getAlbums("random", 10, null, null).observe(owner, sampleAlbum::postValue);
}
public void refreshArtistSample(LifecycleOwner owner) {
artistRepository.getArtists(true, 10).observe(owner, sampleArtist::postValue);
}
public void refreshGenreSample(LifecycleOwner owner) {
genreRepository.getGenres(true, 15).observe(owner, sampleGenres::postValue);
}
public void refreshPlaylistSample(LifecycleOwner owner) {
playlistRepository.getPlaylists(true, 10).observe(owner, playlistSample::postValue);
}
}

View file

@ -0,0 +1,48 @@
package com.cappielloantonio.tempo.viewmodel;
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import com.cappielloantonio.tempo.model.Server;
import com.cappielloantonio.tempo.repository.ServerRepository;
import java.util.List;
public class LoginViewModel extends AndroidViewModel {
private final ServerRepository serverRepository;
private Server toEdit = null;
public LoginViewModel(@NonNull Application application) {
super(application);
serverRepository = new ServerRepository();
}
public LiveData<List<Server>> getServerList() {
return serverRepository.getLiveServer();
}
public void addServer(Server server) {
serverRepository.insert(server);
}
public void deleteServer(Server server) {
if (server != null) {
serverRepository.delete(server);
} else if (toEdit != null) {
serverRepository.delete(toEdit);
}
}
public void setServerToEdit(Server server) {
toEdit = server;
}
public Server getServerToEdit() {
return toEdit;
}
}

View file

@ -0,0 +1,34 @@
package com.cappielloantonio.tempo.viewmodel;
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import com.cappielloantonio.tempo.repository.QueueRepository;
import com.cappielloantonio.tempo.repository.SystemRepository;
public class MainViewModel extends AndroidViewModel {
private static final String TAG = "SearchViewModel";
private final SystemRepository systemRepository;
private final Application application;
public MainViewModel(@NonNull Application application) {
super(application);
this.application = application;
systemRepository = new SystemRepository();
}
public boolean isQueueLoaded() {
QueueRepository queueRepository = new QueueRepository();
return queueRepository.count() != 0;
}
public LiveData<Boolean> ping() {
return systemRepository.ping();
}
}

View file

@ -0,0 +1,148 @@
package com.cappielloantonio.tempo.viewmodel;
import android.app.Application;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.OptIn;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.media3.common.util.UnstableApi;
import com.cappielloantonio.tempo.model.Download;
import com.cappielloantonio.tempo.model.Queue;
import com.cappielloantonio.tempo.repository.ArtistRepository;
import com.cappielloantonio.tempo.repository.QueueRepository;
import com.cappielloantonio.tempo.repository.SongRepository;
import com.cappielloantonio.tempo.subsonic.models.ArtistID3;
import com.cappielloantonio.tempo.subsonic.models.Child;
import com.cappielloantonio.tempo.subsonic.models.PlayQueue;
import com.cappielloantonio.tempo.util.Constants;
import com.cappielloantonio.tempo.util.DownloadUtil;
import com.cappielloantonio.tempo.util.MappingUtil;
import com.cappielloantonio.tempo.util.Preferences;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
@OptIn(markerClass = UnstableApi.class)
public class PlayerBottomSheetViewModel extends AndroidViewModel {
private static final String TAG = "PlayerBottomSheetViewModel";
private final SongRepository songRepository;
private final ArtistRepository artistRepository;
private final QueueRepository queueRepository;
private final MutableLiveData<String> lyricsLiveData = 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) {
super(application);
songRepository = new SongRepository();
artistRepository = new ArtistRepository();
queueRepository = new QueueRepository();
}
public LiveData<List<Queue>> getQueueSong() {
return queueRepository.getLiveQueue();
}
public void setFavorite(Context context, Child media) {
if (media != null) {
if (media.getStarred() != null) {
songRepository.unstar(media.getId());
media.setStarred(null);
} else {
songRepository.star(media.getId());
media.setStarred(new Date());
if (Preferences.isStarredSyncEnabled()) {
DownloadUtil.getDownloadTracker(context).download(
MappingUtil.mapDownload(media),
new Download(media)
);
}
}
}
}
public LiveData<String> getLiveLyrics() {
return lyricsLiveData;
}
public void refreshMediaInfo(LifecycleOwner owner, Child media) {
songRepository.getSongLyrics(media).observe(owner, lyricsLiveData::postValue);
}
public LiveData<Child> getLiveMedia() {
return liveMedia;
}
public void setLiveMedia(LifecycleOwner owner, String mediaType, String mediaId) {
if (mediaType != null) {
switch (mediaType) {
case Constants.MEDIA_TYPE_MUSIC:
songRepository.getSong(mediaId).observe(owner, liveMedia::postValue);
break;
case Constants.MEDIA_TYPE_PODCAST:
liveMedia.postValue(null);
break;
}
}
}
public LiveData<ArtistID3> getLiveArtist() {
return liveArtist;
}
public void setLiveArtist(LifecycleOwner owner, String mediaType, String ArtistId) {
if (mediaType != null) {
switch (mediaType) {
case Constants.MEDIA_TYPE_MUSIC:
artistRepository.getArtist(ArtistId).observe(owner, liveArtist::postValue);
break;
case Constants.MEDIA_TYPE_PODCAST:
liveArtist.postValue(null);
break;
}
}
}
public LiveData<List<Child>> getMediaInstantMix(LifecycleOwner owner, Child media) {
instantMix.setValue(Collections.emptyList());
songRepository.getInstantMix(media, 20).observe(owner, instantMix::postValue);
return instantMix;
}
public LiveData<PlayQueue> getPlayQueue() {
return queueRepository.getPlayQueue();
}
public boolean savePlayQueue() {
Child media = getLiveMedia().getValue();
List<Child> queue = queueRepository.getMedia();
List<String> ids = queue.stream().map(Child::getId).collect(Collectors.toList());
if (media != null) {
queueRepository.savePlayQueue(ids, media.getId(), 0);
return true;
}
return false;
}
public void emptyPlayQueue() {
queueRepository.savePlayQueue(null, null, 0);
}
}

View file

@ -0,0 +1,44 @@
package com.cappielloantonio.tempo.viewmodel;
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.tempo.repository.PlaylistRepository;
import com.cappielloantonio.tempo.subsonic.models.Playlist;
import java.util.List;
public class PlaylistCatalogueViewModel extends AndroidViewModel {
private final PlaylistRepository playlistRepository;
private String type;
private final MutableLiveData<List<Playlist>> playlistList = new MutableLiveData<>(null);
public PlaylistCatalogueViewModel(@NonNull Application application) {
super(application);
playlistRepository = new PlaylistRepository();
}
public LiveData<List<Playlist>> getPlaylistList(LifecycleOwner owner) {
if (playlistList.getValue() == null) {
playlistRepository.getPlaylists(false, -1).observe(owner, playlistList::postValue);
}
return playlistList;
}
public void setType(String type) {
this.type = type;
}
public String getType() {
return type;
}
}

View file

@ -0,0 +1,47 @@
package com.cappielloantonio.tempo.viewmodel;
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.tempo.repository.PlaylistRepository;
import com.cappielloantonio.tempo.subsonic.models.Child;
import com.cappielloantonio.tempo.subsonic.models.Playlist;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class PlaylistChooserViewModel extends AndroidViewModel {
private final PlaylistRepository playlistRepository;
private final MutableLiveData<List<Playlist>> playlists = new MutableLiveData<>(null);
private Child toAdd;
public PlaylistChooserViewModel(@NonNull Application application) {
super(application);
playlistRepository = new PlaylistRepository();
}
public LiveData<List<Playlist>> getPlaylistList(LifecycleOwner owner) {
playlistRepository.getPlaylists(false, -1).observe(owner, playlists::postValue);
return playlists;
}
public void addSongToPlaylist(String playlistId) {
playlistRepository.addSongToPlaylist(playlistId, new ArrayList(Collections.singletonList(toAdd.getId())));
}
public void setSongToAdd(Child song) {
toAdd = song;
}
public Child getSongToAdd() {
return toAdd;
}
}

View file

@ -0,0 +1,96 @@
package com.cappielloantonio.tempo.viewmodel;
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import com.cappielloantonio.tempo.repository.PlaylistRepository;
import com.cappielloantonio.tempo.subsonic.models.Child;
import com.cappielloantonio.tempo.subsonic.models.Playlist;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
public class PlaylistEditorViewModel extends AndroidViewModel {
private static final String TAG = "PlaylistEditorViewModel";
private final PlaylistRepository playlistRepository;
private Child toAdd;
private Playlist toEdit;
private MutableLiveData<List<Child>> songLiveList = new MutableLiveData<>();
public PlaylistEditorViewModel(@NonNull Application application) {
super(application);
playlistRepository = new PlaylistRepository();
}
public void createPlaylist(String name) {
playlistRepository.createPlaylist(null, name, new ArrayList(Collections.singletonList(toAdd.getId())));
}
public void updatePlaylist(String name) {
playlistRepository.deletePlaylist(toEdit.getId());
playlistRepository.createPlaylist(toEdit.getId(), name, getPlaylistSongIds());
}
public void deletePlaylist() {
if (toEdit != null) playlistRepository.deletePlaylist(toEdit.getId());
}
public Child getSongToAdd() {
return toAdd;
}
public void setSongToAdd(Child song) {
this.toAdd = song;
}
public Playlist getPlaylistToEdit() {
return toEdit;
}
public void setPlaylistToEdit(Playlist playlist) {
this.toEdit = playlist;
if (playlist != null) {
this.songLiveList = playlistRepository.getPlaylistSongs(toEdit.getId());
} else {
this.songLiveList = new MutableLiveData<>();
}
}
public LiveData<List<Child>> getPlaylistSongLiveList() {
return songLiveList;
}
public void removeFromPlaylistSongLiveList(int position) {
List<Child> songs = songLiveList.getValue();
Objects.requireNonNull(songs).remove(position);
songLiveList.postValue(songs);
}
public void orderPlaylistSongLiveListAfterSwap(List<Child> songs) {
songLiveList.postValue(songs);
}
private ArrayList<String> getPlaylistSongIds() {
List<Child> songs = songLiveList.getValue();
ArrayList<String> ids = new ArrayList<>();
if (songs != null && !songs.isEmpty()) {
for (Child song : songs) {
ids.add(song.getId());
}
}
return ids;
}
}

View file

@ -0,0 +1,38 @@
package com.cappielloantonio.tempo.viewmodel;
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import com.cappielloantonio.tempo.repository.PlaylistRepository;
import com.cappielloantonio.tempo.subsonic.models.Child;
import com.cappielloantonio.tempo.subsonic.models.Playlist;
import java.util.List;
public class PlaylistPageViewModel extends AndroidViewModel {
private final PlaylistRepository playlistRepository;
private Playlist playlist;
private boolean isOffline;
public PlaylistPageViewModel(@NonNull Application application) {
super(application);
playlistRepository = new PlaylistRepository();
}
public LiveData<List<Child>> getPlaylistSongLiveList() {
return playlistRepository.getPlaylistSongs(playlist.getId());
}
public Playlist getPlaylist() {
return playlist;
}
public void setPlaylist(Playlist playlist) {
this.playlist = playlist;
}
}

View file

@ -0,0 +1,33 @@
package com.cappielloantonio.tempo.viewmodel;
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import com.cappielloantonio.tempo.repository.PodcastRepository;
import com.cappielloantonio.tempo.subsonic.models.PodcastChannel;
public class PodcastChannelBottomSheetViewModel extends AndroidViewModel {
private final PodcastRepository podcastRepository;
private PodcastChannel podcastChannel;
public PodcastChannelBottomSheetViewModel(@NonNull Application application) {
super(application);
podcastRepository = new PodcastRepository();
}
public PodcastChannel getPodcastChannel() {
return podcastChannel;
}
public void setPodcastChannel(PodcastChannel podcastChannel) {
this.podcastChannel = podcastChannel;
}
public void deletePodcastChannel() {
if (podcastChannel != null) podcastRepository.deletePodcastChannel(podcastChannel.getId());
}
}

View file

@ -0,0 +1,35 @@
package com.cappielloantonio.tempo.viewmodel;
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.tempo.repository.PodcastRepository;
import com.cappielloantonio.tempo.subsonic.models.PodcastChannel;
import java.util.List;
public class PodcastChannelCatalogueViewModel extends AndroidViewModel {
private final PodcastRepository podcastRepository;
private final MutableLiveData<List<PodcastChannel>> podcastChannels = new MutableLiveData<>(null);
public PodcastChannelCatalogueViewModel(@NonNull Application application) {
super(application);
podcastRepository = new PodcastRepository();
}
public LiveData<List<PodcastChannel>> getPodcastChannels(LifecycleOwner owner) {
if (podcastChannels.getValue() == null) {
podcastRepository.getPodcastChannels(false, null).observe(owner, podcastChannels::postValue);
}
return podcastChannels;
}
}

View file

@ -0,0 +1,27 @@
package com.cappielloantonio.tempo.viewmodel;
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import com.cappielloantonio.tempo.repository.PodcastRepository;
import com.cappielloantonio.tempo.subsonic.models.InternetRadioStation;
public class PodcastChannelEditorViewModel extends AndroidViewModel {
private static final String TAG = "RadioEditorViewModel";
private final PodcastRepository podcastRepository;
private InternetRadioStation toEdit;
public PodcastChannelEditorViewModel(@NonNull Application application) {
super(application);
podcastRepository = new PodcastRepository();
}
public void createChannel(String url) {
podcastRepository.createPodcastChannel(url);
}
}

View file

@ -0,0 +1,36 @@
package com.cappielloantonio.tempo.viewmodel;
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import com.cappielloantonio.tempo.repository.PodcastRepository;
import com.cappielloantonio.tempo.subsonic.models.PodcastChannel;
import java.util.List;
public class PodcastChannelPageViewModel extends AndroidViewModel {
private final PodcastRepository podcastRepository;
private PodcastChannel podcastChannel;
public PodcastChannelPageViewModel(@NonNull Application application) {
super(application);
podcastRepository = new PodcastRepository();
}
public LiveData<List<PodcastChannel>> getPodcastChannelEpisodes() {
return podcastRepository.getPodcastChannels(true, podcastChannel.getId());
}
public PodcastChannel getPodcastChannel() {
return podcastChannel;
}
public void setPodcastChannel(PodcastChannel podcastChannel) {
this.podcastChannel = podcastChannel;
}
}

View file

@ -0,0 +1,33 @@
package com.cappielloantonio.tempo.viewmodel;
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import com.cappielloantonio.tempo.repository.PodcastRepository;
import com.cappielloantonio.tempo.subsonic.models.PodcastEpisode;
public class PodcastEpisodeBottomSheetViewModel extends AndroidViewModel {
private final PodcastRepository podcastRepository;
private PodcastEpisode podcastEpisode;
public PodcastEpisodeBottomSheetViewModel(@NonNull Application application) {
super(application);
podcastRepository = new PodcastRepository();
}
public PodcastEpisode getPodcastEpisode() {
return podcastEpisode;
}
public void setPodcastEpisode(PodcastEpisode podcast) {
this.podcastEpisode = podcast;
}
public void deletePodcastEpisode() {
if (podcastEpisode != null) podcastRepository.deletePodcastEpisode(podcastEpisode.getId());
}
}

View file

@ -0,0 +1,52 @@
package com.cappielloantonio.tempo.viewmodel;
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.tempo.repository.PodcastRepository;
import com.cappielloantonio.tempo.subsonic.models.PodcastChannel;
import com.cappielloantonio.tempo.subsonic.models.PodcastEpisode;
import java.util.List;
public class PodcastViewModel extends AndroidViewModel {
private final PodcastRepository podcastRepository;
private final MutableLiveData<List<PodcastEpisode>> newestPodcastEpisodes = new MutableLiveData<>(null);
private final MutableLiveData<List<PodcastChannel>> podcastChannels = new MutableLiveData<>(null);
public PodcastViewModel(@NonNull Application application) {
super(application);
podcastRepository = new PodcastRepository();
}
public LiveData<List<PodcastEpisode>> getNewestPodcastEpisodes(LifecycleOwner owner) {
if (newestPodcastEpisodes.getValue() == null) {
podcastRepository.getNewestPodcastEpisodes(20).observe(owner, newestPodcastEpisodes::postValue);
}
return newestPodcastEpisodes;
}
public LiveData<List<PodcastChannel>> getPodcastChannels(LifecycleOwner owner) {
if (podcastChannels.getValue() == null) {
podcastRepository.getPodcastChannels(false, null).observe(owner, podcastChannels::postValue);
}
return podcastChannels;
}
public void refreshNewestPodcastEpisodes(LifecycleOwner owner) {
podcastRepository.getNewestPodcastEpisodes(20).observe(owner, newestPodcastEpisodes::postValue);
}
public void refreshPodcastChannels(LifecycleOwner owner) {
podcastRepository.getPodcastChannels(false, null).observe(owner, podcastChannels::postValue);
}
}

View file

@ -0,0 +1,43 @@
package com.cappielloantonio.tempo.viewmodel;
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import com.cappielloantonio.tempo.repository.RadioRepository;
import com.cappielloantonio.tempo.subsonic.models.InternetRadioStation;
public class RadioEditorViewModel extends AndroidViewModel {
private static final String TAG = "RadioEditorViewModel";
private final RadioRepository radioRepository;
private InternetRadioStation toEdit;
public RadioEditorViewModel(@NonNull Application application) {
super(application);
radioRepository = new RadioRepository();
}
public InternetRadioStation getRadioToEdit() {
return toEdit;
}
public void setRadioToEdit(InternetRadioStation internetRadioStation) {
this.toEdit = internetRadioStation;
}
public void createRadio(String name, String streamURL, String homepageURL) {
radioRepository.createInternetRadioStation(name, streamURL, homepageURL);
}
public void updateRadio(String name, String streamURL, String homepageURL) {
if (toEdit != null) radioRepository.updateInternetRadioStation(toEdit.getId(), name, streamURL, homepageURL);
}
public void deleteRadio() {
if (toEdit != null) radioRepository.deleteInternetRadioStation(toEdit.getId());
}
}

View file

@ -0,0 +1,35 @@
package com.cappielloantonio.tempo.viewmodel;
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.tempo.repository.RadioRepository;
import com.cappielloantonio.tempo.subsonic.models.InternetRadioStation;
import java.util.List;
public class RadioViewModel extends AndroidViewModel {
private final RadioRepository radioRepository;
private final MutableLiveData<List<InternetRadioStation>> internetRadioStations = new MutableLiveData<>(null);
public RadioViewModel(@NonNull Application application) {
super(application);
radioRepository = new RadioRepository();
}
public LiveData<List<InternetRadioStation>> getInternetRadioStations(LifecycleOwner owner) {
radioRepository.getInternetRadioStations().observe(owner, internetRadioStations::postValue);
return internetRadioStations;
}
public void refreshInternetRadioStations(LifecycleOwner owner) {
radioRepository.getInternetRadioStations().observe(owner, internetRadioStations::postValue);
}
}

View file

@ -0,0 +1,84 @@
package com.cappielloantonio.tempo.viewmodel;
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import com.cappielloantonio.tempo.repository.AlbumRepository;
import com.cappielloantonio.tempo.repository.ArtistRepository;
import com.cappielloantonio.tempo.repository.SongRepository;
import com.cappielloantonio.tempo.subsonic.models.AlbumID3;
import com.cappielloantonio.tempo.subsonic.models.ArtistID3;
import com.cappielloantonio.tempo.subsonic.models.Child;
public class RatingViewModel extends AndroidViewModel {
private final SongRepository songRepository;
private final AlbumRepository albumRepository;
private final ArtistRepository artistRepository;
private Child song;
private AlbumID3 album;
private ArtistID3 artist;
public RatingViewModel(@NonNull Application application) {
super(application);
songRepository = new SongRepository();
albumRepository = new AlbumRepository();
artistRepository = new ArtistRepository();
}
public Child getSong() {
return song;
}
public LiveData<Child> getLiveSong() {
return songRepository.getSong(song.getId());
}
public void setSong(Child song) {
this.song = song;
this.album = null;
this.artist = null;
}
public AlbumID3 getAlbum() {
return album;
}
public LiveData<AlbumID3> getLiveAlbum() {
return albumRepository.getAlbum(album.getId());
}
public void setAlbum(AlbumID3 album) {
this.song = null;
this.album = album;
this.artist = null;
}
public ArtistID3 getArtist() {
return artist;
}
public LiveData<ArtistID3> getLiveArtist() {
return artistRepository.getArtist(artist.getId());
}
public void setArtist(ArtistID3 artist) {
this.song = null;
this.album = null;
this.artist = artist;
}
public void rate(int star) {
if (song != null) {
songRepository.setRating(song.getId(), star);
} else if (album != null) {
albumRepository.setRating(album.getId(), star);
} else if (artist != null) {
artistRepository.setRating(artist.getId(), star);
}
}
}

View file

@ -0,0 +1,63 @@
package com.cappielloantonio.tempo.viewmodel;
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import com.cappielloantonio.tempo.model.RecentSearch;
import com.cappielloantonio.tempo.repository.SearchingRepository;
import com.cappielloantonio.tempo.subsonic.models.SearchResult3;
import java.util.ArrayList;
import java.util.List;
public class SearchViewModel extends AndroidViewModel {
private static final String TAG = "SearchViewModel";
private String query = "";
private final SearchingRepository searchingRepository;
public SearchViewModel(@NonNull Application application) {
super(application);
searchingRepository = new SearchingRepository();
}
public String getQuery() {
return query;
}
public void setQuery(String query) {
this.query = query;
if (!query.isEmpty()) {
insertNewSearch(query);
}
}
public LiveData<SearchResult3> search(String title) {
return searchingRepository.search(title);
}
public void insertNewSearch(String search) {
searchingRepository.insert(new RecentSearch(search));
}
public void deleteRecentSearch(String search) {
searchingRepository.delete(new RecentSearch(search));
}
public LiveData<List<String>> getSearchSuggestion(String query) {
return searchingRepository.getSuggestions(query);
}
public List<String> getRecentSearchSuggestion() {
ArrayList<String> suggestions = new ArrayList<>();
suggestions.addAll(searchingRepository.getRecentSearchSuggestion(5));
return suggestions;
}
}

View file

@ -0,0 +1,49 @@
package com.cappielloantonio.tempo.viewmodel;
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import com.cappielloantonio.tempo.interfaces.ScanCallback;
import com.cappielloantonio.tempo.repository.ScanRepository;
public class SettingViewModel extends AndroidViewModel {
private static final String TAG = "SettingViewModel";
private final ScanRepository scanRepository;
public SettingViewModel(@NonNull Application application) {
super(application);
scanRepository = new ScanRepository();
}
public void launchScan(ScanCallback callback) {
scanRepository.startScan(new ScanCallback() {
@Override
public void onError(Exception exception) {
callback.onError(exception);
}
@Override
public void onSuccess(boolean isScanning, long count) {
callback.onSuccess(isScanning, count);
}
});
}
public void getScanStatus(ScanCallback callback) {
scanRepository.getScanStatus(new ScanCallback() {
@Override
public void onError(Exception exception) {
callback.onError(exception);
}
@Override
public void onSuccess(boolean isScanning, long count) {
callback.onSuccess(isScanning, count);
}
});
}
}

View file

@ -0,0 +1,86 @@
package com.cappielloantonio.tempo.viewmodel;
import android.app.Application;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.media3.common.util.UnstableApi;
import com.cappielloantonio.tempo.model.Download;
import com.cappielloantonio.tempo.repository.AlbumRepository;
import com.cappielloantonio.tempo.repository.ArtistRepository;
import com.cappielloantonio.tempo.repository.SongRepository;
import com.cappielloantonio.tempo.subsonic.models.AlbumID3;
import com.cappielloantonio.tempo.subsonic.models.ArtistID3;
import com.cappielloantonio.tempo.subsonic.models.Child;
import com.cappielloantonio.tempo.util.DownloadUtil;
import com.cappielloantonio.tempo.util.MappingUtil;
import com.cappielloantonio.tempo.util.Preferences;
import java.util.Collections;
import java.util.Date;
import java.util.List;
@UnstableApi
public class SongBottomSheetViewModel extends AndroidViewModel {
private final SongRepository songRepository;
private final AlbumRepository albumRepository;
private final ArtistRepository artistRepository;
private Child song;
private final MutableLiveData<List<Child>> instantMix = new MutableLiveData<>(null);
public SongBottomSheetViewModel(@NonNull Application application) {
super(application);
songRepository = new SongRepository();
albumRepository = new AlbumRepository();
artistRepository = new ArtistRepository();
}
public Child getSong() {
return song;
}
public void setSong(Child song) {
this.song = song;
}
public void setFavorite(Context context) {
if (song.getStarred() != null) {
songRepository.unstar(song.getId());
song.setStarred(null);
} else {
songRepository.star(song.getId());
song.setStarred(new Date());
if (Preferences.isStarredSyncEnabled()) {
DownloadUtil.getDownloadTracker(context).download(
MappingUtil.mapDownload(song),
new Download(song)
);
}
}
}
public LiveData<AlbumID3> getAlbum() {
return albumRepository.getAlbum(song.getAlbumId());
}
public LiveData<ArtistID3> getArtist() {
return artistRepository.getArtist(song.getArtistId());
}
public LiveData<List<Child>> getInstantMix(LifecycleOwner owner, Child media) {
instantMix.setValue(Collections.emptyList());
songRepository.getInstantMix(media, 20).observe(owner, instantMix::postValue);
return instantMix;
}
}

View file

@ -0,0 +1,92 @@
package com.cappielloantonio.tempo.viewmodel;
import android.app.Application;
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.tempo.repository.ArtistRepository;
import com.cappielloantonio.tempo.repository.SongRepository;
import com.cappielloantonio.tempo.subsonic.models.AlbumID3;
import com.cappielloantonio.tempo.subsonic.models.ArtistID3;
import com.cappielloantonio.tempo.subsonic.models.Child;
import com.cappielloantonio.tempo.subsonic.models.Genre;
import com.cappielloantonio.tempo.util.Constants;
import java.util.ArrayList;
import java.util.List;
public class SongListPageViewModel extends AndroidViewModel {
private final SongRepository songRepository;
private final ArtistRepository artistRepository;
public String title;
public String toolbarTitle;
public Genre genre;
public ArtistID3 artist;
public AlbumID3 album;
private MutableLiveData<List<Child>> songList;
public ArrayList<String> filters = new ArrayList<>();
public ArrayList<String> filterNames = new ArrayList<>();
public int year = 0;
public SongListPageViewModel(@NonNull Application application) {
super(application);
songRepository = new SongRepository();
artistRepository = new ArtistRepository();
}
public LiveData<List<Child>> getSongList() {
songList = new MutableLiveData<>(new ArrayList<>());
switch (title) {
case Constants.MEDIA_BY_GENRE:
songList = songRepository.getSongsByGenre(genre.getGenre(), 0);
break;
case Constants.MEDIA_BY_ARTIST:
songList = artistRepository.getTopSongs(artist.getName(), 50);
break;
case Constants.MEDIA_BY_GENRES:
songList = songRepository.getSongsByGenres(filters);
break;
case Constants.MEDIA_BY_YEAR:
songList = songRepository.getRandomSample(500, year, year + 10);
break;
case Constants.MEDIA_STARRED:
songList = songRepository.getStarredSongs(false, -1);
break;
}
return songList;
}
public void getSongsByPage(LifecycleOwner owner) {
switch (title) {
case Constants.MEDIA_BY_GENRE:
int page = (songList.getValue() != null ? songList.getValue().size() : 0) / 100;
songRepository.getSongsByGenre(genre.getGenre(), page).observe(owner, children -> {
List<Child> currentMedia = songList.getValue();
currentMedia.addAll(children);
songList.setValue(currentMedia);
});
break;
case Constants.MEDIA_BY_ARTIST:
case Constants.MEDIA_BY_GENRES:
case Constants.MEDIA_BY_YEAR:
case Constants.MEDIA_STARRED:
break;
}
}
public String getFiltersTitle() {
return TextUtils.join(", ", filterNames);
}
}

View file

@ -0,0 +1,31 @@
package com.cappielloantonio.tempo.viewmodel;
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.tempo.repository.SongRepository;
import com.cappielloantonio.tempo.subsonic.models.Child;
import java.util.List;
public class StarredSyncViewModel extends AndroidViewModel {
private final SongRepository songRepository;
private final MutableLiveData<List<Child>> starredTracks = new MutableLiveData<>(null);
public StarredSyncViewModel(@NonNull Application application) {
super(application);
songRepository = new SongRepository();
}
public LiveData<List<Child>> getStarredTracks(LifecycleOwner owner) {
songRepository.getStarredSongs(false, -1).observe(owner, starredTracks::postValue);
return starredTracks;
}
}