Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 0 additions & 19 deletions .fleet/receipt.json

This file was deleted.

25 changes: 14 additions & 11 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
# This workflow will build a Java project with Gradle
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-gradle

name: Build and test with Gradle

on:
push:
branches: [ master ]
branches: [ main ]
pull_request:
branches: [ master ]
branches: [ main ]
workflow_dispatch:

jobs:
Expand All @@ -24,10 +21,16 @@ jobs:
distribution: 'temurin'
java-version: '21'

- name: Build with Gradle
uses: gradle/gradle-build-action@v3
with:
arguments: build
- name: Set up Gradle
uses: gradle/actions/setup-gradle@v6

- name: Codecov
uses: codecov/codecov-action@v7
# Release iOS framework linking needs more memory than the macOS runner has
# (~6 GB Gradle daemon peak) and swaps for 15+ min per framework, so link
# debug frameworks only. They verify the same code, minus LLVM optimization.
- name: Build with Gradle
run: >
./gradlew
:androidApp:assembleDebug
:androidApp:lintDebug
:composeApp:linkDebugFrameworkIosSimulatorArm64
:composeApp:linkDebugFrameworkIosArm64
29 changes: 13 additions & 16 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,25 @@ A Kotlin Multiplatform (Android + iOS) sample app demonstrating [KStateMachine](
./gradlew build

# Android debug APK
./gradlew :composeApp:assembleDebug

# Run Android unit tests
./gradlew :composeApp:testDebugUnitTest

# Run a single test class
./gradlew :composeApp:testDebugUnitTest --tests "org.example.project.MyTest"
./gradlew :androidApp:assembleDebug
```

iOS builds require Xcode — open `iosApp/iosApp.xcodeproj` or use `./gradlew :composeApp:linkDebugFrameworkIosSimulatorArm64`.

## Architecture

The app uses **MVI** with **KStateMachine** for state logic, **Voyager** for navigation/screen models, and **Koin** for DI.
The app uses **MVI** with **KStateMachine** for state logic and the multiplatform **Lifecycle ViewModel** (`org.jetbrains.androidx.lifecycle`). There is no DI framework and no navigation library — a single screen creates its ViewModel via `viewModel { }`.

### Modules

- **`:composeApp`** — KMP shared module (Android + iOS) with all app logic and UI. Uses AGP 9's `com.android.kotlin.multiplatform.library` plugin (`kotlin { android { } }` block, no legacy `android { }`). `androidResources { enable = true }` is required, otherwise Compose resources (`Res.drawable.*`) are not packaged into the APK. Produces the `ComposeApp` iOS framework.
- **`:androidApp`** — thin `com.android.application` module (`MainActivity`, manifest, launcher icons). Uses AGP 9 built-in Kotlin, so the `kotlin-android` plugin is not applied. AGP 9 forbids applying KMP and `com.android.application` in one module, which is why this split exists.

### Data flow

```
UI (StickManGameScreen)
→ sendEvent(ControlEvent) → StickManGameScreenModel
→ sendEvent(ControlEvent) → StickManGameViewModel
→ machine.processEvent(event) [KStateMachine]
→ onTransitionComplete / onStateEntry callbacks
→ intent { state { ... } / sendEffect(...) }
Expand All @@ -44,23 +43,23 @@ UI (StickManGameScreen)

- **`StateControl.kt`** — defines all domain types: `ControlEvent` (sealed interface of input events) and `HeroState` (sealed class extending KStateMachine's `DefaultState`). `HeroState` instances are used directly as KStateMachine state nodes.

- **`StickmanGameScreenModel.kt`** — Voyager `ScreenModel` that constructs the KStateMachine with `createStateMachineBlocking`. The machine has `ChildMode.PARALLEL` at the root with two parallel regions: `"Movement"` (Standing/Jumping/Ducking/AirAttacking) and `"Fire"` (NotShooting/Shooting). Transition callbacks call `intent { }` to update `MviModel` state and emit effects.
- **`StickManGameViewModel.kt`** — `ViewModel` that constructs the KStateMachine with `createStateMachineBlocking`. The machine has `ChildMode.PARALLEL` at the root with two parallel regions: `"Movement"` (Standing/Jumping/Ducking/AirAttacking) and `"Fire"` (NotShooting/Shooting). Transition callbacks call `intent { }` to update `MviModel` state and emit effects.

- **`Mvi.kt`** — Generic MVI infrastructure. `MviModel<State, Effect>` holds a `StateFlow` for state and a `Channel`-backed flow for one-shot effects. `MviModelHost` provides `intent { }` (launches coroutine on model scope) and `state` shorthand.

- **`ModelConst.kt`** — Game constants (`JUMP_DURATION_MS`, `SHOOTING_INTERVAL_MS`, `INITIAL_AMMO`) and data types: `ModelData` (state snapshot) and `ModelEffect` (sealed interface of effects).

- **`Timers.kt`** — `singleShotTimer` and `tickerFlow` coroutine utilities used inside state machine `onEntry`/`onExit` blocks.

- **`StickManGameScreen.kt`** — Compose `Screen` (Voyager). Uses `MutableInteractionSource` to detect press/release for Duck and Fire buttons, which map to paired `ControlEvent`s. Hero sprite is selected by checking the active state list for combinations of movement + fire states via the `List<HeroState>.hasState<T>()` extension.
- **`StickManGameScreen.kt`** — `@Composable` screen (Material 3) that obtains its ViewModel with `viewModel { StickManGameViewModel() }`. Uses `MutableInteractionSource` to detect press/release for Duck and Fire buttons, which map to paired `ControlEvent`s. Hero sprite is selected by checking the active state list for combinations of movement + fire states via the `List<HeroState>.hasState<T>()` extension.

- **`App.kt`** — Root `@Composable` that wraps `StickManGameScreen` in a Voyager `Navigator`.
- **`App.kt`** — Root `@Composable`: Material 3 theme around `StickManGameScreen`.

- **`KoinModule.kt`** (androidMain) — Koin module registering `StickManGameScreenModel` as a singleton. Note: this file has **no package declaration** and lives at the root of `androidMain/kotlin/`.
- **`MainActivity.kt`** (`androidApp`) — enables edge-to-edge and sets `App()` as content.

### KStateMachine usage pattern

States are declared as `HeroState` subclass objects/classes and added with `addState`/`addInitialState`. Transitions are configured with `transition<EventType>` and `transitionOn<EventType>` (for conditional targets). The machine is polled via `machine.processEvent(event)` called from the screen model.
States are declared as `HeroState` subclass objects/classes and added with `addState`/`addInitialState`. Transitions are configured with `transition<EventType>` and `transitionOn<EventType>` (for conditional targets). The machine is polled via `machine.processEvent(event)` called from the ViewModel.

`buildCreationArguments { doNotThrowOnMultipleTransitionsMatch = true }` is set because both parallel regions can match the same event simultaneously.

Expand All @@ -69,5 +68,3 @@ States are declared as `HeroState` subclass objects/classes and added with `addS
**`class` vs `object` for states:** `AirAttacking` and `Shooting` are `class` (not `object`) because they hold mutable instance state — `isDuckPressed: Boolean` and `shootingTimer: Job` respectively. All other `HeroState` subclasses are `object` singletons.

**`activeStates` always has exactly 2 entries:** Because the machine root uses `ChildMode.PARALLEL`, `ModelData.activeStates` always contains one state from the `"Movement"` region and one from the `"Fire"` region. The UI's `heroDrawable()` function checks combinations of both to pick the correct sprite.

**Koin initialization via `androidx.startup`:** `KoinInitializer` implements `Initializer<KoinApplication>` and is registered in `AndroidManifest.xml`. Koin starts automatically — there is no custom `Application` subclass.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ the correct sprite.

## 🏗️ Architecture

**MVI + KStateMachine + Voyager + Koin**
**MVI + KStateMachine + Lifecycle ViewModel**

```
UI (StickManGameScreen)
→ sendEvent(ControlEvent)
→ StickManGameScreenModel.machine.processEvent() [KStateMachine]
→ StickManGameViewModel.machine.processEvent() [KStateMachine]
→ onTransitionComplete / onStateEntry callbacks
→ intent { state { … } / sendEffect(…) }
→ MviModel.stateFlow / effectFlow
Expand All @@ -85,7 +85,7 @@ UI (StickManGameScreen)
| Layer | Responsibility |
|------------------------------|------------------------------------------------------------------|
| `StateControl.kt` | Domain types — `ControlEvent` & `HeroState` sealed hierarchies |
| `StickmanGameScreenModel.kt` | Builds the machine; bridges KStateMachine → MVI |
| `StickManGameViewModel.kt` | Builds the machine; bridges KStateMachine → MVI |
| `Mvi.kt` | Generic `MviModel<State, Effect>` (StateFlow + Channel) |
| `ModelConst.kt` | Game constants and `ModelData` / `ModelEffect` types |
| `Timers.kt` | `singleShotTimer` & `tickerFlow` coroutine helpers |
Expand All @@ -98,7 +98,7 @@ UI (StickManGameScreen)
**Android**

```bash
./gradlew :composeApp:assembleDebug
./gradlew :androidApp:assembleDebug
# then install the APK or run directly from Android Studio
```

Expand Down
44 changes: 44 additions & 0 deletions androidApp/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget

plugins {
alias(libs.plugins.androidApplication)
alias(libs.plugins.compose.compiler)
}

android {
namespace = "com.github.nsk90.composesample"
compileSdk = libs.versions.android.compileSdk.get().toInt()

defaultConfig {
applicationId = "com.github.nsk90.composesample"
minSdk = libs.versions.android.minSdk.get().toInt()
targetSdk = libs.versions.android.targetSdk.get().toInt()
versionCode = 2
versionName = libs.versions.app.get()
}
buildTypes {
release {
isMinifyEnabled = false
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21
}
buildFeatures {
compose = true
}
}

kotlin {
compilerOptions {
jvmTarget.set(JvmTarget.JVM_21)
}
}

dependencies {
implementation(projects.composeApp)
implementation(libs.androidx.activity.compose)
implementation(libs.compose.ui.tooling.preview)
debugImplementation(libs.compose.ui.tooling)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand All @@ -18,16 +17,6 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.startup"
android:exported="false"
tools:node="merge" tools:replace="android:authorities">
<meta-data
android:name="com.github.nsk90.composesample.KoinInitializer"
android:value="androidx.startup" />
</provider>
</application>

</manifest>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package com.github.nsk90.composesample
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
enableEdgeToEdge()
super.onCreate(savedInstanceState)

setContent {
Expand All @@ -20,4 +22,4 @@ class MainActivity : ComponentActivity() {
@Composable
fun AppAndroidPreview() {
App()
}
}
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
alias(libs.plugins.androidApplication) apply false
alias(libs.plugins.androidMultiplatformLibrary) apply false
alias(libs.plugins.jetbrainsCompose) apply false
alias(libs.plugins.compose.compiler) apply false
alias(libs.plugins.kotlinMultiplatform) apply false
}
}
68 changes: 13 additions & 55 deletions composeApp/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,88 +1,46 @@
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
import org.jetbrains.kotlin.gradle.dsl.JvmTarget

plugins {
alias(libs.plugins.kotlinMultiplatform)
alias(libs.plugins.androidApplication)
alias(libs.plugins.androidMultiplatformLibrary)
alias(libs.plugins.jetbrainsCompose)
alias(libs.plugins.compose.compiler)
}

kotlin {
androidTarget {
@OptIn(ExperimentalKotlinGradlePluginApi::class)
android {
namespace = "com.github.nsk90.composesample.shared"
compileSdk = libs.versions.android.compileSdk.get().toInt()
minSdk = libs.versions.android.minSdk.get().toInt()
compilerOptions {
jvmTarget.set(JvmTarget.JVM_21)
}
androidResources {
enable = true
}
}

listOf(iosArm64(), iosSimulatorArm64()).forEach { iosTarget ->
iosTarget.binaries.framework {
baseName = "ComposeApp"
isStatic = true
binaryOption("bundleId", "com.github.nsk90.composesample.ComposeApp")
}
}

sourceSets {
androidMain.dependencies {
implementation(libs.androidx.activity.compose)
implementation(libs.koin.android)
implementation(libs.androidx.startup.runtime)
}
commonMain.dependencies {
api(libs.koin.core)
implementation(libs.koin.compose)
implementation(libs.navigator.koin)
implementation(libs.navigator.screen.model)

implementation(libs.kstatemachine)
implementation(libs.kstatemachine.coroutines)

implementation(libs.compose.runtime)
implementation(libs.compose.foundation)
implementation(libs.compose.material)
implementation(libs.compose.material3)
implementation(libs.compose.ui)
implementation(libs.compose.components.resources)
implementation(libs.compose.ui.tooling.preview)
implementation(libs.androidx.lifecycle.viewmodel)
implementation(libs.androidx.lifecycle.viewmodel.compose)
implementation(libs.androidx.lifecycle.runtime.compose)
}
}
}

android {
namespace = "com.github.nsk90.composesample"
compileSdk = libs.versions.android.compileSdk.get().toInt()

sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
sourceSets["main"].res.srcDirs("src/androidMain/res")
sourceSets["main"].resources.srcDirs("src/commonMain/resources")

defaultConfig {
applicationId = "com.github.nsk90.composesample"
minSdk = libs.versions.android.minSdk.get().toInt()
targetSdk = libs.versions.android.targetSdk.get().toInt()
versionCode = 2
versionName = libs.versions.app.get()
}
packaging {
resources {
excludes += "/META-INF/{AL2.0,LGPL2.1}"
}
}
buildTypes {
getByName("release") {
isMinifyEnabled = false
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21
}
buildFeatures {
compose = true
}
dependencies {
debugImplementation(libs.compose.ui.tooling)
}
}
6 changes: 0 additions & 6 deletions composeApp/src/androidMain/kotlin/KoinModule.kt

This file was deleted.

This file was deleted.

Loading
Loading