Files
everything-claude-code/rules/kotlin/patterns.md

3.3 KiB

paths
paths
**/*.kt
**/*.kts

Kotlin Patterns

This file extends common/patterns.md with Kotlin and Android/KMP specific content.

Dependency Injection

Prefer constructor injection. Use Koin (KMP) or Hilt (Android-only):

// Koin — declare modules
val dataModule = module {
    single<ItemRepository> { ItemRepositoryImpl(get(), get()) }
    factory { GetItemsUseCase(get()) }
    viewModelOf(::ItemListViewModel)
}

// Hilt — annotations
@HiltViewModel
class ItemListViewModel @Inject constructor(
    private val getItems: GetItemsUseCase
) : ViewModel()

ViewModel Pattern

Single state object, event sink, one-way data flow:

data class ScreenState(
    val items: List<Item> = emptyList(),
    val isLoading: Boolean = false
)

class ScreenViewModel(private val useCase: GetItemsUseCase) : ViewModel() {
    private val _state = MutableStateFlow(ScreenState())
    val state = _state.asStateFlow()

    fun onEvent(event: ScreenEvent) {
        when (event) {
            is ScreenEvent.Load -> load()
            is ScreenEvent.Delete -> delete(event.id)
        }
    }
}

Repository Pattern

  • suspend functions return Result<T> or custom error type
  • Flow for reactive streams
  • Coordinate local + remote data sources
interface ItemRepository {
    suspend fun getById(id: String): Result<Item>
    fun observeAll(): Flow<List<Item>>
}

UseCase Pattern

Single responsibility, operator fun invoke:

class GetItemUseCase(private val repository: ItemRepository) {
    suspend operator fun invoke(id: String): Result<Item> {
        return repository.getById(id)
    }
}

expect/actual (KMP)

Use for platform-specific implementations:

// commonMain
expect fun platformName(): String
expect class SecureStorage {
    fun save(key: String, value: String)
    fun get(key: String): String?
}

// androidMain
actual fun platformName(): String = "Android"
actual class SecureStorage {
    actual fun save(key: String, value: String) { /* EncryptedSharedPreferences */ }
    actual fun get(key: String): String? { /* ... */ }
}

// iosMain
actual fun platformName(): String = "iOS"
actual class SecureStorage {
    actual fun save(key: String, value: String) { /* Keychain */ }
    actual fun get(key: String): String? { /* ... */ }
}

Coroutine Patterns

  • Use viewModelScope in ViewModels, coroutineScope for structured child work
  • Use stateIn(WhileSubscribed(5_000)) for StateFlow from cold Flows
  • Use supervisorScope when child failures should be independent

Builder Pattern with DSL

class HttpClientConfig {
    var baseUrl: String = ""
    var timeout: Long = 30_000
    private val interceptors = mutableListOf<Interceptor>()

    fun interceptor(block: () -> Interceptor) {
        interceptors.add(block())
    }
}

fun httpClient(block: HttpClientConfig.() -> Unit): HttpClient {
    val config = HttpClientConfig().apply(block)
    return HttpClient(config)
}

// Usage
val client = httpClient {
    baseUrl = "https://api.example.com"
    timeout = 15_000
    interceptor { AuthInterceptor(tokenProvider) }
}

References

See skill: kotlin-coroutines-flows for detailed coroutine patterns. See skill: android-clean-architecture for module and layer patterns.