Christoffer Niska
09/23/2021, 8:34 AMtheapache64
09/23/2021, 9:04 AMCsaba Kozák
09/23/2021, 9:06 AMstatusText
there is no need to check the previous state. Just check the current state map it to a string resource and emit the StatusText()
composable.
Clearing focus and hiding keyboard are side effects. You should not call these directly in you composable functions. For example you can use:
LaunchedEffect(uiState.status) {
// manage focus and keyboard
}
This effect will take a key, and it only launches when the key changes.
There is no need to store the previous state in your case. BTW, compose will only recompose the parts of your compose tree, when the input changes.
https://developer.android.com/jetpack/compose/lifecycle
https://developer.android.com/jetpack/compose/side-effectsChristoffer Niska
09/23/2021, 9:08 AMtheapache64
09/23/2021, 9:09 AMChristoffer Niska
09/23/2021, 9:10 AMChristoffer Niska
09/23/2021, 9:13 AMChristoffer Niska
09/23/2021, 2:04 PMZach Klippenstein (he/him) [MOD]
09/23/2021, 3:49 PMMutableState
instead of MutableStateFlow
.
Line 41-48: You don’t need to remember
basic computations like this. It makes the code more complex, and you’re really not saving anything by doing the remember here (remember isn’t free as well, and i’d wager running the when
on every recomposition might actually be cheaper than remembering it).
Line 60: It’s a bit of a code smell for a state change in your view model to trigger an “event” callback from a composition. It’s unnecessarily complex, and breaks layering/UDF. The code path in the view model itself that triggers the internal state change to Success should be responsible for dispatching other events downstream. Your composition should just be responsible for rendering the success state. That said, when you actually do need to invoke a callback from an effect, your LaunchedEffect
is only capturing the value of the onSuccess
parameter on each recomposition where it’s (re)started. If onSuccess
changes without requestStatus
, you’ll have an outdated function. Always use rememberUpdatedState
for (non-composable) callbacks that are invoked from long-lived effect handlers (or anything else that can change via recomposition during the lifetime of the effect but shouldn’t cause the effect to restart).Christoffer Niska
09/24/2021, 6:47 AMChristoffer Niska
09/24/2021, 7:05 AMZach Klippenstein (he/him) [MOD]
09/24/2021, 4:57 PMChristoffer Niska
09/24/2021, 5:17 PMColton Idle
09/27/2021, 2:29 AMChristoffer Niska
10/05/2021, 9:24 AM