Open ryanw-mobile opened 1 month ago
This is an example how we could implement this using Flow (which simulates RxJava/XML)
class SearchViewModel : ViewModel() {
private val _query = MutableStateFlow("")
init {
viewModelScope.launch {
_query
.debounce(300) // Debounce the input by 300ms
.distinctUntilChanged() // Only process if the value has changed
.collectLatest { searchQuery ->
performSearch(searchQuery) // Perform the search operation
}
}
}
fun onSearchQueryChanged(query: String) {
_query.value = query // Update the query in StateFlow
}
private fun performSearch(query: String) {
// Perform the search (e.g., network/API call)
}
}
This can be a good application we use Kotlin Flow so the search keyword submission can be automated (and we don't need the search button)