Claude Cursor Skill

compose-multiplatform-screen

Use when adding or extending a Compose Multiplatform screen in commonMain — a new screen, ViewModel, UiState, actions/intents, one-shot side effects (navigate, snackbar), MVI or MVVM presentation, collectAsStateWithLifecycle, koinViewModel, testTag objects, @Preview of screen sta

LLM Mart · 0 points · 0 views 0 listing impressions 0 install-command copies
Download kushpawan-kmp-mobile-skills-skills_compose-multiplatform-screen-c04c11f.zip · 16 KB

Install

skills CLI npx skills add https://github.com/Kushpawan/kmp-mobile-skills/tree/main/skills/compose-multiplatform-screen
Claude Code claude plugin marketplace add https://llmmart.ai/marketplace.json && claude plugin install kushpawan-kmp-mobile-skills@llmmart
Git git clone https://github.com/Kushpawan/kmp-mobile-skills.git

The skills CLI installs just this skill, for any of its supported agents. Claude Code installs the whole kushpawan/kmp-mobile-skills collection as a plugin from our marketplace. Git is the plain clone.

Skill manifest

Compose Multiplatform screen

Build every screen as three parts: a stateful entry that collects state, a pure view that renders UiState and emits actions, and an effect handler that is the only place one-shot events (navigation, snackbars) are executed. The ViewModel owns state and never touches navigation, Context or Compose types.

When to use

  • Adding a new screen to a Compose Multiplatform app (commonMain).
  • Adding state, actions or side effects to an existing screen.
  • Writing or fixing ViewModel tests for a screen.
  • Symptoms: a navigation event fires twice or never, state "resets" after rotation, a snackbar is lost when the app is backgrounded, a ViewModel holds a NavController.

When not to use

  • Graph structure, routes, tabs, deep links, auth gates → compose-multiplatform-navigation.
  • A reusable button/input/card for the design system → compose-design-system-component.
  • Implementing a screen from a Figma frame → figma-to-mobile-ui (then come back here for the ViewModel and tests).
  • SwiftUI screens over shared logic → that is iOS-native UI; see Related skills.

Step 0 — Read the project first

Do not write code until you can answer each question from the repository itself.

  1. Nearest neighbour. Open the most similar existing screen. A real neighbour beats this template; copy its file layout, naming and package structure.
  2. Presentation pattern. Search for the ViewModel base class (grep -rn "abstract class .*ViewModel" --include=*.kt). Is there an MVI base (state + effect, with helpers such as setState {}, reduce {} or postSideEffect()), a plain ViewModel with MutableStateFlow, or several generations side by side?
  3. Effect idiom. Channel(...).receiveAsFlow(), MutableSharedFlow, or events-as-state?
  4. DI. Koin (koinViewModel()), Kodein, manual factories, or viewModel { } initializers.
  5. Navigation. navigation-compose (NavHostController), Voyager, Decompose, Navigation 3.
  6. Design system. Theme object (AppTheme, MaterialTheme extensions), component prefix, token names for colours/spacing/typography.
  7. Tags and tests. Existing *TestTags objects and their string format; test libraries (kotlin.test, kotlinx-coroutines-test, Turbine); test naming convention.
  8. Versions and platform targets. gradle/libs.versions.toml: the Compose Multiplatform version decides the preview import (see Quick reference) and which APIs below exist. Compose Multiplatform 1.13 requires Kotlin 2.4.20 (2.4.0 minimum for Kotlin/Native), Android compileSdk 37.1 and minSdk 24 (its CHANGELOG, "Migration Notes"); a project pinned below that cannot take 1.13-only APIs. Note the Android targetSdk (insets, step 3) and whether a window-size helper already exists (WindowSizeClass, currentWindowAdaptiveInfo*, a project wrapper).

Rules that follow from Step 0

  • Extending an existing screen: stay in its idiom. A half-migrated ViewModel (some state in an MVI reducer, some in loose MutableStateFlows; some events in a Channel, some in a SharedFlow) is worse than either pattern. Migrate a screen only as its own, reviewed change.
  • New screen: use the project's newest pattern. With no precedent, use the MVI shape below and the small base class in references/mvi-viewmodel-base.md.
  • Never add a second DI framework, navigation library or effect mechanism.

Workflow

1. Define the contract (UiState, Action, Effect)

data class NoteEditorUiState(
    val title: String = "",
    val body: String = "",
    val isSaving: Boolean = false,
    val error: NoteEditorError? = null,
) {
    // Derived flags live in state, not in the composable: one place to test them.
    val canSave: Boolean get() = !isSaving && title.isNotBlank()
}

sealed interface NoteEditorAction {
    data class TitleChanged(val value: String) : NoteEditorAction
    data object SaveClicked : NoteEditorAction
}

sealed interface NoteEditorEffect {          // one-shot only; durable things belong in state
    data class NoteSaved(val noteId: String) : NoteEditorEffect
}

Done when: every value the view renders is a UiState property; every user gesture is an Action; every effect is something that must happen once (navigate, show a transient message). Dialog visibility, errors and loading are state, not effects — they must survive rotation.

2. Write the ViewModel

  • Extend the project's base, or MviViewModel<State, Effect> from the reference.
  • One public entry point: fun onAction(action: XAction).
  • Update state atomically (MutableStateFlow.update { }), never value = value.copy(...) from concurrent coroutines.
  • Capture the state you need before launching a coroutine (val draft = currentState); reading it after a suspension point may see newer input.
  • Repositories/use cases are constructor-injected and main-safe. Inject a dispatcher only where this class itself switches context.
  • No NavController, no Context, no @Composable, no androidx.compose.* imports.

Done when the ViewModel compiles in commonMain with zero Compose imports.

3. Write the screen (entry + view + effect handler)

@Composable
fun NoteEditorScreen(
    onNoteSaved: (noteId: String) -> Unit,
    viewModel: NoteEditorViewModel = koinViewModel(),
) {
    val state by viewModel.state.collectAsStateWithLifecycle()
    NoteEditorView(state = state, onAction = viewModel::onAction)
    CollectEffects(viewModel.effects) { effect ->
        when (effect) {
            is NoteEditorEffect.NoteSaved -> onNoteSaved(effect.noteId)
        }
    }
}
  • NoteEditorView(state, onAction, modifier) is pure: no ViewModel, no koinInject, no LaunchedEffect that navigates. Only view-local state (LazyListState, focus, scroll) may be remembered there.
  • CollectEffects (in the reference) collects with the lifecycle at STARTED on Dispatchers.Main.immediate. It is the only place a navigation callback is invoked.
  • Navigation is passed in as lambdas (or the project's navigator). The nav graph owns the NavController; the screen does not.
  • Use collectAsStateWithLifecycle() (lifecycle-runtime-compose), not collectAsState(): it stops collecting while the host is below STARTED.

Layout rules for every view

  • Insets. Android 16 with targetSdk 36 draws edge-to-edge and the opt-out (windowOptOutEdgeToEdgeEnforcement) is disabled. Pass Scaffold's innerPadding to the content, or use WindowInsets.safeDrawing / Modifier.imePadding(); the same insets cover the iOS safe area. Android host-app setup (enableEdgeToEdge(), bar contrast) → Google's edge-to-edge skill.
  • Window size, not device type. At targetSdk 36 Android ignores orientation, resizability and aspect-ratio restrictions on large screens (sw ≥ 600dp); the opt-out ends at targetSdk 37. iOS 27 makes iPhone apps resizable (iPhone apps on iPad, iPhone Mirroring). Branch layout in commonMain on the window size class, never on orientation, platform or "is tablet", so the same code runs on both platforms and survives a live resize. Compose Multiplatform documents currentWindowAdaptiveInfoV2().windowSizeClass from org.jetbrains.compose.material3.adaptive:adaptive; use the project's helper if it has one.
  • Cheap derived UI state. For a condition read from fast-changing state (scroll offset past a threshold, an animation fraction), Compose 1.13 adds computedStateOf { }; Google recommends it over derivedStateOf unless the calculation is heavy or benefits from caching. It is in the common runtime used by Compose Multiplatform 1.13; on older versions keep derivedStateOf. Business-derived flags still belong in UiState.
  • Text input. Compose 1.13 deprecates value-based BasicTextField and VisualTransformation (use TextFieldState with InputTransformation/OutputTransformation); Material3 1.5.0 keeps its value-based TextField overloads for now. Extending a screen: keep its existing text-field shape. New state-based fields: hold rememberTextFieldState() in the view and send changes as actions (snapshotFlow { state.text.toString() }), unless the project already allows TextFieldState in ViewModels — it brings a Compose foundation import into the ViewModel.

Done when the view can be called from a preview with a literal UiState and a no-op lambda.

4. Add TestTags and semantics

object NoteEditorTestTags {
    const val SCREEN = "note_editor_screen"
    const val TITLE_FIELD = "note_editor_title_field"
    const val SAVE_BUTTON = "note_editor_save_button"
    fun tagChip(tagId: String) = "note_editor_tag_chip_$tagId"   // stable backend id, never an index
}
  • Tag the screen root and every interactive element, loader, error and empty state.
  • testTag is a locator; contentDescription is spoken prose. Never reuse one string for both.
  • Compose Multiplatform maps testTag to the iOS accessibilityIdentifier, so XCUITest/Appium can use the same tags. On Android, UiAutomator/Appium additionally need Modifier.semantics { testTagsAsResourceId = true } near the root (Android source set).
  • Follow the project's existing tag format if one exists.

5. Add previews

  • One preview per meaningful state: empty, filled, saving, error. Preview the view, never the stateful entry (Koin is not running in previews).
  • Wrap in the app theme (AppTheme { }), not a bare MaterialTheme, so tokens resolve.
  • Previews in commonMain need an Android target; they render in Android Studio / IntelliJ with the Kotlin Multiplatform plugin.
  • Add a preview at a compact and an expanded width when the layout branches on window size.

6. Register DI and the route

  • Koin: viewModel { params -> NoteEditorViewModel(noteId = params.getOrNull(), notesRepository = get()) } (or viewModelOf(::NoteEditorViewModel) when there are no runtime parameters).
  • Pass route arguments with koinViewModel(parameters = { parametersOf(route.noteId) }).
  • Inside a NavHost, koinViewModel() is scoped to the current back-stack entry: each visit to the destination gets its own ViewModel, cleared when the entry is popped.
  • Route definition and graph wiring → compose-multiplatform-navigation.

7. Write ViewModel tests (commonTest)

  • Dispatchers.setMain(StandardTestDispatcher()) in @BeforeTest, resetMain() in @AfterTest (viewModelScope runs on Dispatchers.Main.immediate).
  • Hand-written fakes for repositories; no mocking framework needed.
  • Start collecting effects before triggering the action (an eager collector in backgroundScope on UnconfinedTestDispatcher(testScheduler), or Turbine's test { }).
  • One behaviour per test; assert both the final state and the emitted effects.
  • Do not test composables here; tests cover reducer, state derivation and effects.

Full code for every step — base class, screen, previews, Koin module, fake, tests — is in references/worked-example-note-editor.md.

Quick reference

Need API (multiplatform)
Collect state androidx.lifecycle.compose.collectAsStateWithLifecycle
Lifecycle-aware effect collection LocalLifecycleOwner (androidx.lifecycle.compose) + repeatOnLifecycle(Lifecycle.State.STARTED)
ViewModel (Koin 4) org.koin.compose.viewmodel.koinViewModel; module DSL org.koin.core.module.dsl.viewModel / viewModelOf
ViewModel (no DI) androidx.lifecycle.viewmodel.compose.viewModel { NoteEditorViewModel(...) }
Preview annotation, CMP 1.10+ androidx.compose.ui.tooling.preview.Preview (artifact org.jetbrains.compose.ui:ui-tooling-preview)
Preview annotation, CMP < 1.10 org.jetbrains.compose.ui.tooling.preview.Preview (deprecated from 1.10)
Intercept back (unsaved changes) see compose-multiplatform-navigation → back handling
Test main dispatcher kotlinx.coroutines.test.setMain / resetMain
Window size class (CMP docs) currentWindowAdaptiveInfoV2().windowSizeClass (androidx.compose.material3.adaptive)
System-bar / safe-area insets Scaffold innerPadding, WindowInsets.safeDrawing, Modifier.imePadding()
Cheap derived condition, Compose 1.13+ computedStateOf { listState.firstVisibleItemIndex > 0 }
Effect transport Delivery Use when
Channel(BUFFERED).receiveAsFlow() Exactly one collector; buffered while nobody collects Default for navigation/snackbar effects
MutableSharedFlow() (replay 0) Dropped if no subscriber at emit time (init, rotation, background) Avoid for effects
MutableSharedFlow(replay = 1) Re-delivered to every new subscriber Avoid: double navigation after rotation
Event in UiState + "consumed" action Survives anything; needs an explicit ack Must-not-lose events (payment result, irreversible action)

Verify

Run what applies and report each check as PASS, FAIL or NOT RUN (reason). Never report a check as passed that you did not run. Replace :shared with the module that holds the screen.

./gradlew :shared:compileKotlinMetadata            # common code compiles (no Android/iOS-only APIs)
./gradlew :shared:testAndroidHostTest              # JVM tests (task name with the KMP Android library plugin)
./gradlew :shared:testDebugUnitTest                # JVM tests (task name with com.android.library + androidTarget)
./gradlew :shared:iosSimulatorArm64Test            # same tests on iOS (macOS only)
./gradlew :composeApp:assembleDebug                # Android app still builds

Use ./gradlew :shared:tasks --all | grep -i test if the task names differ. Also check:

  • grep -rn "androidx.compose" <logic package> returns nothing for the ViewModel files.
  • Every interactive element in the new view has a testTag from the screen's TestTags object.
  • Previews render. Optional headless check when Google's Android CLI is installed (command -v android): android preview list / android preview render render @Preview functions to PNG without a device. Not verified on Kotlin Multiplatform modules (shared module on the Android-KMP plugin plus a separate androidApp); if it fails, use android studio render-compose-preview with Android Studio open, or open the file in the IDE. Report NOT RUN if neither is available.
  • Resize check: Android resizable emulator or split screen, and on iOS 27 a resized iPhone window (iPhone app on iPad or iPhone Mirroring). The layout switches at the size-class breakpoint and input and scroll position survive. Report NOT RUN without a device or simulator.
  • Largest text size: Android font scale 200% and, on Compose Multiplatform 1.13+ (which follows iOS Dynamic Type), the largest iOS accessibility text size. Nothing clips or overlaps.

Common mistakes

Mistake Why it bites Fix
Navigating inside the ViewModel or holding a NavController there Leaks the back-stack owner, breaks tests and iOS reuse Emit an effect; navigate in the effect handler
Navigation in a stray LaunchedEffect(state.saved) Fires again on recomposition/rotation when the flag is still true Use an effect, or reset the flag with an explicit action
Effects in MutableSharedFlow() with no replay Effects emitted in init or during rotation are silently dropped Channel(Channel.BUFFERED).receiveAsFlow()
Collecting effects with plain LaunchedEffect { flow.collect } Keeps collecting while stopped; events consumed off-screen repeatOnLifecycle(STARTED) + Dispatchers.Main.immediate
collectAsState() for ViewModel state Keeps collecting in background on Android collectAsStateWithLifecycle()
_state.value = _state.value.copy(...) from several coroutines Lost updates under concurrency _state.update { it.copy(...) }
Reading state.value after a suspend call in a save flow Saves text typed after the click Snapshot the draft before launching
Base class calls an abstract initialState() from its constructor Subclass fields are not initialised yet: wrong or null defaults Pass the initial state as a constructor argument
Dialog visibility or error as an effect Lost on rotation / process death Keep it in UiState
Computing isSaveEnabled inside the composable Untested logic, duplicated across platforms Derived property on UiState
Previewing the stateful screen Preview needs Koin/ViewModel and fails Preview the pure view with literal states
Debounce or async work between onValueChange and the text field's value Lost keystrokes, cursor jumps Keep the text update synchronous; debounce side work (search) separately
Effect test collects after the action Misses the emission or becomes flaky Start the collector first, then act, then advanceUntilIdle()
Mixing idioms when extending an old screen Two state sources, two event channels Stay in the screen's existing idiom
Content not padded by innerPadding / insets Under the status bar or gesture bar at targetSdk 36; under the notch on iOS Apply innerPadding or WindowInsets.safeDrawing
Layout chosen by orientation, platform or "is tablet" Wrong on resized iPhone windows (iOS 27) and on large screens at targetSdk 36+ Window size class in commonMain
derivedStateOf for a cheap boolean on scroll position (Compose 1.13+) Caching overhead on every scroll frame computedStateOf; keep derivedStateOf for heavy work

Related skills

  • compose-multiplatform-navigation — routes, NavHost, tabs, auth gate, deep links, back handling.
  • compose-design-system-component — reusable components and theme tokens used by the view.
  • figma-to-mobile-ui — turning a Figma frame into the view.
  • compose-multiplatform-localization — strings, plurals and formatting in the view.
  • chrisbanes/skills — compose-state-and-effects, kotlin-concurrency-and-flow, compose-ui-testing-patterns.
  • Meet-Miyani/compose-skill — an opinionated Compose/CMP architecture skill set.
  • android/skills — Jetpack Compose skills for Android-only apps, including edge-to-edge (host-app insets and system bars) and adaptive (size classes, foldables, desktop input).
  • Apple's app-resizability skill ships with Xcode 27.1+ and is exported with xcrun agent skills export --output-dir <dir>; use it for the iOS host app's resizing behaviour (scene setup, UIRequiresFullScreen). It is not redistributed here.
Files (kmp-mobile-skills)
  • references
    • mvi-viewmodel-base.md 8.7 KB
      Loading…
    • worked-example-note-editor.md 19.7 KB
      Loading…
  • SKILL.md 19.5 KB
    ---
    name: compose-multiplatform-screen
    description: Use when adding or extending a Compose Multiplatform screen in commonMain — a new screen, ViewModel, UiState, actions/intents, one-shot side effects (navigate, snackbar), MVI or MVVM presentation, collectAsStateWithLifecycle, koinViewModel, testTag objects, @Preview of screen states, ViewModel unit tests, edge-to-edge insets, window size classes, resizable windows on Android and iOS. Also for effects lost or delivered twice, state not updating, navigation fired from a ViewModel, content under the status bar, a layout that breaks when the window is resized.
    license: Apache-2.0
    compatibility: Kotlin Multiplatform project with Compose Multiplatform 1.8+ and the multiplatform lifecycle-viewmodel / lifecycle-runtime-compose libraries (Compose Multiplatform 1.13 needs Kotlin 2.4.20 and Android compileSdk 37.1). Commands assume Gradle with Android and iOS targets.
    metadata:
      category: ui
      platforms: android, ios
    ---
    
    # Compose Multiplatform screen
    
    Build every screen as three parts: a **stateful entry** that collects state, a **pure view** that
    renders `UiState` and emits actions, and an **effect handler** that is the only place one-shot
    events (navigation, snackbars) are executed. The ViewModel owns state and never touches
    navigation, `Context` or Compose types.
    
    ## When to use
    
    - Adding a new screen to a Compose Multiplatform app (`commonMain`).
    - Adding state, actions or side effects to an existing screen.
    - Writing or fixing ViewModel tests for a screen.
    - Symptoms: a navigation event fires twice or never, state "resets" after rotation, a snackbar is
      lost when the app is backgrounded, a ViewModel holds a `NavController`.
    
    ## When not to use
    
    - Graph structure, routes, tabs, deep links, auth gates → `compose-multiplatform-navigation`.
    - A reusable button/input/card for the design system → `compose-design-system-component`.
    - Implementing a screen from a Figma frame → `figma-to-mobile-ui` (then come back here for the
      ViewModel and tests).
    - SwiftUI screens over shared logic → that is iOS-native UI; see Related skills.
    
    ## Step 0 — Read the project first
    
    Do not write code until you can answer each question from the repository itself.
    
    1. **Nearest neighbour.** Open the most similar existing screen. A real neighbour beats this
       template; copy its file layout, naming and package structure.
    2. **Presentation pattern.** Search for the ViewModel base class
       (`grep -rn "abstract class .*ViewModel" --include=*.kt`). Is there an MVI base
       (state + effect, with helpers such as `setState {}`, `reduce {}` or `postSideEffect()`), a plain `ViewModel` with
       `MutableStateFlow`, or several generations side by side?
    3. **Effect idiom.** `Channel(...).receiveAsFlow()`, `MutableSharedFlow`, or events-as-state?
    4. **DI.** Koin (`koinViewModel()`), Kodein, manual factories, or `viewModel { }` initializers.
    5. **Navigation.** navigation-compose (`NavHostController`), Voyager, Decompose, Navigation 3.
    6. **Design system.** Theme object (`AppTheme`, `MaterialTheme` extensions), component prefix,
       token names for colours/spacing/typography.
    7. **Tags and tests.** Existing `*TestTags` objects and their string format; test libraries
       (`kotlin.test`, `kotlinx-coroutines-test`, Turbine); test naming convention.
    8. **Versions and platform targets.** `gradle/libs.versions.toml`: the Compose Multiplatform version
       decides the preview import (see Quick reference) and which APIs below exist. Compose
       Multiplatform 1.13 requires Kotlin 2.4.20 (2.4.0 minimum for Kotlin/Native), Android
       `compileSdk` 37.1 and `minSdk` 24 (its CHANGELOG, "Migration Notes"); a project pinned below that
       cannot take 1.13-only APIs. Note the Android `targetSdk` (insets, step 3) and whether a window-size
       helper already exists (`WindowSizeClass`, `currentWindowAdaptiveInfo*`, a project wrapper).
    
    **Rules that follow from Step 0**
    
    - **Extending an existing screen: stay in its idiom.** A half-migrated ViewModel (some state in an
      MVI reducer, some in loose `MutableStateFlow`s; some events in a `Channel`, some in a
      `SharedFlow`) is worse than either pattern. Migrate a screen only as its own, reviewed change.
    - **New screen:** use the project's newest pattern. With no precedent, use the MVI shape below
      and the small base class in [references/mvi-viewmodel-base.md](references/mvi-viewmodel-base.md).
    - Never add a second DI framework, navigation library or effect mechanism.
    
    ## Workflow
    
    ### 1. Define the contract (UiState, Action, Effect)
    
    ```kotlin
    data class NoteEditorUiState(
        val title: String = "",
        val body: String = "",
        val isSaving: Boolean = false,
        val error: NoteEditorError? = null,
    ) {
        // Derived flags live in state, not in the composable: one place to test them.
        val canSave: Boolean get() = !isSaving && title.isNotBlank()
    }
    
    sealed interface NoteEditorAction {
        data class TitleChanged(val value: String) : NoteEditorAction
        data object SaveClicked : NoteEditorAction
    }
    
    sealed interface NoteEditorEffect {          // one-shot only; durable things belong in state
        data class NoteSaved(val noteId: String) : NoteEditorEffect
    }
    ```
    
    Done when: every value the view renders is a `UiState` property; every user gesture is an
    `Action`; every effect is something that must happen **once** (navigate, show a transient message).
    Dialog visibility, errors and loading are **state**, not effects — they must survive rotation.
    
    ### 2. Write the ViewModel
    
    - Extend the project's base, or `MviViewModel<State, Effect>` from the reference.
    - One public entry point: `fun onAction(action: XAction)`.
    - Update state atomically (`MutableStateFlow.update { }`), never `value = value.copy(...)` from
      concurrent coroutines.
    - Capture the state you need **before** launching a coroutine (`val draft = currentState`);
      reading it after a suspension point may see newer input.
    - Repositories/use cases are constructor-injected and main-safe. Inject a dispatcher only where
      this class itself switches context.
    - No `NavController`, no `Context`, no `@Composable`, no `androidx.compose.*` imports.
    
    Done when the ViewModel compiles in `commonMain` with zero Compose imports.
    
    ### 3. Write the screen (entry + view + effect handler)
    
    ```kotlin
    @Composable
    fun NoteEditorScreen(
        onNoteSaved: (noteId: String) -> Unit,
        viewModel: NoteEditorViewModel = koinViewModel(),
    ) {
        val state by viewModel.state.collectAsStateWithLifecycle()
        NoteEditorView(state = state, onAction = viewModel::onAction)
        CollectEffects(viewModel.effects) { effect ->
            when (effect) {
                is NoteEditorEffect.NoteSaved -> onNoteSaved(effect.noteId)
            }
        }
    }
    ```
    
    - `NoteEditorView(state, onAction, modifier)` is **pure**: no ViewModel, no `koinInject`, no
      `LaunchedEffect` that navigates. Only view-local state (`LazyListState`, focus, scroll) may be
      `remember`ed there.
    - `CollectEffects` (in the reference) collects with the lifecycle at `STARTED` on
      `Dispatchers.Main.immediate`. It is the **only** place a navigation callback is invoked.
    - Navigation is passed in as lambdas (or the project's navigator). The nav graph owns the
      `NavController`; the screen does not.
    - Use `collectAsStateWithLifecycle()` (lifecycle-runtime-compose), not `collectAsState()`: it
      stops collecting while the host is below `STARTED`.
    
    **Layout rules for every view**
    
    - **Insets.** Android 16 with `targetSdk` 36 draws edge-to-edge and the opt-out
      (`windowOptOutEdgeToEdgeEnforcement`) is disabled. Pass `Scaffold`'s `innerPadding` to the content,
      or use `WindowInsets.safeDrawing` / `Modifier.imePadding()`; the same insets cover the iOS safe area.
      Android host-app setup (`enableEdgeToEdge()`, bar contrast) → Google's `edge-to-edge` skill.
    - **Window size, not device type.** At `targetSdk` 36 Android ignores orientation, resizability and
      aspect-ratio restrictions on large screens (sw ≥ 600dp); the opt-out ends at `targetSdk` 37. iOS 27
      makes iPhone apps resizable (iPhone apps on iPad, iPhone Mirroring). Branch layout in `commonMain` on
      the window size class, never on orientation, platform or "is tablet", so the same code runs on both
      platforms and survives a live resize. Compose Multiplatform documents
      `currentWindowAdaptiveInfoV2().windowSizeClass` from
      `org.jetbrains.compose.material3.adaptive:adaptive`; use the project's helper if it has one.
    - **Cheap derived UI state.** For a condition read from fast-changing state (scroll offset past a
      threshold, an animation fraction), Compose 1.13 adds `computedStateOf { }`; Google recommends it
      over `derivedStateOf` unless the calculation is heavy or benefits from caching. It is in the common
      runtime used by Compose Multiplatform 1.13; on older versions keep `derivedStateOf`. Business-derived
      flags still belong in `UiState`.
    - **Text input.** Compose 1.13 deprecates value-based `BasicTextField` and `VisualTransformation` (use
      `TextFieldState` with `InputTransformation`/`OutputTransformation`); Material3 1.5.0 keeps its
      value-based `TextField` overloads for now. Extending a screen: keep its existing text-field shape.
      New state-based fields: hold `rememberTextFieldState()` in the view and send changes as actions
      (`snapshotFlow { state.text.toString() }`), unless the project already allows `TextFieldState` in
      ViewModels — it brings a Compose foundation import into the ViewModel.
    
    Done when the view can be called from a preview with a literal `UiState` and a no-op lambda.
    
    ### 4. Add TestTags and semantics
    
    ```kotlin
    object NoteEditorTestTags {
        const val SCREEN = "note_editor_screen"
        const val TITLE_FIELD = "note_editor_title_field"
        const val SAVE_BUTTON = "note_editor_save_button"
        fun tagChip(tagId: String) = "note_editor_tag_chip_$tagId"   // stable backend id, never an index
    }
    ```
    
    - Tag the screen root and every interactive element, loader, error and empty state.
    - `testTag` is a locator; `contentDescription` is spoken prose. Never reuse one string for both.
    - Compose Multiplatform maps `testTag` to the iOS `accessibilityIdentifier`, so XCUITest/Appium
      can use the same tags. On Android, UiAutomator/Appium additionally need
      `Modifier.semantics { testTagsAsResourceId = true }` near the root (Android source set).
    - Follow the project's existing tag format if one exists.
    
    ### 5. Add previews
    
    - One preview per meaningful state: empty, filled, saving, error. Preview the **view**, never the
      stateful entry (Koin is not running in previews).
    - Wrap in the app theme (`AppTheme { }`), not a bare `MaterialTheme`, so tokens resolve.
    - Previews in `commonMain` need an Android target; they render in Android Studio / IntelliJ with
      the Kotlin Multiplatform plugin.
    - Add a preview at a compact and an expanded width when the layout branches on window size.
    
    ### 6. Register DI and the route
    
    - Koin: `viewModel { params -> NoteEditorViewModel(noteId = params.getOrNull(), notesRepository = get()) }`
      (or `viewModelOf(::NoteEditorViewModel)` when there are no runtime parameters).
    - Pass route arguments with `koinViewModel(parameters = { parametersOf(route.noteId) })`.
    - Inside a `NavHost`, `koinViewModel()` is scoped to the current back-stack entry: each visit to the
      destination gets its own ViewModel, cleared when the entry is popped.
    - Route definition and graph wiring → `compose-multiplatform-navigation`.
    
    ### 7. Write ViewModel tests (commonTest)
    
    - `Dispatchers.setMain(StandardTestDispatcher())` in `@BeforeTest`, `resetMain()` in `@AfterTest`
      (`viewModelScope` runs on `Dispatchers.Main.immediate`).
    - Hand-written fakes for repositories; no mocking framework needed.
    - **Start collecting effects before triggering the action** (an eager collector in
      `backgroundScope` on `UnconfinedTestDispatcher(testScheduler)`, or Turbine's `test { }`).
    - One behaviour per test; assert **both** the final state and the emitted effects.
    - Do not test composables here; tests cover reducer, state derivation and effects.
    
    Full code for every step — base class, screen, previews, Koin module, fake, tests — is in
    [references/worked-example-note-editor.md](references/worked-example-note-editor.md).
    
    ## Quick reference
    
    | Need | API (multiplatform) |
    |---|---|
    | Collect state | `androidx.lifecycle.compose.collectAsStateWithLifecycle` |
    | Lifecycle-aware effect collection | `LocalLifecycleOwner` (`androidx.lifecycle.compose`) + `repeatOnLifecycle(Lifecycle.State.STARTED)` |
    | ViewModel (Koin 4) | `org.koin.compose.viewmodel.koinViewModel`; module DSL `org.koin.core.module.dsl.viewModel` / `viewModelOf` |
    | ViewModel (no DI) | `androidx.lifecycle.viewmodel.compose.viewModel { NoteEditorViewModel(...) }` |
    | Preview annotation, CMP 1.10+ | `androidx.compose.ui.tooling.preview.Preview` (artifact `org.jetbrains.compose.ui:ui-tooling-preview`) |
    | Preview annotation, CMP < 1.10 | `org.jetbrains.compose.ui.tooling.preview.Preview` (deprecated from 1.10) |
    | Intercept back (unsaved changes) | see `compose-multiplatform-navigation` → back handling |
    | Test main dispatcher | `kotlinx.coroutines.test.setMain` / `resetMain` |
    | Window size class (CMP docs) | `currentWindowAdaptiveInfoV2().windowSizeClass` (`androidx.compose.material3.adaptive`) |
    | System-bar / safe-area insets | `Scaffold` `innerPadding`, `WindowInsets.safeDrawing`, `Modifier.imePadding()` |
    | Cheap derived condition, Compose 1.13+ | `computedStateOf { listState.firstVisibleItemIndex > 0 }` |
    
    | Effect transport | Delivery | Use when |
    |---|---|---|
    | `Channel(BUFFERED).receiveAsFlow()` | Exactly one collector; buffered while nobody collects | Default for navigation/snackbar effects |
    | `MutableSharedFlow()` (replay 0) | Dropped if no subscriber at emit time (init, rotation, background) | Avoid for effects |
    | `MutableSharedFlow(replay = 1)` | Re-delivered to every new subscriber | Avoid: double navigation after rotation |
    | Event in `UiState` + "consumed" action | Survives anything; needs an explicit ack | Must-not-lose events (payment result, irreversible action) |
    
    ## Verify
    
    Run what applies and report each check as **PASS**, **FAIL** or **NOT RUN (reason)**. Never report a
    check as passed that you did not run. Replace `:shared` with the module that holds the screen.
    
    ```bash
    ./gradlew :shared:compileKotlinMetadata            # common code compiles (no Android/iOS-only APIs)
    ./gradlew :shared:testAndroidHostTest              # JVM tests (task name with the KMP Android library plugin)
    ./gradlew :shared:testDebugUnitTest                # JVM tests (task name with com.android.library + androidTarget)
    ./gradlew :shared:iosSimulatorArm64Test            # same tests on iOS (macOS only)
    ./gradlew :composeApp:assembleDebug                # Android app still builds
    ```
    
    Use `./gradlew :shared:tasks --all | grep -i test` if the task names differ. Also check:
    
    - `grep -rn "androidx.compose" <logic package>` returns nothing for the ViewModel files.
    - Every interactive element in the new view has a `testTag` from the screen's `TestTags` object.
    - Previews render. Optional headless check when Google's Android CLI is installed
      (`command -v android`): `android preview list` / `android preview render` render `@Preview`
      functions to PNG without a device. Not verified on Kotlin Multiplatform modules (shared module on the
      Android-KMP plugin plus a separate `androidApp`); if it fails, use `android studio
      render-compose-preview` with Android Studio open, or open the file in the IDE. Report NOT RUN if
      neither is available.
    - Resize check: Android resizable emulator or split screen, and on iOS 27 a resized iPhone window
      (iPhone app on iPad or iPhone Mirroring). The layout switches at the size-class breakpoint and input
      and scroll position survive. Report NOT RUN without a device or simulator.
    - Largest text size: Android font scale 200% and, on Compose Multiplatform 1.13+ (which follows iOS
      Dynamic Type), the largest iOS accessibility text size. Nothing clips or overlaps.
    
    ## Common mistakes
    
    | Mistake | Why it bites | Fix |
    |---|---|---|
    | Navigating inside the ViewModel or holding a `NavController` there | Leaks the back-stack owner, breaks tests and iOS reuse | Emit an effect; navigate in the effect handler |
    | Navigation in a stray `LaunchedEffect(state.saved)` | Fires again on recomposition/rotation when the flag is still true | Use an effect, or reset the flag with an explicit action |
    | Effects in `MutableSharedFlow()` with no replay | Effects emitted in `init` or during rotation are silently dropped | `Channel(Channel.BUFFERED).receiveAsFlow()` |
    | Collecting effects with plain `LaunchedEffect { flow.collect }` | Keeps collecting while stopped; events consumed off-screen | `repeatOnLifecycle(STARTED)` + `Dispatchers.Main.immediate` |
    | `collectAsState()` for ViewModel state | Keeps collecting in background on Android | `collectAsStateWithLifecycle()` |
    | `_state.value = _state.value.copy(...)` from several coroutines | Lost updates under concurrency | `_state.update { it.copy(...) }` |
    | Reading `state.value` after a `suspend` call in a save flow | Saves text typed after the click | Snapshot the draft before launching |
    | Base class calls an abstract `initialState()` from its constructor | Subclass fields are not initialised yet: wrong or null defaults | Pass the initial state as a constructor argument |
    | Dialog visibility or error as an effect | Lost on rotation / process death | Keep it in `UiState` |
    | Computing `isSaveEnabled` inside the composable | Untested logic, duplicated across platforms | Derived property on `UiState` |
    | Previewing the stateful screen | Preview needs Koin/ViewModel and fails | Preview the pure view with literal states |
    | Debounce or async work between `onValueChange` and the text field's value | Lost keystrokes, cursor jumps | Keep the text update synchronous; debounce side work (search) separately |
    | Effect test collects after the action | Misses the emission or becomes flaky | Start the collector first, then act, then `advanceUntilIdle()` |
    | Mixing idioms when extending an old screen | Two state sources, two event channels | Stay in the screen's existing idiom |
    | Content not padded by `innerPadding` / insets | Under the status bar or gesture bar at `targetSdk` 36; under the notch on iOS | Apply `innerPadding` or `WindowInsets.safeDrawing` |
    | Layout chosen by orientation, platform or "is tablet" | Wrong on resized iPhone windows (iOS 27) and on large screens at `targetSdk` 36+ | Window size class in `commonMain` |
    | `derivedStateOf` for a cheap boolean on scroll position (Compose 1.13+) | Caching overhead on every scroll frame | `computedStateOf`; keep `derivedStateOf` for heavy work |
    
    ## Related skills
    
    - `compose-multiplatform-navigation` — routes, NavHost, tabs, auth gate, deep links, back handling.
    - `compose-design-system-component` — reusable components and theme tokens used by the view.
    - `figma-to-mobile-ui` — turning a Figma frame into the view.
    - `compose-multiplatform-localization` — strings, plurals and formatting in the view.
    - [chrisbanes/skills](https://github.com/chrisbanes/skills) — `compose-state-and-effects`,
      `kotlin-concurrency-and-flow`, `compose-ui-testing-patterns`.
    - [Meet-Miyani/compose-skill](https://github.com/Meet-Miyani/compose-skill) — an opinionated
      Compose/CMP architecture skill set.
    - [android/skills](https://github.com/android/skills) — Jetpack Compose skills for Android-only apps,
      including [edge-to-edge](https://github.com/android/skills/tree/main/system/edge-to-edge) (host-app
      insets and system bars) and [adaptive](https://github.com/android/skills/tree/main/jetpack-compose/adaptive)
      (size classes, foldables, desktop input).
    - Apple's `app-resizability` skill ships with Xcode 27.1+ and is exported with
      `xcrun agent skills export --output-dir <dir>`; use it for the iOS host app's resizing behaviour
      (scene setup, `UIRequiresFullScreen`). It is not redistributed here.
    

Comments

Sign in to join the conversation.

No comments yet. Start the conversation.

Reviews (0)

No reviews yet.