agentleFS
wrinfotelwrinfotel/verified-android-skills/skills/compose-architecture/SKILL.md

compose-architecture

Skill0 starsChanged 33 days agoMITView on GitHub

Jetpack Compose architecture discipline: layer separation, unidirectional data flow, ViewModel construction and coroutine scopes, lifecycle-aware state collection. Use when creating screens, wiring state holders to composables, or reviewing UI-layer structure. Core rules have machine checks in checks/.

1 other project uses a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

8 rules. About 0 are common, 3 are distinctive, and 5 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈3

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 5

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    After ANY screen or state-holder edit, run the Verification step (bottom of this file).

    line 15
  2. Very specific:

    MUST read UI state from a state holder: a ViewModel for screen UI state, a @Stable plain class for UI logic.

    line 20
  3. Very specific:

    MUST pass child composables plain state + event lambdas.

    line 47
  4. Very specific:

    MUST obtain ViewModels via viewModel() / hiltViewModel() (or DI scoped with SavedStateHandle).

    line 73
  5. Very specific:

    MUST NOT use GlobalScope in UI code — its lifetime is unmanaged and outlives the screen.

    line 88
  6. Very specific:

    MUST collect state-holder flows with collectAsStateWithLifecycle() (androidx.lifecycle:lifecycle-runtime-compose).

    line 104
  7. Distinctive:

    Run after every screen or state-holder edit (from the project root):

    line 123
  8. Distinctive:

    All four must pass (exit 0) before declaring the task done.

    line 132

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Compose Architecture

Operational rules for a coding agent. RIGHT/WRONG pairs are canonical. After ANY screen or state-holder edit, run the Verification step (bottom of this file).

1. Layer separation

MUST read UI state from a state holder: a ViewModel for screen UI state, a @Stable plain class for UI logic. MUST NOT import or call the data layer (Room DAOs, Retrofit services, DataStore, *Repository) from any file that declares a @Composable.

WRONG:

@Composable
fun FeedScreen() {
    val posts = remember { postsRepository.posts() }   // data layer in UI
}

RIGHT:

@Composable
fun FeedScreen(viewModel: FeedViewModel = viewModel()) {
    val uiState by viewModel.uiState.collectAsStateWithLifecycle()
}

WHY: dependencies point downward only (UI → state holder → domain → data). A composable that reaches into the data layer bypasses the single source of truth, loses config-change retention, and cannot be previewed or unit-tested.

2. Unidirectional data flow

MUST pass child composables plain state + event lambdas. MUST NOT pass a ViewModel instance down into a reusable composable ("Don't pass ViewModel instances down to other composable functions" — official guidance, see references/architecture.md #3).

WRONG:

@Composable
fun Feed(viewModel: FeedViewModel) {
    FeedItem(item = viewModel.selected, onClick = { viewModel.toggle() })
}

RIGHT:

@Composable
fun Feed(uiState: FeedUiState, onToggle: (String) -> Unit) {
    FeedItem(item = uiState.selected, onClick = { onToggle(uiState.id) })
}

Events travel up to the screen-level composable that owns the ViewModel; that composable is the single source of truth for handling the event.

3. ViewModel construction & coroutine scopes

MUST obtain ViewModels via viewModel() / hiltViewModel() (or DI scoped with SavedStateHandle). MUST NOT construct one inside a composable — a manual FooViewModel() call creates a fresh instance per composition, with no retention and no sharing.

WRONG:

val viewModel = FeedViewModel()

RIGHT:

val viewModel: FeedViewModel = viewModel()

MUST NOT use GlobalScope in UI code — its lifetime is unmanaged and outlives the screen. Use rememberCoroutineScope() inside event callbacks and LaunchedEffect(key) for effects keyed to composition.

WRONG:

GlobalScope.launch { analytics.logOpen(screen) }

RIGHT:

LaunchedEffect(Unit) { analytics.logOpen(screen) }

4. Lifecycle-aware state collection

MUST collect state-holder flows with collectAsStateWithLifecycle() (androidx.lifecycle:lifecycle-runtime-compose). Plain collectAsState() keeps collecting while the UI is stopped, wasting resources.

WRONG:

val uiState by viewModel.uiState.collectAsState()

RIGHT:

val uiState by viewModel.uiState.collectAsStateWithLifecycle()

Plain collectAsState() is allowed only in KMP-common code where the Android lifecycle API is unavailable; leave a comment at the call site.

Verification

Run after every screen or state-holder edit (from the project root):

bash <skill-dir>/checks/check_no_data_layer_in_ui.sh        <src-dir>
bash <skill-dir>/checks/check_viewmodel_construction.sh     <src-dir>
bash <skill-dir>/checks/check_no_global_scope.sh            <src-dir>
bash <skill-dir>/checks/check_collect_as_state_lifecycle.sh <src-dir>

All four must pass (exit 0) before declaring the task done. Checks are deterministic import/call-site heuristics — they catch the documented anti-patterns above, not every architecture violation. The parameter shape of §2 is verified by review against references/architecture.md.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.