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;
|
2023-03-02 10:48:30 +01:00
|
|
|
import androidx.annotation.OptIn;
|
2020-12-05 21:31:12 +01:00
|
|
|
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;
|
2023-03-02 10:48:30 +01:00
|
|
|
import androidx.media3.common.util.UnstableApi;
|
2020-12-05 21:31:12 +01:00
|
|
|
|
2022-02-07 09:47:46 +01:00
|
|
|
import com.cappielloantonio.play.model.Media;
|
2023-03-06 21:59:10 +01:00
|
|
|
import com.cappielloantonio.play.model.Queue;
|
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;
|
2023-03-06 21:59:10 +01:00
|
|
|
import com.cappielloantonio.play.subsonic.models.ArtistID3;
|
|
|
|
|
import com.cappielloantonio.play.subsonic.models.Child;
|
2020-12-05 21:31:12 +01:00
|
|
|
|
2023-01-04 09:14:15 +01:00
|
|
|
import java.util.Collections;
|
2023-03-06 21:59:10 +01:00
|
|
|
import java.util.Date;
|
2020-12-05 21:31:12 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
|
2023-03-02 10:48:30 +01:00
|
|
|
@OptIn(markerClass = UnstableApi.class)
|
2020-12-05 21:31:12 +01:00
|
|
|
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
|
|
|
|
2023-03-06 21:59:10 +01:00
|
|
|
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);
|
2023-01-04 09:14:15 +01:00
|
|
|
|
2021-12-20 18:15:09 +01:00
|
|
|
|
2020-12-05 21:31:12 +01:00
|
|
|
public PlayerBottomSheetViewModel(@NonNull Application application) {
|
|
|
|
|
super(application);
|
|
|
|
|
|
2023-03-10 15:21:02 +01:00
|
|
|
songRepository = new SongRepository();
|
|
|
|
|
artistRepository = new ArtistRepository();
|
|
|
|
|
queueRepository = new QueueRepository();
|
2020-12-05 21:31:12 +01:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2023-03-06 21:59:10 +01:00
|
|
|
public void setFavorite(Context context, Child media) {
|
2022-02-07 17:34:46 +01:00
|
|
|
if (media != null) {
|
2023-03-06 21:59:10 +01:00
|
|
|
if (media.getStarred() != null) {
|
2022-02-07 17:34:46 +01:00
|
|
|
songRepository.unstar(media.getId());
|
2023-03-06 21:59:10 +01:00
|
|
|
media.setStarred(null);
|
2021-09-02 14:12:13 +02:00
|
|
|
} else {
|
2022-02-07 17:34:46 +01:00
|
|
|
songRepository.star(media.getId());
|
2023-03-06 21:59:10 +01:00
|
|
|
media.setStarred(new Date());
|
2021-09-13 11:38:52 +02:00
|
|
|
|
2023-03-06 21:59:10 +01:00
|
|
|
// TODO
|
|
|
|
|
/* if (Preferences.isStarredSyncEnabled()) {
|
2022-01-02 11:30:16 +01:00
|
|
|
DownloadUtil.getDownloadTracker(context).download(
|
2023-03-10 15:21:02 +01:00
|
|
|
MappingUtil.mapMediaItem(media, false),
|
2022-02-07 17:34:46 +01:00
|
|
|
MappingUtil.mapDownload(media, null, null)
|
2022-01-02 11:30:16 +01:00
|
|
|
);
|
2023-03-06 21:59:10 +01: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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-06 21:59:10 +01:00
|
|
|
public void refreshMediaInfo(LifecycleOwner owner, Child media) {
|
2022-02-07 17:34:46 +01:00
|
|
|
songRepository.getSongLyrics(media).observe(owner, lyricsLiveData::postValue);
|
2021-11-24 15:39:15 +01:00
|
|
|
}
|
2021-12-19 16:48:43 +01:00
|
|
|
|
2023-03-06 21:59:10 +01:00
|
|
|
public LiveData<Child> getLiveMedia() {
|
2022-02-07 17:34:46 +01:00
|
|
|
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) {
|
2023-03-06 21:59:10 +01:00
|
|
|
if (mediaType != null) {
|
2022-02-07 17:34:46 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2023-03-06 21:59:10 +01:00
|
|
|
public LiveData<ArtistID3> getLiveArtist() {
|
2021-12-31 16:08:19 +01:00
|
|
|
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) {
|
2023-03-06 21:59:10 +01:00
|
|
|
if (mediaType != null) {
|
2022-02-07 17:34:46 +01:00
|
|
|
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
|
|
|
}
|
2023-01-04 09:14:15 +01: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
|
|
|
instantMix.setValue(Collections.emptyList());
|
|
|
|
|
|
|
|
|
|
songRepository.getInstantMix(media, 20).observe(owner, instantMix::postValue);
|
|
|
|
|
|
|
|
|
|
return instantMix;
|
|
|
|
|
}
|
2020-12-05 21:31:12 +01:00
|
|
|
}
|