Is there any way I can improve my code? I'm using compose-router
Copy code
var selectedUser: User? by remember { mutableStateOf(null) }
when(currentScreen) {
is Routing.Root.Main -> {
MainScreen(onChatClick = { user ->
selectedUser = user
// change current screen
}
}
is Routing.Root.Conversation -> {
when (val user = selectedUser) {
null -> throw IllegalStateException("selectedUser is null")
else -> ConversationScreen(user)
}
}
}
I don't like the way I'm handling
selectedUser
here
h
Halil Ozercan
09/19/2020, 10:48 AM
it feels like
selectedUser
shouldn't be a state in here. Your actual state is
currentScreen
. Maybe
selectedUser
should be a non-nullable constructor value in
Routing.Root.Conversation
class.
Halil Ozercan
09/19/2020, 10:49 AM
In that case, you will need to change your
onChatClick
function to modify
currentScreen
, instead of selected user because we are moving the user logic from one state into another one.