Homework

Homework — your cart

Homework — cement MVI.

  1. Trace the flow. In your own words, write what happens when you tap "+": Event → ViewModel → new State → UI. Three sentences.
  2. Use an event. Wire up CartEvent.ClearCart: add a "Clear cart" button at the bottom of CartScreen that sends onEvent(CartEvent.ClearCart). Check it — the cart empties, the total becomes €0.00.
  3. Think about the total. Why is totalPrice a get() 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's Greeting finally becomes a real PicaNavHost().