Hi guys, i have a screen like ```@Composable fun M...
# compose-android
b
Hi guys, i have a screen like
Copy code
@Composable
fun MyComponent(uiState: MyUiState, firstName: String, lastName: String) {
  Column {
        Text(text = uiState.data)
        TextField(value = firstName, onValueChange = {})
        TextField(value = lastName, onValueChange = {})
    }
}
Where
MyUiState
is represented into the viewmodel as
StateFlow
firstName
and
lastName
as
State
used both in
TextField.
Is it a good practice to add
firstName
and
lastName
into the StateFlow
MyUiStateFlow
?
c
From what I understood, the
uiState
should contain everything that the UI will show. Also working with events, makes it a lot easier to manage and update the state in the viewmodel.
b
c
Ah wow, didn't run into an issue like that before, but nice to know that for text fields it is not the recommended way! Thanks for the tip Brabo-hi 😄
d
Love that blog post.
c
Unpopular opinion (seemingly): My state classes always just look like class MyState{ val firstName by mutableStateOf("") val lastName by mutableStateOf("") ... like 10 other properties } I never have any issues. shipped 5 apps to prod (with millions of users and better than 99%+ crash free sessions). dont really use Flows for state. /shruggie
s
How does your typical ViewModel look like in this case Colton?
c
Not sure if this is what you're asking about. but like 95% of my VMs look something like this
Copy code
class MyVM(appState: AppStateHolder): ViewModel(){
val screenState = ScreenState()
fun doX(){
   scope.launch{
    val result = doNetworkCallOrSomething
    screenState.foo = result.mapToDomainOrUiLayer
  }
}
}
of course, I might have a UseCase or repository that might have some flow of data that I'm observing. but a lot of times i just stick with snapshot state for a lot of the simple stuff. So I don't necessarily expose a single big flowable state. but my state might have like 8 snapshot states and 2 flowables.
d
I would go the opposite route and pass the ViewModel into the screen state holder.
Google docs suggest that way of doing.
c
I'm not sure if this is what you mean, but by passing the viewModel to a screen composable breaks the possibility for any preview to render. If you rely on previews (like me) this is not what you might want to do 😄
c
@dewildte are you talking to me, or the OP?
d
You lol
But it applies to both I guess.
Sorry I miss read the documentation.
c
🙏
d
The docs suggest passing your business state into your UI state.
There can be two state holders, one for pure UI level stuff that should not be persisted pass screen destruction. And one for business state that often needs to survive screen destruction.
I am digressing though.
d
@Colton Idle the possible gotcha with your approach is making sure you don't do "unnecessary work" - the lifecycle aware flow collection APIs help with that usually.
c
not sure i follow. but yeah I dont use too many flows and so i dont really have any long running collections.