Debouncing TextFields in Compose

28/12/2023

Efficient Input Handling in Compose

Debouncing user input in Android’s Compose framework is essential for creating responsive and resource-efficient applications. This note demonstrates two approaches to implement debouncing for TextFields using Kotlin.

ViewModel Approach

class DebounceViewModel : ViewModel() {
    val userInput = MutableStateFlow("")
    val debouncedInput = userInput.debounce(2500)
        .distinctUntilChanged()
        .flatMapLatest { input ->
            processInput(input)
        }

    private fun processInput(input: String): String {
        // Implementation goes here...
    }
}

This method involves a ViewModel (DebounceViewModel) that debounces and processes the input text. Here, debounce(300) ensures a 300-millisecond delay between user inputs before processing, reducing unnecessary database queries or operations. distinctUntilChanged prevents repeated processing of the same input, while flatMapLatest ensures only the latest input is considered.

Composable Function Approach

@Composable
fun DebouncingTextField(onInputChanged: (String) -> Unit) {
    var textState by remember { mutableStateOf(TextFieldValue("")) }

    TextField(
        value = textState,
        onValueChange = { textState = it },
        label = { Text("Enter text") }
    )

    LaunchedEffect(key1 = textState) {
        if (textState.text.isNotEmpty()) {
            delay(300)
            onInputChanged(textState.text)
        }
    }
}

This Composable function that utilizes LaunchedEffect with a 300-millisecond delay (delay(300)) to debounce the input. The function reacts only to non-empty inputs and invokes onInputChanged after the specified delay, ensuring the application’s responsiveness and efficiency in handling user inputs.

Both methods present practical and adaptable solutions for input debouncing in Compose. Depending on your application’s requirements and structure, you may choose the one that best suits your needs.