2020-05-23 15:09:07 +00:00
|
|
|
package code.name.monkey.retromusic.fragments
|
|
|
|
|
|
|
|
import androidx.lifecycle.LiveData
|
|
|
|
import androidx.lifecycle.MutableLiveData
|
2020-07-20 19:05:48 +00:00
|
|
|
import androidx.lifecycle.ViewModel
|
2020-05-23 15:09:07 +00:00
|
|
|
import androidx.lifecycle.viewModelScope
|
2020-08-29 18:53:15 +00:00
|
|
|
import code.name.monkey.retromusic.db.PlaylistEntity
|
2020-08-20 06:49:08 +00:00
|
|
|
import code.name.monkey.retromusic.db.PlaylistWithSongs
|
2020-08-29 18:53:15 +00:00
|
|
|
import code.name.monkey.retromusic.db.SongEntity
|
2020-05-23 15:09:07 +00:00
|
|
|
import code.name.monkey.retromusic.fragments.ReloadType.*
|
2020-08-21 14:19:15 +00:00
|
|
|
import code.name.monkey.retromusic.helper.MusicPlayerRemote
|
2020-05-23 15:09:07 +00:00
|
|
|
import code.name.monkey.retromusic.interfaces.MusicServiceEventListener
|
|
|
|
import code.name.monkey.retromusic.model.*
|
2020-08-13 17:08:37 +00:00
|
|
|
import code.name.monkey.retromusic.repository.RealRepository
|
2020-05-23 15:09:07 +00:00
|
|
|
import kotlinx.coroutines.Dispatchers.IO
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
|
2020-07-20 19:05:48 +00:00
|
|
|
class LibraryViewModel(
|
2020-08-21 14:19:15 +00:00
|
|
|
private val repository: RealRepository
|
2020-07-20 19:05:48 +00:00
|
|
|
) : ViewModel(), MusicServiceEventListener {
|
2020-05-23 15:09:07 +00:00
|
|
|
|
2020-08-13 08:24:36 +00:00
|
|
|
private val paletteColor = MutableLiveData<Int>()
|
2020-07-28 19:18:34 +00:00
|
|
|
private val albums = MutableLiveData<List<Album>>()
|
|
|
|
private val songs = MutableLiveData<List<Song>>()
|
|
|
|
private val artists = MutableLiveData<List<Artist>>()
|
2020-09-05 14:03:12 +00:00
|
|
|
private val playlists = MutableLiveData<List<PlaylistWithSongs>>()
|
2020-09-05 20:13:45 +00:00
|
|
|
private val legacyPlaylists = MutableLiveData<List<Playlist>>()
|
2020-07-28 19:18:34 +00:00
|
|
|
private val genres = MutableLiveData<List<Genre>>()
|
|
|
|
private val home = MutableLiveData<List<Home>>()
|
|
|
|
|
|
|
|
val paletteColorLiveData: LiveData<Int> = paletteColor
|
2020-09-05 14:03:12 +00:00
|
|
|
|
2020-09-09 12:37:25 +00:00
|
|
|
init {
|
|
|
|
fetchHomeSections()
|
|
|
|
}
|
|
|
|
|
2020-09-05 14:03:12 +00:00
|
|
|
private fun loadLibraryContent() = viewModelScope.launch(IO) {
|
2020-09-09 12:37:25 +00:00
|
|
|
fetchHomeSections()
|
2020-09-05 14:03:12 +00:00
|
|
|
fetchSongs()
|
|
|
|
fetchAlbums()
|
|
|
|
fetchArtists()
|
|
|
|
fetchGenres()
|
|
|
|
fetchPlaylists()
|
2020-05-23 15:09:07 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 14:03:12 +00:00
|
|
|
fun getSongs(): LiveData<List<Song>> {
|
|
|
|
fetchSongs()
|
|
|
|
return songs
|
2020-05-23 15:09:07 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 14:03:12 +00:00
|
|
|
fun getAlbums(): LiveData<List<Album>> {
|
|
|
|
fetchAlbums()
|
|
|
|
return albums
|
|
|
|
}
|
|
|
|
|
|
|
|
fun getArtists(): LiveData<List<Artist>> {
|
|
|
|
fetchArtists()
|
|
|
|
return artists
|
|
|
|
}
|
|
|
|
|
|
|
|
fun getPlaylists(): LiveData<List<PlaylistWithSongs>> {
|
|
|
|
fetchPlaylists()
|
|
|
|
return playlists
|
|
|
|
}
|
|
|
|
|
2020-09-05 20:13:45 +00:00
|
|
|
fun getLegacyPlaylist(): LiveData<List<Playlist>> {
|
|
|
|
fetchLegacyPlaylist()
|
|
|
|
return legacyPlaylists
|
|
|
|
}
|
|
|
|
|
2020-09-05 14:03:12 +00:00
|
|
|
fun getGenre(): LiveData<List<Genre>> {
|
|
|
|
fetchGenres()
|
|
|
|
return genres
|
|
|
|
}
|
2020-05-23 15:09:07 +00:00
|
|
|
|
2020-09-05 14:03:12 +00:00
|
|
|
fun getHome(): LiveData<List<Home>> {
|
|
|
|
return home
|
|
|
|
}
|
2020-05-23 15:09:07 +00:00
|
|
|
|
2020-09-05 14:03:12 +00:00
|
|
|
private fun fetchSongs() {
|
|
|
|
viewModelScope.launch(IO) {
|
|
|
|
songs.postValue(repository.allSongs())
|
2020-05-23 15:09:07 +00:00
|
|
|
}
|
2020-09-05 14:03:12 +00:00
|
|
|
}
|
2020-05-25 22:40:40 +00:00
|
|
|
|
2020-09-05 14:03:12 +00:00
|
|
|
private fun fetchAlbums() {
|
|
|
|
viewModelScope.launch(IO) {
|
|
|
|
albums.postValue(repository.fetchAlbums())
|
2020-05-23 15:09:07 +00:00
|
|
|
}
|
2020-09-05 14:03:12 +00:00
|
|
|
}
|
2020-05-23 15:09:07 +00:00
|
|
|
|
2020-09-05 14:03:12 +00:00
|
|
|
private fun fetchArtists() {
|
|
|
|
viewModelScope.launch(IO) {
|
|
|
|
artists.postValue(repository.fetchArtists())
|
2020-05-23 15:09:07 +00:00
|
|
|
}
|
2020-09-05 14:03:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun fetchPlaylists() {
|
|
|
|
viewModelScope.launch(IO) {
|
|
|
|
playlists.postValue(repository.fetchPlaylistWithSongs())
|
2020-08-20 06:49:08 +00:00
|
|
|
}
|
2020-09-05 14:03:12 +00:00
|
|
|
}
|
2020-05-23 15:09:07 +00:00
|
|
|
|
2020-09-05 20:13:45 +00:00
|
|
|
private fun fetchLegacyPlaylist() {
|
|
|
|
viewModelScope.launch(IO) {
|
|
|
|
legacyPlaylists.postValue(repository.fetchLegacyPlaylist())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-05 14:03:12 +00:00
|
|
|
private fun fetchGenres() {
|
|
|
|
viewModelScope.launch(IO) {
|
|
|
|
genres.postValue(repository.fetchGenres())
|
2020-05-23 15:09:07 +00:00
|
|
|
}
|
2020-09-05 14:03:12 +00:00
|
|
|
}
|
2020-05-23 15:09:07 +00:00
|
|
|
|
2020-09-05 14:03:12 +00:00
|
|
|
private fun fetchHomeSections() {
|
|
|
|
viewModelScope.launch(IO) {
|
|
|
|
home.postValue(repository.homeSections())
|
|
|
|
}
|
|
|
|
}
|
2020-08-13 08:24:36 +00:00
|
|
|
|
2020-05-23 15:09:07 +00:00
|
|
|
fun forceReload(reloadType: ReloadType) = viewModelScope.launch {
|
|
|
|
when (reloadType) {
|
2020-09-05 14:03:12 +00:00
|
|
|
Songs -> fetchSongs()
|
|
|
|
Albums -> fetchAlbums()
|
|
|
|
Artists -> fetchArtists()
|
|
|
|
HomeSections -> fetchHomeSections()
|
|
|
|
Playlists -> fetchPlaylists()
|
|
|
|
Genres -> fetchGenres()
|
2020-05-23 15:09:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-28 19:18:34 +00:00
|
|
|
fun updateColor(newColor: Int) {
|
|
|
|
paletteColor.postValue(newColor)
|
|
|
|
}
|
|
|
|
|
2020-05-23 15:09:07 +00:00
|
|
|
override fun onMediaStoreChanged() {
|
2020-08-13 08:24:36 +00:00
|
|
|
println("onMediaStoreChanged")
|
2020-09-09 12:37:25 +00:00
|
|
|
loadLibraryContent()
|
2020-05-23 15:09:07 +00:00
|
|
|
}
|
|
|
|
|
2020-08-13 08:24:36 +00:00
|
|
|
override fun onServiceConnected() {
|
|
|
|
println("onServiceConnected")
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onServiceDisconnected() {
|
|
|
|
println("onServiceDisconnected")
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onQueueChanged() {
|
|
|
|
println("onQueueChanged")
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onPlayingMetaChanged() {
|
|
|
|
println("onPlayingMetaChanged")
|
2020-08-31 12:30:07 +00:00
|
|
|
|
2020-08-13 08:24:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onPlayStateChanged() {
|
|
|
|
println("onPlayStateChanged")
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onRepeatModeChanged() {
|
|
|
|
println("onRepeatModeChanged")
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onShuffleModeChanged() {
|
|
|
|
println("onShuffleModeChanged")
|
|
|
|
}
|
2020-07-28 19:18:34 +00:00
|
|
|
|
2020-08-29 18:53:15 +00:00
|
|
|
fun shuffleSongs() = viewModelScope.launch(IO) {
|
|
|
|
val songs = repository.allSongs()
|
|
|
|
MusicPlayerRemote.openAndShuffleQueue(
|
|
|
|
songs,
|
|
|
|
true
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun renameRoomPlaylist(playListId: Int, name: String) = viewModelScope.launch(IO) {
|
|
|
|
repository.renameRoomPlaylist(playListId, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun deleteSongsInPlaylist(songs: List<SongEntity>) = viewModelScope.launch(IO) {
|
|
|
|
repository.deleteSongsInPlaylist(songs)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun deleteSongsFromPlaylist(playlists: List<PlaylistEntity>) = viewModelScope.launch(IO) {
|
2020-09-10 19:22:10 +00:00
|
|
|
repository.deletePlaylistSongs(playlists)
|
2020-08-29 18:53:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fun deleteRoomPlaylist(playlists: List<PlaylistEntity>) = viewModelScope.launch(IO) {
|
|
|
|
repository.deleteRoomPlaylist(playlists)
|
|
|
|
}
|
|
|
|
|
|
|
|
suspend fun albumById(id: Int) = repository.albumById(id)
|
|
|
|
suspend fun artistById(id: Int) = repository.artistById(id)
|
|
|
|
suspend fun favoritePlaylist() = repository.favoritePlaylist()
|
|
|
|
suspend fun isFavoriteSong(song: SongEntity) = repository.isFavoriteSong(song)
|
|
|
|
suspend fun insertSongs(songs: List<SongEntity>) = repository.insertSongs(songs)
|
|
|
|
suspend fun removeSongFromPlaylist(songEntity: SongEntity) =
|
|
|
|
repository.removeSongFromPlaylist(songEntity)
|
|
|
|
|
2020-09-05 20:13:45 +00:00
|
|
|
suspend fun checkPlaylistExists(playlistName: String): List<PlaylistEntity> =
|
|
|
|
repository.checkPlaylistExists(playlistName)
|
|
|
|
|
|
|
|
suspend fun createPlaylist(playlistEntity: PlaylistEntity): Long =
|
|
|
|
repository.createPlaylist(playlistEntity)
|
|
|
|
|
2020-09-09 12:37:25 +00:00
|
|
|
fun importPlaylists() = viewModelScope.launch(IO) {
|
|
|
|
val playlists = repository.fetchLegacyPlaylist()
|
|
|
|
playlists.forEach { playlist ->
|
2020-09-10 19:22:10 +00:00
|
|
|
val playlistEntity = repository.checkPlaylistExists(playlist.name).firstOrNull()
|
2020-09-09 12:37:25 +00:00
|
|
|
if (playlistEntity != null) {
|
|
|
|
val songEntities = playlist.getSongs().map {
|
|
|
|
it.toSongEntity(playlistEntity.playListId)
|
|
|
|
}
|
|
|
|
repository.insertSongs(songEntities)
|
|
|
|
} else {
|
|
|
|
val playListId = createPlaylist(PlaylistEntity(playlist.name))
|
|
|
|
val songEntities = playlist.getSongs().map {
|
|
|
|
it.toSongEntity(playListId.toInt())
|
|
|
|
}
|
|
|
|
repository.insertSongs(songEntities)
|
|
|
|
}
|
|
|
|
forceReload(Playlists)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun deleteTracks(songs: List<Song>) = viewModelScope.launch(IO) {
|
|
|
|
repository.deleteSongs(songs)
|
2020-09-10 19:22:10 +00:00
|
|
|
fetchPlaylists()
|
|
|
|
loadLibraryContent()
|
2020-09-09 12:37:25 +00:00
|
|
|
}
|
2020-05-23 15:09:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum class ReloadType {
|
|
|
|
Songs,
|
|
|
|
Albums,
|
|
|
|
Artists,
|
2020-08-20 09:52:38 +00:00
|
|
|
HomeSections,
|
|
|
|
Playlists,
|
|
|
|
Genres,
|
2020-05-23 15:09:07 +00:00
|
|
|
}
|