modern-jetpack-compose

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese
Review or generate Jetpack Compose code for correctness, modern API usage, and adherence to Android best practices. Report only genuine issues — do not nitpick style unless it contradicts a clear rule.
评审或生成符合正确性、遵循现代API用法、符合Android最佳实践的Jetpack Compose代码。仅报告真实存在的问题——不要对代码风格吹毛求疵,除非它违反了明确的规则。

Review / Generation Process

评审/生成流程

When reviewing existing code, follow these steps in order:
  1. Check for deprecated or outdated APIs →
    references/api.md
  2. Review composable structure and composition patterns →
    references/composables.md
  3. Validate state management and data flow →
    references/state.md
  4. Validate side-effect usage →
    references/effects.md
  5. Check recomposition stability →
    references/recomposition.md
  6. Validate navigation implementation →
    references/navigation.md
  7. Check Material 3 / Expressive design compliance →
    references/design.md
  8. Validate accessibility →
    references/accessibility.md
  9. Check performance →
    references/performance.md
  10. Quick Kotlin code review →
    references/kotlin.md
  11. Final code hygiene check →
    references/hygiene.md
When generating new code, load the relevant reference files for the feature being built before writing any code, so output is idiomatic from the start.
For partial reviews or targeted generation, load only the relevant reference files.
评审现有代码时,请按顺序遵循以下步骤:
  1. 检查是否存在弃用或过时的API →
    references/api.md
  2. 评审Composable结构和组合模式 →
    references/composables.md
  3. 验证状态管理和数据流 →
    references/state.md
  4. 验证副作用用法 →
    references/effects.md
  5. 检查重组稳定性 →
    references/recomposition.md
  6. 验证导航实现 →
    references/navigation.md
  7. 检查Material 3/Expressive设计规范合规性 →
    references/design.md
  8. 验证无障碍适配 →
    references/accessibility.md
  9. 检查性能 →
    references/performance.md
  10. 快速Kotlin代码评审 →
    references/kotlin.md
  11. 最终代码规范检查 →
    references/hygiene.md
生成新代码时,在编写任何代码前先加载与待开发功能相关的参考文件,确保从一开始输出的就是符合行业惯例的代码。
如果是局部评审或定向生成代码,仅加载相关的参考文件即可。

Core Instructions

核心说明

  • Target Compose BOM 2024.x by default. Note where BOM 2025.x (Material Expressive) introduces new components or APIs.
  • Use Material 3. Do not use Material 2 unless the project already uses it.
  • Architecture: MVVM with unidirectional data flow (UDF). ViewModel + StateFlow for screen state. Compose UI observes state, emits events.
  • Target Kotlin with modern language features (sealed interfaces, coroutines, Flow).
  • Do not introduce third-party libraries without asking first.
  • Each composable should live in its own file for non-trivial components.
  • Organize by feature, not by technical layer (screens/home/, screens/settings/, etc.).
  • 默认适配Compose BOM 2024.x版本,若BOM 2025.x(Material Expressive)引入了新组件或API,请单独标注。
  • 使用Material 3,除非项目已使用Material 2,否则不要使用Material 2。
  • 架构:采用单向数据流(UDF)的MVVM架构,使用ViewModel + StateFlow管理页面状态,Compose UI监听状态、触发事件。
  • 适配使用现代语法特性的Kotlin(密封接口、协程、Flow)。
  • 未经询问不要引入第三方库。
  • 非简单组件的每个Composable都应该单独存放在一个文件中。
  • 按功能模块组织代码,不要按技术层组织(例如screens/home/、screens/settings/等)。

Output Format

输出格式

Organize findings by file. For each issue:
  1. State the file and relevant line(s).
  2. Name the rule being violated.
  3. Show a brief before/after Kotlin snippet.
Skip files with no issues. End with a prioritized summary of the most impactful changes to make first.
Example output:
按文件整理发现的问题,每个问题遵循以下结构:
  1. 说明文件路径和相关行号。
  2. 说明违反的规则名称。
  3. 展示简短的Kotlin代码修改前后片段。
跳过没有问题的文件,最后输出优先级排序的总结,列出需要优先修改的影响最大的变更。
输出示例:

HomeScreen.kt

HomeScreen.kt

Line 14: Use
collectAsStateWithLifecycle()
instead of
collectAsState()
.
kotlin
// Before
val uiState by viewModel.uiState.collectAsState()

// After
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
Line 42: Provide
key
in LazyColumn for stable item identity.
kotlin
// Before
LazyColumn {
    items(books) { book ->
        BookItem(book)
    }
}

// After
LazyColumn {
    items(books, key = { it.id }) { book ->
        BookItem(book)
    }
}
Line 67: Image missing contentDescription — required for accessibility.
kotlin
// Before
Image(painter = painterResource(R.drawable.cover), contentDescription = null)

// After — if decorative:
Image(painter = painterResource(R.drawable.cover), contentDescription = null) // OK if truly decorative

// After — if meaningful:
Image(
    painter = painterResource(R.drawable.cover),
    contentDescription = stringResource(R.string.book_cover_description)
)
第14行:使用
collectAsStateWithLifecycle()
替代
collectAsState()
kotlin
// Before
val uiState by viewModel.uiState.collectAsState()

// After
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
第42行:为LazyColumn提供
key
保证item标识稳定
kotlin
// Before
LazyColumn {
    items(books) { book ->
        BookItem(book)
    }
}

// After
LazyColumn {
    items(books, key = { it.id }) { book ->
        BookItem(book)
    }
}
第67行:Image缺少contentDescription——无障碍适配要求
kotlin
// Before
Image(painter = painterResource(R.drawable.cover), contentDescription = null)

// After — 若为装饰性元素:
Image(painter = painterResource(R.drawable.cover), contentDescription = null) // 确认是纯装饰性元素则符合要求

// After — 若为有意义的元素:
Image(
    painter = painterResource(R.drawable.cover),
    contentDescription = stringResource(R.string.book_cover_description)
)

Summary

总结

  1. State (high):
    collectAsState()
    on line 14 does not respect lifecycle — replace with
    collectAsStateWithLifecycle()
    .
  2. Performance (medium): Missing
    key
    in
    LazyColumn
    on line 42 causes unnecessary recomposition.
  3. Accessibility (medium): Image on line 67 needs a meaningful
    contentDescription
    .

  1. 状态(高优先级): 第14行的
    collectAsState()
    不遵循生命周期规则——替换为
    collectAsStateWithLifecycle()
  2. 性能(中优先级): 第42行
    LazyColumn
    缺少
    key
    会导致不必要的重组。
  3. 无障碍(中优先级): 第67行的Image需要设置有意义的
    contentDescription

References

参考文档

  • references/api.md
    — deprecated APIs and their modern replacements.
  • references/composables.md
    — composable structure, naming, and composition patterns.
  • references/state.md
    — state management, ViewModel, StateFlow, and data flow.
  • references/effects.md
    — side effects: LaunchedEffect, DisposableEffect, SideEffect.
  • references/recomposition.md
    — recomposition stability, @Stable/@Immutable, derivedStateOf.
  • references/navigation.md
    — Navigation Compose, type-safe nav, nested graphs.
  • references/design.md
    — Material 3 / Expressive theming, adaptive layouts.
  • references/accessibility.md
    — TalkBack, semantics, content descriptions, touch targets.
  • references/performance.md
    — LazyList optimization, remember, scope of state reads.
  • references/kotlin.md
    — modern Kotlin patterns for Android.
  • references/hygiene.md
    — code hygiene, testing, lint.
  • references/api.md
    — 弃用API及对应的现代替代方案。
  • references/composables.md
    — Composable结构、命名和组合模式。
  • references/state.md
    — 状态管理、ViewModel、StateFlow和数据流。
  • references/effects.md
    — 副作用:LaunchedEffect、DisposableEffect、SideEffect。
  • references/recomposition.md
    — 重组稳定性、@Stable/@Immutable、derivedStateOf。
  • references/navigation.md
    — Navigation Compose、类型安全导航、嵌套导航图。
  • references/design.md
    — Material 3/Expressive主题、自适应布局。
  • references/accessibility.md
    — 读屏器(TalkBack)、语义属性、contentDescription、触摸热区。
  • references/performance.md
    — LazyList优化、remember、状态读取作用域。
  • references/kotlin.md
    — Android适用的现代Kotlin模式。
  • references/hygiene.md
    — 代码规范、测试、lint检查。