2020-12-05 21:31:12 +01:00
|
|
|
package com.cappielloantonio.play.viewmodel;
|
|
|
|
|
|
|
|
|
|
import android.app.Application;
|
2021-09-13 11:28:48 +02:00
|
|
|
import android.content.Context;
|
2020-12-05 21:31:12 +01:00
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
import androidx.lifecycle.AndroidViewModel;
|
2021-12-19 16:48:43 +01:00
|
|
|
import androidx.lifecycle.LifecycleOwner;
|
2020-12-05 21:31:12 +01:00
|
|
|
import androidx.lifecycle.LiveData;
|
2021-12-19 16:48:43 +01:00
|
|
|
import androidx.lifecycle.MutableLiveData;
|
2020-12-05 21:31:12 +01:00
|
|
|
|
2021-12-31 16:08:19 +01:00
|
|
|
import com.cappielloantonio.play.model.Album;
|
2021-11-24 15:39:15 +01:00
|
|
|
import com.cappielloantonio.play.model.Artist;
|
2021-07-28 15:28:32 +02:00
|
|
|
import com.cappielloantonio.play.model.Queue;
|
2020-12-05 21:31:12 +01:00
|
|
|
import com.cappielloantonio.play.model.Song;
|
2021-11-24 15:39:15 +01:00
|
|
|
import com.cappielloantonio.play.repository.ArtistRepository;
|
2020-12-05 21:31:12 +01:00
|
|
|
import com.cappielloantonio.play.repository.QueueRepository;
|
2020-12-08 11:12:44 +01:00
|
|
|
import com.cappielloantonio.play.repository.SongRepository;
|
2022-01-01 21:55:15 +01:00
|
|
|
import com.cappielloantonio.play.util.DownloadUtil;
|
|
|
|
|
import com.cappielloantonio.play.util.MappingUtil;
|
2021-09-13 11:38:52 +02:00
|
|
|
import com.cappielloantonio.play.util.PreferenceUtil;
|
2020-12-05 21:31:12 +01:00
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
public class PlayerBottomSheetViewModel extends AndroidViewModel {
|
2020-12-08 11:12:44 +01:00
|
|
|
private static final String TAG = "PlayerBottomSheetViewModel";
|
2021-04-19 14:44:30 +02:00
|
|
|
|
2021-09-02 14:12:13 +02:00
|
|
|
private final SongRepository songRepository;
|
2021-11-24 15:39:15 +01:00
|
|
|
private final ArtistRepository artistRepository;
|
2021-09-02 14:12:13 +02:00
|
|
|
private final QueueRepository queueRepository;
|
2020-12-05 21:31:12 +01:00
|
|
|
|
2021-12-20 18:15:09 +01:00
|
|
|
private final MutableLiveData<String> lyricsLiveData = new MutableLiveData<>(null);
|
2021-12-31 16:08:19 +01:00
|
|
|
|
|
|
|
|
private final MutableLiveData<Song> liveSong = new MutableLiveData<>(null);
|
|
|
|
|
private final MutableLiveData<Album> liveAlbum = new MutableLiveData<>(null);
|
|
|
|
|
private final MutableLiveData<Artist> liveArtist = new MutableLiveData<>(null);
|
2021-12-20 18:15:09 +01:00
|
|
|
|
2020-12-05 21:31:12 +01:00
|
|
|
public PlayerBottomSheetViewModel(@NonNull Application application) {
|
|
|
|
|
super(application);
|
|
|
|
|
|
2020-12-08 11:12:44 +01:00
|
|
|
songRepository = new SongRepository(application);
|
2021-11-24 15:39:15 +01:00
|
|
|
artistRepository = new ArtistRepository(application);
|
2020-12-05 21:31:12 +01:00
|
|
|
queueRepository = new QueueRepository(application);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-28 15:28:32 +02:00
|
|
|
public LiveData<List<Queue>> getQueueSong() {
|
2021-12-30 18:13:50 +01:00
|
|
|
return queueRepository.getLiveQueue();
|
2020-12-05 21:31:12 +01:00
|
|
|
}
|
|
|
|
|
|
2021-12-31 16:08:19 +01:00
|
|
|
public void setFavorite(Context context, Song song) {
|
2021-09-02 14:12:13 +02:00
|
|
|
if (song != null) {
|
|
|
|
|
if (song.isFavorite()) {
|
|
|
|
|
songRepository.unstar(song.getId());
|
|
|
|
|
song.setFavorite(false);
|
|
|
|
|
} else {
|
|
|
|
|
songRepository.star(song.getId());
|
|
|
|
|
song.setFavorite(true);
|
2021-09-13 11:38:52 +02:00
|
|
|
|
2021-12-30 18:13:50 +01:00
|
|
|
if (PreferenceUtil.getInstance(context).isStarredSyncEnabled()) {
|
2022-01-01 21:55:15 +01:00
|
|
|
DownloadUtil.getDownloadTracker(context).download(MappingUtil.mapMediaItem(context, song, false));
|
2021-09-13 11:38:52 +02:00
|
|
|
}
|
2021-09-02 14:12:13 +02:00
|
|
|
}
|
2021-07-29 17:14:57 +02:00
|
|
|
}
|
2020-12-08 11:12:44 +01:00
|
|
|
}
|
2020-12-05 21:31:12 +01:00
|
|
|
|
2021-12-31 16:08:19 +01:00
|
|
|
public LiveData<String> getLiveLyrics() {
|
|
|
|
|
return lyricsLiveData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void refreshSongInfo(LifecycleOwner owner, Song song) {
|
|
|
|
|
songRepository.getSongLyrics(song).observe(owner, lyricsLiveData::postValue);
|
2021-11-24 15:39:15 +01:00
|
|
|
}
|
2021-12-19 16:48:43 +01:00
|
|
|
|
2021-12-20 18:15:09 +01:00
|
|
|
public LiveData<Song> getLiveSong() {
|
2021-12-31 16:08:19 +01:00
|
|
|
return liveSong;
|
2021-12-20 18:15:09 +01:00
|
|
|
}
|
|
|
|
|
|
2021-12-31 16:08:19 +01:00
|
|
|
public void setLiveSong(LifecycleOwner owner, String songId) {
|
|
|
|
|
songRepository.getSong(songId).observe(owner, liveSong::postValue);
|
2021-12-19 16:48:43 +01:00
|
|
|
}
|
|
|
|
|
|
2021-12-31 16:08:19 +01:00
|
|
|
public LiveData<Artist> getLiveArtist() {
|
|
|
|
|
return liveArtist;
|
2021-12-19 16:48:43 +01:00
|
|
|
}
|
2021-12-20 18:15:09 +01:00
|
|
|
|
2021-12-31 16:08:19 +01:00
|
|
|
public void setLiveArtist(LifecycleOwner owner, String ArtistId) {
|
|
|
|
|
artistRepository.getArtist(ArtistId).observe(owner, liveArtist::postValue);
|
|
|
|
|
}
|
2020-12-05 21:31:12 +01:00
|
|
|
}
|