Skip to content

Lazy List, grids, and the async render pipeline - #24

Merged
colemancda merged 7 commits into
masterfrom
feature/lazy-containers
Jul 23, 2026
Merged

Lazy List, grids, and the async render pipeline#24
colemancda merged 7 commits into
masterfrom
feature/lazy-containers

Conversation

@colemancda

Copy link
Copy Markdown
Member

Final step of the Compose-backed architecture (#18#23). Adds the lazy container path — the one unbounded-tree case the design deferred — plus the async threading pipeline it exposed.

Lazy List

List emits a node with count and an item-provider callback, not inline rows. The provider resolves each row subtree on demand, keyed by element id so row @State survives scroll and reorder. The interpreter renders a LazyColumn fetching each newly-visible row through SwiftCallbacks.itemNode(id, index) — one JNI call per visible row, re-fetched when the tree (hence the provider id) changes. A 10,000-row list contributes one node plus the handful of rows on screen.

itemNode is the last of the fixed five-plus-one Kotlin→Swift externals; the bridge surface still never grows per-view.

Grids

LazyVGrid/LazyHGrid with real GridItem track specs (.flexible, .fixed, .adaptive(minimum:)) → Compose LazyVerticalGrid/LazyHorizontalGrid with GridCells.Fixed/Adaptive. Cells resolve eagerly (bounded in practice) but lay out in a real lazy grid.

The async render pipeline

Pull-to-refresh was the first async state path (every prior callback ran synchronously on the main thread), and it surfaced a real bug: a Swift Task writing @State runs on a native background thread, and JNI object creation there fails with ClassNotFoundException — a background thread has neither the app class loader nor the right to touch Compose state.

The fix is the threading pipeline the plan specified: state changes now coalesce onto a platform main-thread scheduler instead of rendering inline. BridgeRuntime takes a scheduler; the Android host provides a main-looper Handler one. A dirty flag collapses multiple same-tick writes into one render, and nothing evaluates synchronously inside a callback (no reentrancy). This is also correct for the synchronous callbacks — they just post to the next frame.

Verification

  • swift test: 30 tests — List emits count + provider (not inline rows), the provider resolves rows on demand, row identity is keyed by element id (survives reorder), refreshable attaches its action, grid track specs (fixed count + adaptive minimum).
  • Emulator:
    • Lazy List: Rows 1–30 render, each fetched through itemNode; scrolls smoothly.
    • Pull-to-refresh: the full async path — pull → Swift Task (1s sleep, background thread) → @State insert → marshaled to the main looper → re-evaluate → materialize → recompose — prepends "Row 31".
    • Grids: three fixed columns, adaptive columns, and a horizontal two-row grid filling column-major; cell padding/background survive the lazy grid.
    • No crash under the async path (the scheduler fix).

Architecture complete

The gallery now runs entirely on the Compose-backed renderer: all 12 screens, real NavigationStack navigation, and the full control/modifier/presentation/lazy surface — evaluated in native Swift, materialized as typed nodes, rendered by Jetpack Compose which owns identity, layout, animation, input, and virtualization.

List emits count plus a provider callback rather than inline rows; the provider
resolves each row subtree on demand, keyed by element id so row state survives
scroll and reorder. refreshable attaches a refresh action through a context sink.
itemNode materializes a lazy row on demand. State changes now coalesce onto a
platform main-thread scheduler rather than running inline: a Swift Task driving
an async state write is on a native thread with neither the app class loader
(needed for JNI object creation) nor the right to touch Compose state.
List renders a LazyColumn fetching each visible row through itemNode, wrapped
in a PullToRefreshBox when refreshable; grids render LazyVerticalGrid/
LazyHorizontalGrid with fixed or adaptive tracks.
@colemancda
colemancda merged commit ce028e5 into master Jul 23, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant