@h1dr0n/skill-pool 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +296 -0
- package/bin/cli.js +157 -0
- package/package.json +41 -0
- package/skills/api/agents/backend-specialist.md +69 -0
- package/skills/api/agents/database-optimizer.md +176 -0
- package/skills/api/manifest.yaml +20 -0
- package/skills/api/rules/auth-security.md +45 -0
- package/skills/api/skills/api-patterns/SKILL.md +81 -0
- package/skills/api/skills/api-patterns/api-style.md +42 -0
- package/skills/api/skills/api-patterns/auth.md +24 -0
- package/skills/api/skills/api-patterns/documentation.md +26 -0
- package/skills/api/skills/api-patterns/graphql.md +41 -0
- package/skills/api/skills/api-patterns/rate-limiting.md +31 -0
- package/skills/api/skills/api-patterns/response.md +37 -0
- package/skills/api/skills/api-patterns/rest.md +40 -0
- package/skills/api/skills/api-patterns/scripts/api_validator.py +211 -0
- package/skills/api/skills/api-patterns/security-testing.md +122 -0
- package/skills/api/skills/api-patterns/trpc.md +41 -0
- package/skills/api/skills/api-patterns/versioning.md +22 -0
- package/skills/api/skills/database-patterns.md +126 -0
- package/skills/api/skills/deployment-patterns.md +105 -0
- package/skills/api/skills/docker-patterns.md +135 -0
- package/skills/common/agents/code-reviewer.md +78 -0
- package/skills/common/agents/planner.md +80 -0
- package/skills/common/agents/security-reviewer.md +82 -0
- package/skills/common/agents/software-architect.md +81 -0
- package/skills/common/manifest.yaml +25 -0
- package/skills/common/rules/coding-style.md +39 -0
- package/skills/common/rules/git-workflow.md +33 -0
- package/skills/common/rules/security.md +25 -0
- package/skills/common/skills/architecture/SKILL.md +55 -0
- package/skills/common/skills/architecture/context-discovery.md +43 -0
- package/skills/common/skills/architecture/examples.md +94 -0
- package/skills/common/skills/architecture/pattern-selection.md +68 -0
- package/skills/common/skills/architecture/patterns-reference.md +50 -0
- package/skills/common/skills/architecture/trade-off-analysis.md +77 -0
- package/skills/common/skills/brainstorming/SKILL.md +163 -0
- package/skills/common/skills/brainstorming/dynamic-questioning.md +350 -0
- package/skills/common/skills/clean-code.md +99 -0
- package/skills/common/skills/code-review-checklist.md +86 -0
- package/skills/common/skills/plan-writing/SKILL.md +152 -0
- package/skills/common/skills/skill-feedback.md +94 -0
- package/skills/common/skills/tdd-workflow.md +130 -0
- package/skills/common/skills/verification-loop.md +112 -0
- package/skills/cpp/agents/cpp-build-resolver.md +90 -0
- package/skills/cpp/agents/cpp-reviewer.md +72 -0
- package/skills/cpp/manifest.yaml +15 -0
- package/skills/cpp/skills/cpp-coding-standards.md +722 -0
- package/skills/cpp/skills/cpp-testing.md +323 -0
- package/skills/devops/agents/devops-automator.md +376 -0
- package/skills/devops/agents/sre.md +90 -0
- package/skills/devops/manifest.yaml +20 -0
- package/skills/devops/skills/deployment-patterns.md +427 -0
- package/skills/devops/skills/deployment-procedures/SKILL.md +241 -0
- package/skills/devops/skills/docker-patterns.md +364 -0
- package/skills/devops/skills/e2e-testing.md +326 -0
- package/skills/devops/skills/github-ops.md +144 -0
- package/skills/django/manifest.yaml +16 -0
- package/skills/django/skills/django-patterns.md +734 -0
- package/skills/django/skills/django-security.md +593 -0
- package/skills/django/skills/django-tdd.md +729 -0
- package/skills/django/skills/django-verification.md +469 -0
- package/skills/dotnet/agents/csharp-reviewer.md +101 -0
- package/skills/dotnet/manifest.yaml +14 -0
- package/skills/dotnet/skills/csharp-testing.md +321 -0
- package/skills/dotnet/skills/dotnet-patterns.md +321 -0
- package/skills/go/agents/code-reviewer.md +76 -0
- package/skills/go/agents/go-build-resolver.md +94 -0
- package/skills/go/agents/go-reviewer.md +76 -0
- package/skills/go/manifest.yaml +17 -0
- package/skills/go/rules/go-style.md +55 -0
- package/skills/go/skills/golang-patterns.md +674 -0
- package/skills/go/skills/golang-testing.md +720 -0
- package/skills/java/agents/java-build-resolver.md +153 -0
- package/skills/java/agents/java-reviewer.md +92 -0
- package/skills/java/manifest.yaml +18 -0
- package/skills/java/skills/java-coding-standards.md +147 -0
- package/skills/java/skills/jpa-patterns.md +151 -0
- package/skills/java/skills/springboot-patterns.md +314 -0
- package/skills/java/skills/springboot-security.md +272 -0
- package/skills/kotlin/agents/kotlin-build-resolver.md +118 -0
- package/skills/kotlin/agents/kotlin-reviewer.md +159 -0
- package/skills/kotlin/manifest.yaml +17 -0
- package/skills/kotlin/skills/kotlin-coroutines-flows.md +284 -0
- package/skills/kotlin/skills/kotlin-patterns.md +711 -0
- package/skills/kotlin/skills/kotlin-testing.md +824 -0
- package/skills/laravel/manifest.yaml +15 -0
- package/skills/laravel/skills/laravel-patterns.md +409 -0
- package/skills/laravel/skills/laravel-security.md +279 -0
- package/skills/laravel/skills/laravel-tdd.md +277 -0
- package/skills/laravel/skills/laravel-verification.md +173 -0
- package/skills/mobile/agents/dart-build-resolver.md +201 -0
- package/skills/mobile/agents/flutter-reviewer.md +243 -0
- package/skills/mobile/manifest.yaml +19 -0
- package/skills/mobile/skills/android-clean-architecture.md +339 -0
- package/skills/mobile/skills/dart-flutter-patterns.md +563 -0
- package/skills/mobile/skills/swiftui-patterns.md +259 -0
- package/skills/nestjs/manifest.yaml +13 -0
- package/skills/nestjs/skills/nestjs-patterns.md +230 -0
- package/skills/perl/manifest.yaml +13 -0
- package/skills/perl/skills/perl-patterns.md +504 -0
- package/skills/perl/skills/perl-security.md +503 -0
- package/skills/perl/skills/perl-testing.md +475 -0
- package/skills/python/agents/python-reviewer.md +98 -0
- package/skills/python/manifest.yaml +18 -0
- package/skills/python/rules/python-style.md +69 -0
- package/skills/python/skills/python-patterns/SKILL.md +441 -0
- package/skills/python/skills/python-patterns.md +90 -0
- package/skills/python/skills/python-testing.md +81 -0
- package/skills/rust/agents/rust-build-resolver.md +148 -0
- package/skills/rust/agents/rust-reviewer.md +94 -0
- package/skills/rust/manifest.yaml +16 -0
- package/skills/rust/rules/rust-style.md +107 -0
- package/skills/rust/skills/rust-patterns.md +499 -0
- package/skills/rust/skills/rust-testing.md +500 -0
- package/skills/security/agents/accessibility-auditor.md +316 -0
- package/skills/security/agents/security-reviewer.md +108 -0
- package/skills/security/manifest.yaml +19 -0
- package/skills/security/skills/red-team-tactics/SKILL.md +199 -0
- package/skills/security/skills/security-bounty-hunter.md +99 -0
- package/skills/security/skills/security-review.md +495 -0
- package/skills/security/skills/security-scan.md +165 -0
- package/skills/security/skills/vulnerability-scanner/SKILL.md +276 -0
- package/skills/security/skills/vulnerability-scanner/checklists.md +121 -0
- package/skills/security/skills/vulnerability-scanner/scripts/security_scan.py +458 -0
- package/skills/swift/manifest.yaml +16 -0
- package/skills/swift/skills/swift-actor-persistence.md +142 -0
- package/skills/swift/skills/swift-concurrency.md +216 -0
- package/skills/swift/skills/swift-protocol-di-testing.md +190 -0
- package/skills/swift/skills/swiftui-patterns.md +259 -0
- package/skills/unity/agents/game-designer.md +167 -0
- package/skills/unity/agents/unity-architect.md +52 -0
- package/skills/unity/agents/unity-editor-tool-developer.md +310 -0
- package/skills/unity/agents/unity-multiplayer-engineer.md +321 -0
- package/skills/unity/agents/unity-shader-graph-artist.md +269 -0
- package/skills/unity/manifest.yaml +21 -0
- package/skills/unity/rules/csharp-patterns.md +48 -0
- package/skills/unity/rules/unity-specific.md +53 -0
- package/skills/unity/skills/systematic-debugging.md +92 -0
- package/skills/unity/skills/unity-architecture.md +173 -0
- package/skills/unreal/agents/level-designer.md +208 -0
- package/skills/unreal/agents/technical-artist.md +229 -0
- package/skills/unreal/agents/unreal-multiplayer-architect.md +313 -0
- package/skills/unreal/agents/unreal-systems-engineer.md +310 -0
- package/skills/unreal/agents/unreal-technical-artist.md +256 -0
- package/skills/unreal/agents/unreal-world-builder.md +273 -0
- package/skills/unreal/manifest.yaml +21 -0
- package/skills/unreal/skills/unreal-patterns.md +183 -0
- package/skills/web/agents/frontend-specialist.md +71 -0
- package/skills/web/agents/ui-designer.md +383 -0
- package/skills/web/agents/ux-architect.md +469 -0
- package/skills/web/manifest.yaml +22 -0
- package/skills/web/rules/accessibility.md +54 -0
- package/skills/web/rules/css-performance.md +52 -0
- package/skills/web/skills/e2e-testing.md +132 -0
- package/skills/web/skills/frontend-design/SKILL.md +452 -0
- package/skills/web/skills/frontend-design/animation-guide.md +331 -0
- package/skills/web/skills/frontend-design/color-system.md +311 -0
- package/skills/web/skills/frontend-design/decision-trees.md +418 -0
- package/skills/web/skills/frontend-design/motion-graphics.md +306 -0
- package/skills/web/skills/frontend-design/scripts/accessibility_checker.py +183 -0
- package/skills/web/skills/frontend-design/scripts/ux_audit.py +722 -0
- package/skills/web/skills/frontend-design/typography-system.md +345 -0
- package/skills/web/skills/frontend-design/ux-psychology.md +1116 -0
- package/skills/web/skills/frontend-design/visual-effects.md +383 -0
- package/skills/web/skills/react-nextjs.md +135 -0
- package/skills/web/skills/tailwind-patterns/SKILL.md +269 -0
- package/src/adapters/antigravity.js +164 -0
- package/src/adapters/claude.js +188 -0
- package/src/adapters/cursor.js +161 -0
- package/src/adapters/index.js +67 -0
- package/src/adapters/windsurf.js +158 -0
- package/src/commands/add.js +266 -0
- package/src/commands/create.js +127 -0
- package/src/commands/diff.js +78 -0
- package/src/commands/info.js +88 -0
- package/src/commands/init.js +224 -0
- package/src/commands/install.js +90 -0
- package/src/commands/list.js +54 -0
- package/src/commands/remove.js +101 -0
- package/src/commands/targets.js +32 -0
- package/src/commands/update.js +57 -0
- package/src/core/manifest.js +57 -0
- package/src/core/plugins.js +86 -0
- package/src/core/resolver.js +84 -0
- package/src/core/tracker.js +49 -0
- package/src/utils/fs.js +80 -0
- package/src/utils/git.js +52 -0
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: kotlin-reviewer
|
|
3
|
+
description: Kotlin and Android/KMP code reviewer. Reviews Kotlin code for idiomatic patterns, coroutine safety, Compose best practices, clean architecture violations, and common Android pitfalls.
|
|
4
|
+
tools: ["Read", "Grep", "Glob", "Bash"]
|
|
5
|
+
model: sonnet
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
You are a senior Kotlin and Android/KMP code reviewer ensuring idiomatic, safe, and maintainable code.
|
|
9
|
+
|
|
10
|
+
## Your Role
|
|
11
|
+
|
|
12
|
+
- Review Kotlin code for idiomatic patterns and Android/KMP best practices
|
|
13
|
+
- Detect coroutine misuse, Flow anti-patterns, and lifecycle bugs
|
|
14
|
+
- Enforce clean architecture module boundaries
|
|
15
|
+
- Identify Compose performance issues and recomposition traps
|
|
16
|
+
- You DO NOT refactor or rewrite code — you report findings only
|
|
17
|
+
|
|
18
|
+
## Workflow
|
|
19
|
+
|
|
20
|
+
### Step 1: Gather Context
|
|
21
|
+
|
|
22
|
+
Run `git diff --staged` and `git diff` to see changes. If no diff, check `git log --oneline -5`. Identify Kotlin/KTS files that changed.
|
|
23
|
+
|
|
24
|
+
### Step 2: Understand Project Structure
|
|
25
|
+
|
|
26
|
+
Check for:
|
|
27
|
+
- `build.gradle.kts` or `settings.gradle.kts` to understand module layout
|
|
28
|
+
- `CLAUDE.md` for project-specific conventions
|
|
29
|
+
- Whether this is Android-only, KMP, or Compose Multiplatform
|
|
30
|
+
|
|
31
|
+
### Step 2b: Security Review
|
|
32
|
+
|
|
33
|
+
Apply the Kotlin/Android security guidance before continuing:
|
|
34
|
+
- exported Android components, deep links, and intent filters
|
|
35
|
+
- insecure crypto, WebView, and network configuration usage
|
|
36
|
+
- keystore, token, and credential handling
|
|
37
|
+
- platform-specific storage and permission risks
|
|
38
|
+
|
|
39
|
+
If you find a CRITICAL security issue, stop the review and hand off to `security-reviewer` before doing any further analysis.
|
|
40
|
+
|
|
41
|
+
### Step 3: Read and Review
|
|
42
|
+
|
|
43
|
+
Read changed files fully. Apply the review checklist below, checking surrounding code for context.
|
|
44
|
+
|
|
45
|
+
### Step 4: Report Findings
|
|
46
|
+
|
|
47
|
+
Use the output format below. Only report issues with >80% confidence.
|
|
48
|
+
|
|
49
|
+
## Review Checklist
|
|
50
|
+
|
|
51
|
+
### Architecture (CRITICAL)
|
|
52
|
+
|
|
53
|
+
- **Domain importing framework** — `domain` module must not import Android, Ktor, Room, or any framework
|
|
54
|
+
- **Data layer leaking to UI** — Entities or DTOs exposed to presentation layer (must map to domain models)
|
|
55
|
+
- **ViewModel business logic** — Complex logic belongs in UseCases, not ViewModels
|
|
56
|
+
- **Circular dependencies** — Module A depends on B and B depends on A
|
|
57
|
+
|
|
58
|
+
### Coroutines & Flows (HIGH)
|
|
59
|
+
|
|
60
|
+
- **GlobalScope usage** — Must use structured scopes (`viewModelScope`, `coroutineScope`)
|
|
61
|
+
- **Catching CancellationException** — Must rethrow or not catch; swallowing breaks cancellation
|
|
62
|
+
- **Missing `withContext` for IO** — Database/network calls on `Dispatchers.Main`
|
|
63
|
+
- **StateFlow with mutable state** — Using mutable collections inside StateFlow (must copy)
|
|
64
|
+
- **Flow collection in `init {}`** — Should use `stateIn()` or launch in scope
|
|
65
|
+
- **Missing `WhileSubscribed`** — `stateIn(scope, SharingStarted.Eagerly)` when `WhileSubscribed` is appropriate
|
|
66
|
+
|
|
67
|
+
```kotlin
|
|
68
|
+
// BAD — swallows cancellation
|
|
69
|
+
try { fetchData() } catch (e: Exception) { log(e) }
|
|
70
|
+
|
|
71
|
+
// GOOD — preserves cancellation
|
|
72
|
+
try { fetchData() } catch (e: CancellationException) { throw e } catch (e: Exception) { log(e) }
|
|
73
|
+
// or use runCatching and check
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### Compose (HIGH)
|
|
77
|
+
|
|
78
|
+
- **Unstable parameters** — Composables receiving mutable types cause unnecessary recomposition
|
|
79
|
+
- **Side effects outside LaunchedEffect** — Network/DB calls must be in `LaunchedEffect` or ViewModel
|
|
80
|
+
- **NavController passed deep** — Pass lambdas instead of `NavController` references
|
|
81
|
+
- **Missing `key()` in LazyColumn** — Items without stable keys cause poor performance
|
|
82
|
+
- **`remember` with missing keys** — Computation not recalculated when dependencies change
|
|
83
|
+
- **Object allocation in parameters** — Creating objects inline causes recomposition
|
|
84
|
+
|
|
85
|
+
```kotlin
|
|
86
|
+
// BAD — new lambda every recomposition
|
|
87
|
+
Button(onClick = { viewModel.doThing(item.id) })
|
|
88
|
+
|
|
89
|
+
// GOOD — stable reference
|
|
90
|
+
val onClick = remember(item.id) { { viewModel.doThing(item.id) } }
|
|
91
|
+
Button(onClick = onClick)
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Kotlin Idioms (MEDIUM)
|
|
95
|
+
|
|
96
|
+
- **`!!` usage** — Non-null assertion; prefer `?.`, `?:`, `requireNotNull`, or `checkNotNull`
|
|
97
|
+
- **`var` where `val` works** — Prefer immutability
|
|
98
|
+
- **Java-style patterns** — Static utility classes (use top-level functions), getters/setters (use properties)
|
|
99
|
+
- **String concatenation** — Use string templates `"Hello $name"` instead of `"Hello " + name`
|
|
100
|
+
- **`when` without exhaustive branches** — Sealed classes/interfaces should use exhaustive `when`
|
|
101
|
+
- **Mutable collections exposed** — Return `List` not `MutableList` from public APIs
|
|
102
|
+
|
|
103
|
+
### Android Specific (MEDIUM)
|
|
104
|
+
|
|
105
|
+
- **Context leaks** — Storing `Activity` or `Fragment` references in singletons/ViewModels
|
|
106
|
+
- **Missing ProGuard rules** — Serialized classes without `@Keep` or ProGuard rules
|
|
107
|
+
- **Hardcoded strings** — User-facing strings not in `strings.xml` or Compose resources
|
|
108
|
+
- **Missing lifecycle handling** — Collecting Flows in Activities without `repeatOnLifecycle`
|
|
109
|
+
|
|
110
|
+
### Security (CRITICAL)
|
|
111
|
+
|
|
112
|
+
- **Exported component exposure** — Activities, services, or receivers exported without proper guards
|
|
113
|
+
- **Insecure crypto/storage** — Homegrown crypto, plaintext secrets, or weak keystore usage
|
|
114
|
+
- **Unsafe WebView/network config** — JavaScript bridges, cleartext traffic, permissive trust settings
|
|
115
|
+
- **Sensitive logging** — Tokens, credentials, PII, or secrets emitted to logs
|
|
116
|
+
|
|
117
|
+
If any CRITICAL security issue is present, stop and escalate to `security-reviewer`.
|
|
118
|
+
|
|
119
|
+
### Gradle & Build (LOW)
|
|
120
|
+
|
|
121
|
+
- **Version catalog not used** — Hardcoded versions instead of `libs.versions.toml`
|
|
122
|
+
- **Unnecessary dependencies** — Dependencies added but not used
|
|
123
|
+
- **Missing KMP source sets** — Declaring `androidMain` code that could be `commonMain`
|
|
124
|
+
|
|
125
|
+
## Output Format
|
|
126
|
+
|
|
127
|
+
```
|
|
128
|
+
[CRITICAL] Domain module imports Android framework
|
|
129
|
+
File: domain/src/main/kotlin/com/app/domain/UserUseCase.kt:3
|
|
130
|
+
Issue: `import android.content.Context` — domain must be pure Kotlin with no framework dependencies.
|
|
131
|
+
Fix: Move Context-dependent logic to data or platforms layer. Pass data via repository interface.
|
|
132
|
+
|
|
133
|
+
[HIGH] StateFlow holding mutable list
|
|
134
|
+
File: presentation/src/main/kotlin/com/app/ui/ListViewModel.kt:25
|
|
135
|
+
Issue: `_state.value.items.add(newItem)` mutates the list inside StateFlow — Compose won't detect the change.
|
|
136
|
+
Fix: Use `_state.update { it.copy(items = it.items + newItem) }`
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## Summary Format
|
|
140
|
+
|
|
141
|
+
End every review with:
|
|
142
|
+
|
|
143
|
+
```
|
|
144
|
+
## Review Summary
|
|
145
|
+
|
|
146
|
+
| Severity | Count | Status |
|
|
147
|
+
|----------|-------|--------|
|
|
148
|
+
| CRITICAL | 0 | pass |
|
|
149
|
+
| HIGH | 1 | block |
|
|
150
|
+
| MEDIUM | 2 | info |
|
|
151
|
+
| LOW | 0 | note |
|
|
152
|
+
|
|
153
|
+
Verdict: BLOCK — HIGH issues must be fixed before merge.
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
## Approval Criteria
|
|
157
|
+
|
|
158
|
+
- **Approve**: No CRITICAL or HIGH issues
|
|
159
|
+
- **Block**: Any CRITICAL or HIGH issues — must fix before merge
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
name: kotlin
|
|
2
|
+
version: 0.1.0
|
|
3
|
+
description: Idiomatic Kotlin patterns, coroutines, testing with Kotest/MockK, and build tooling for Android/KMP projects
|
|
4
|
+
depends:
|
|
5
|
+
- common
|
|
6
|
+
tags:
|
|
7
|
+
- kotlin
|
|
8
|
+
- android
|
|
9
|
+
- kmp
|
|
10
|
+
rules: []
|
|
11
|
+
skills:
|
|
12
|
+
- skills/kotlin-patterns.md
|
|
13
|
+
- skills/kotlin-testing.md
|
|
14
|
+
- skills/kotlin-coroutines-flows.md
|
|
15
|
+
agents:
|
|
16
|
+
- agents/kotlin-reviewer.md
|
|
17
|
+
- agents/kotlin-build-resolver.md
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: kotlin-coroutines-flows
|
|
3
|
+
description: Kotlin Coroutines and Flow patterns for Android and KMP — structured concurrency, Flow operators, StateFlow, error handling, and testing.
|
|
4
|
+
origin: ECC
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
# Kotlin Coroutines & Flows
|
|
8
|
+
|
|
9
|
+
Patterns for structured concurrency, Flow-based reactive streams, and coroutine testing in Android and Kotlin Multiplatform projects.
|
|
10
|
+
|
|
11
|
+
## When to Activate
|
|
12
|
+
|
|
13
|
+
- Writing async code with Kotlin coroutines
|
|
14
|
+
- Using Flow, StateFlow, or SharedFlow for reactive data
|
|
15
|
+
- Handling concurrent operations (parallel loading, debounce, retry)
|
|
16
|
+
- Testing coroutines and Flows
|
|
17
|
+
- Managing coroutine scopes and cancellation
|
|
18
|
+
|
|
19
|
+
## Structured Concurrency
|
|
20
|
+
|
|
21
|
+
### Scope Hierarchy
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
Application
|
|
25
|
+
└── viewModelScope (ViewModel)
|
|
26
|
+
└── coroutineScope { } (structured child)
|
|
27
|
+
├── async { } (concurrent task)
|
|
28
|
+
└── async { } (concurrent task)
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Always use structured concurrency — never `GlobalScope`:
|
|
32
|
+
|
|
33
|
+
```kotlin
|
|
34
|
+
// BAD
|
|
35
|
+
GlobalScope.launch { fetchData() }
|
|
36
|
+
|
|
37
|
+
// GOOD — scoped to ViewModel lifecycle
|
|
38
|
+
viewModelScope.launch { fetchData() }
|
|
39
|
+
|
|
40
|
+
// GOOD — scoped to composable lifecycle
|
|
41
|
+
LaunchedEffect(key) { fetchData() }
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Parallel Decomposition
|
|
45
|
+
|
|
46
|
+
Use `coroutineScope` + `async` for parallel work:
|
|
47
|
+
|
|
48
|
+
```kotlin
|
|
49
|
+
suspend fun loadDashboard(): Dashboard = coroutineScope {
|
|
50
|
+
val items = async { itemRepository.getRecent() }
|
|
51
|
+
val stats = async { statsRepository.getToday() }
|
|
52
|
+
val profile = async { userRepository.getCurrent() }
|
|
53
|
+
Dashboard(
|
|
54
|
+
items = items.await(),
|
|
55
|
+
stats = stats.await(),
|
|
56
|
+
profile = profile.await()
|
|
57
|
+
)
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### SupervisorScope
|
|
62
|
+
|
|
63
|
+
Use `supervisorScope` when child failures should not cancel siblings:
|
|
64
|
+
|
|
65
|
+
```kotlin
|
|
66
|
+
suspend fun syncAll() = supervisorScope {
|
|
67
|
+
launch { syncItems() } // failure here won't cancel syncStats
|
|
68
|
+
launch { syncStats() }
|
|
69
|
+
launch { syncSettings() }
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Flow Patterns
|
|
74
|
+
|
|
75
|
+
### Cold Flow — One-Shot to Stream Conversion
|
|
76
|
+
|
|
77
|
+
```kotlin
|
|
78
|
+
fun observeItems(): Flow<List<Item>> = flow {
|
|
79
|
+
// Re-emits whenever the database changes
|
|
80
|
+
itemDao.observeAll()
|
|
81
|
+
.map { entities -> entities.map { it.toDomain() } }
|
|
82
|
+
.collect { emit(it) }
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### StateFlow for UI State
|
|
87
|
+
|
|
88
|
+
```kotlin
|
|
89
|
+
class DashboardViewModel(
|
|
90
|
+
observeProgress: ObserveUserProgressUseCase
|
|
91
|
+
) : ViewModel() {
|
|
92
|
+
val progress: StateFlow<UserProgress> = observeProgress()
|
|
93
|
+
.stateIn(
|
|
94
|
+
scope = viewModelScope,
|
|
95
|
+
started = SharingStarted.WhileSubscribed(5_000),
|
|
96
|
+
initialValue = UserProgress.EMPTY
|
|
97
|
+
)
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
`WhileSubscribed(5_000)` keeps the upstream active for 5 seconds after the last subscriber leaves — survives configuration changes without restarting.
|
|
102
|
+
|
|
103
|
+
### Combining Multiple Flows
|
|
104
|
+
|
|
105
|
+
```kotlin
|
|
106
|
+
val uiState: StateFlow<HomeState> = combine(
|
|
107
|
+
itemRepository.observeItems(),
|
|
108
|
+
settingsRepository.observeTheme(),
|
|
109
|
+
userRepository.observeProfile()
|
|
110
|
+
) { items, theme, profile ->
|
|
111
|
+
HomeState(items = items, theme = theme, profile = profile)
|
|
112
|
+
}.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), HomeState())
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Flow Operators
|
|
116
|
+
|
|
117
|
+
```kotlin
|
|
118
|
+
// Debounce search input
|
|
119
|
+
searchQuery
|
|
120
|
+
.debounce(300)
|
|
121
|
+
.distinctUntilChanged()
|
|
122
|
+
.flatMapLatest { query -> repository.search(query) }
|
|
123
|
+
.catch { emit(emptyList()) }
|
|
124
|
+
.collect { results -> _state.update { it.copy(results = results) } }
|
|
125
|
+
|
|
126
|
+
// Retry with exponential backoff
|
|
127
|
+
fun fetchWithRetry(): Flow<Data> = flow { emit(api.fetch()) }
|
|
128
|
+
.retryWhen { cause, attempt ->
|
|
129
|
+
if (cause is IOException && attempt < 3) {
|
|
130
|
+
delay(1000L * (1 shl attempt.toInt()))
|
|
131
|
+
true
|
|
132
|
+
} else {
|
|
133
|
+
false
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### SharedFlow for One-Time Events
|
|
139
|
+
|
|
140
|
+
```kotlin
|
|
141
|
+
class ItemListViewModel : ViewModel() {
|
|
142
|
+
private val _effects = MutableSharedFlow<Effect>()
|
|
143
|
+
val effects: SharedFlow<Effect> = _effects.asSharedFlow()
|
|
144
|
+
|
|
145
|
+
sealed interface Effect {
|
|
146
|
+
data class ShowSnackbar(val message: String) : Effect
|
|
147
|
+
data class NavigateTo(val route: String) : Effect
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
private fun deleteItem(id: String) {
|
|
151
|
+
viewModelScope.launch {
|
|
152
|
+
repository.delete(id)
|
|
153
|
+
_effects.emit(Effect.ShowSnackbar("Item deleted"))
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Collect in Composable
|
|
159
|
+
LaunchedEffect(Unit) {
|
|
160
|
+
viewModel.effects.collect { effect ->
|
|
161
|
+
when (effect) {
|
|
162
|
+
is Effect.ShowSnackbar -> snackbarHostState.showSnackbar(effect.message)
|
|
163
|
+
is Effect.NavigateTo -> navController.navigate(effect.route)
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
## Dispatchers
|
|
170
|
+
|
|
171
|
+
```kotlin
|
|
172
|
+
// CPU-intensive work
|
|
173
|
+
withContext(Dispatchers.Default) { parseJson(largePayload) }
|
|
174
|
+
|
|
175
|
+
// IO-bound work
|
|
176
|
+
withContext(Dispatchers.IO) { database.query() }
|
|
177
|
+
|
|
178
|
+
// Main thread (UI) — default in viewModelScope
|
|
179
|
+
withContext(Dispatchers.Main) { updateUi() }
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
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.
|
|
183
|
+
|
|
184
|
+
## Cancellation
|
|
185
|
+
|
|
186
|
+
### Cooperative Cancellation
|
|
187
|
+
|
|
188
|
+
Long-running loops must check for cancellation:
|
|
189
|
+
|
|
190
|
+
```kotlin
|
|
191
|
+
suspend fun processItems(items: List<Item>) = coroutineScope {
|
|
192
|
+
for (item in items) {
|
|
193
|
+
ensureActive() // throws CancellationException if cancelled
|
|
194
|
+
process(item)
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
### Cleanup with try/finally
|
|
200
|
+
|
|
201
|
+
```kotlin
|
|
202
|
+
viewModelScope.launch {
|
|
203
|
+
try {
|
|
204
|
+
_state.update { it.copy(isLoading = true) }
|
|
205
|
+
val data = repository.fetch()
|
|
206
|
+
_state.update { it.copy(data = data) }
|
|
207
|
+
} finally {
|
|
208
|
+
_state.update { it.copy(isLoading = false) } // always runs, even on cancellation
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
## Testing
|
|
214
|
+
|
|
215
|
+
### Testing StateFlow with Turbine
|
|
216
|
+
|
|
217
|
+
```kotlin
|
|
218
|
+
@Test
|
|
219
|
+
fun `search updates item list`() = runTest {
|
|
220
|
+
val fakeRepository = FakeItemRepository().apply { emit(testItems) }
|
|
221
|
+
val viewModel = ItemListViewModel(GetItemsUseCase(fakeRepository))
|
|
222
|
+
|
|
223
|
+
viewModel.state.test {
|
|
224
|
+
assertEquals(ItemListState(), awaitItem()) // initial
|
|
225
|
+
|
|
226
|
+
viewModel.onSearch("query")
|
|
227
|
+
val loading = awaitItem()
|
|
228
|
+
assertTrue(loading.isLoading)
|
|
229
|
+
|
|
230
|
+
val loaded = awaitItem()
|
|
231
|
+
assertFalse(loaded.isLoading)
|
|
232
|
+
assertEquals(1, loaded.items.size)
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
### Testing with TestDispatcher
|
|
238
|
+
|
|
239
|
+
```kotlin
|
|
240
|
+
@Test
|
|
241
|
+
fun `parallel load completes correctly`() = runTest {
|
|
242
|
+
val viewModel = DashboardViewModel(
|
|
243
|
+
itemRepo = FakeItemRepo(),
|
|
244
|
+
statsRepo = FakeStatsRepo()
|
|
245
|
+
)
|
|
246
|
+
|
|
247
|
+
viewModel.load()
|
|
248
|
+
advanceUntilIdle()
|
|
249
|
+
|
|
250
|
+
val state = viewModel.state.value
|
|
251
|
+
assertNotNull(state.items)
|
|
252
|
+
assertNotNull(state.stats)
|
|
253
|
+
}
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
### Faking Flows
|
|
257
|
+
|
|
258
|
+
```kotlin
|
|
259
|
+
class FakeItemRepository : ItemRepository {
|
|
260
|
+
private val _items = MutableStateFlow<List<Item>>(emptyList())
|
|
261
|
+
|
|
262
|
+
override fun observeItems(): Flow<List<Item>> = _items
|
|
263
|
+
|
|
264
|
+
fun emit(items: List<Item>) { _items.value = items }
|
|
265
|
+
|
|
266
|
+
override suspend fun getItemsByCategory(category: String): Result<List<Item>> {
|
|
267
|
+
return Result.success(_items.value.filter { it.category == category })
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
## Anti-Patterns to Avoid
|
|
273
|
+
|
|
274
|
+
- Using `GlobalScope` — leaks coroutines, no structured cancellation
|
|
275
|
+
- Collecting Flows in `init {}` without a scope — use `viewModelScope.launch`
|
|
276
|
+
- Using `MutableStateFlow` with mutable collections — always use immutable copies: `_state.update { it.copy(list = it.list + newItem) }`
|
|
277
|
+
- Catching `CancellationException` — let it propagate for proper cancellation
|
|
278
|
+
- Using `flowOn(Dispatchers.Main)` to collect — collection dispatcher is the caller's dispatcher
|
|
279
|
+
- Creating `Flow` in `@Composable` without `remember` — recreates the flow every recomposition
|
|
280
|
+
|
|
281
|
+
## References
|
|
282
|
+
|
|
283
|
+
See skill: `compose-multiplatform-patterns` for UI consumption of Flows.
|
|
284
|
+
See skill: `android-clean-architecture` for where coroutines fit in layers.
|