https://kotlinlang.org logo
Title
s

Smorg

05/20/2019, 1:54 AM
Does anyone have an idea how I can simplify this? The only thing that is changing is the argument name
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

Shawn

05/20/2019, 2:04 AM
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
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

Smorg

05/20/2019, 6:10 AM
yeah, thanks. did the hoisting for
text
already, but will do for
setState
too. thought it could be simpler