discard old search results when new results come in

This commit is contained in:
Satindar Dhillon 2023-01-10 12:51:41 -08:00
parent 8faf23d4ba
commit 8349a05dfd

View file

@ -56,10 +56,24 @@ class LibraryViewModel @Inject constructor(
}
}
fun load(clearPreviousSearch: Boolean = false) {
viewModelScope.launch {
if (searchTextLiveData.value != "") {
performSearch(clearPreviousSearch)
} else {
syncItems()
}
}
}
private suspend fun syncItems() {
val syncStart = LocalDateTime.now()
val lastSyncDate = getLastSyncTime() ?: LocalDateTime.MIN
CoroutineScope(Dispatchers.Main).launch {
isRefreshing = false
}
withContext(Dispatchers.IO) {
performItemSync(cursor = null, since = lastSyncDate.toString(), count = 0)
}
@ -80,10 +94,6 @@ class LibraryViewModel @Inject constructor(
val items = dataService.db.savedItemDao().getLibraryData()
itemsLiveData.postValue(items)
CoroutineScope(Dispatchers.Main).launch {
isRefreshing = false
}
}
}
@ -108,7 +118,7 @@ class LibraryViewModel @Inject constructor(
}
val previousItems = if (clearPreviousSearch) listOf() else searchedItems
searchedItems = previousItems.plus(searchResult.cardsData)
searchedItems = searchResult.cardsData
itemsLiveData.postValue(searchedItems)
CoroutineScope(Dispatchers.Main).launch {
@ -116,16 +126,6 @@ class LibraryViewModel @Inject constructor(
}
}
fun load(clearPreviousSearch: Boolean = false) {
viewModelScope.launch {
if (searchTextLiveData.value != "") {
performSearch(clearPreviousSearch)
} else {
syncItems()
}
}
}
fun handleSavedItemAction(itemID: String, action: SavedItemAction) {
when (action) {
SavedItemAction.Delete -> {