https://kotlinlang.org logo
Title
m

Mark

01/23/2023, 6:16 AM
Is this a documentation bug? It says to not delay updating the textfield when the value changes. But then the example does exactly that. Maybe there should be a comment explaining that this is what not to do? https://developer.android.com/jetpack/compose/text#state-mgmt
s

Stylianos Gakis

01/23/2023, 11:33 AM
Which example are you referring to?
m

Mark

01/23/2023, 11:34 AM
Do not delay making updates to the state: When you callonValueChange, update your TextField synchronously and immediately:
s

Stylianos Gakis

01/23/2023, 11:35 AM
Yes, but which example is it that “does exactly that” in the compose text documentation that you linked?
m

Mark

01/23/2023, 11:35 AM
The example directly following that colon
class SignUpViewModel(private val userRepository: UserRepository) : ViewModel() {
   fun updateUsername(input: String) {
        viewModelScope.launch {
            // async operation
            val isUsernameAvailable = userRepository.isUsernameAvailable(input)
            // ...
            if (!isUsernameAvailable) {
                // modify error state
            }
           username.value = input
       }
   }
}
Shouldn’t it be:
class SignUpViewModel(private val userRepository: UserRepository) : ViewModel() {
   fun updateUsername(input: String) {
        username.value = input
        viewModelScope.launch {
            // async operation
            val isUsernameAvailable = userRepository.isUsernameAvailable(input)
            // ...
            if (!isUsernameAvailable) {
                // modify error state
            }
       }
   }
}
s

Stylianos Gakis

01/23/2023, 11:46 AM
Aha I see now, yes this is odd. On one had, right above this it does say “Do not delay” so it may be that they are giving this as an example of how not to do it? Regardless I do agree that this is not clear. I’d create a bug report for the documentation and see what they respond with.
a

Albert Chang

01/23/2023, 12:21 PM
viewModelScope
uses
Dispatchers.Main.immediate
, which will run the job immediately and synchronously if it's already on main thread.
m

Mark

01/23/2023, 12:22 PM
isUsernameAvailable
takes time though, and who knows on which thread
a

Albert Chang

01/23/2023, 12:23 PM
Read your second reply again.
And all callbacks in compose are run on main thread.
m

Mark

01/23/2023, 12:26 PM
Sorry, I don’t understand. If
isUsernameAvailable
takes ages, how can it possibly set the value immediately?
s

Stylianos Gakis

01/23/2023, 12:27 PM
userRepository.isUsernameAvailable may switch dispatcher to say IO For example though, and this function will be waiting for that response to come back before it sets
username.value
. I feel like I am also missing smth here.
m

Mark

01/23/2023, 12:28 PM
Even if it doesn’t switch dispatcher, it still takes potentially a duration longer than “immediate”.
s

Stylianos Gakis

01/23/2023, 12:29 PM
If it doesn’t switch dispatcher, then it would work synchronously as it should, since it’s using
Dispatchers.main.immediate
, it’s just that then it’d make the UI hang as far as I understand at least.
m

Mark

01/23/2023, 12:30 PM
It says
synchronously and immediately
so that still wouldn’t comply
l

Landry Norris

01/23/2023, 2:29 PM
Wouldn't it make more sense to have the username update immediately, with some sort of method that asynchronously checks for error state? (bonus points for observing the username value and mapping it to potential error state). If the check takes some amount of time, such as when my phone has an sd card, or there's a lot of usernames taken, you don't want to slow down the text field's responsiveness.
e

eygraber

01/23/2023, 2:38 PM
Another issue with not updating the state immediately is that the
TextField
internal state can get messed up and start showing incorrect values / treatment because of how it interacts with the IME
a

Albert Chang

01/23/2023, 2:58 PM
Ok, I didn't notice the async operation comment. I guess that's an example of bad pattern.
c

Colton Idle

01/23/2023, 5:51 PM
Filed a bug? I'll star 🌠
a

Ale Stamato

01/26/2023, 10:56 AM
This doc is fixed now. TY for flagging and sorry for the confusion🙏
c

Colton Idle

01/26/2023, 7:18 PM
we did it!
e

eygraber

01/26/2023, 7:27 PM
@Ale Stamato the link in the note that should go to Effective state management for TextField in Compose instead goes to kotlinx StateFlow documentation