Agent Skillsmaxrave-dev/SimpMusic › android-viewmodel

android-viewmodel

GitHub

提供Android ViewModel最佳实践,指导使用StateFlow管理UI状态,SharedFlow处理一次性事件。涵盖线程安全更新、生命周期感知收集及协程作用域规范。

.claude/skills/android-viewmodel/SKILL.md maxrave-dev/SimpMusic

触发场景

实现Android ViewModel 配置Kotlin StateFlow和SharedFlow 处理UI状态与一次性事件

安装

npx skills add maxrave-dev/SimpMusic --skill android-viewmodel -g -y
更多选项

非标准路径

npx skills add https://github.com/maxrave-dev/SimpMusic/tree/dev/.claude/skills/android-viewmodel -g -y

不安装直接使用

npx skills use maxrave-dev/SimpMusic@android-viewmodel

指定 Agent (Claude Code)

npx skills add maxrave-dev/SimpMusic --skill android-viewmodel -a claude-code -g -y

安装 repo 全部 skill

npx skills add maxrave-dev/SimpMusic --all -g -y

预览 repo 内 skill

npx skills add maxrave-dev/SimpMusic --list

SKILL.md

Frontmatter
{
    "name": "android-viewmodel",
    "description": "Best practices for implementing Android ViewModels, specifically focused on StateFlow for UI state and SharedFlow for one-off events."
}

Android ViewModel & State Management

Instructions

Use ViewModel to hold state and business logic. It must outlive configuration changes.

1. UI State (StateFlow)

  • What: Represents the persistent state of the UI (e.g., Loading, Success(data), Error).
  • Type: StateFlow<UiState>.
  • Initialization: Must have an initial value.
  • Exposure: Expose as a read-only StateFlow backing a private MutableStateFlow.
    private val _uiState = MutableStateFlow<UiState>(UiState.Loading)
    val uiState: StateFlow<UiState> = _uiState.asStateFlow()
    
  • Updates: Update state using .update { oldState -> ... } for thread safety.

2. One-Off Events (SharedFlow)

  • What: Transient events like "Show Toast", "Navigate to Screen", "Show Snackbar".
  • Type: SharedFlow<UiEvent>.
  • Configuration: Must use replay = 0 to prevent events from re-triggering on screen rotation.
    private val _uiEvent = MutableSharedFlow<UiEvent>(replay = 0)
    val uiEvent: SharedFlow<UiEvent> = _uiEvent.asSharedFlow()
    
  • Sending: Use .emit(event) (suspend) or .tryEmit(event).

3. Collecting in UI

  • Compose: Use collectAsStateWithLifecycle() for StateFlow.
    val state by viewModel.uiState.collectAsStateWithLifecycle()
    
    For SharedFlow, use LaunchedEffect with LocalLifecycleOwner.
  • Views (XML): Use repeatOnLifecycle(Lifecycle.State.STARTED) within a coroutine.

4. Scope

  • Use viewModelScope for all coroutines started by the ViewModel.
  • Ideally, specific operations should be delegated to UseCases or Repositories.

版本历史

  • 11f5926 当前 2026-07-30 20:16

同 Skill 集合

.claude/skills/android-accessibility/SKILL.md
.claude/skills/android-architecture/SKILL.md
.claude/skills/android-coroutines/SKILL.md
.claude/skills/android-data-layer/SKILL.md
.claude/skills/android-emulator-skill/SKILL.md
.claude/skills/android-gradle-logic/SKILL.md
.claude/skills/android-retrofit/SKILL.md
.claude/skills/android-testing/SKILL.md
.claude/skills/coil-compose/SKILL.md
.claude/skills/compose-navigation/SKILL.md
.claude/skills/compose-performance-audit/SKILL.md
.claude/skills/compose-ui/SKILL.md
.claude/skills/gradle-build-performance/SKILL.md
.claude/skills/jetpack-compose/SKILL.md
.claude/skills/kotlin-concurrency-expert/SKILL.md
.claude/skills/marketing-ideas/SKILL.md
.claude/skills/mobile-android-design/SKILL.md
.claude/skills/xml-to-compose-migration/SKILL.md
.claude/skills/find-skills/SKILL.md
.claude/skills/kmp/SKILL.md
.claude/skills/ui-ux-pro-max/SKILL.md

元信息

文件数
0
版本
f7205d5
Hash
683f79dd
收录时间
2026-07-30 20:16

首页 - Wiki
Copyright © 2011-2026 iteam. Current version is 2.155.2. UTC+08:00, 2026-08-01 06:13
浙ICP备14020137号-1 $访客地图$