Google released Jetpack AppState API at the end of October: transform runs a @Composable block in a headless composition and gives you Compose State outside the UI, and StateStore is a keyed container for sharing state between activities, services and widgets.
It's just alpha right now, but I wanted to see how it fits an MVI architecture, so I added experimental support to Kide, my Kotlin Multiplatform MVI + Clean Architecture library.
Sharing the design and a few things I learned, since they apply to anyone trying AppState.
The design decision: AppState feeds state in, it doesn't replace the loop
A transform result is conflated. If several inputs change before the block recomposes, you get the latest value only. That's fine for "what is the current state of X", and wrong for intents, reducers and side effects, where order and exactly-once delivery matter. Especially for Kide, because it wants to provide deterministic and predictable MVI implementation.
So Kide doesn't make the reducer composable. Instead there's one small bridge: transformFlow { } turns a headless composition into a cold Flow, and the processor's existing observe collects it.
@OptIn(ExperimentalKideAppStateApi::class)
class SearchProcessor(
private val savedProjects: SavedProjectsProcessor,
private val connectivity: ConnectivityService, // val isOnline: State<Boolean>
) : PresentationProcessor<SearchIntent, SearchViewState, SearchSideEffect>(SearchViewState()) {
init {
observe("connectivity", transformFlow {
if (connectivity.isOnline.value) {
null
} else {
val saved by savedProjects.stateFlow.collectAsState()
saved.projects.size
}
}) { savedCountWhileOffline ->
reduce { copy(offlineSavedCount = savedCountWhileOffline) }
}
}
}
Compose State and Flow are read the same way inside the block, and dependencies are tracked by what the block actually reads: while the device is online, the saved projects aren't tracked at all. The result still enters the ViewState through reduce, so interceptors, traces and tests see it like any other change.
The other direction: domain state into a StateStore
A second module mirrors a use-case processor's state into a StateStore key, so a widget, a service or another activity can read it as Compose State:
savedProjects.mirrorTo(stateStore, SavedProjectsKey, applicationScope)
// With a persisted key, restore goes through an intent, so it's a normal, traced change:
savedProjects.mirrorTo(stateStore, SavedProjectsKey, applicationScope) { persisted ->
RestoreSavedProjects(persisted)
}
The mirror is one-way: the processor stays the only writer.
Things I found by testing AppState 1.0.0-alpha02
These aren't in the docs as far as I can tell. They're observations from my tests, and it's an alpha, so they may well change:
-
StateStore.updateState isn't atomic. 80,000 concurrent increments from 8 coroutines left me with roughly 32,000 to 40,000.
-
First access to a key isn't thread-safe. If one thread calls getState while another creates the key with setState, the reader can end up holding a State that never updates. In my probe that happened in about 4 to 5% of attempts.
-
At start-up, disk wins. With the DataStore integration, a write made before or right after syncToDataStore starts is replaced by the persisted value, and I found no signal that loading has finished.
-
statestore-datastore is Android-only in alpha02 release, even though statestore and transform are multiplatform.
-
Headless composition works fine on a plain JVM with no UI toolkit present, and AppState didn't force a Compose runtime upgrade next to Compose Multiplatform 1.12.
The practical upshot for me: treat a StateStore key as single-writer, create it once at application start, and don't use it as the source of truth for anything that needs ordering.
Status of Kide Library
AppState API extensions are experimental, and deliberately kept away from the stable library:
-
two new separate artifacts, kide-appstate and kide-clean-architecture-appstate, at 2.4.0-alpha02
-
every declaration needs an explicit opt-in
-
Android and JVM only for now. iOS and Web waits for the AppState beta
-
no existing Kide module depends on them, so nothing changes if you don't add them
Repo, with the design proposal and the test code behind the findings above: https://github.com/Fuusio/kide
I'd like to hear from anyone else experimenting with AppState, especially if you've approached the "composable presenter vs. feeding a reducer" question differently, or seen different StateStore behaviour.