CoursesCompose Multiplatform, one screen from state to pixels

Puzzle 1 · 2 of 4

Which work does a moving card need?

Move the shared card as the user scrolls. Predict which parts of a screen update can be reused.

All puzzles in this course

The problem

The card slides vertically as you scroll. Its content and size stay the same. Which work needs to run in response to the scrolling value?

See it

Which work does a moving card need? State changes Read in phase Placement Paint position

Scroll sideways to see the full diagram.

  1. The scroll position changes. The text and size stay constant.

    Step 1 of 4
  2. Compose tracks the phase that reads the position.

    Step 2 of 4
  3. Placement reads the new offset and moves the existing card.

    Step 3 of 4
  4. The card appears at its new position. This update can reuse the composition.

    Step 4 of 4

The building block

There are three useful rendering phases. Composition decides which UI to describe. Layout measures sizes and places each item. Drawing paints the result. A tracked state read records the phase that actually reads the value.

In this version the observable value is read inside the placement lambda, which is code that runs later when Compose places the card. The position value is already an integer number of pixels:

Box(
    modifier = Modifier.offset {
        IntOffset(x = 0, y = position.value)
    }
) {
    Text("Saved item")
}

Now a change in position can update placement without recomposing this box for that change. Related drawing can still run. If you first evaluate the same value up in composition, you have already created a composition dependency by the time the modifier receives it.

Suppose the position only controls a painted color. If the color is read exclusively while drawing, Compose can redraw it without recomposing or relaying out the UI for that change. Choose the phase by the work your value actually drives.

Go deeperMove versus measure
Why might a card changing its height require more work than a card moving vertically?

A different height can require new measurements and can move neighboring items. Moving an item of the same size can reuse its measurement.

Back to the main puzzle

Check yourself

The position is read only during placement. What can Compose reuse for that change?

Not quite. The recorded read does not require composition for this change. Re-read that part ↑

Not quite. The position is read in placement, after measurement. Re-read that part ↑

Right. Moving an already measured card is placement work.

Moving the read into a local variable at the top of the composable has what effect?

Not quite. The modifier receives a value already evaluated in composition. Re-read that part ↑

Right. Follow where evaluation happens.

Not quite. The location of a variable does not disable observation. Re-read that part ↑

A value affects only paint and is read only during drawing. What follows when it changes?

Not quite. The layout has no new information to compute from the color. Re-read that part ↑

Right. A drawing dependency can invalidate drawing directly.

Not quite. Drawing owns the dependency and can update independently. Re-read that part ↑

Sources

What you now know

Where a value is read determines which work that value can invalidate. A read only during placement can move a card without changing its UI description.