搜索

PythonReactSEOPDF
GitHub Skill

compose-state-generator

Generate ViewModel, UiState, and state management code for Compose applications. Use when: (1) Creating ViewModel with StateFlow, (2) Designing UiState sealed classes, (3) Implementing state hoisting patterns, (4) Adding one-time events/effects, (5) Connecting ViewModel to Compose screens. Triggers: "viewmodel", "uistate", "stateflow", "state management", "mvi", "one-time event", "side effect", "compose state".

shoheikawano☆ 3 仓库 Stars0 仓库 Forks

安装与使用

请根据来源仓库和你的 AI 工具说明安装此技能。先阅读技能文件和依赖要求,再添加到工作流。

查看来源 ↗使用指南

技能说明

Compose State Generator

Generate idiomatic state management code for Compose following UDF (Unidirectional Data Flow).

Coding Standards

MANDATORY: This skill follows kotlin-compose-standards. All generated code MUST comply with these standards:

  • Trailing comma EXCEPT for Modifier arguments
  • Parameter order: required → Modifier → optional
  • Each Modifier method on its own line
  • New line at end of file

Quick Start

// UiState
sealed interface ProfileUiState {
    data object Loading : ProfileUiState
    data class Success(val user: User) : ProfileUiState
    data class Error(val message: String) : ProfileUiState
}

// ViewModel
class ProfileViewModel(
    private val userRepository: UserRepository,
) : ViewModel() {
    private val _uiState = MutableStateFlow<ProfileUiState>(ProfileUiState.Loading)
    val uiState: StateFlow<ProfileUiState> = _uiState.asStateFlow()
}

Decision Tree

What kind of state?
├─ Screen-level state → ViewModel + UiState
├─ Component-level state → State hoisting or remember
└─ Shared across screens → SharedFlow or StateHolder

Is there async loading?
├─ Yes → Sealed interface (Loading/Success/Error)
└─ No → Data class with fields

Need one-time events?
├─ Yes → Channel + Flow for events
└─ No → Just StateFlow for state

References

File When to Read
viewmodel-patterns.md ViewModel structure, StateFlow setup
uistate-design.md Sealed vs data class, state modeling
multiplatform.md KMP ViewModel alternatives

Standard ViewModel Template

class FeatureViewModel(
    private val repository: FeatureRepository,
    savedStateHandle: SavedStateHandle,  // Optional: for args
) : ViewModel() {

    private val _uiState = MutableStateFlow(FeatureUiState())
    val uiState: StateFlow<FeatureUiState> = _uiState.asStateFlow()

    private val _events = Channel<FeatureEvent>(Channel.BUFFERED)
    val events = _events.receiveAsFlow()

    init {
        loadData()
    }

    fun onAction(action: FeatureAction) {
        when (action) {
            is FeatureAction.Refresh -> loadData()
            is FeatureAction.ItemClick -> handleItemClick(action.id)
        }
    }

    private fun loadData() {
        viewModelScope.launch {
            _uiState.update { it.copy(isLoading = true) }
            repository.getData()
                .onSuccess { data ->
                    _uiState.update { it.copy(isLoading = false, items = data) }
                }
                .onFailure { error ->
                    _uiState.update { it.copy(isLoading = false, error = error.message) }
                }
        }
    }
}

Output Location

feature/
├── FeatureScreen.kt        # UI
├── FeatureViewModel.kt     # ViewModel
├── FeatureUiState.kt       # State model (or in ViewModel file if small)
└── FeatureAction.kt        # Actions/Events (optional, if complex)

使用示例

查看来源仓库中的使用示例与配置说明。

查看来源 ↗

来源历史

收录时间
2026-01-20
索引更新时间
2026-01-26
Branch
main

收录与同步日期属于本站记录。源码修改历史请查看来源仓库。

GitHub ↗

chat Comments (0)

chat_bubble_outline

No comments yet. Be the first to share your thoughts!