Your First Kotlin Android App: Polishing the App

Aug 22 2023 · Kotlin 1.8.20, Android 13, Android Studio Flamingo | 2022.2.1

Part 1: Build Out the App

05. Keep Track of Game Rounds

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 04. Display the Score Next episode: 06. Improve the Alert Title

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In this episode, you’ll modify BullsEye to keep track of the current round of the game and display it. This is a review of even more techniques you’ve learned in this learning path.

var currentRound by rememberSaveable { mutableStateOf(1) }
GameDetail(
  totalScore = totalScore,
  round = currentRound, // New Code
  modifier = Modifier.fillMaxWidth()
)
@Composable
fun GameDetail(
  modifier: Modifier = Modifier,
  totalScore: Int = 0,
  round: Int = 1, // New Code
) {
  //...
    GameInfo(label = stringResource(id = R.string.score_label), value = totalScore)
    GameInfo(label = stringResource(id = R.string.current_round_label), value = round) // New Code
  //...
}
@Composable
fun ResultDialog(
  hideDialog: () -> Unit,
  onRoundIncrement: () -> Unit, // New Code
  //...
){
  //...
}
AlertDialog(
  onDismissRequest = {
    hideDialog()
    onRoundIncrement() // New Code
  },
  confirmButton = {
    TextButton(
      onClick = {
        hideDialog()
        onRoundIncrement() // New Code
      }
    ) {
      Text(stringResource(id = R.string.result_dialog_button_text))
    }
  },
  //...
)
ResultDialog(
  //...
  onRoundIncrement = {
    currentRound += 1
  }
)
ResultDialog(
  //...
  onRoundIncrement = {
    currentRound += 1
    targetValue = Random.nextInt(1, 100) // New Code
  }
)