Removed unnecessary debouncing code from search functionality.

* Refactored SearchViewModelTest.
This commit is contained in:
MohitMaliFtechiz 2023-12-20 12:15:50 +05:30
parent d0de528dc8
commit 3eda149636
3 changed files with 4 additions and 66 deletions

View File

@ -64,6 +64,7 @@ import org.kiwix.kiwixmobile.core.search.viewmodel.Action
import org.kiwix.kiwixmobile.core.search.viewmodel.Action.ActivityResultReceived import org.kiwix.kiwixmobile.core.search.viewmodel.Action.ActivityResultReceived
import org.kiwix.kiwixmobile.core.search.viewmodel.Action.ClickedSearchInText import org.kiwix.kiwixmobile.core.search.viewmodel.Action.ClickedSearchInText
import org.kiwix.kiwixmobile.core.search.viewmodel.Action.ExitedSearch import org.kiwix.kiwixmobile.core.search.viewmodel.Action.ExitedSearch
import org.kiwix.kiwixmobile.core.search.viewmodel.Action.Filter
import org.kiwix.kiwixmobile.core.search.viewmodel.Action.OnItemClick import org.kiwix.kiwixmobile.core.search.viewmodel.Action.OnItemClick
import org.kiwix.kiwixmobile.core.search.viewmodel.Action.OnItemLongClick import org.kiwix.kiwixmobile.core.search.viewmodel.Action.OnItemLongClick
import org.kiwix.kiwixmobile.core.search.viewmodel.Action.OnOpenInNewTabClick import org.kiwix.kiwixmobile.core.search.viewmodel.Action.OnOpenInNewTabClick
@ -222,7 +223,7 @@ class SearchFragment : BaseFragment() {
searchView?.setOnQueryTextListener( searchView?.setOnQueryTextListener(
SimpleTextListener { SimpleTextListener {
if (it.isNotEmpty()) { if (it.isNotEmpty()) {
searchViewModel.searchResults(it) searchViewModel.actions.trySend(Filter(it)).isSuccess
} }
} }
) )

View File

@ -88,20 +88,10 @@ class SearchViewModel @Inject constructor(
val actions = Channel<Action>(Channel.UNLIMITED) val actions = Channel<Action>(Channel.UNLIMITED)
private val filter = ConflatedBroadcastChannel("") private val filter = ConflatedBroadcastChannel("")
private val searchOrigin = ConflatedBroadcastChannel(FromWebView) private val searchOrigin = ConflatedBroadcastChannel(FromWebView)
private val debouncedSearchQuery = MutableStateFlow("")
init { init {
viewModelScope.launch { reducer() } viewModelScope.launch { reducer() }
viewModelScope.launch { actionMapper() } viewModelScope.launch { actionMapper() }
viewModelScope.launch { debouncedSearchQuery() }
}
private suspend fun debouncedSearchQuery() {
// Observe and collect the debounced search query
debouncedSearchQuery
.collect { query ->
actions.trySend(Filter(query)).isSuccess
}
} }
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
@ -211,10 +201,6 @@ class SearchViewModel @Inject constructor(
}, LATEST) }, LATEST)
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
} }
fun searchResults(query: String) {
debouncedSearchQuery.value = query
}
} }
data class SearchResultsWithTerm(val searchTerm: String, val suggestionSearch: SuggestionSearch?) data class SearchResultsWithTerm(val searchTerm: String, val suggestionSearch: SuggestionSearch?)

View File

@ -29,12 +29,10 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.consumeAsFlow import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.test.TestCoroutineDispatcher import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.TestCoroutineScheduler
import kotlinx.coroutines.test.TestCoroutineScope import kotlinx.coroutines.test.TestCoroutineScope
import kotlinx.coroutines.test.TestScope import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.resetMain import kotlinx.coroutines.test.resetMain
@ -59,6 +57,7 @@ import org.kiwix.kiwixmobile.core.search.viewmodel.Action.ClickedSearchInText
import org.kiwix.kiwixmobile.core.search.viewmodel.Action.ConfirmedDelete import org.kiwix.kiwixmobile.core.search.viewmodel.Action.ConfirmedDelete
import org.kiwix.kiwixmobile.core.search.viewmodel.Action.CreatedWithArguments import org.kiwix.kiwixmobile.core.search.viewmodel.Action.CreatedWithArguments
import org.kiwix.kiwixmobile.core.search.viewmodel.Action.ExitedSearch import org.kiwix.kiwixmobile.core.search.viewmodel.Action.ExitedSearch
import org.kiwix.kiwixmobile.core.search.viewmodel.Action.Filter
import org.kiwix.kiwixmobile.core.search.viewmodel.Action.OnItemClick import org.kiwix.kiwixmobile.core.search.viewmodel.Action.OnItemClick
import org.kiwix.kiwixmobile.core.search.viewmodel.Action.OnItemLongClick import org.kiwix.kiwixmobile.core.search.viewmodel.Action.OnItemLongClick
import org.kiwix.kiwixmobile.core.search.viewmodel.Action.OnOpenInNewTabClick import org.kiwix.kiwixmobile.core.search.viewmodel.Action.OnOpenInNewTabClick
@ -110,54 +109,6 @@ internal class SearchViewModelTest {
viewModel = SearchViewModel(recentSearchDao, zimReaderContainer, searchResultGenerator) viewModel = SearchViewModel(recentSearchDao, zimReaderContainer, searchResultGenerator)
} }
@Nested
inner class DebouncedTest {
@Test
fun `Search action is debounced`() = runTest {
val searchTerm1 = "query1"
val searchTerm2 = "query2"
val searchTerm3 = "query3"
val searchOrigin = FromWebView
val suggestionSearch: SuggestionSearch = mockk()
viewModel.state
.test(this)
.also {
searchResult(searchTerm1, suggestionSearch, testScheduler)
delay(100)
searchResult(searchTerm2, suggestionSearch, testScheduler)
delay(100)
searchResult(searchTerm3, suggestionSearch, testScheduler)
it.assertValue(
SearchState(
searchTerm3,
SearchResultsWithTerm(searchTerm3, suggestionSearch),
emptyList(),
searchOrigin
)
)
}
.finish()
}
private fun searchResult(
searchTerm: String,
suggestionSearch: SuggestionSearch,
testScheduler: TestCoroutineScheduler
) {
coEvery {
searchResultGenerator.generateSearchResults(searchTerm, zimFileReader)
} returns suggestionSearch
viewModel.searchResults(searchTerm)
recentsFromDb.trySend(emptyList()).isSuccess
viewModel.actions.trySend(ScreenWasStartedFrom(FromWebView)).isSuccess
testScheduler.apply {
advanceTimeBy(400)
runCurrent()
}
}
}
@Nested @Nested
inner class StateTests { inner class StateTests {
@Test @Test
@ -301,7 +252,7 @@ internal class SearchViewModelTest {
coEvery { coEvery {
searchResultGenerator.generateSearchResults(searchTerm, zimFileReader) searchResultGenerator.generateSearchResults(searchTerm, zimFileReader)
} returns suggestionSearch } returns suggestionSearch
viewModel.searchResults(searchTerm) viewModel.actions.trySend(Filter(searchTerm)).isSuccess
recentsFromDb.trySend(databaseResults).isSuccess recentsFromDb.trySend(databaseResults).isSuccess
viewModel.actions.trySend(ScreenWasStartedFrom(searchOrigin)).isSuccess viewModel.actions.trySend(ScreenWasStartedFrom(searchOrigin)).isSuccess
testScheduler.apply { testScheduler.apply {