s
-.kt
m
If compiler recommended that, it was IMO bad recommendation
if you only have one case + else then if is much better choice
w
Isn't a "bad" recommendation, you could extract the common functions:
Copy code
fun showToastError(stringRes: Int) {
            Toast.makeText(context, getString(stringRes), Toast.LENGTH_LONG).show()
        }
        fun sendMessage() {
            val message = ContactUsParams(
                    contact = ContactUsDetails(
                            email = contactUsEmailEditTextInput.text.toString(),
                            message = contactUsMessageEditTextInput.text.toString()
                    )
            )
            viewModel.sendMessage(message)
        }
        when (shouldShowEmailField) {
            //user has system provided random email, so show email input for them to give us their email
            true -> {
                when (contactUsEmailEditTextInput.text.toString()) {
                    "" -> showToastError(R.string.errors_email_format)
                    else -> {
                        when (contactUsMessageEditTextInput.text.toString()) {
                            "" -> showToastError(R.string.activerecord_errors_messages_blank)
                            else -> sendMessage()
                        }
                    }
                }
            }

            //we already have their email, so only show the message field and not the email input field
            false -> {
                when (contactUsMessageEditTextInput.text.toString()) {
                    "" -> showToastError(R.string.activerecord_errors_messages_blank)
                    else -> sendMessage()
                }
            }
        }
Can go further and do it if the same
when
dot the
contactUsMessageEditTextInput
...
s
ok thanks everyone 😃 I’ll factor out the repeated code into funtions
a
There you go
Copy code
when {
    shouldShowEmailField && contactUsEmailEditTextInput.text.isNullOrEmpty() -> {
        Toast.makeText(context, getString(R.string.errors_email_format), Toast.LENGTH_LONG).show()
    }
    contactUsMessageEditTextInput.text.isNullOrEmpty() -> {
        Toast.makeText(context, getString(R.string.activerecord_errors_messages_blank), Toast.LENGTH_LONG).show()
    }
    else -> {
        val message = ContactUsParams(
                contact = ContactUsDetails(
                        email = contactUsEmailEditTextInput.text.toString(),
                        message = contactUsMessageEditTextInput.text.toString()
                )
        )
        viewModel.sendMessage(message)
    }
}
s
Thank you =_