OpenParable
Skip to content
← Back to skills

Write Viewmodel

Development
write-viewmodel

Write Viewmodel is an Agent Skill for development published in the igorescodro/alkaa repository on GitHub: a SKILL.md instruction file an AI agent can load to do this task.

Use when writing a new ViewModel or modifying an existing one in the Alkaa project. Triggers on tasks like "add a ViewModel", "create VM for X screen", "implement state handling", or "connect use case to UI".

View on GitHub ↗
QUICK START

How to use this skill

Bring this guide into your coding agent with a prompt tailored to the tool you use.

  1. Open your project in Codex.
  2. Copy and paste the prompt below.
  3. Review files and risks before approving any action.
Prompt to paste
Inspect the Agent Skill at https://github.com/igorescodro/alkaa/blob/HEAD/.claude/skills/write-viewmodel/SKILL.md and all supporting files. Report provenance, permissions, scripts, dependencies, and security risks. Propose an exact installation into .agents/skills/write-viewmodel, but wait for my approval before writing files or running scripts.

Copying this prompt does not install or run the skill. Review third-party files before use.

Write ViewModel

Overview

ViewModels in Alkaa bridge domain use cases and Compose UI via cold flows and sealed state classes. They contain no business logic — only orchestration, mapping, and scope management.

Structure

  1. Sealed State Class — data object for stateless states, data class for states with data, ImmutableList for list payloads → see references/CODE_PATTERNS.md
  2. Flow Exposure — Cold Flow<ViewState> by default; hot StateFlow only when multiple collectors share the same stream → see references/CODE_PATTERNS.md
  3. Use Case Injection — Inject use cases as constructor parameters; never repositories or other ViewModels → see references/CODE_PATTERNS.md
  4. Coroutine Scope — applicationScope for DB mutations that survive ViewModel destruction; viewModelScope for UI-bound work → see references/CODE_PATTERNS.md
  5. Mappers — Dedicated mapper class injected as constructor param; never map inline → see references/CODE_PATTERNS.md

For unit test setup and fake patterns → see references/CODE_PATTERNS.md and the write-unit-tests skill.

Scope Rules

SituationScope
DB insert / update / deleteapplicationScope.launch { }
Network call that should persistapplicationScope.launch { }
Debounced UI updatesviewModelScope
Short-lived UI-bound workviewModelScope

Common Mistakes

MistakeFix
Calling repository directlyWrap in a use case first
Exposing StateFlow for a screen-scoped streamReturn cold Flow<ViewState>
Mapping models inline in ViewModelCreate a XxxMapper class
Using viewModelScope for DB writesUse applicationScope.launch { }
Depending on another ViewModelExtract shared logic into a use case
Testing with mocksWrite a XxxFake that implements the use case interface

Related Skills

  • Fake patterns and test structure → use write-unit-tests skill
  • Composable and screen wiring → use write-composable skill