2023-06-17 15:30:23 +02:00
|
|
|
package com.cappielloantonio.tempo.viewmodel;
|
2020-11-20 15:38:08 +01:00
|
|
|
|
2020-11-21 18:41:35 +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-21 18:41:35 +01:00
|
|
|
import androidx.lifecycle.LiveData;
|
2021-07-28 15:28:32 +02:00
|
|
|
import androidx.lifecycle.MutableLiveData;
|
2020-11-20 15:38:08 +01:00
|
|
|
|
2023-08-02 10:53:21 +02:00
|
|
|
import com.cappielloantonio.tempo.interfaces.StarCallback;
|
2023-06-17 15:30:23 +02:00
|
|
|
import com.cappielloantonio.tempo.model.Chronology;
|
2023-08-02 10:53:21 +02:00
|
|
|
import com.cappielloantonio.tempo.model.Favorite;
|
2024-03-23 21:33:11 +01:00
|
|
|
import com.cappielloantonio.tempo.model.HomeSector;
|
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.ChronologyRepository;
|
2023-08-02 10:53:21 +02:00
|
|
|
import com.cappielloantonio.tempo.repository.FavoriteRepository;
|
2024-06-08 18:53:58 +02:00
|
|
|
import com.cappielloantonio.tempo.repository.PlaylistRepository;
|
2023-09-17 16:40:41 +02:00
|
|
|
import com.cappielloantonio.tempo.repository.SharingRepository;
|
2023-06-17 15:30:23 +02:00
|
|
|
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;
|
2024-06-08 18:53:58 +02:00
|
|
|
import com.cappielloantonio.tempo.subsonic.models.Playlist;
|
2023-09-17 16:40:41 +02:00
|
|
|
import com.cappielloantonio.tempo.subsonic.models.Share;
|
2023-06-17 15:30:23 +02:00
|
|
|
import com.cappielloantonio.tempo.util.Preferences;
|
2024-03-23 21:33:11 +01:00
|
|
|
import com.google.common.reflect.TypeToken;
|
|
|
|
|
import com.google.gson.Gson;
|
2020-11-20 15:38:08 +01:00
|
|
|
|
2023-08-02 10:53:21 +02:00
|
|
|
import java.util.ArrayList;
|
2022-01-17 17:32:59 +01:00
|
|
|
import java.util.Calendar;
|
2021-11-26 16:09:49 +01:00
|
|
|
import java.util.Collections;
|
2022-01-17 17:32:59 +01:00
|
|
|
import java.util.Comparator;
|
2023-08-02 10:53:21 +02:00
|
|
|
import java.util.HashMap;
|
2020-11-20 15:38:08 +01:00
|
|
|
import java.util.List;
|
2024-06-08 18:53:58 +02:00
|
|
|
import java.util.stream.Collectors;
|
2020-11-20 15:38:08 +01:00
|
|
|
|
2020-11-21 18:41:35 +01:00
|
|
|
public class HomeViewModel extends AndroidViewModel {
|
2020-11-22 19:11:38 +01:00
|
|
|
private static final String TAG = "HomeViewModel";
|
2021-08-17 14:46:10 +02:00
|
|
|
|
|
|
|
|
private final SongRepository songRepository;
|
|
|
|
|
private final AlbumRepository albumRepository;
|
2022-01-17 17:32:59 +01:00
|
|
|
private final ArtistRepository artistRepository;
|
2022-12-28 14:25:59 +01:00
|
|
|
private final ChronologyRepository chronologyRepository;
|
2023-08-02 10:53:21 +02:00
|
|
|
private final FavoriteRepository favoriteRepository;
|
2024-06-08 18:53:58 +02:00
|
|
|
private final PlaylistRepository playlistRepository;
|
2023-09-17 16:40:41 +02:00
|
|
|
private final SharingRepository sharingRepository;
|
2021-08-17 14:46:10 +02:00
|
|
|
|
2023-03-06 21:59:10 +01:00
|
|
|
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);
|
2021-08-17 14:46:10 +02:00
|
|
|
private final MutableLiveData<List<Integer>> years = new MutableLiveData<>(null);
|
2023-03-06 21:59:10 +01:00
|
|
|
private final MutableLiveData<List<AlbumID3>> recentlyAddedAlbumSample = new MutableLiveData<>(null);
|
2021-07-27 16:58:38 +02:00
|
|
|
|
2022-12-28 14:25:59 +01:00
|
|
|
private final MutableLiveData<List<Chronology>> thisGridTopSong = new MutableLiveData<>(null);
|
2023-03-06 21:59:10 +01:00
|
|
|
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);
|
2024-06-08 18:53:58 +02:00
|
|
|
private final MutableLiveData<List<Playlist>> pinnedPlaylists = new MutableLiveData<>(null);
|
2023-09-17 16:40:41 +02:00
|
|
|
private final MutableLiveData<List<Share>> shares = new MutableLiveData<>(null);
|
|
|
|
|
|
2024-03-23 21:33:11 +01:00
|
|
|
private List<HomeSector> sectors;
|
2022-12-28 14:25:59 +01:00
|
|
|
|
2020-11-21 18:41:35 +01:00
|
|
|
public HomeViewModel(@NonNull Application application) {
|
|
|
|
|
super(application);
|
2020-11-20 15:38:08 +01:00
|
|
|
|
2024-03-23 21:33:11 +01:00
|
|
|
setHomeSectorList();
|
|
|
|
|
|
2023-03-10 15:21:02 +01:00
|
|
|
songRepository = new SongRepository();
|
|
|
|
|
albumRepository = new AlbumRepository();
|
|
|
|
|
artistRepository = new ArtistRepository();
|
|
|
|
|
chronologyRepository = new ChronologyRepository();
|
2023-08-02 10:53:21 +02:00
|
|
|
favoriteRepository = new FavoriteRepository();
|
2024-06-08 18:53:58 +02:00
|
|
|
playlistRepository = new PlaylistRepository();
|
2023-09-17 16:40:41 +02:00
|
|
|
sharingRepository = new SharingRepository();
|
2023-08-02 10:53:21 +02:00
|
|
|
|
|
|
|
|
setOfflineFavorite();
|
2020-11-20 15:38:08 +01:00
|
|
|
}
|
|
|
|
|
|
2023-03-06 21:59:10 +01:00
|
|
|
public LiveData<List<Child>> getDiscoverSongSample(LifecycleOwner owner) {
|
2022-09-05 08:21:01 +02:00
|
|
|
if (dicoverSongSample.getValue() == null) {
|
|
|
|
|
songRepository.getRandomSample(10, null, null).observe(owner, dicoverSongSample::postValue);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-28 15:46:05 +02:00
|
|
|
return dicoverSongSample;
|
2020-11-21 18:41:35 +01:00
|
|
|
}
|
|
|
|
|
|
2023-06-30 18:23:12 +02:00
|
|
|
public LiveData<List<Child>> getRandomShuffleSample() {
|
2025-05-26 14:06:25 +01:00
|
|
|
return songRepository.getRandomSample(1000, null, null);
|
2023-06-30 18:23:12 +02:00
|
|
|
}
|
|
|
|
|
|
2024-05-26 19:38:17 +02:00
|
|
|
public LiveData<List<Chronology>> getChronologySample(LifecycleOwner owner) {
|
|
|
|
|
Calendar cal = Calendar.getInstance();
|
2023-03-06 21:59:10 +01:00
|
|
|
String server = Preferences.getServerId();
|
2024-05-26 19:38:17 +02:00
|
|
|
|
|
|
|
|
int currentWeek = cal.get(Calendar.WEEK_OF_YEAR);
|
|
|
|
|
long start = cal.getTimeInMillis();
|
|
|
|
|
|
|
|
|
|
cal.set(Calendar.WEEK_OF_YEAR, currentWeek - 1);
|
|
|
|
|
long end = cal.getTimeInMillis();
|
|
|
|
|
|
|
|
|
|
chronologyRepository.getChronology(server, start, end).observe(owner, thisGridTopSong::postValue);
|
2022-12-28 14:25:59 +01:00
|
|
|
return thisGridTopSong;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-06 21:59:10 +01:00
|
|
|
public LiveData<List<AlbumID3>> getRecentlyReleasedAlbums(LifecycleOwner owner) {
|
2022-09-05 08:21:01 +02:00
|
|
|
if (newReleasedAlbum.getValue() == null) {
|
|
|
|
|
int currentYear = Calendar.getInstance().get(Calendar.YEAR);
|
2022-01-17 17:32:59 +01:00
|
|
|
|
2022-09-05 08:21:01 +02:00
|
|
|
albumRepository.getAlbums("byYear", 500, currentYear, currentYear).observe(owner, albums -> {
|
2023-05-27 11:58:41 +02:00
|
|
|
if (albums != null) {
|
|
|
|
|
albums.sort(Comparator.comparing(AlbumID3::getCreated).reversed());
|
|
|
|
|
newReleasedAlbum.postValue(albums.subList(0, Math.min(20, albums.size())));
|
|
|
|
|
}
|
2022-09-05 08:21:01 +02:00
|
|
|
});
|
|
|
|
|
}
|
2022-01-17 17:32:59 +01:00
|
|
|
|
|
|
|
|
return newReleasedAlbum;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-06 21:59:10 +01:00
|
|
|
public LiveData<List<Child>> getStarredTracksSample(LifecycleOwner owner) {
|
2022-09-05 08:21:01 +02:00
|
|
|
if (starredTracksSample.getValue() == null) {
|
|
|
|
|
songRepository.getStarredSongs(true, 10).observe(owner, starredTracksSample::postValue);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-17 16:17:41 +02:00
|
|
|
return starredTracksSample;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-06 21:59:10 +01:00
|
|
|
public LiveData<List<ArtistID3>> getStarredArtistsSample(LifecycleOwner owner) {
|
2022-09-05 08:21:01 +02:00
|
|
|
if (starredArtistsSample.getValue() == null) {
|
|
|
|
|
artistRepository.getStarredArtists(true, 10).observe(owner, starredArtistsSample::postValue);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-17 17:32:59 +01:00
|
|
|
return starredArtistsSample;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-06 21:59:10 +01:00
|
|
|
public LiveData<List<ArtistID3>> getBestOfArtists(LifecycleOwner owner) {
|
2023-01-06 17:49:49 +01:00
|
|
|
if (bestOfArtists.getValue() == null) {
|
|
|
|
|
artistRepository.getStarredArtists(true, 20).observe(owner, bestOfArtists::postValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return bestOfArtists;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-06 21:59:10 +01:00
|
|
|
public LiveData<List<Child>> getStarredTracks(LifecycleOwner owner) {
|
2022-09-05 08:21:01 +02:00
|
|
|
if (starredTracks.getValue() == null) {
|
|
|
|
|
songRepository.getStarredSongs(true, 20).observe(owner, starredTracks::postValue);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-17 17:32:59 +01:00
|
|
|
return starredTracks;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-06 21:59:10 +01:00
|
|
|
public LiveData<List<AlbumID3>> getStarredAlbums(LifecycleOwner owner) {
|
2022-09-05 08:21:01 +02:00
|
|
|
if (starredAlbums.getValue() == null) {
|
|
|
|
|
albumRepository.getStarredAlbums(true, 20).observe(owner, starredAlbums::postValue);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-17 17:32:59 +01:00
|
|
|
return starredAlbums;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-06 21:59:10 +01:00
|
|
|
public LiveData<List<ArtistID3>> getStarredArtists(LifecycleOwner owner) {
|
2022-09-05 08:21:01 +02:00
|
|
|
if (starredArtists.getValue() == null) {
|
|
|
|
|
artistRepository.getStarredArtists(true, 20).observe(owner, starredArtists::postValue);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-17 17:32:59 +01:00
|
|
|
return starredArtists;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-17 14:46:10 +02:00
|
|
|
public LiveData<List<Integer>> getYearList(LifecycleOwner owner) {
|
2022-09-05 08:21:01 +02:00
|
|
|
if (years.getValue() == null) {
|
|
|
|
|
albumRepository.getDecades().observe(owner, years::postValue);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-28 15:46:05 +02:00
|
|
|
return years;
|
2020-11-21 18:41:35 +01:00
|
|
|
}
|
2020-11-20 15:38:08 +01:00
|
|
|
|
2023-03-06 21:59:10 +01:00
|
|
|
public LiveData<List<AlbumID3>> getMostPlayedAlbums(LifecycleOwner owner) {
|
2022-09-05 08:21:01 +02:00
|
|
|
if (mostPlayedAlbumSample.getValue() == null) {
|
|
|
|
|
albumRepository.getAlbums("frequent", 20, null, null).observe(owner, mostPlayedAlbumSample::postValue);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-27 12:10:28 +02:00
|
|
|
return mostPlayedAlbumSample;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-06 21:59:10 +01:00
|
|
|
public LiveData<List<AlbumID3>> getMostRecentlyAddedAlbums(LifecycleOwner owner) {
|
2022-09-05 08:21:01 +02:00
|
|
|
if (recentlyAddedAlbumSample.getValue() == null) {
|
|
|
|
|
albumRepository.getAlbums("newest", 20, null, null).observe(owner, recentlyAddedAlbumSample::postValue);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-27 12:10:28 +02:00
|
|
|
return recentlyAddedAlbumSample;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-06 21:59:10 +01:00
|
|
|
public LiveData<List<AlbumID3>> getRecentlyPlayedAlbumList(LifecycleOwner owner) {
|
2022-09-05 08:21:01 +02:00
|
|
|
if (recentlyPlayedAlbumSample.getValue() == null) {
|
|
|
|
|
albumRepository.getAlbums("recent", 20, null, null).observe(owner, recentlyPlayedAlbumSample::postValue);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-27 12:10:28 +02:00
|
|
|
return recentlyPlayedAlbumSample;
|
|
|
|
|
}
|
2021-08-10 15:42:39 +02:00
|
|
|
|
2023-03-06 21:59:10 +01:00
|
|
|
public LiveData<List<Child>> getMediaInstantMix(LifecycleOwner owner, Child media) {
|
2023-01-04 09:14:15 +01:00
|
|
|
mediaInstantMix.setValue(Collections.emptyList());
|
|
|
|
|
|
2024-06-02 19:18:16 +02:00
|
|
|
songRepository.getInstantMix(media.getId(), 20).observe(owner, mediaInstantMix::postValue);
|
2023-01-04 09:14:15 +01:00
|
|
|
|
|
|
|
|
return mediaInstantMix;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-10 17:46:03 +01:00
|
|
|
public LiveData<List<Child>> getArtistInstantMix(LifecycleOwner owner, ArtistID3 artist) {
|
2023-03-10 19:02:25 +01:00
|
|
|
artistInstantMix.setValue(Collections.emptyList());
|
|
|
|
|
|
|
|
|
|
artistRepository.getTopSongs(artist.getName(), 10).observe(owner, artistInstantMix::postValue);
|
2023-03-10 17:46:03 +01:00
|
|
|
|
|
|
|
|
return artistInstantMix;
|
2023-01-06 17:49:49 +01:00
|
|
|
}
|
2023-01-04 09:14:15 +01:00
|
|
|
|
2023-03-10 17:46:03 +01:00
|
|
|
public LiveData<List<Child>> getArtistBestOf(LifecycleOwner owner, ArtistID3 artist) {
|
2023-03-10 19:02:25 +01:00
|
|
|
artistBestOf.setValue(Collections.emptyList());
|
|
|
|
|
|
|
|
|
|
artistRepository.getTopSongs(artist.getName(), 10).observe(owner, artistBestOf::postValue);
|
2023-03-10 17:46:03 +01:00
|
|
|
|
|
|
|
|
return artistBestOf;
|
2023-01-04 09:14:15 +01:00
|
|
|
}
|
|
|
|
|
|
2024-06-08 18:53:58 +02:00
|
|
|
public LiveData<List<Playlist>> getPinnedPlaylists(LifecycleOwner owner) {
|
|
|
|
|
pinnedPlaylists.setValue(Collections.emptyList());
|
|
|
|
|
|
|
|
|
|
playlistRepository.getPlaylists(false, -1).observe(owner, remotes -> {
|
|
|
|
|
playlistRepository.getPinnedPlaylists().observe(owner, locals -> {
|
|
|
|
|
if (remotes != null && locals != null) {
|
|
|
|
|
List<Playlist> toReturn = remotes.stream()
|
|
|
|
|
.filter(remote -> locals.stream().anyMatch(local -> local.getId().equals(remote.getId())))
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
|
|
|
|
pinnedPlaylists.setValue(toReturn);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return pinnedPlaylists;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-17 16:40:41 +02:00
|
|
|
public LiveData<List<Share>> getShares(LifecycleOwner owner) {
|
|
|
|
|
if (shares.getValue() == null) {
|
|
|
|
|
sharingRepository.getShares().observe(owner, shares::postValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return shares;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-13 21:10:10 +01:00
|
|
|
public LiveData<List<Child>> getAllStarredTracks() {
|
|
|
|
|
return songRepository.getStarredSongs(false, -1);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-26 19:38:17 +02:00
|
|
|
public void changeChronologyPeriod(LifecycleOwner owner, int period) {
|
|
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
|
|
String server = Preferences.getServerId();
|
|
|
|
|
int currentWeek = cal.get(Calendar.WEEK_OF_YEAR);
|
|
|
|
|
|
|
|
|
|
long start = 0;
|
|
|
|
|
long end = 0;
|
|
|
|
|
|
|
|
|
|
if (period == 0) {
|
|
|
|
|
start = cal.getTimeInMillis();
|
|
|
|
|
cal.set(Calendar.WEEK_OF_YEAR, currentWeek - 1);
|
|
|
|
|
end = cal.getTimeInMillis();
|
|
|
|
|
} else if (period == 1) {
|
|
|
|
|
start = cal.getTimeInMillis();
|
|
|
|
|
cal.set(Calendar.WEEK_OF_YEAR, currentWeek - 4);
|
|
|
|
|
end = cal.getTimeInMillis();
|
|
|
|
|
} else if (period == 2) {
|
|
|
|
|
start = cal.getTimeInMillis();
|
|
|
|
|
cal.set(Calendar.WEEK_OF_YEAR, currentWeek - 52);
|
|
|
|
|
end = cal.getTimeInMillis();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
chronologyRepository.getChronology(server, start, end).observe(owner, thisGridTopSong::postValue);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-10 15:42:39 +02:00
|
|
|
public void refreshDiscoverySongSample(LifecycleOwner owner) {
|
2021-08-17 14:46:10 +02:00
|
|
|
songRepository.getRandomSample(10, null, null).observe(owner, dicoverSongSample::postValue);
|
2021-08-10 15:42:39 +02:00
|
|
|
}
|
|
|
|
|
|
2021-08-17 16:17:41 +02:00
|
|
|
public void refreshSimilarSongSample(LifecycleOwner owner) {
|
|
|
|
|
songRepository.getStarredSongs(true, 10).observe(owner, starredTracksSample::postValue);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-17 17:32:59 +01:00
|
|
|
public void refreshRadioArtistSample(LifecycleOwner owner) {
|
|
|
|
|
artistRepository.getStarredArtists(true, 10).observe(owner, starredArtistsSample::postValue);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-06 17:49:49 +01:00
|
|
|
public void refreshBestOfArtist(LifecycleOwner owner) {
|
|
|
|
|
artistRepository.getStarredArtists(true, 20).observe(owner, bestOfArtists::postValue);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-17 17:32:59 +01:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-10 15:42:39 +02:00
|
|
|
public void refreshMostPlayedAlbums(LifecycleOwner owner) {
|
2021-09-01 16:53:10 +02:00
|
|
|
albumRepository.getAlbums("frequent", 20, null, null).observe(owner, mostPlayedAlbumSample::postValue);
|
2021-08-10 15:42:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void refreshMostRecentlyAddedAlbums(LifecycleOwner owner) {
|
2021-09-01 16:53:10 +02:00
|
|
|
albumRepository.getAlbums("newest", 20, null, null).observe(owner, recentlyAddedAlbumSample::postValue);
|
2021-08-10 15:42:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void refreshRecentlyPlayedAlbumList(LifecycleOwner owner) {
|
2021-09-01 16:53:10 +02:00
|
|
|
albumRepository.getAlbums("recent", 20, null, null).observe(owner, recentlyPlayedAlbumSample::postValue);
|
2021-08-10 15:42:39 +02:00
|
|
|
}
|
2023-08-02 10:53:21 +02:00
|
|
|
|
2023-09-17 16:40:41 +02:00
|
|
|
public void refreshShares(LifecycleOwner owner) {
|
|
|
|
|
sharingRepository.getShares().observe(owner, this.shares::postValue);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-23 21:33:11 +01:00
|
|
|
private void setHomeSectorList() {
|
|
|
|
|
if (Preferences.getHomeSectorList() != null && !Preferences.getHomeSectorList().equals("null")) {
|
|
|
|
|
sectors = new Gson().fromJson(
|
|
|
|
|
Preferences.getHomeSectorList(),
|
|
|
|
|
new TypeToken<List<HomeSector>>() {
|
|
|
|
|
}.getType()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<HomeSector> getHomeSectorList() {
|
|
|
|
|
return sectors;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean checkHomeSectorVisibility(String sectorId) {
|
|
|
|
|
return sectors != null && sectors.stream().filter(sector -> sector.getId().equals(sectorId))
|
|
|
|
|
.findAny()
|
2024-03-23 22:41:44 +01:00
|
|
|
.orElse(null) == null;
|
2024-03-23 21:33:11 +01:00
|
|
|
}
|
|
|
|
|
|
2023-08-02 10:53:21 +02:00
|
|
|
public void setOfflineFavorite() {
|
|
|
|
|
ArrayList<Favorite> favorites = getFavorites();
|
|
|
|
|
ArrayList<Favorite> favoritesToSave = getFavoritesToSave(favorites);
|
|
|
|
|
ArrayList<Favorite> favoritesToDelete = getFavoritesToDelete(favorites, favoritesToSave);
|
|
|
|
|
|
|
|
|
|
manageFavoriteToSave(favoritesToSave);
|
|
|
|
|
manageFavoriteToDelete(favoritesToDelete);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ArrayList<Favorite> getFavorites() {
|
|
|
|
|
return new ArrayList<>(favoriteRepository.getFavorites());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ArrayList<Favorite> getFavoritesToSave(ArrayList<Favorite> favorites) {
|
|
|
|
|
HashMap<String, Favorite> filteredMap = new HashMap<>();
|
|
|
|
|
|
|
|
|
|
for (Favorite favorite : favorites) {
|
|
|
|
|
String key = favorite.toString();
|
|
|
|
|
|
|
|
|
|
if (!filteredMap.containsKey(key) || favorite.getTimestamp() > filteredMap.get(key).getTimestamp()) {
|
|
|
|
|
filteredMap.put(key, favorite);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new ArrayList<>(filteredMap.values());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ArrayList<Favorite> getFavoritesToDelete(ArrayList<Favorite> favorites, ArrayList<Favorite> favoritesToSave) {
|
|
|
|
|
ArrayList<Favorite> favoritesToDelete = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
for (Favorite favorite : favorites) {
|
|
|
|
|
if (!favoritesToSave.contains(favorite)) {
|
|
|
|
|
favoritesToDelete.add(favorite);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return favoritesToDelete;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void manageFavoriteToSave(ArrayList<Favorite> favoritesToSave) {
|
|
|
|
|
for (Favorite favorite : favoritesToSave) {
|
|
|
|
|
if (favorite.getToStar()) {
|
|
|
|
|
favoriteToStar(favorite);
|
|
|
|
|
} else {
|
|
|
|
|
favoriteToUnstar(favorite);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void manageFavoriteToDelete(ArrayList<Favorite> favoritesToDelete) {
|
|
|
|
|
for (Favorite favorite : favoritesToDelete) {
|
|
|
|
|
favoriteRepository.delete(favorite);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void favoriteToStar(Favorite favorite) {
|
|
|
|
|
if (favorite.getSongId() != null) {
|
|
|
|
|
favoriteRepository.star(favorite.getSongId(), null, null, new StarCallback() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onSuccess() {
|
|
|
|
|
favoriteRepository.delete(favorite);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else if (favorite.getAlbumId() != null) {
|
|
|
|
|
favoriteRepository.star(null, favorite.getAlbumId(), null, new StarCallback() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onSuccess() {
|
|
|
|
|
favoriteRepository.delete(favorite);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else if (favorite.getArtistId() != null) {
|
|
|
|
|
favoriteRepository.star(null, null, favorite.getArtistId(), new StarCallback() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onSuccess() {
|
|
|
|
|
favoriteRepository.delete(favorite);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void favoriteToUnstar(Favorite favorite) {
|
|
|
|
|
if (favorite.getSongId() != null) {
|
|
|
|
|
favoriteRepository.unstar(favorite.getSongId(), null, null, new StarCallback() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onSuccess() {
|
|
|
|
|
favoriteRepository.delete(favorite);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else if (favorite.getAlbumId() != null) {
|
|
|
|
|
favoriteRepository.unstar(null, favorite.getAlbumId(), null, new StarCallback() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onSuccess() {
|
|
|
|
|
favoriteRepository.delete(favorite);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else if (favorite.getArtistId() != null) {
|
|
|
|
|
favoriteRepository.unstar(null, null, favorite.getArtistId(), new StarCallback() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onSuccess() {
|
|
|
|
|
favoriteRepository.delete(favorite);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-20 15:38:08 +01:00
|
|
|
}
|