Last active
May 13, 2025 08:13
-
-
Save thasneemp/f94ac09d8077770df6f90ad7bb54ead5 to your computer and use it in GitHub Desktop.
Android EditText text change with flow coroutine debounce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun EditText.textChanges(): Flow<String> { | |
| return callbackFlow { | |
| val listener = doOnTextChanged { text, _, _, _ -> trySend(text.toString()) } | |
| awaitClose { removeTextChangedListener(listener) } | |
| }.onStart { emit(text.toString()) } | |
| } | |
| private fun searchQuery(searchQuery: String): Flow<List<String>> { | |
| val items = listOf("Hello", "How are You", "Oh no!", "Iam Good!") | |
| return flow { | |
| delay(200) | |
| emit(items.filter { it.contains(searchQuery) }) | |
| } | |
| } | |
| getDataBinding().userNameEditText.textChanges().filterNot { it.isBlank() } | |
| .debounce(300).distinctUntilChanged().flatMapLatest { searchQuery(it) } | |
| .onEach { | |
| Log.d("SEARCH_TEST", it.toString()) | |
| }.launchIn(lifecycleScope) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment