2020-08-11 18:29:44 +00:00
|
|
|
package code.name.monkey.retromusic.fragments.playlists
|
2020-07-20 19:05:48 +00:00
|
|
|
|
|
|
|
import androidx.lifecycle.LiveData
|
|
|
|
import androidx.lifecycle.MutableLiveData
|
|
|
|
import androidx.lifecycle.ViewModel
|
|
|
|
import androidx.lifecycle.viewModelScope
|
2020-08-20 09:52:38 +00:00
|
|
|
import code.name.monkey.retromusic.db.PlaylistWithSongs
|
2020-07-20 19:05:48 +00:00
|
|
|
import code.name.monkey.retromusic.interfaces.MusicServiceEventListener
|
|
|
|
import code.name.monkey.retromusic.model.Song
|
2020-08-13 17:08:37 +00:00
|
|
|
import code.name.monkey.retromusic.repository.RealRepository
|
2020-08-20 09:52:38 +00:00
|
|
|
import kotlinx.coroutines.Dispatchers
|
2020-07-20 19:05:48 +00:00
|
|
|
import kotlinx.coroutines.Dispatchers.Main
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
import kotlinx.coroutines.withContext
|
|
|
|
|
|
|
|
class PlaylistDetailsViewModel(
|
2020-08-13 17:08:37 +00:00
|
|
|
private val realRepository: RealRepository,
|
2020-08-20 09:52:38 +00:00
|
|
|
private var playlist: PlaylistWithSongs
|
2020-07-20 19:05:48 +00:00
|
|
|
) : ViewModel(), MusicServiceEventListener {
|
2020-08-20 09:52:38 +00:00
|
|
|
|
2020-07-20 19:05:48 +00:00
|
|
|
private val _playListSongs = MutableLiveData<List<Song>>()
|
2020-08-20 09:52:38 +00:00
|
|
|
private val _playlist = MutableLiveData<PlaylistWithSongs>().apply {
|
2020-07-20 19:05:48 +00:00
|
|
|
postValue(playlist)
|
|
|
|
}
|
|
|
|
|
2020-08-20 09:52:38 +00:00
|
|
|
fun getPlaylist(): LiveData<PlaylistWithSongs> = _playlist
|
2020-07-20 19:05:48 +00:00
|
|
|
|
|
|
|
fun getSongs(): LiveData<List<Song>> = _playListSongs
|
|
|
|
|
|
|
|
init {
|
|
|
|
loadPlaylistSongs(playlist)
|
|
|
|
}
|
|
|
|
|
2020-08-20 09:52:38 +00:00
|
|
|
private fun loadPlaylistSongs(playlist: PlaylistWithSongs) = viewModelScope.launch(Dispatchers.IO) {
|
|
|
|
val songs: List<Song> = realRepository.playlistSongs(playlist)
|
2020-07-20 19:05:48 +00:00
|
|
|
withContext(Main) { _playListSongs.postValue(songs) }
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onMediaStoreChanged() {
|
2020-08-20 09:52:38 +00:00
|
|
|
/*if (playlist !is AbsCustomPlaylist) {
|
2020-07-20 19:05:48 +00:00
|
|
|
// Playlist deleted
|
|
|
|
if (!PlaylistsUtil.doesPlaylistExist(App.getContext(), playlist.id)) {
|
|
|
|
//TODO Finish the page
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Playlist renamed
|
|
|
|
val playlistName =
|
|
|
|
PlaylistsUtil.getNameForPlaylist(App.getContext(), playlist.id.toLong())
|
|
|
|
if (playlistName != playlist.name) {
|
2020-08-13 17:08:37 +00:00
|
|
|
viewModelScope.launch {
|
|
|
|
playlist = realRepository.playlist(playlist.id)
|
|
|
|
_playlist.postValue(playlist)
|
|
|
|
}
|
2020-07-20 19:05:48 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-20 09:52:38 +00:00
|
|
|
loadPlaylistSongs(playlist)*/
|
2020-07-20 19:05:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onServiceConnected() {}
|
|
|
|
override fun onServiceDisconnected() {}
|
|
|
|
override fun onQueueChanged() {}
|
|
|
|
override fun onPlayingMetaChanged() {}
|
|
|
|
override fun onPlayStateChanged() {}
|
|
|
|
override fun onRepeatModeChanged() {}
|
|
|
|
override fun onShuffleModeChanged() {}
|
|
|
|
}
|