Does anyone have an idea how I can simplify this? ...
# getting-started
s
Does anyone have an idea how I can simplify this? The only thing that is changing is the argument name
Copy code
when (textInputEditText.id) {
            R.id.editText_fullName -> {
                setState { copy(signUpData = signUpData.copy(fullName = textInputEditText.text.toString())) }
            }
            R.id.editText_username -> {
                setState { copy(signUpData = signUpData.copy(username = textInputEditText.text.toString())) }
            }
            R.id.editText_email -> {
                setState { copy(signUpData = signUpData.copy(email = textInputEditText.text.toString())) }
            }
            R.id.editText_password -> {
                setState { copy(signUpData = signUpData.copy(password = textInputEditText.text.toString())) }
            }
        }
s
There’s a couple things you could hoist if you really want to, namely the
setState
lambda and
textInputEditText.text.toString()
it might look something more like this, but it’s debatable how much more readable it really is
Copy code
val text = textInputEditText.text.toString()

val updatedSignup = when (textInputEditText.id) {
    R.id.editText_fullName -> signUpData.copy(fullName = text)) 
    R.id.editText_username -> signUpData.copy(userName = text))
    R.id.editText_email -> signUpData.copy(email = text))
    R.id.editText_password -> signUpData.copy(password = text))
}

setState {
    copy(signUpData = updatedSignup)
}
👍 3
s
yeah, thanks. did the hoisting for
text
already, but will do for
setState
too. thought it could be simpler