Theme
/
Theory

MVI: one direction, one truth

The menu looks nice but it's lifeless. For the app to "remember" anything (what's in the cart) and redraw when it changes, you need state and a place for it to live: a ViewModel.

Pica uses one pattern everywhere — MVI (Model–View–Intent). The idea: data flows one way.

  User taps                         (Event)
        │
        ▼
  ViewModel handles it, makes new    (State)
        │
        ▼
  UI redraws from the new state      (View)
        │
        └──────────── user acts again ────────────────┐
                                                       ▲

Three parts:

  • Event — what happened: "add pizza", "increase quantity". Modelled as a sealed interface CartEvent — a finite list of events.
  • State — everything the screen must show: the cart items, the total. Modelled as a data class CartUiState.
  • ViewModel — takes an Event, produces a new State. It holds state in a MutableStateFlow, and the UI observes it.

Why this way? The ViewModel survives rotation (state isn't lost), there's one source of truth (no "which total is real?"), and the logic is easy to test without any UI.

We start with the cart — a perfect first example because it's purely in memory: no network, no database. Just Event → ViewModel → State → UI. (Networking and payments plug in later — right now the pattern is what matters.)