From cda0a2f824d0c3744d50ea76e4d0b336d6deb932 Mon Sep 17 00:00:00 2001 From: Roger Branham Date: Tue, 14 Sep 2021 15:40:07 -0500 Subject: [PATCH] Update to make more idiomatic Since there is an emphasis on writing concise code, I think it would be a good idea to have the when expression directly be an argument. It will save a variable definition and is more in line with Kotlin style. Code lab documents probably need to be updated as well but could not find if they were open source. --- .../com/example/diceroller/MainActivity.kt | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/DiceRoller/app/src/main/java/com/example/diceroller/MainActivity.kt b/DiceRoller/app/src/main/java/com/example/diceroller/MainActivity.kt index 0bc619a..688a059 100755 --- a/DiceRoller/app/src/main/java/com/example/diceroller/MainActivity.kt +++ b/DiceRoller/app/src/main/java/com/example/diceroller/MainActivity.kt @@ -53,19 +53,21 @@ class MainActivity : AppCompatActivity() { // Find the ImageView in the layout val diceImage: ImageView = findViewById(R.id.imageView) + + // Update the ImageView with the correct drawable resource ID + diceImage.setImageResource( - // Determine which drawable resource ID to use based on the dice roll - val drawableResource = when (diceRoll) { - 1 -> R.drawable.dice_1 - 2 -> R.drawable.dice_2 - 3 -> R.drawable.dice_3 - 4 -> R.drawable.dice_4 - 5 -> R.drawable.dice_5 - else -> R.drawable.dice_6 - } + // Determine which drawable resource ID to use based on the dice roll + when (diceRoll) { + 1 -> R.drawable.dice_1 + 2 -> R.drawable.dice_2 + 3 -> R.drawable.dice_3 + 4 -> R.drawable.dice_4 + 5 -> R.drawable.dice_5 + else -> R.drawable.dice_6 + } - // Update the ImageView with the correct drawable resource ID - diceImage.setImageResource(drawableResource) + ) // Update the content description diceImage.contentDescription = diceRoll.toString()