From 8349a05dfd73f696fe4694751c476077002385cc Mon Sep 17 00:00:00 2001 From: Satindar Dhillon Date: Tue, 10 Jan 2023 12:51:41 -0800 Subject: [PATCH] discard old search results when new results come in --- .../omnivore/ui/library/LibraryViewModel.kt | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/android/Omnivore/app/src/main/java/app/omnivore/omnivore/ui/library/LibraryViewModel.kt b/android/Omnivore/app/src/main/java/app/omnivore/omnivore/ui/library/LibraryViewModel.kt index 1b43c0d67..b555199d6 100644 --- a/android/Omnivore/app/src/main/java/app/omnivore/omnivore/ui/library/LibraryViewModel.kt +++ b/android/Omnivore/app/src/main/java/app/omnivore/omnivore/ui/library/LibraryViewModel.kt @@ -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 -> {