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-07-19 21:00:30 +00:00
|
|
|
import code.name.monkey.retromusic.adapter.HomeAdapter
|
2020-05-23 15:09:07 +00:00
|
|
|
import code.name.monkey.retromusic.fragments.ReloadType.*
|
|
|
|
import code.name.monkey.retromusic.interfaces.MusicServiceEventListener
|
|
|
|
import code.name.monkey.retromusic.model.*
|
|
|
|
import code.name.monkey.retromusic.providers.RepositoryImpl
|
|
|
|
import kotlinx.coroutines.Deferred
|
|
|
|
import kotlinx.coroutines.Dispatchers.IO
|
|
|
|
import kotlinx.coroutines.async
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
|
2020-07-20 19:05:48 +00:00
|
|
|
class LibraryViewModel(
|
|
|
|
private val repository: RepositoryImpl
|
|
|
|
) : ViewModel(), MusicServiceEventListener {
|
2020-05-23 15:09:07 +00:00
|
|
|
|
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>>()
|
|
|
|
private val playlists = MutableLiveData<List<Playlist>>()
|
|
|
|
private val genres = MutableLiveData<List<Genre>>()
|
|
|
|
private val home = MutableLiveData<List<Home>>()
|
|
|
|
private val paletteColor = MutableLiveData<Int>()
|
|
|
|
|
|
|
|
val paletteColorLiveData: LiveData<Int> = paletteColor
|
|
|
|
val homeLiveData: LiveData<List<Home>> = home
|
|
|
|
val albumsLiveData: LiveData<List<Album>> = albums
|
|
|
|
val songsLiveData: LiveData<List<Song>> = songs
|
|
|
|
val artistsLiveData: LiveData<List<Artist>> = artists
|
|
|
|
val playlisitsLiveData: LiveData<List<Playlist>> = playlists
|
|
|
|
val genresLiveData: LiveData<List<Genre>> = genres
|
2020-05-23 15:09:07 +00:00
|
|
|
|
|
|
|
init {
|
|
|
|
viewModelScope.launch {
|
|
|
|
loadLibraryContent()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun loadLibraryContent() = viewModelScope.launch {
|
2020-07-28 19:18:34 +00:00
|
|
|
songs.value = loadSongs.await()
|
|
|
|
albums.value = loadAlbums.await()
|
|
|
|
artists.value = loadArtists.await()
|
|
|
|
playlists.value = loadPlaylists.await()
|
|
|
|
genres.value = loadGenres.await()
|
2020-06-07 18:21:49 +00:00
|
|
|
loadHomeSections()
|
2020-05-23 15:09:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun loadHomeSections() = viewModelScope.launch {
|
|
|
|
val list = mutableListOf<Home>()
|
|
|
|
val result = listOf(
|
2020-07-20 19:05:48 +00:00
|
|
|
repository.topArtists(),
|
|
|
|
repository.topAlbums(),
|
|
|
|
repository.recentArtists(),
|
2020-08-11 21:31:09 +00:00
|
|
|
repository.recentAlbums()/*,
|
2020-07-20 19:05:48 +00:00
|
|
|
repository.suggestions(),
|
2020-07-25 20:22:37 +00:00
|
|
|
repository.favoritePlaylist(),
|
2020-08-11 21:31:09 +00:00
|
|
|
repository.homeGenres()*/
|
2020-05-23 15:09:07 +00:00
|
|
|
)
|
2020-07-19 21:00:30 +00:00
|
|
|
result.forEach {
|
|
|
|
if (it != null && it.arrayList.isNotEmpty()) {
|
|
|
|
if (it.homeSection == HomeAdapter.SUGGESTIONS) {
|
|
|
|
if (it.arrayList.size > 9) {
|
|
|
|
list.add(it)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
list.add(it)
|
|
|
|
}
|
2020-05-23 15:09:07 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-28 19:18:34 +00:00
|
|
|
home.value = list
|
2020-05-23 15:09:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private val loadSongs: Deferred<List<Song>>
|
|
|
|
get() = viewModelScope.async(IO) {
|
2020-07-20 19:05:48 +00:00
|
|
|
repository.allSongs()
|
2020-05-23 15:09:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private val loadAlbums: Deferred<List<Album>>
|
|
|
|
get() = viewModelScope.async(IO) {
|
2020-07-20 19:05:48 +00:00
|
|
|
repository.allAlbums()
|
2020-05-23 15:09:07 +00:00
|
|
|
}
|
2020-05-25 22:40:40 +00:00
|
|
|
|
2020-05-23 15:09:07 +00:00
|
|
|
private val loadArtists: Deferred<List<Artist>>
|
|
|
|
get() = viewModelScope.async(IO) {
|
2020-07-20 19:05:48 +00:00
|
|
|
repository.allArtists()
|
2020-05-23 15:09:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private val loadPlaylists: Deferred<List<Playlist>>
|
|
|
|
get() = viewModelScope.async(IO) {
|
2020-07-20 19:05:48 +00:00
|
|
|
repository.allPlaylists()
|
2020-05-23 15:09:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private val loadGenres: Deferred<List<Genre>>
|
|
|
|
get() = viewModelScope.async(IO) {
|
2020-07-20 19:05:48 +00:00
|
|
|
repository.allGenres()
|
2020-05-23 15:09:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fun forceReload(reloadType: ReloadType) = viewModelScope.launch {
|
|
|
|
when (reloadType) {
|
2020-07-28 19:18:34 +00:00
|
|
|
Songs -> songs.value = loadSongs.await()
|
|
|
|
Albums -> albums.value = loadAlbums.await()
|
|
|
|
Artists -> artists.value = loadArtists.await()
|
|
|
|
HomeSections -> songs.value = loadSongs.await()
|
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() {
|
|
|
|
loadLibraryContent()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onServiceConnected() {}
|
|
|
|
override fun onServiceDisconnected() {}
|
|
|
|
override fun onQueueChanged() {}
|
|
|
|
override fun onPlayingMetaChanged() {}
|
|
|
|
override fun onPlayStateChanged() {}
|
|
|
|
override fun onRepeatModeChanged() {}
|
|
|
|
override fun onShuffleModeChanged() {}
|
2020-07-28 19:18:34 +00:00
|
|
|
|
2020-05-23 15:09:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum class ReloadType {
|
|
|
|
Songs,
|
|
|
|
Albums,
|
|
|
|
Artists,
|
|
|
|
HomeSections
|
|
|
|
}
|