AgentSkillsCN

refactor

遵循官方Android架构规范进行安全重构。在不改变原有行为的前提下,提升代码质量。

SKILL.md
--- frontmatter
name: refactor
description: Safe refactoring aligned with official Android architecture. Improve code quality without changing behavior.
argument-hint: "<target-and-goal>"
allowed-tools:
  - Read
  - Write
  - Edit
  - Glob
  - Grep
  - Bash
  - Task

Refactor (Android Architecture Aligned)

Refactor existing code to align with the official Android architecture. No external behavior changes.

Principles

  • No behavior changes: Refactoring improves internal structure only
  • Incremental steps: Avoid large changes at once
  • Build verification at each step: Revert immediately if broken

Refactoring Patterns

Layer Separation (Highest Priority)

When business logic exists in Activity/Fragment:

  1. Extract UI State as a data class
  2. Create a ViewModel and move logic into it
  3. Expose state via StateFlow<UiState>
  4. UI collects with collectAsStateWithLifecycle()
  5. Define user actions as sealed interface

Introducing Repository Pattern:

  1. Extract data access into a Repository interface
  2. Create implementation class (inject DataSource)
  3. ViewModel depends only on Repository

ViewModel Improvements

  • Consolidate multiple MutableState → single UiState data class
  • Migrate LiveDataStateFlow
  • Apply stateIn(SharingStarted.WhileSubscribed(5_000))
  • Convert one-off events to state (event → state pattern)

Compose Improvements

  • Split large Composables (one Composable ≈ one responsibility)
  • Apply state hoisting (make Composables stateless)
  • Optimize with remember / derivedStateOf
  • Add @Preview functions
  • Replace NavController passing with callback pattern

DI Introduction/Improvement

  • Migrate Manual DI → Hilt
  • @HiltViewModel + @Inject constructor
  • Use @Binds for interface bindings
  • Optimize scopes

Kotlin Idioms

  • Use when expressions
  • Convert to sealed class/sealed interface
  • Leverage extension functions
  • Remove unnecessary !! (use ?.let, requireNotNull, defaults)
  • Use data class appropriately

Build Configuration Improvements

  • Consolidate into Version Catalog (libs.versions.toml)
  • Introduce Convention Plugins (for multi-module projects)
  • Remove unused dependencies

Procedure

Step 1: Current State Analysis

  • List issues in the target code
  • Create a refactoring plan (tracked with TodoWrite)

Step 2: Incremental Execution

  • Execute one step at a time following the plan
  • After each step: ./gradlew assembleDebug

Step 3: Final Verification

bash
./gradlew assembleDebug
./gradlew test
./gradlew lint

Step 4: Change Summary

Report before/after architecture comparison and improvements made.

Target

$ARGUMENTS