跪拜 Guibai
← All articles
Android

Jetpack AppState Transform: Compose State, Flow, and Async Logic in a Single ViewModel Block

By Ehtan_Z ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Compose's reactive state model can now be used for state orchestration outside the UI layer, letting ViewModels define derived state from mixed State and Flow sources without converting everything to Flow. But the alpha API demands attention to threading, lifecycle, and the fact that updates are asynchronous, so teams should evaluate it against existing Flow patterns before relying on it.

Summary

AppState Transform, now in alpha, introduces a way to run a composable block inside a ViewModel and expose the result as a Compose State. It uses a headless composition to track reads of Compose State and support Flow collection via collectAsState, so you can compute a derived state from mixed inputs in one place. The library is positioned as an alternative to Flow.combine and derivedStateOf when inputs are a mix of State and Flow and you need conditional collection or side effects.

The article walks through a task list example: filtering with a toggle, switching online/offline data sources, aggregating filters into a UI state, and debounced search with LaunchedEffect. In each case, Transform runs a block that reads State, selects a Flow based on conditions, and outputs a new State. Compared to Flow pipelines, the block form can be more direct for mixed inputs, but it introduces async updates driven by composition, so the output is not synchronous like derivedStateOf.

Transform has caveats: it runs on Dispatchers.Default by default, keeps collecting as long as the ViewModel scope lives, and is not for event-driven scenarios that need every intermediate change. The article also covers configuration with version catalog and testing utilities, and suggests trying it on a small feature before adopting it widely.

Takeaways
— A composable block can run in a ViewModel scope and return a State.
— Transform tracks reads of Compose State and supports Flow collection via collectAsState.
— It uses a headless Composition, so no UI is created.
— Conditional collection lets you switch data sources based on state, with cancellation managed by composition.
— LaunchedEffect can be used inside the block for debouncing and cancelling async work.
— It is not for event-driven scenarios that require every intermediate state change.
— The default dispatcher is Dispatchers.Default; updates are asynchronous.
— With viewModelScope, the transformation continues even when the UI is in the background.
— Testing requires transformTest and TransformTestScope.
— The library is alpha version 1.0.0-alpha01.
Conclusions

By letting a block read both State and Flow directly, Transform removes the need to convert everything to Flow, simplifying mixed-input state derivation.

The headless Composition approach means Compose's runtime is now a general-purpose reactive engine, not just a UI toolkit.

Transform's conditional collection is concise, but it relies on composition's lifecycle to cancel collection, which may be non-obvious to developers expecting Flow's explicit operators.

The comparison table suggests Transform is best with mixed inputs; if all inputs are Flow or all State, existing tools remain simpler.

Because updates are asynchronous and composition-driven, developers must reason about timing differently than with derivedStateOf's synchronous reads.

Concepts & terms
Headless Composition
A Compose composition that doesn't render UI; it tracks state reads and recomputes values.
AppState Transform
A Jetpack API that runs a composable block in a non-UI scope (like ViewModel) and exposes a derived State.
derivedStateOf
A Compose function that derives a State from other State reads, computing synchronously on read.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗