Android / KMP Architecture Skills for Claude Code[android-presentation-mvi]
name: android-presentation-mvi
description: | MVI presentation layer for Android/KMP - State, Action, Event, ViewModel, Root/Screen composable split, UI models, UiText error mapping, and process death with SavedStateHandle. Use this skill whenever creating or reviewing a ViewModel, defining screen state, actions, or events, structuring composables, mapping errors to UI strings, or handling process death. Trigger on phrases like “add a ViewModel”, “create a screen”, “MVI”, “state”, “action”, “event”, “screen composable”, “UiText”, “SavedStateHandle”, “ObserveAsEvents”, or “UI model”.
Android / KMP Presentation Layer (MVI)
Overview
Every screen has:
- State — a single data class holding all UI state fields.
- Action (Intent) — a sealed interface of all user-triggered actions.
- Event — a sealed interface of one-time side effects (navigation, snackbar).
- ViewModel — holds
StateFlow<State>, processesAction, emitsEventviaChannel.
State
| |
Always update state with .update { } — never replace the entire flow:
| |
Action (Intent)
| |
Event (one-time side effects)
| |
ViewModel
| |
Coroutine Dispatchers
Do not inject unless the class is unit-tested and dispatches to a non-main dispatcher. For ViewModel tests, use Dispatchers.setMain(UnconfinedTestDispatcher()) in test setup.
For blocking code that doesn’t support suspension, wrap it:
| |
Only inject CoroutineDispatcher when:
- The class dispatches to a non-main dispatcher (e.g.,
IO), AND - That class is directly unit-tested.
Mapping Errors to UI Strings
UiText (core:presentation) wraps strings that originate from — or could originate from — a string resource:
| |
When to use UiText: For any string that comes from a string resource, could be localized, or might be either a resource or a dynamic value depending on context (e.g., error messages that map to R.string.*).
When to use plain String: For values that are always dynamic and never come from resources — e.g., a user’s name, a formatted date, a currency amount. These should be exposed as String directly in the state or UI model.
| |
Define DataError.toUiText() extension functions in core:presentation (or feature presentation) that map error enums to UiText.StringResource.
UI Model (Presentation Model)
When a domain model needs UI-specific formatting (dates, units, currency), create a dedicated UI model in the presentation layer:
| |
UI models are always suffixed with Ui (e.g., NoteUi, TodoItemUi).
Composable Structure
Both the Root and Screen composable live in the same file (e.g., NoteListScreen.kt).
Root Composable (suffixed Root)
Receives the ViewModel (via koinViewModel()) and any callbacks needed for navigation. Observes events. Passes state and onAction down.
Screen Composable (suffixed Screen)
Receives only state and onAction. No ViewModel reference. Can be previewed independently.
| |
Process Death
When a screen involves complex forms or critical user input, restore essential fields using SavedStateHandle:
| |
Only save what truly matters after process death — not the entire state.
Naming Conventions
| Thing | Convention | Example |
|---|---|---|
| ViewModel | <Screen>ViewModel | NoteListViewModel |
| State | <Screen>State | NoteListState |
| Action | <Screen>Action | NoteListAction |
| Event | <Screen>Event | NoteListEvent |
| Root composable | <Screen>Root | NoteListRoot |
| Screen composable | <Screen>Screen | NoteListScreen |
| UI model | <Model>Ui | NoteUi, TodoItemUi |
Checklist: Adding a New Screen
- Define
State,Action,Eventinfeature:presentation - Implement
ViewModelinfeature:presentation - Create
<Screen>Rootcomposable (holds ViewModel, observes events) - Create
<Screen>Screencomposable (pure state + onAction, previewable) - Map any domain errors to
UiTextvia extension functions - Add
SavedStateHandlefor any form fields that must survive process death