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
|
|
|
|
2020-09-06 07:18:47 +00:00
|
|
|
import androidx.lifecycle.LiveData
|
|
|
|
import androidx.lifecycle.ViewModel
|
|
|
|
import androidx.lifecycle.liveData
|
2020-09-23 20:55:12 +00:00
|
|
|
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
|
2020-09-06 07:18:47 +00:00
|
|
|
import code.name.monkey.retromusic.network.Result
|
2020-07-20 19:51:15 +00:00
|
|
|
import code.name.monkey.retromusic.network.model.LastFmAlbum
|
2020-08-13 17:08:37 +00:00
|
|
|
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,
|
2020-09-17 21:25:41 +00:00
|
|
|
private val albumId: Long
|
2020-09-23 20:55:12 +00:00
|
|
|
) : 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
|
|
|
}
|
|
|
|
|
2020-09-17 21:25:41 +00:00
|
|
|
fun getArtist(artistId: Long): LiveData<Artist> = liveData(IO) {
|
2020-09-12 19:39:46 +00:00
|
|
|
val artist = repository.artistById(artistId)
|
2020-09-06 07:18:47 +00:00
|
|
|
emit(artist)
|
2020-05-25 22:40:40 +00:00
|
|
|
}
|
|
|
|
|
2020-09-06 07:18:47 +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
|
|
|
}
|
|
|
|
|
2020-09-06 07:18:47 +00:00
|
|
|
fun getMoreAlbums(artist: Artist): LiveData<List<Album>> = liveData(IO) {
|
2020-09-17 21:25:41 +00:00
|
|
|
artist.albums.filter { item -> item.id != albumId }.let { albums ->
|
2020-09-06 07:18:47 +00:00
|
|
|
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
|
|
|
}
|