Chapter 4 Update LiveData with Conditional Game Logic

Kotlin and Android Development featuring Jetpack — by Michael Fazio (30 / 125)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Summary and Next Steps | TOC | Create GameHandler 👉

Let’s recap what we’ve done so far:

  • Created our Penny Drop Android app and configured the Navigation component.
  • Built both PickPlayersFragment and GameFragment with associated ViewModel classes and layout files.
  • Set up data binding in both screens.

We’re at the point where we have an app, and it looks like a game, but we need to get our game logic in place. To do that, we’ll build a separate GameHandler class that will handle the game actions and leave the GameViewModel to take care of game state and UI interaction.

By doing this, the GameViewModel handles everything for the UI and where the game is at a given moment. The GameHandler class, on the other hand, will consist of stateless functions that take care of rolling and passing, then return a result to GameViewModel. This allows us to have most of the game logic isolated but also keep any Android-specific components out of GameHandler.

Once we’re done with this chapter, we’ll have a full, working game. Using the UI components we set up previously, we’ll be able to handle real user interactions plus all the rules for Penny Drop.

👈 Summary and Next Steps | TOC | Create GameHandler 👉

Kotlin and Android Development featuring Jetpack by Michael Fazio can be purchased in other book formats directly from the Pragmatic Programmers. If you notice a code error or formatting mistake, please let us know here so that we can fix it.

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.