mirror of
https://github.com/affaan-m/everything-claude-code.git
synced 2026-04-09 10:53:34 +08:00
feat: add Kotlin, Android, and KMP rules, agent, skills, and command
This commit is contained in:
328
skills/android-clean-architecture/SKILL.md
Normal file
328
skills/android-clean-architecture/SKILL.md
Normal file
@@ -0,0 +1,328 @@
|
||||
---
|
||||
name: android-clean-architecture
|
||||
description: Clean Architecture patterns for Android and Kotlin Multiplatform projects — module structure, dependency rules, UseCases, Repositories, and data layer patterns.
|
||||
origin: ECC
|
||||
---
|
||||
|
||||
# Android Clean Architecture
|
||||
|
||||
Clean Architecture patterns for Android and KMP projects. Covers module boundaries, dependency inversion, UseCase/Repository patterns, and data layer design with Room, SQLDelight, and Ktor.
|
||||
|
||||
## When to Activate
|
||||
|
||||
- Structuring Android or KMP project modules
|
||||
- Implementing UseCases, Repositories, or DataSources
|
||||
- Designing data flow between layers (domain, data, presentation)
|
||||
- Setting up dependency injection with Koin or Hilt
|
||||
- Working with Room, SQLDelight, or Ktor in a layered architecture
|
||||
|
||||
## Module Structure
|
||||
|
||||
### Recommended Layout
|
||||
|
||||
```
|
||||
project/
|
||||
├── app/ # Android entry point, DI wiring, Application class
|
||||
├── core/ # Shared utilities, base classes, error types
|
||||
├── domain/ # UseCases, domain models, repository interfaces (pure Kotlin)
|
||||
├── data/ # Repository implementations, DataSources, DB, network
|
||||
├── presentation/ # Screens, ViewModels, UI models, navigation
|
||||
├── design-system/ # Reusable Compose components, theme, typography
|
||||
└── feature/ # Feature modules (optional, for larger projects)
|
||||
├── auth/
|
||||
├── settings/
|
||||
└── profile/
|
||||
```
|
||||
|
||||
### Dependency Rules
|
||||
|
||||
```
|
||||
app → presentation, domain, data, core
|
||||
presentation → domain, design-system, core
|
||||
data → domain, core
|
||||
domain → core (or no dependencies)
|
||||
core → (nothing)
|
||||
```
|
||||
|
||||
**Critical**: `domain` must NEVER depend on `data`, `presentation`, or any framework. It contains pure Kotlin only.
|
||||
|
||||
## Domain Layer
|
||||
|
||||
### UseCase Pattern
|
||||
|
||||
Each UseCase represents one business operation. Use `operator fun invoke` for clean call sites:
|
||||
|
||||
```kotlin
|
||||
class GetItemsByCategoryUseCase(
|
||||
private val repository: ItemRepository
|
||||
) {
|
||||
suspend operator fun invoke(category: String): Result<List<Item>> {
|
||||
return repository.getItemsByCategory(category)
|
||||
}
|
||||
}
|
||||
|
||||
// Flow-based UseCase for reactive streams
|
||||
class ObserveUserProgressUseCase(
|
||||
private val repository: UserRepository
|
||||
) {
|
||||
operator fun invoke(userId: String): Flow<UserProgress> {
|
||||
return repository.observeProgress(userId)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Domain Models
|
||||
|
||||
Domain models are plain Kotlin data classes — no framework annotations:
|
||||
|
||||
```kotlin
|
||||
data class Item(
|
||||
val id: String,
|
||||
val title: String,
|
||||
val description: String,
|
||||
val tags: List<String>,
|
||||
val status: Status
|
||||
)
|
||||
|
||||
enum class Status { DRAFT, ACTIVE, ARCHIVED }
|
||||
```
|
||||
|
||||
### Repository Interfaces
|
||||
|
||||
Defined in domain, implemented in data:
|
||||
|
||||
```kotlin
|
||||
interface ItemRepository {
|
||||
suspend fun getItemsByCategory(category: String): Result<List<Item>>
|
||||
suspend fun saveItem(item: Item): Result<Unit>
|
||||
fun observeItems(): Flow<List<Item>>
|
||||
}
|
||||
```
|
||||
|
||||
## Data Layer
|
||||
|
||||
### Repository Implementation
|
||||
|
||||
Coordinates between local and remote data sources:
|
||||
|
||||
```kotlin
|
||||
class ItemRepositoryImpl(
|
||||
private val localDataSource: ItemLocalDataSource,
|
||||
private val remoteDataSource: ItemRemoteDataSource
|
||||
) : ItemRepository {
|
||||
|
||||
override suspend fun getItemsByCategory(category: String): Result<List<Item>> {
|
||||
return runCatching {
|
||||
val remote = remoteDataSource.fetchItems(category)
|
||||
localDataSource.insertItems(remote.map { it.toEntity() })
|
||||
localDataSource.getItemsByCategory(category).map { it.toDomain() }
|
||||
}
|
||||
}
|
||||
|
||||
override fun observeItems(): Flow<List<Item>> {
|
||||
return localDataSource.observeAll().map { entities ->
|
||||
entities.map { it.toDomain() }
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Mapper Pattern
|
||||
|
||||
Keep mappers as extension functions near the data models:
|
||||
|
||||
```kotlin
|
||||
// In data layer
|
||||
fun ItemEntity.toDomain() = Item(
|
||||
id = id,
|
||||
title = title,
|
||||
description = description,
|
||||
tags = tags.split("|"),
|
||||
status = Status.valueOf(status)
|
||||
)
|
||||
|
||||
fun ItemDto.toEntity() = ItemEntity(
|
||||
id = id,
|
||||
title = title,
|
||||
description = description,
|
||||
tags = tags.joinToString("|"),
|
||||
status = status
|
||||
)
|
||||
```
|
||||
|
||||
### Room Database (Android)
|
||||
|
||||
```kotlin
|
||||
@Entity(tableName = "items")
|
||||
data class ItemEntity(
|
||||
@PrimaryKey val id: String,
|
||||
val title: String,
|
||||
val description: String,
|
||||
val tags: String,
|
||||
val status: String
|
||||
)
|
||||
|
||||
@Dao
|
||||
interface ItemDao {
|
||||
@Query("SELECT * FROM items WHERE category = :category")
|
||||
suspend fun getByCategory(category: String): List<ItemEntity>
|
||||
|
||||
@Upsert
|
||||
suspend fun upsert(items: List<ItemEntity>)
|
||||
|
||||
@Query("SELECT * FROM items")
|
||||
fun observeAll(): Flow<List<ItemEntity>>
|
||||
}
|
||||
```
|
||||
|
||||
### SQLDelight (KMP)
|
||||
|
||||
```sql
|
||||
-- Item.sq
|
||||
CREATE TABLE ItemEntity (
|
||||
id TEXT NOT NULL PRIMARY KEY,
|
||||
title TEXT NOT NULL,
|
||||
description TEXT NOT NULL,
|
||||
tags TEXT NOT NULL,
|
||||
status TEXT NOT NULL
|
||||
);
|
||||
|
||||
getByCategory:
|
||||
SELECT * FROM ItemEntity WHERE category = ?;
|
||||
|
||||
upsert:
|
||||
INSERT OR REPLACE INTO ItemEntity (id, title, description, tags, status)
|
||||
VALUES (?, ?, ?, ?, ?);
|
||||
|
||||
observeAll:
|
||||
SELECT * FROM ItemEntity;
|
||||
```
|
||||
|
||||
### Ktor Network Client (KMP)
|
||||
|
||||
```kotlin
|
||||
class ItemRemoteDataSource(private val client: HttpClient) {
|
||||
|
||||
suspend fun fetchItems(category: String): List<ItemDto> {
|
||||
return client.get("api/items") {
|
||||
parameter("category", category)
|
||||
}.body()
|
||||
}
|
||||
}
|
||||
|
||||
// HttpClient setup with content negotiation
|
||||
val httpClient = HttpClient {
|
||||
install(ContentNegotiation) { json(Json { ignoreUnknownKeys = true }) }
|
||||
install(Logging) { level = LogLevel.HEADERS }
|
||||
defaultRequest { url("https://api.example.com/") }
|
||||
}
|
||||
```
|
||||
|
||||
## Dependency Injection
|
||||
|
||||
### Koin (KMP-friendly)
|
||||
|
||||
```kotlin
|
||||
// Domain module
|
||||
val domainModule = module {
|
||||
factory { GetItemsByCategoryUseCase(get()) }
|
||||
factory { ObserveUserProgressUseCase(get()) }
|
||||
}
|
||||
|
||||
// Data module
|
||||
val dataModule = module {
|
||||
single<ItemRepository> { ItemRepositoryImpl(get(), get()) }
|
||||
single { ItemLocalDataSource(get()) }
|
||||
single { ItemRemoteDataSource(get()) }
|
||||
}
|
||||
|
||||
// Presentation module
|
||||
val presentationModule = module {
|
||||
viewModelOf(::ItemListViewModel)
|
||||
viewModelOf(::DashboardViewModel)
|
||||
}
|
||||
```
|
||||
|
||||
### Hilt (Android-only)
|
||||
|
||||
```kotlin
|
||||
@Module
|
||||
@InstallIn(SingletonComponent::class)
|
||||
abstract class RepositoryModule {
|
||||
@Binds
|
||||
abstract fun bindItemRepository(impl: ItemRepositoryImpl): ItemRepository
|
||||
}
|
||||
|
||||
@HiltViewModel
|
||||
class ItemListViewModel @Inject constructor(
|
||||
private val getItems: GetItemsByCategoryUseCase
|
||||
) : ViewModel()
|
||||
```
|
||||
|
||||
## Error Handling
|
||||
|
||||
### Result/Try Pattern
|
||||
|
||||
Use `Result<T>` or a custom sealed type for error propagation:
|
||||
|
||||
```kotlin
|
||||
sealed interface Try<out T> {
|
||||
data class Success<T>(val value: T) : Try<T>
|
||||
data class Failure(val error: AppError) : Try<Nothing>
|
||||
}
|
||||
|
||||
sealed interface AppError {
|
||||
data class Network(val message: String) : AppError
|
||||
data class Database(val message: String) : AppError
|
||||
data object Unauthorized : AppError
|
||||
}
|
||||
|
||||
// In ViewModel — map to UI state
|
||||
viewModelScope.launch {
|
||||
when (val result = getItems(category)) {
|
||||
is Try.Success -> _state.update { it.copy(items = result.value, isLoading = false) }
|
||||
is Try.Failure -> _state.update { it.copy(error = result.error.toMessage(), isLoading = false) }
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Convention Plugins (Gradle)
|
||||
|
||||
For KMP projects, use convention plugins to reduce build file duplication:
|
||||
|
||||
```kotlin
|
||||
// build-logic/src/main/kotlin/kmp-library.gradle.kts
|
||||
plugins {
|
||||
id("org.jetbrains.kotlin.multiplatform")
|
||||
}
|
||||
|
||||
kotlin {
|
||||
androidTarget()
|
||||
iosX64(); iosArm64(); iosSimulatorArm64()
|
||||
sourceSets {
|
||||
commonMain.dependencies { /* shared deps */ }
|
||||
commonTest.dependencies { implementation(kotlin("test")) }
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Apply in modules:
|
||||
|
||||
```kotlin
|
||||
// domain/build.gradle.kts
|
||||
plugins { id("kmp-library") }
|
||||
```
|
||||
|
||||
## Anti-Patterns to Avoid
|
||||
|
||||
- Importing Android framework classes in `domain` — keep it pure Kotlin
|
||||
- Exposing database entities or DTOs to the UI layer — always map to domain models
|
||||
- Putting business logic in ViewModels — extract to UseCases
|
||||
- Using `GlobalScope` or unstructured coroutines — use `viewModelScope` or structured concurrency
|
||||
- Fat repository implementations — split into focused DataSources
|
||||
- Circular module dependencies — if A depends on B, B must not depend on A
|
||||
|
||||
## References
|
||||
|
||||
See skill: `compose-multiplatform-patterns` for UI patterns.
|
||||
See skill: `kotlin-coroutines-flows` for async patterns.
|
||||
295
skills/compose-multiplatform-patterns/SKILL.md
Normal file
295
skills/compose-multiplatform-patterns/SKILL.md
Normal file
@@ -0,0 +1,295 @@
|
||||
---
|
||||
name: compose-multiplatform-patterns
|
||||
description: Compose Multiplatform and Jetpack Compose patterns for KMP projects — state management, navigation, theming, performance, and platform-specific UI.
|
||||
origin: ECC
|
||||
---
|
||||
|
||||
# Compose Multiplatform Patterns
|
||||
|
||||
Patterns for building shared UI across Android, iOS, Desktop, and Web using Compose Multiplatform and Jetpack Compose. Covers state management, navigation, theming, and performance.
|
||||
|
||||
## When to Activate
|
||||
|
||||
- Building Compose UI (Jetpack Compose or Compose Multiplatform)
|
||||
- Managing UI state with ViewModels and Compose state
|
||||
- Implementing navigation in KMP or Android projects
|
||||
- Designing reusable composables and design systems
|
||||
- Optimizing recomposition and rendering performance
|
||||
|
||||
## State Management
|
||||
|
||||
### ViewModel + Single State Object
|
||||
|
||||
Use a single data class for screen state. Expose it as `StateFlow` and collect in Compose:
|
||||
|
||||
```kotlin
|
||||
data class ItemListState(
|
||||
val items: List<Item> = emptyList(),
|
||||
val isLoading: Boolean = false,
|
||||
val error: String? = null,
|
||||
val searchQuery: String = ""
|
||||
)
|
||||
|
||||
class ItemListViewModel(
|
||||
private val getItems: GetItemsUseCase
|
||||
) : ViewModel() {
|
||||
private val _state = MutableStateFlow(ItemListState())
|
||||
val state: StateFlow<ItemListState> = _state.asStateFlow()
|
||||
|
||||
fun onSearch(query: String) {
|
||||
_state.update { it.copy(searchQuery = query) }
|
||||
loadItems(query)
|
||||
}
|
||||
|
||||
private fun loadItems(query: String) {
|
||||
viewModelScope.launch {
|
||||
_state.update { it.copy(isLoading = true) }
|
||||
getItems(query).fold(
|
||||
onSuccess = { items -> _state.update { it.copy(items = items, isLoading = false) } },
|
||||
onFailure = { e -> _state.update { it.copy(error = e.message, isLoading = false) } }
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Collecting State in Compose
|
||||
|
||||
```kotlin
|
||||
@Composable
|
||||
fun ItemListScreen(viewModel: ItemListViewModel = koinViewModel()) {
|
||||
val state by viewModel.state.collectAsStateWithLifecycle()
|
||||
|
||||
ItemListContent(
|
||||
state = state,
|
||||
onSearch = viewModel::onSearch
|
||||
)
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun ItemListContent(
|
||||
state: ItemListState,
|
||||
onSearch: (String) -> Unit
|
||||
) {
|
||||
// Stateless composable — easy to preview and test
|
||||
}
|
||||
```
|
||||
|
||||
### Event Sink Pattern
|
||||
|
||||
For complex screens, use a sealed interface for events instead of multiple callback lambdas:
|
||||
|
||||
```kotlin
|
||||
sealed interface ItemListEvent {
|
||||
data class Search(val query: String) : ItemListEvent
|
||||
data class Delete(val itemId: String) : ItemListEvent
|
||||
data object Refresh : ItemListEvent
|
||||
}
|
||||
|
||||
// In ViewModel
|
||||
fun onEvent(event: ItemListEvent) {
|
||||
when (event) {
|
||||
is ItemListEvent.Search -> onSearch(event.query)
|
||||
is ItemListEvent.Delete -> deleteItem(event.itemId)
|
||||
is ItemListEvent.Refresh -> loadItems()
|
||||
}
|
||||
}
|
||||
|
||||
// In Composable — single lambda instead of many
|
||||
ItemListContent(
|
||||
state = state,
|
||||
onEvent = viewModel::onEvent
|
||||
)
|
||||
```
|
||||
|
||||
## Navigation
|
||||
|
||||
### Type-Safe Navigation (Compose Navigation 2.8+)
|
||||
|
||||
Define routes as `@Serializable` objects:
|
||||
|
||||
```kotlin
|
||||
@Serializable data object HomeRoute
|
||||
@Serializable data class DetailRoute(val id: String)
|
||||
@Serializable data object SettingsRoute
|
||||
|
||||
@Composable
|
||||
fun AppNavHost(navController: NavHostController = rememberNavController()) {
|
||||
NavHost(navController, startDestination = HomeRoute) {
|
||||
composable<HomeRoute> {
|
||||
HomeScreen(onNavigateToDetail = { id -> navController.navigate(DetailRoute(id)) })
|
||||
}
|
||||
composable<DetailRoute> { backStackEntry ->
|
||||
val route = backStackEntry.toRoute<DetailRoute>()
|
||||
DetailScreen(id = route.id)
|
||||
}
|
||||
composable<SettingsRoute> { SettingsScreen() }
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Dialog and Bottom Sheet Navigation
|
||||
|
||||
Use `dialog()` and overlay patterns instead of imperative show/hide:
|
||||
|
||||
```kotlin
|
||||
NavHost(navController, startDestination = HomeRoute) {
|
||||
composable<HomeRoute> { /* ... */ }
|
||||
dialog<ConfirmDeleteRoute> { backStackEntry ->
|
||||
val route = backStackEntry.toRoute<ConfirmDeleteRoute>()
|
||||
ConfirmDeleteDialog(
|
||||
itemId = route.itemId,
|
||||
onConfirm = { navController.popBackStack() },
|
||||
onDismiss = { navController.popBackStack() }
|
||||
)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Composable Design
|
||||
|
||||
### Slot-Based APIs
|
||||
|
||||
Design composables with slot parameters for flexibility:
|
||||
|
||||
```kotlin
|
||||
@Composable
|
||||
fun AppCard(
|
||||
modifier: Modifier = Modifier,
|
||||
header: @Composable () -> Unit = {},
|
||||
content: @Composable ColumnScope.() -> Unit,
|
||||
actions: @Composable RowScope.() -> Unit = {}
|
||||
) {
|
||||
Card(modifier = modifier) {
|
||||
Column {
|
||||
header()
|
||||
Column(content = content)
|
||||
Row(horizontalArrangement = Arrangement.End, content = actions)
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Modifier Ordering
|
||||
|
||||
Modifier order matters — apply in this sequence:
|
||||
|
||||
```kotlin
|
||||
Text(
|
||||
text = "Hello",
|
||||
modifier = Modifier
|
||||
.padding(16.dp) // 1. Layout (padding, size)
|
||||
.clip(RoundedCornerShape(8.dp)) // 2. Shape
|
||||
.background(Color.White) // 3. Drawing (background, border)
|
||||
.clickable { } // 4. Interaction
|
||||
)
|
||||
```
|
||||
|
||||
## KMP Platform-Specific UI
|
||||
|
||||
### expect/actual for Platform Composables
|
||||
|
||||
```kotlin
|
||||
// commonMain
|
||||
@Composable
|
||||
expect fun PlatformStatusBar(darkIcons: Boolean)
|
||||
|
||||
// androidMain
|
||||
@Composable
|
||||
actual fun PlatformStatusBar(darkIcons: Boolean) {
|
||||
val systemUiController = rememberSystemUiController()
|
||||
SideEffect { systemUiController.setStatusBarColor(Color.Transparent, darkIcons) }
|
||||
}
|
||||
|
||||
// iosMain
|
||||
@Composable
|
||||
actual fun PlatformStatusBar(darkIcons: Boolean) {
|
||||
// iOS handles this via UIKit interop or Info.plist
|
||||
}
|
||||
```
|
||||
|
||||
## Performance
|
||||
|
||||
### Stable Types for Skippable Recomposition
|
||||
|
||||
Mark classes as `@Stable` or `@Immutable` when all properties are stable:
|
||||
|
||||
```kotlin
|
||||
@Immutable
|
||||
data class ItemUiModel(
|
||||
val id: String,
|
||||
val title: String,
|
||||
val description: String,
|
||||
val progress: Float
|
||||
)
|
||||
```
|
||||
|
||||
### Use `key()` and Lazy Lists Correctly
|
||||
|
||||
```kotlin
|
||||
LazyColumn {
|
||||
items(
|
||||
items = items,
|
||||
key = { it.id } // Stable keys enable item reuse and animations
|
||||
) { item ->
|
||||
ItemRow(item = item)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Defer Reads with `derivedStateOf`
|
||||
|
||||
```kotlin
|
||||
val listState = rememberLazyListState()
|
||||
val showScrollToTop by remember {
|
||||
derivedStateOf { listState.firstVisibleItemIndex > 5 }
|
||||
}
|
||||
```
|
||||
|
||||
### Avoid Allocations in Recomposition
|
||||
|
||||
```kotlin
|
||||
// BAD — new lambda and list every recomposition
|
||||
items.filter { it.isActive }.forEach { ActiveItem(it, onClick = { handle(it) }) }
|
||||
|
||||
// GOOD — remember filtered list, use method reference or remembered lambda
|
||||
val activeItems = remember(items) { items.filter { it.isActive } }
|
||||
activeItems.forEach { ActiveItem(it, onClick = remember { { handle(it) } }) }
|
||||
```
|
||||
|
||||
## Theming
|
||||
|
||||
### Material 3 Dynamic Theming
|
||||
|
||||
```kotlin
|
||||
@Composable
|
||||
fun AppTheme(
|
||||
darkTheme: Boolean = isSystemInDarkTheme(),
|
||||
dynamicColor: Boolean = true,
|
||||
content: @Composable () -> Unit
|
||||
) {
|
||||
val colorScheme = when {
|
||||
dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
|
||||
if (darkTheme) dynamicDarkColorScheme(LocalContext.current)
|
||||
else dynamicLightColorScheme(LocalContext.current)
|
||||
}
|
||||
darkTheme -> darkColorScheme()
|
||||
else -> lightColorScheme()
|
||||
}
|
||||
|
||||
MaterialTheme(colorScheme = colorScheme, content = content)
|
||||
}
|
||||
```
|
||||
|
||||
## Anti-Patterns to Avoid
|
||||
|
||||
- Using `mutableStateOf` in ViewModels when `MutableStateFlow` with `collectAsStateWithLifecycle` is safer for lifecycle
|
||||
- Passing `NavController` deep into composables — pass lambda callbacks instead
|
||||
- Heavy computation inside `@Composable` functions — move to ViewModel or `remember {}`
|
||||
- Using `LaunchedEffect(Unit)` as a substitute for ViewModel init — it re-runs on configuration change in some setups
|
||||
- Creating new object instances in composable parameters — causes unnecessary recomposition
|
||||
|
||||
## References
|
||||
|
||||
See skill: `android-clean-architecture` for module structure and layering.
|
||||
See skill: `kotlin-coroutines-flows` for coroutine and Flow patterns.
|
||||
279
skills/kotlin-coroutines-flows/SKILL.md
Normal file
279
skills/kotlin-coroutines-flows/SKILL.md
Normal file
@@ -0,0 +1,279 @@
|
||||
---
|
||||
name: kotlin-coroutines-flows
|
||||
description: Kotlin Coroutines and Flow patterns for Android and KMP — structured concurrency, Flow operators, StateFlow, error handling, and testing.
|
||||
origin: ECC
|
||||
---
|
||||
|
||||
# Kotlin Coroutines & Flows
|
||||
|
||||
Patterns for structured concurrency, Flow-based reactive streams, and coroutine testing in Android and Kotlin Multiplatform projects.
|
||||
|
||||
## When to Activate
|
||||
|
||||
- Writing async code with Kotlin coroutines
|
||||
- Using Flow, StateFlow, or SharedFlow for reactive data
|
||||
- Handling concurrent operations (parallel loading, debounce, retry)
|
||||
- Testing coroutines and Flows
|
||||
- Managing coroutine scopes and cancellation
|
||||
|
||||
## Structured Concurrency
|
||||
|
||||
### Scope Hierarchy
|
||||
|
||||
```
|
||||
Application
|
||||
└── viewModelScope (ViewModel)
|
||||
└── coroutineScope { } (structured child)
|
||||
├── async { } (concurrent task)
|
||||
└── async { } (concurrent task)
|
||||
```
|
||||
|
||||
Always use structured concurrency — never `GlobalScope`:
|
||||
|
||||
```kotlin
|
||||
// BAD
|
||||
GlobalScope.launch { fetchData() }
|
||||
|
||||
// GOOD — scoped to ViewModel lifecycle
|
||||
viewModelScope.launch { fetchData() }
|
||||
|
||||
// GOOD — scoped to composable lifecycle
|
||||
LaunchedEffect(key) { fetchData() }
|
||||
```
|
||||
|
||||
### Parallel Decomposition
|
||||
|
||||
Use `coroutineScope` + `async` for parallel work:
|
||||
|
||||
```kotlin
|
||||
suspend fun loadDashboard(): Dashboard = coroutineScope {
|
||||
val items = async { itemRepository.getRecent() }
|
||||
val stats = async { statsRepository.getToday() }
|
||||
val profile = async { userRepository.getCurrent() }
|
||||
Dashboard(
|
||||
items = items.await(),
|
||||
stats = stats.await(),
|
||||
profile = profile.await()
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
### SupervisorScope
|
||||
|
||||
Use `supervisorScope` when child failures should not cancel siblings:
|
||||
|
||||
```kotlin
|
||||
suspend fun syncAll() = supervisorScope {
|
||||
launch { syncItems() } // failure here won't cancel syncStats
|
||||
launch { syncStats() }
|
||||
launch { syncSettings() }
|
||||
}
|
||||
```
|
||||
|
||||
## Flow Patterns
|
||||
|
||||
### Cold Flow — One-Shot to Stream Conversion
|
||||
|
||||
```kotlin
|
||||
fun observeItems(): Flow<List<Item>> = flow {
|
||||
// Re-emits whenever the database changes
|
||||
itemDao.observeAll()
|
||||
.map { entities -> entities.map { it.toDomain() } }
|
||||
.collect { emit(it) }
|
||||
}
|
||||
```
|
||||
|
||||
### StateFlow for UI State
|
||||
|
||||
```kotlin
|
||||
class DashboardViewModel(
|
||||
observeProgress: ObserveUserProgressUseCase
|
||||
) : ViewModel() {
|
||||
val progress: StateFlow<UserProgress> = observeProgress()
|
||||
.stateIn(
|
||||
scope = viewModelScope,
|
||||
started = SharingStarted.WhileSubscribed(5_000),
|
||||
initialValue = UserProgress.EMPTY
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
`WhileSubscribed(5_000)` keeps the upstream active for 5 seconds after the last subscriber leaves — survives configuration changes without restarting.
|
||||
|
||||
### Combining Multiple Flows
|
||||
|
||||
```kotlin
|
||||
val uiState: StateFlow<HomeState> = combine(
|
||||
itemRepository.observeItems(),
|
||||
settingsRepository.observeTheme(),
|
||||
userRepository.observeProfile()
|
||||
) { items, theme, profile ->
|
||||
HomeState(items = items, theme = theme, profile = profile)
|
||||
}.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), HomeState())
|
||||
```
|
||||
|
||||
### Flow Operators
|
||||
|
||||
```kotlin
|
||||
// Debounce search input
|
||||
searchQuery
|
||||
.debounce(300)
|
||||
.distinctUntilChanged()
|
||||
.flatMapLatest { query -> repository.search(query) }
|
||||
.catch { emit(emptyList()) }
|
||||
.collect { results -> _state.update { it.copy(results = results) } }
|
||||
|
||||
// Retry with exponential backoff
|
||||
fun fetchWithRetry(): Flow<Data> = flow { emit(api.fetch()) }
|
||||
.retry(3) { cause ->
|
||||
cause is IOException && run { delay(1000L * (1 shl (3 - remainingAttempts))) ; true }
|
||||
}
|
||||
```
|
||||
|
||||
### SharedFlow for One-Time Events
|
||||
|
||||
```kotlin
|
||||
class ItemListViewModel : ViewModel() {
|
||||
private val _effects = MutableSharedFlow<Effect>()
|
||||
val effects: SharedFlow<Effect> = _effects.asSharedFlow()
|
||||
|
||||
sealed interface Effect {
|
||||
data class ShowSnackbar(val message: String) : Effect
|
||||
data class NavigateTo(val route: String) : Effect
|
||||
}
|
||||
|
||||
private fun deleteItem(id: String) {
|
||||
viewModelScope.launch {
|
||||
repository.delete(id)
|
||||
_effects.emit(Effect.ShowSnackbar("Item deleted"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Collect in Composable
|
||||
LaunchedEffect(Unit) {
|
||||
viewModel.effects.collect { effect ->
|
||||
when (effect) {
|
||||
is Effect.ShowSnackbar -> snackbarHostState.showSnackbar(effect.message)
|
||||
is Effect.NavigateTo -> navController.navigate(effect.route)
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Dispatchers
|
||||
|
||||
```kotlin
|
||||
// CPU-intensive work
|
||||
withContext(Dispatchers.Default) { parseJson(largePayload) }
|
||||
|
||||
// IO-bound work
|
||||
withContext(Dispatchers.IO) { database.query() }
|
||||
|
||||
// Main thread (UI) — default in viewModelScope
|
||||
withContext(Dispatchers.Main) { updateUi() }
|
||||
```
|
||||
|
||||
In KMP, use `Dispatchers.Default` and `Dispatchers.Main` (available on all platforms). `Dispatchers.IO` is JVM/Android only — use `Dispatchers.Default` on other platforms or provide via DI.
|
||||
|
||||
## Cancellation
|
||||
|
||||
### Cooperative Cancellation
|
||||
|
||||
Long-running loops must check for cancellation:
|
||||
|
||||
```kotlin
|
||||
suspend fun processItems(items: List<Item>) {
|
||||
for (item in items) {
|
||||
ensureActive() // throws CancellationException if cancelled
|
||||
process(item)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Cleanup with try/finally
|
||||
|
||||
```kotlin
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
_state.update { it.copy(isLoading = true) }
|
||||
val data = repository.fetch()
|
||||
_state.update { it.copy(data = data) }
|
||||
} finally {
|
||||
_state.update { it.copy(isLoading = false) } // always runs, even on cancellation
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Testing
|
||||
|
||||
### Testing StateFlow with Turbine
|
||||
|
||||
```kotlin
|
||||
@Test
|
||||
fun `search updates item list`() = runTest {
|
||||
val fakeRepository = FakeItemRepository(items = testItems)
|
||||
val viewModel = ItemListViewModel(GetItemsUseCase(fakeRepository))
|
||||
|
||||
viewModel.state.test {
|
||||
assertEquals(ItemListState(), awaitItem()) // initial
|
||||
|
||||
viewModel.onSearch("query")
|
||||
val loading = awaitItem()
|
||||
assertTrue(loading.isLoading)
|
||||
|
||||
val loaded = awaitItem()
|
||||
assertFalse(loaded.isLoading)
|
||||
assertEquals(1, loaded.items.size)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Testing with TestDispatcher
|
||||
|
||||
```kotlin
|
||||
@Test
|
||||
fun `parallel load completes correctly`() = runTest {
|
||||
val viewModel = DashboardViewModel(
|
||||
itemRepo = FakeItemRepo(),
|
||||
statsRepo = FakeStatsRepo()
|
||||
)
|
||||
|
||||
viewModel.load()
|
||||
advanceUntilIdle()
|
||||
|
||||
val state = viewModel.state.value
|
||||
assertNotNull(state.items)
|
||||
assertNotNull(state.stats)
|
||||
}
|
||||
```
|
||||
|
||||
### Faking Flows
|
||||
|
||||
```kotlin
|
||||
class FakeItemRepository : ItemRepository {
|
||||
private val _items = MutableStateFlow<List<Item>>(emptyList())
|
||||
|
||||
override fun observeItems(): Flow<List<Item>> = _items
|
||||
|
||||
fun emit(items: List<Item>) { _items.value = items }
|
||||
|
||||
override suspend fun getItemsByCategory(category: String): Result<List<Item>> {
|
||||
return Result.success(_items.value.filter { it.category == category })
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Anti-Patterns to Avoid
|
||||
|
||||
- Using `GlobalScope` — leaks coroutines, no structured cancellation
|
||||
- Collecting Flows in `init {}` without a scope — use `viewModelScope.launch`
|
||||
- Using `MutableStateFlow` with mutable collections — always use immutable copies: `_state.update { it.copy(list = it.list + newItem) }`
|
||||
- Catching `CancellationException` — let it propagate for proper cancellation
|
||||
- Using `flowOn(Dispatchers.Main)` to collect — collection dispatcher is the caller's dispatcher
|
||||
- Creating `Flow` in `@Composable` without `remember` — recreates the flow every recomposition
|
||||
|
||||
## References
|
||||
|
||||
See skill: `compose-multiplatform-patterns` for UI consumption of Flows.
|
||||
See skill: `android-clean-architecture` for where coroutines fit in layers.
|
||||
Reference in New Issue
Block a user