Mark
01/23/2023, 6:16 AMStylianos Gakis
01/23/2023, 11:33 AMMark
01/23/2023, 11:34 AMDo not delay making updates to the state: When you callonValueChange, update your TextField synchronously and immediately:
Stylianos Gakis
01/23/2023, 11:35 AMMark
01/23/2023, 11:35 AMclass 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
}
}
}
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
}
}
}
}
Stylianos Gakis
01/23/2023, 11:46 AMAlbert Chang
01/23/2023, 12:21 PMviewModelScope
uses Dispatchers.Main.immediate
, which will run the job immediately and synchronously if it's already on main thread.Mark
01/23/2023, 12:22 PMisUsernameAvailable
takes time though, and who knows on which threadAlbert Chang
01/23/2023, 12:23 PMMark
01/23/2023, 12:26 PMisUsernameAvailable
takes ages, how can it possibly set the value immediately?Stylianos Gakis
01/23/2023, 12:27 PMusername.value
. I feel like I am also missing smth here.Mark
01/23/2023, 12:28 PMStylianos Gakis
01/23/2023, 12:29 PMDispatchers.main.immediate
, it’s just that then it’d make the UI hang as far as I understand at least.Mark
01/23/2023, 12:30 PMsynchronously and immediately
so that still wouldn’t complyLandry Norris
01/23/2023, 2:29 PMeygraber
01/23/2023, 2:38 PMTextField
internal state can get messed up and start showing incorrect values / treatment because of how it interacts with the IMEAlbert Chang
01/23/2023, 2:58 PMColton Idle
01/23/2023, 5:51 PMAle Stamato
01/26/2023, 10:56 AMColton Idle
01/26/2023, 7:18 PMeygraber
01/26/2023, 7:27 PM