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;
|
2021-07-31 16:37:41 +02:00
|
|
|
import androidx.lifecycle.LiveData;
|
2021-09-13 11:38:52 +02:00
|
|
|
import androidx.preference.Preference;
|
2020-12-01 20:04:54 +01:00
|
|
|
|
|
|
|
|
import com.cappielloantonio.play.model.Album;
|
|
|
|
|
import com.cappielloantonio.play.model.Artist;
|
|
|
|
|
import com.cappielloantonio.play.model.Song;
|
|
|
|
|
import com.cappielloantonio.play.repository.AlbumRepository;
|
|
|
|
|
import com.cappielloantonio.play.repository.ArtistRepository;
|
|
|
|
|
import com.cappielloantonio.play.repository.SongRepository;
|
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;
|
2021-09-13 11:28:48 +02:00
|
|
|
|
|
|
|
|
import java.util.Collections;
|
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
|
|
|
|
|
|
|
|
private Song song;
|
|
|
|
|
|
|
|
|
|
public SongBottomSheetViewModel(@NonNull Application application) {
|
|
|
|
|
super(application);
|
|
|
|
|
|
|
|
|
|
songRepository = new SongRepository(application);
|
|
|
|
|
albumRepository = new AlbumRepository(application);
|
|
|
|
|
artistRepository = new ArtistRepository(application);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Song getSong() {
|
|
|
|
|
return song;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-27 11:01:02 +02:00
|
|
|
public void setSong(Song song) {
|
|
|
|
|
this.song = song;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-13 11:28:48 +02:00
|
|
|
public void setFavorite(Context context) {
|
2021-07-29 16:00:00 +02:00
|
|
|
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()) {
|
2021-12-29 10:22:10 +01:00
|
|
|
// DownloadUtil.getDownloadTracker(context).download(Collections.singletonList(song), null, null);
|
2021-09-13 11:38:52 +02:00
|
|
|
}
|
2021-07-29 16:00:00 +02:00
|
|
|
}
|
2020-12-01 20:04:54 +01:00
|
|
|
}
|
|
|
|
|
|
2021-07-31 16:37:41 +02:00
|
|
|
public LiveData<Album> getAlbum() {
|
|
|
|
|
return albumRepository.getAlbum(song.getAlbumId());
|
2020-12-01 20:04:54 +01:00
|
|
|
}
|
|
|
|
|
|
2021-07-31 16:37:41 +02:00
|
|
|
public LiveData<Artist> getArtist() {
|
|
|
|
|
return artistRepository.getArtist(song.getArtistId());
|
2020-12-01 20:04:54 +01:00
|
|
|
}
|
|
|
|
|
}
|