I have the following code in dire need of refactoring.
buttonYes.setOnClickListener {
if (answer) {
buttonYes.setBackgroundColor(green)
disableButton(buttonNo)
lottie_wondering.setAnimation(R.raw.quiz_good_answer)
lottie_wondering.playAnimation()
} else {
buttonYes.setBackgroundColor(red)
disableButton(buttonNo)
lottie_wondering.setAnimation(R.raw.quiz_bad_answer)
lottie_wondering.playAnimation()
}
showExplanation()
}
buttonNo.setOnClickListener {
if (!answer) {
buttonNo.setBackgroundColor(green)
disableButton(buttonYes)
lottie_wondering.setAnimation(R.raw.quiz_good_answer)
lottie_wondering.playAnimation()
} else {
buttonNo.setBackgroundColor(red)
disableButton(buttonYes)
lottie_wondering.setAnimation(R.raw.quiz_bad_answer)
lottie_wondering.playAnimation()
}
showExplanation()
}
What is the most ellegant way to rewrite the code to be more conscise and understandable? Is it possible to "group" both buttonYes and buttonNo and create the listener for both? Or should I instead employ some IF/ELSE statements?
The code may use the latest Kotlin and AndroidAPI features.
Aucun commentaire:
Enregistrer un commentaire