Defining Your UI State

The first step in adding state to a Compose application is to define the models you will use to store it. For Coda Pizza, you need a place to hold the state of which toppings are selected. Create a new file called Pizza.kt in the com.bignerdranch.android.codapizza.model package. In this file, create a new data class called Pizza.

Give your data class one property: the toppings on the pizza, a Map<Topping, ToppingPlacement>. If a topping is present on the pizza, it will be added to this map as a key. The value will be the topping’s position on the pizza. If a topping is not on the pizza, it will not have an entry in this map.

Listing 27.1  The Pizza class (Pizza.kt)

data class Pizza(
    val toppings: Map<Topping, ToppingPlacement> = emptyMap()
)

Representing your pizza this way makes it easy to determine whether and where a topping is on the pizza. It also prevents you from making unsupported combinations, like adding two instances of pepperoni to the entire pizza. (Coda Pizza does not have an option to change the quantity of a topping, only its placement.)

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.9.237