Added clear history fun & change observableHistorySongs to MutableLiveData
This commit is contained in:
parent
0cdc83818a
commit
71282919d3
1 changed files with 18 additions and 7 deletions
|
@ -49,6 +49,7 @@ class LibraryViewModel(
|
||||||
private val genres = MutableLiveData<List<Genre>>()
|
private val genres = MutableLiveData<List<Genre>>()
|
||||||
private val searchResults = MutableLiveData<List<Any>>()
|
private val searchResults = MutableLiveData<List<Any>>()
|
||||||
private val fabMargin = MutableLiveData(0)
|
private val fabMargin = MutableLiveData(0)
|
||||||
|
private val songHistory = MutableLiveData<List<Song>>()
|
||||||
val paletteColor: LiveData<Int> = _paletteColor
|
val paletteColor: LiveData<Int> = _paletteColor
|
||||||
|
|
||||||
init {
|
init {
|
||||||
|
@ -320,20 +321,30 @@ class LibraryViewModel(
|
||||||
emit(repository.contributor())
|
emit(repository.contributor())
|
||||||
}
|
}
|
||||||
|
|
||||||
fun observableHistorySongs(): LiveData<List<Song>> = liveData {
|
fun observableHistorySongs(): LiveData<List<Song>> {
|
||||||
val songs = repository.historySong().map {
|
val songs = repository.historySong().map {
|
||||||
it.toSong()
|
it.toSong()
|
||||||
}
|
}
|
||||||
emit(songs)
|
songHistory.value = songs
|
||||||
// Cleaning up deleted or moved songs
|
// Cleaning up deleted or moved songs
|
||||||
songs.forEach { song ->
|
viewModelScope.launch {
|
||||||
if (!File(song.data).exists() || song.id == -1L) {
|
songs.forEach { song ->
|
||||||
repository.deleteSongInHistory(song.id)
|
if (!File(song.data).exists() || song.id == -1L) {
|
||||||
|
repository.deleteSongInHistory(song.id)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
emit(repository.historySong().map {
|
songHistory.value = repository.historySong().map {
|
||||||
it.toSong()
|
it.toSong()
|
||||||
})
|
}
|
||||||
|
return songHistory
|
||||||
|
}
|
||||||
|
|
||||||
|
fun clearHistory() {
|
||||||
|
viewModelScope.launch(IO) {
|
||||||
|
repository.clearSongHistory()
|
||||||
|
}
|
||||||
|
songHistory.value = emptyList()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun favorites() = repository.favorites()
|
fun favorites() = repository.favorites()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue