tempus/app/src/main/java/com/cappielloantonio/play/viewmodel/PlayerBottomSheetViewModel.java

123 lines
4.2 KiB
Java
Raw Normal View History

2020-12-05 21:31:12 +01:00
package com.cappielloantonio.play.viewmodel;
import android.app.Application;
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;
import androidx.lifecycle.LifecycleOwner;
2020-12-05 21:31:12 +01:00
import androidx.lifecycle.LiveData;
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
import com.cappielloantonio.play.model.Queue;
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;
import com.cappielloantonio.play.subsonic.models.ArtistID3;
import com.cappielloantonio.play.subsonic.models.Child;
2023-03-10 16:56:53 +01:00
import com.cappielloantonio.play.util.Constants;
2020-12-05 21:31:12 +01:00
import java.util.Collections;
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;
private final ArtistRepository artistRepository;
2021-09-02 14:12:13 +02:00
private final QueueRepository queueRepository;
2020-12-05 21:31:12 +01:00
private final MutableLiveData<String> lyricsLiveData = new MutableLiveData<>(null);
2021-12-31 16:08:19 +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);
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() {
return queueRepository.getLiveQueue();
2020-12-05 21:31:12 +01:00
}
public void setFavorite(Context context, Child media) {
if (media != null) {
if (media.getStarred() != null) {
songRepository.unstar(media.getId());
media.setStarred(null);
2021-09-02 14:12:13 +02:00
} else {
songRepository.star(media.getId());
media.setStarred(new Date());
// TODO
/* if (Preferences.isStarredSyncEnabled()) {
DownloadUtil.getDownloadTracker(context).download(
2023-03-10 15:21:02 +01:00
MappingUtil.mapMediaItem(media, false),
MappingUtil.mapDownload(media, null, null)
);
} */
2021-09-02 14:12:13 +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 refreshMediaInfo(LifecycleOwner owner, Child media) {
songRepository.getSongLyrics(media).observe(owner, lyricsLiveData::postValue);
}
public LiveData<Child> getLiveMedia() {
return liveMedia;
}
public void setLiveMedia(LifecycleOwner owner, String mediaType, String mediaId) {
if (mediaType != null) {
switch (mediaType) {
2023-03-10 16:56:53 +01:00
case Constants.MEDIA_TYPE_MUSIC:
songRepository.getSong(mediaId).observe(owner, liveMedia::postValue);
break;
2023-03-10 16:56:53 +01:00
case Constants.MEDIA_TYPE_PODCAST:
liveMedia.postValue(null);
break;
}
}
}
public LiveData<ArtistID3> getLiveArtist() {
2021-12-31 16:08:19 +01:00
return liveArtist;
}
public void setLiveArtist(LifecycleOwner owner, String mediaType, String ArtistId) {
if (mediaType != null) {
switch (mediaType) {
2023-03-10 16:56:53 +01:00
case Constants.MEDIA_TYPE_MUSIC:
artistRepository.getArtist(ArtistId).observe(owner, liveArtist::postValue);
break;
2023-03-10 16:56:53 +01:00
case Constants.MEDIA_TYPE_PODCAST:
liveArtist.postValue(null);
break;
}
}
2021-12-31 16:08:19 +01:00
}
public LiveData<List<Child>> getMediaInstantMix(LifecycleOwner owner, Child media) {
instantMix.setValue(Collections.emptyList());
songRepository.getInstantMix(media, 20).observe(owner, instantMix::postValue);
return instantMix;
}
2020-12-05 21:31:12 +01:00
}