rememberQueryAndSearchOnChange

fun <Q, S : SearchState<*, Q>> rememberQueryAndSearchOnChange(actions: SearchActions<*, Q, S>, initialQuery: Q, debounceTime: Long = 300, resultCollectionContext: CoroutineContext = Dispatchers.Main): MutableState<Q>

Utility composable function to remember a search query, observe changes to it and start the search after a debounce time. While the actual search is always executed on the Dispatchers.Default context, the results are collected on resultCollectionContext, which defaults to Dispatchers.Main.

Usage:

@Composable
fun SearchUI(document: DocumentController) {
var query by rememberQueryAndSearchOnChange(document.textSearch.actions, "")
TextField(value = query, onValueChange = { query = it })

// observe the search state
val searchState by document.textSearch.state.collectAsState()

// display the search results
when (searchState.status) {
SearchStatus.SEARCHING -> {...}
SearchStatus.COMPLETED -> {...}
SearchStatus.IDLE -> {...}
}
}

Return

a MutableState that holds the current query value and can be used to update it

Parameters

actions

the search actions to be performed

initialQuery

the initial query value

debounceTime

the time in milliseconds to debounce the query changes. The query will only start searching after this time has passed since the last query change.

resultCollectionContext

the CoroutineContext in which the search results are collected.