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

64 lines
2.2 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.LiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.liveData
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
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 {
2020-07-20 19:05:48 +00:00
2020-08-31 12:30:07 +00:00
fun getAlbum(): LiveData<Album> = liveData(IO) {
2020-09-15 18:15:35 +00:00
emit(repository.albumByIdAsync(albumId))
2020-08-31 12:30:07 +00:00
}
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 getAlbumInfo(album: Album): LiveData<Result<LastFmAlbum>> = liveData {
emit(Result.Loading)
2020-09-12 19:39:46 +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() {
}
override fun onServiceConnected() {}
override fun onServiceDisconnected() {}
override fun onQueueChanged() {}
override fun onPlayingMetaChanged() {}
override fun onPlayStateChanged() {}
override fun onRepeatModeChanged() {}
override fun onShuffleModeChanged() {}
2020-10-06 08:46:04 +00:00
}