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;
|
2022-02-07 09:47:46 +01:00
|
|
|
import com.cappielloantonio.play.model.Media;
|
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
|
|
|
|
2022-02-07 17:34:46 +01:00
|
|
|
private final MutableLiveData<Media> liveMedia = new MutableLiveData<>(null);
|
2021-12-31 16:08:19 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2022-02-07 17:34:46 +01:00
|
|
|
public void setFavorite(Context context, Media media) {
|
|
|
|
|
if (media != null) {
|
|
|
|
|
if (media.isStarred()) {
|
|
|
|
|
songRepository.unstar(media.getId());
|
|
|
|
|
media.setStarred(false);
|
2021-09-02 14:12:13 +02:00
|
|
|
} else {
|
2022-02-07 17:34:46 +01:00
|
|
|
songRepository.star(media.getId());
|
|
|
|
|
media.setStarred(true);
|
2021-09-13 11:38:52 +02:00
|
|
|
|
2021-12-30 18:13:50 +01:00
|
|
|
if (PreferenceUtil.getInstance(context).isStarredSyncEnabled()) {
|
2022-01-02 11:30:16 +01:00
|
|
|
DownloadUtil.getDownloadTracker(context).download(
|
2022-02-07 17:34:46 +01:00
|
|
|
MappingUtil.mapMediaItem(context, media, false),
|
|
|
|
|
MappingUtil.mapDownload(media, null, null)
|
2022-01-02 11:30:16 +01:00
|
|
|
);
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-07 17:34:46 +01:00
|
|
|
public void refreshMediaInfo(LifecycleOwner owner, Media media) {
|
|
|
|
|
songRepository.getSongLyrics(media).observe(owner, lyricsLiveData::postValue);
|
2021-11-24 15:39:15 +01:00
|
|
|
}
|
2021-12-19 16:48:43 +01:00
|
|
|
|
2022-02-07 17:34:46 +01:00
|
|
|
public LiveData<Media> getLiveMedia() {
|
|
|
|
|
return liveMedia;
|
2021-12-20 18:15:09 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-07 17:34:46 +01:00
|
|
|
public void setLiveMedia(LifecycleOwner owner, String mediaType, String mediaId) {
|
|
|
|
|
if(mediaType != null) {
|
|
|
|
|
switch (mediaType) {
|
|
|
|
|
case Media.MEDIA_TYPE_MUSIC:
|
|
|
|
|
songRepository.getSong(mediaId).observe(owner, liveMedia::postValue);
|
|
|
|
|
break;
|
|
|
|
|
case Media.MEDIA_TYPE_PODCAST:
|
|
|
|
|
liveMedia.postValue(null);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
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
|
|
|
|
2022-02-07 17:34:46 +01:00
|
|
|
public void setLiveArtist(LifecycleOwner owner, String mediaType, String ArtistId) {
|
|
|
|
|
if(mediaType != null) {
|
|
|
|
|
switch (mediaType) {
|
|
|
|
|
case Media.MEDIA_TYPE_MUSIC:
|
|
|
|
|
artistRepository.getArtist(ArtistId).observe(owner, liveArtist::postValue);
|
|
|
|
|
break;
|
|
|
|
|
case Media.MEDIA_TYPE_PODCAST:
|
|
|
|
|
liveArtist.postValue(null);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-31 16:08:19 +01:00
|
|
|
}
|
2020-12-05 21:31:12 +01:00
|
|
|
}
|