2020-12-01 20:04:54 +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-01 20:04:54 +01:00
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
import androidx.lifecycle.AndroidViewModel;
|
2023-01-04 09:14:15 +01:00
|
|
|
import androidx.lifecycle.LifecycleOwner;
|
2021-07-31 16:37:41 +02:00
|
|
|
import androidx.lifecycle.LiveData;
|
2023-01-04 09:14:15 +01:00
|
|
|
import androidx.lifecycle.MutableLiveData;
|
|
|
|
|
import androidx.media3.common.util.UnstableApi;
|
2020-12-01 20:04:54 +01:00
|
|
|
|
|
|
|
|
import com.cappielloantonio.play.repository.AlbumRepository;
|
|
|
|
|
import com.cappielloantonio.play.repository.ArtistRepository;
|
|
|
|
|
import com.cappielloantonio.play.repository.SongRepository;
|
2023-03-06 21:59:10 +01:00
|
|
|
import com.cappielloantonio.play.subsonic.models.AlbumID3;
|
|
|
|
|
import com.cappielloantonio.play.subsonic.models.ArtistID3;
|
|
|
|
|
import com.cappielloantonio.play.subsonic.models.Child;
|
2021-09-13 11:28:48 +02: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;
|
2023-01-04 09:14:15 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
@UnstableApi
|
2020-12-01 20:04:54 +01:00
|
|
|
public class SongBottomSheetViewModel extends AndroidViewModel {
|
2021-09-02 14:12:13 +02:00
|
|
|
private final SongRepository songRepository;
|
|
|
|
|
private final AlbumRepository albumRepository;
|
|
|
|
|
private final ArtistRepository artistRepository;
|
2020-12-01 20:04:54 +01:00
|
|
|
|
2023-03-06 21:59:10 +01:00
|
|
|
private Child song;
|
2020-12-01 20:04:54 +01:00
|
|
|
|
2023-03-06 21:59:10 +01:00
|
|
|
private final MutableLiveData<List<Child>> instantMix = new MutableLiveData<>(null);
|
2023-01-04 09:14:15 +01:00
|
|
|
|
2020-12-01 20:04:54 +01:00
|
|
|
public SongBottomSheetViewModel(@NonNull Application application) {
|
|
|
|
|
super(application);
|
|
|
|
|
|
|
|
|
|
songRepository = new SongRepository(application);
|
|
|
|
|
albumRepository = new AlbumRepository(application);
|
|
|
|
|
artistRepository = new ArtistRepository(application);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-06 21:59:10 +01:00
|
|
|
public Child getSong() {
|
2020-12-01 20:04:54 +01:00
|
|
|
return song;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-06 21:59:10 +01:00
|
|
|
public void setSong(Child song) {
|
2021-04-27 11:01:02 +02:00
|
|
|
this.song = song;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-13 11:28:48 +02:00
|
|
|
public void setFavorite(Context context) {
|
2023-03-06 21:59:10 +01:00
|
|
|
if (song.getStarred() != null) {
|
2021-07-29 16:00:00 +02:00
|
|
|
songRepository.unstar(song.getId());
|
2023-03-06 21:59:10 +01:00
|
|
|
song.setStarred(null);
|
2021-07-29 16:00:00 +02:00
|
|
|
} else {
|
|
|
|
|
songRepository.star(song.getId());
|
2023-03-06 21:59:10 +01:00
|
|
|
song.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(
|
|
|
|
|
MappingUtil.mapMediaItem(context, song, false),
|
|
|
|
|
MappingUtil.mapDownload(song, null, null)
|
|
|
|
|
);
|
2023-03-06 21:59:10 +01:00
|
|
|
} */
|
2021-07-29 16:00:00 +02:00
|
|
|
}
|
2020-12-01 20:04:54 +01:00
|
|
|
}
|
|
|
|
|
|
2023-03-06 21:59:10 +01:00
|
|
|
public LiveData<AlbumID3> getAlbum() {
|
2021-07-31 16:37:41 +02:00
|
|
|
return albumRepository.getAlbum(song.getAlbumId());
|
2020-12-01 20:04:54 +01:00
|
|
|
}
|
|
|
|
|
|
2023-03-06 21:59:10 +01:00
|
|
|
public LiveData<ArtistID3> getArtist() {
|
2021-07-31 16:37:41 +02:00
|
|
|
return artistRepository.getArtist(song.getArtistId());
|
2020-12-01 20:04:54 +01:00
|
|
|
}
|
2023-01-04 09:14:15 +01:00
|
|
|
|
2023-03-06 21:59:10 +01:00
|
|
|
public LiveData<List<Child>> getInstantMix(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-01 20:04:54 +01:00
|
|
|
}
|