79 lines
2.6 KiB
Kotlin
79 lines
2.6 KiB
Kotlin
/*
|
|
* 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.
|
|
*
|
|
*/
|
|
package code.name.monkey.retromusic.fragments.albums
|
|
|
|
import androidx.lifecycle.*
|
|
import code.name.monkey.retromusic.interfaces.IMusicServiceEventListener
|
|
import code.name.monkey.retromusic.model.Album
|
|
import code.name.monkey.retromusic.model.Artist
|
|
import code.name.monkey.retromusic.network.Result
|
|
import code.name.monkey.retromusic.network.model.LastFmAlbum
|
|
import code.name.monkey.retromusic.repository.RealRepository
|
|
import kotlinx.coroutines.Dispatchers.IO
|
|
import kotlinx.coroutines.launch
|
|
|
|
class AlbumDetailsViewModel(
|
|
private val repository: RealRepository,
|
|
private val albumId: Long
|
|
) : ViewModel(), IMusicServiceEventListener {
|
|
private val albumDetails = MutableLiveData<Album>()
|
|
|
|
init {
|
|
fetchAlbum()
|
|
}
|
|
|
|
private fun fetchAlbum() {
|
|
viewModelScope.launch(IO) {
|
|
albumDetails.postValue(repository.albumByIdAsync(albumId))
|
|
}
|
|
}
|
|
|
|
fun getAlbum(): LiveData<Album> = albumDetails
|
|
|
|
fun getArtist(artistId: Long): LiveData<Artist> = liveData(IO) {
|
|
val artist = repository.artistById(artistId)
|
|
emit(artist)
|
|
}
|
|
|
|
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)
|
|
emit(repository.albumInfo(album.artistName, album.title))
|
|
}
|
|
|
|
fun getMoreAlbums(artist: Artist): LiveData<List<Album>> = liveData(IO) {
|
|
artist.albums.filter { item -> item.id != albumId }.let { albums ->
|
|
if (albums.isNotEmpty()) emit(albums)
|
|
}
|
|
}
|
|
|
|
override fun onMediaStoreChanged() {
|
|
fetchAlbum()
|
|
}
|
|
|
|
override fun onServiceConnected() {}
|
|
override fun onServiceDisconnected() {}
|
|
override fun onQueueChanged() {}
|
|
override fun onPlayingMetaChanged() {}
|
|
override fun onPlayStateChanged() {}
|
|
override fun onRepeatModeChanged() {}
|
|
override fun onShuffleModeChanged() {}
|
|
override fun onFavoriteStateChanged() {}
|
|
}
|