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-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;
|
2021-04-27 11:01:02 +02:00
|
|
|
import com.cappielloantonio.play.service.MusicPlayerRemote;
|
2021-09-13 11:28:48 +02:00
|
|
|
import com.cappielloantonio.play.util.DownloadUtil;
|
2021-09-13 11:38:52 +02:00
|
|
|
import com.cappielloantonio.play.util.PreferenceUtil;
|
2020-12-05 21:31:12 +01:00
|
|
|
|
2021-09-13 11:28:48 +02:00
|
|
|
import java.util.Collections;
|
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);
|
|
|
|
|
private final MutableLiveData<Song> songLiveData = new MutableLiveData<>(null);
|
|
|
|
|
|
2021-09-02 14:12:13 +02:00
|
|
|
private final LiveData<List<Queue>> queueSong;
|
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);
|
|
|
|
|
|
|
|
|
|
queueSong = queueRepository.getLiveQueue();
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-28 15:28:32 +02:00
|
|
|
public LiveData<List<Queue>> getQueueSong() {
|
2020-12-05 21:31:12 +01:00
|
|
|
return queueSong;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-13 17:29:37 +02:00
|
|
|
public Song getCurrentSong() {
|
|
|
|
|
return MusicPlayerRemote.getCurrentSong();
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-13 11:28:48 +02:00
|
|
|
public void setFavorite(Context context) {
|
2021-11-24 15:39:15 +01:00
|
|
|
Song song = getCurrentSong();
|
2021-07-29 17:14:57 +02:00
|
|
|
|
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
|
|
|
|
|
|
|
|
if(PreferenceUtil.getInstance(context).isStarredSyncEnabled()) {
|
|
|
|
|
DownloadUtil.getDownloadTracker(context).download(Collections.singletonList(song), null, null);
|
|
|
|
|
}
|
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-04-16 18:00:19 +02:00
|
|
|
public void orderSongAfterSwap(List<Song> songs) {
|
|
|
|
|
queueRepository.insertAllAndStartNew(songs);
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-16 10:08:31 +02:00
|
|
|
public void removeSong(int position) {
|
|
|
|
|
queueRepository.deleteByPosition(position);
|
|
|
|
|
}
|
2021-11-24 15:39:15 +01:00
|
|
|
|
|
|
|
|
public LiveData<Artist> getArtist() {
|
|
|
|
|
Song song = getCurrentSong();
|
|
|
|
|
return artistRepository.getArtist(song.getArtistId());
|
|
|
|
|
}
|
2021-12-19 16:48:43 +01:00
|
|
|
|
2021-12-20 18:15:09 +01:00
|
|
|
public LiveData<Song> getLiveSong() {
|
|
|
|
|
return songLiveData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public LiveData<String> getLiveLyrics() {
|
|
|
|
|
return lyricsLiveData;
|
2021-12-19 16:48:43 +01:00
|
|
|
}
|
|
|
|
|
|
2021-12-20 18:15:09 +01:00
|
|
|
public void refreshSongInfo(LifecycleOwner owner, Song song) {
|
|
|
|
|
songLiveData.postValue(song);
|
|
|
|
|
songRepository.getSongLyrics(song).observe(owner, lyricsLiveData::postValue);
|
2021-12-19 16:48:43 +01:00
|
|
|
}
|
2021-12-20 18:15:09 +01:00
|
|
|
|
2020-12-05 21:31:12 +01:00
|
|
|
}
|