PlayerAndroid/app/src/main/java/code/name/monkey/retromusic/fragments/albums/AlbumDetailsViewModel.kt

79 lines
2.6 KiB
Kotlin
Raw Normal View History

2020-10-06 08:46:04 +00:00
/*
* Copyright (c) 2020 Hemanth Savarla.
*
* Licensed under the GNU General Public License v3
*
* This is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
*/
2020-08-11 18:29:44 +00:00
package code.name.monkey.retromusic.fragments.albums
2020-05-25 22:40:40 +00:00
import androidx.lifecycle.*
import code.name.monkey.retromusic.interfaces.IMusicServiceEventListener
2020-05-25 22:40:40 +00:00
import code.name.monkey.retromusic.model.Album
import code.name.monkey.retromusic.model.Artist
import code.name.monkey.retromusic.network.Result
2020-07-20 19:51:15 +00:00
import code.name.monkey.retromusic.network.model.LastFmAlbum
import code.name.monkey.retromusic.repository.RealRepository
2020-08-31 12:30:07 +00:00
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.launch
2020-05-25 22:40:40 +00:00
class AlbumDetailsViewModel(
2020-09-12 19:39:46 +00:00
private val repository: RealRepository,
private val albumId: Long
) : ViewModel(), IMusicServiceEventListener {
private val albumDetails = MutableLiveData<Album>()
2020-07-20 19:05:48 +00:00
init {
fetchAlbum()
2020-08-31 12:30:07 +00:00
}
private fun fetchAlbum() {
viewModelScope.launch(IO) {
albumDetails.postValue(repository.albumByIdAsync(albumId))
}
}
fun getAlbum(): LiveData<Album> = albumDetails
fun getArtist(artistId: Long): LiveData<Artist> = liveData(IO) {
2020-09-12 19:39:46 +00:00
val artist = repository.artistById(artistId)
emit(artist)
2020-05-25 22:40:40 +00:00
}
fun getAlbumArtist(artistName: String): LiveData<Artist> = liveData(IO) {
val artist = repository.albumArtistByName(artistName)
emit(artist)
}
fun getAlbumInfo(album: Album): LiveData<Result<LastFmAlbum>> = liveData {
emit(Result.Loading)
2021-10-11 06:17:55 +00:00
emit(repository.albumInfo(album.artistName, album.title))
2020-05-25 22:40:40 +00:00
}
fun getMoreAlbums(artist: Artist): LiveData<List<Album>> = liveData(IO) {
artist.albums.filter { item -> item.id != albumId }.let { albums ->
if (albums.isNotEmpty()) emit(albums)
2020-07-24 18:28:15 +00:00
}
2020-05-25 22:40:40 +00:00
}
2020-06-07 18:21:49 +00:00
override fun onMediaStoreChanged() {
fetchAlbum()
2020-06-07 18:21:49 +00:00
}
override fun onServiceConnected() {}
override fun onServiceDisconnected() {}
override fun onQueueChanged() {}
override fun onPlayingMetaChanged() {}
override fun onPlayStateChanged() {}
override fun onRepeatModeChanged() {}
override fun onShuffleModeChanged() {}
override fun onFavoriteStateChanged() {}
2020-10-06 08:46:04 +00:00
}