Added clear history fun & change observableHistorySongs to MutableLiveData

This commit is contained in:
Omar 2022-03-04 14:03:45 +02:00
parent 0cdc83818a
commit 71282919d3

View file

@ -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()