Alex Vanyo
05/14/2021, 7:31 PMSignInScreen
in Jetsurvey
.
The sign-in button is only enabled when the emailState
and passwordState
are deemed valid, which works most of the time.
However, suppose email
is valid, so the button is enabled. If the email
is updated to become invalid just before the sign-in button is tapped (but within the same frame), onSignInSubmitted
will be called with the invalid email
, bypassing the validation.Casey Brooks
05/14/2021, 7:41 PMonSignInSubmitted
. And then the view itself just passively displays whether the email is valid or not, having it passed in from its function parameters.
In general, lifting the state up into the “parent” composables and passing the relevant values in through the function parameters can help eliminate these kinds of subtle UI issues/race conditions. If you’re concerned about this kind of situation, it’s probably a good indicator that the state should be lifted up and passed in, rather than remember [ }
-d directly in the functionAdam Powell
05/14/2021, 7:48 PMAlex Vanyo
05/14/2021, 8:13 PMonClick
lambda itself the method to sign in should also indicate a failure so that the resulting navigation shouldn’t occur. Or perhaps the “navigation to run on success” is itself passed as a lambda to the data model?Adam Powell
05/14/2021, 8:17 PMif (dataModel.submit(data)) {
navigateTo(destination)
}
Adam Powell
05/14/2021, 8:18 PMsuspend fun
to the rescue)Alex Vanyo
05/14/2021, 8:34 PMsuspend fun
could also implicitly handle any more complicated logic that the data model is doing. If the requirement was to cancel an ongoing request if the inputs changed (email, password), or prevent multiple simultaneous requests from the client UI, then the data model would be in control of its own state and whatever any of its functions return as it gets updates on what the user is doing.Adam Powell
05/14/2021, 8:36 PMAdam Powell
05/14/2021, 8:40 PMMutatorMutex
for one exampleAlex Vanyo
05/14/2021, 9:00 PMactor
pattern quite a bit for serially handling events in the data model (pre-Compose). It’s useful that suspend fun
+ cancellation can support a lot of implementation behavior with a “common” contract.Adam Powell
05/14/2021, 9:32 PM