Is there any way I can improve my code? I'm using ...
# compose
s
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
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.
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.
s
Ooh yeah that's it, thanks!
s
I see you migrated the app to compose