Homework
Homework — your cart
Homework — cement MVI.
- Trace the flow. In your own words, write what happens when you tap "+": Event → ViewModel → new State → UI. Three sentences.
- Use an event. Wire up
CartEvent.ClearCart: add a "Clear cart" button at the bottom ofCartScreenthat sendsonEvent(CartEvent.ClearCart). Check it — the cart empties, the total becomes €0.00. - Think about the total. Why is
totalPriceaget()property, not a stored field? What would go wrong if you stored it separately and forgot to update it on a quantity change?
Required tests:
- Immutability test: changing a quantity updates the total (if not — remember the "mutate in place" gotcha).
- Clear test: "Clear" empties the cart and the total becomes €0.00.
To submit: a short description of the flow + a screenshot of the working "−/+" and total.
Tip. You have a working cart with real state — but the screens are still separate: the menu doesn't lead to the cart. Next lesson is navigation with Nav3 (the new, still alpha Android navigation library): we connect menu → pizza detail → cart into one flow, and
MainActivity'sGreetingfinally becomes a realPicaNavHost().