diff --git a/.fleet/receipt.json b/.fleet/receipt.json deleted file mode 100644 index 21fa8a6..0000000 --- a/.fleet/receipt.json +++ /dev/null @@ -1,19 +0,0 @@ -// Project generated by Kotlin Multiplatform Wizard -{ - "spec": { - "template_id": "kmt", - "targets": { - "android": { - "ui": [ - "compose" - ] - }, - "ios": { - "ui": [ - "compose" - ] - } - } - }, - "timestamp": "2024-08-24T07:21:06.672781883Z" -} \ No newline at end of file diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index 1c765d5..ad5d06b 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -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: @@ -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 \ No newline at end of file + # 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 diff --git a/CLAUDE.md b/CLAUDE.md index 38866f3..39c9eb4 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -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(...) } @@ -44,7 +43,7 @@ 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` 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. @@ -52,15 +51,15 @@ UI (StickManGameScreen) - **`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.hasState()` 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.hasState()` 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` and `transitionOn` (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` and `transitionOn` (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. @@ -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` and is registered in `AndroidManifest.xml`. Koin starts automatically — there is no custom `Application` subclass. diff --git a/README.md b/README.md index 0a33de6..e05ce5b 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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` (StateFlow + Channel) | | `ModelConst.kt` | Game constants and `ModelData` / `ModelEffect` types | | `Timers.kt` | `singleShotTimer` & `tickerFlow` coroutine helpers | @@ -98,7 +98,7 @@ UI (StickManGameScreen) **Android** ```bash -./gradlew :composeApp:assembleDebug +./gradlew :androidApp:assembleDebug # then install the APK or run directly from Android Studio ``` diff --git a/androidApp/build.gradle.kts b/androidApp/build.gradle.kts new file mode 100644 index 0000000..3fada6d --- /dev/null +++ b/androidApp/build.gradle.kts @@ -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) +} diff --git a/composeApp/src/androidMain/AndroidManifest.xml b/androidApp/src/main/AndroidManifest.xml similarity index 66% rename from composeApp/src/androidMain/AndroidManifest.xml rename to androidApp/src/main/AndroidManifest.xml index 6e79028..9456ae9 100644 --- a/composeApp/src/androidMain/AndroidManifest.xml +++ b/androidApp/src/main/AndroidManifest.xml @@ -1,6 +1,5 @@ - + - - - - - \ No newline at end of file + diff --git a/composeApp/src/androidMain/kotlin/com/github/nsk80/composesample/MainActivity.kt b/androidApp/src/main/kotlin/com/github/nsk90/composesample/MainActivity.kt similarity index 87% rename from composeApp/src/androidMain/kotlin/com/github/nsk80/composesample/MainActivity.kt rename to androidApp/src/main/kotlin/com/github/nsk90/composesample/MainActivity.kt index 93de9b4..e5f7ef2 100644 --- a/composeApp/src/androidMain/kotlin/com/github/nsk80/composesample/MainActivity.kt +++ b/androidApp/src/main/kotlin/com/github/nsk90/composesample/MainActivity.kt @@ -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 { @@ -20,4 +22,4 @@ class MainActivity : ComponentActivity() { @Composable fun AppAndroidPreview() { App() -} \ No newline at end of file +} diff --git a/composeApp/src/androidMain/res/drawable-v24/ic_launcher_foreground.xml b/androidApp/src/main/res/drawable-v24/ic_launcher_foreground.xml similarity index 100% rename from composeApp/src/androidMain/res/drawable-v24/ic_launcher_foreground.xml rename to androidApp/src/main/res/drawable-v24/ic_launcher_foreground.xml diff --git a/composeApp/src/androidMain/res/drawable/ic_launcher_background.xml b/androidApp/src/main/res/drawable/ic_launcher_background.xml similarity index 100% rename from composeApp/src/androidMain/res/drawable/ic_launcher_background.xml rename to androidApp/src/main/res/drawable/ic_launcher_background.xml diff --git a/composeApp/src/androidMain/res/mipmap-anydpi-v26/ic_launcher.xml b/androidApp/src/main/res/mipmap-anydpi-v26/ic_launcher.xml similarity index 100% rename from composeApp/src/androidMain/res/mipmap-anydpi-v26/ic_launcher.xml rename to androidApp/src/main/res/mipmap-anydpi-v26/ic_launcher.xml diff --git a/composeApp/src/androidMain/res/mipmap-anydpi-v26/ic_launcher_round.xml b/androidApp/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml similarity index 100% rename from composeApp/src/androidMain/res/mipmap-anydpi-v26/ic_launcher_round.xml rename to androidApp/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml diff --git a/composeApp/src/androidMain/res/mipmap-hdpi/ic_launcher.png b/androidApp/src/main/res/mipmap-hdpi/ic_launcher.png similarity index 100% rename from composeApp/src/androidMain/res/mipmap-hdpi/ic_launcher.png rename to androidApp/src/main/res/mipmap-hdpi/ic_launcher.png diff --git a/composeApp/src/androidMain/res/mipmap-hdpi/ic_launcher_round.png b/androidApp/src/main/res/mipmap-hdpi/ic_launcher_round.png similarity index 100% rename from composeApp/src/androidMain/res/mipmap-hdpi/ic_launcher_round.png rename to androidApp/src/main/res/mipmap-hdpi/ic_launcher_round.png diff --git a/composeApp/src/androidMain/res/mipmap-mdpi/ic_launcher.png b/androidApp/src/main/res/mipmap-mdpi/ic_launcher.png similarity index 100% rename from composeApp/src/androidMain/res/mipmap-mdpi/ic_launcher.png rename to androidApp/src/main/res/mipmap-mdpi/ic_launcher.png diff --git a/composeApp/src/androidMain/res/mipmap-mdpi/ic_launcher_round.png b/androidApp/src/main/res/mipmap-mdpi/ic_launcher_round.png similarity index 100% rename from composeApp/src/androidMain/res/mipmap-mdpi/ic_launcher_round.png rename to androidApp/src/main/res/mipmap-mdpi/ic_launcher_round.png diff --git a/composeApp/src/androidMain/res/mipmap-xhdpi/ic_launcher.png b/androidApp/src/main/res/mipmap-xhdpi/ic_launcher.png similarity index 100% rename from composeApp/src/androidMain/res/mipmap-xhdpi/ic_launcher.png rename to androidApp/src/main/res/mipmap-xhdpi/ic_launcher.png diff --git a/composeApp/src/androidMain/res/mipmap-xhdpi/ic_launcher_round.png b/androidApp/src/main/res/mipmap-xhdpi/ic_launcher_round.png similarity index 100% rename from composeApp/src/androidMain/res/mipmap-xhdpi/ic_launcher_round.png rename to androidApp/src/main/res/mipmap-xhdpi/ic_launcher_round.png diff --git a/composeApp/src/androidMain/res/mipmap-xxhdpi/ic_launcher.png b/androidApp/src/main/res/mipmap-xxhdpi/ic_launcher.png similarity index 100% rename from composeApp/src/androidMain/res/mipmap-xxhdpi/ic_launcher.png rename to androidApp/src/main/res/mipmap-xxhdpi/ic_launcher.png diff --git a/composeApp/src/androidMain/res/mipmap-xxhdpi/ic_launcher_round.png b/androidApp/src/main/res/mipmap-xxhdpi/ic_launcher_round.png similarity index 100% rename from composeApp/src/androidMain/res/mipmap-xxhdpi/ic_launcher_round.png rename to androidApp/src/main/res/mipmap-xxhdpi/ic_launcher_round.png diff --git a/composeApp/src/androidMain/res/mipmap-xxxhdpi/ic_launcher.png b/androidApp/src/main/res/mipmap-xxxhdpi/ic_launcher.png similarity index 100% rename from composeApp/src/androidMain/res/mipmap-xxxhdpi/ic_launcher.png rename to androidApp/src/main/res/mipmap-xxxhdpi/ic_launcher.png diff --git a/composeApp/src/androidMain/res/mipmap-xxxhdpi/ic_launcher_round.png b/androidApp/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png similarity index 100% rename from composeApp/src/androidMain/res/mipmap-xxxhdpi/ic_launcher_round.png rename to androidApp/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png diff --git a/composeApp/src/androidMain/res/values/strings.xml b/androidApp/src/main/res/values/strings.xml similarity index 100% rename from composeApp/src/androidMain/res/values/strings.xml rename to androidApp/src/main/res/values/strings.xml diff --git a/build.gradle.kts b/build.gradle.kts index 9b4b419..d0c8dec 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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 -} \ No newline at end of file +} diff --git a/composeApp/build.gradle.kts b/composeApp/build.gradle.kts index 656e5bf..3ef7f68 100644 --- a/composeApp/build.gradle.kts +++ b/composeApp/build.gradle.kts @@ -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) - } -} diff --git a/composeApp/src/androidMain/kotlin/KoinModule.kt b/composeApp/src/androidMain/kotlin/KoinModule.kt deleted file mode 100644 index 28def77..0000000 --- a/composeApp/src/androidMain/kotlin/KoinModule.kt +++ /dev/null @@ -1,6 +0,0 @@ -import com.github.nsk90.composesample.StickManGameScreenModel -import org.koin.dsl.module - -val koinModule = module { - single { StickManGameScreenModel() } -} diff --git a/composeApp/src/androidMain/kotlin/com/github/nsk80/composesample/KoinInitializer.kt b/composeApp/src/androidMain/kotlin/com/github/nsk80/composesample/KoinInitializer.kt deleted file mode 100644 index 3403756..0000000 --- a/composeApp/src/androidMain/kotlin/com/github/nsk80/composesample/KoinInitializer.kt +++ /dev/null @@ -1,17 +0,0 @@ -package com.github.nsk90.composesample - -import android.content.Context -import androidx.startup.Initializer -import koinModule -import org.koin.android.ext.koin.androidContext -import org.koin.core.KoinApplication -import org.koin.core.context.startKoin - -class KoinInitializer : Initializer { - override fun create(context: Context) = startKoin { - androidContext(context) - modules(koinModule) - } - - override fun dependencies() = mutableListOf>>() -} \ No newline at end of file diff --git a/composeApp/src/commonMain/composeResources/drawable/compose-multiplatform.xml b/composeApp/src/commonMain/composeResources/drawable/compose-multiplatform.xml deleted file mode 100644 index c0bcfb2..0000000 --- a/composeApp/src/commonMain/composeResources/drawable/compose-multiplatform.xml +++ /dev/null @@ -1,36 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/composeApp/src/commonMain/kotlin/com/github/nsk90/composesample/App.kt b/composeApp/src/commonMain/kotlin/com/github/nsk90/composesample/App.kt index 8d27bd1..5f4895e 100644 --- a/composeApp/src/commonMain/kotlin/com/github/nsk90/composesample/App.kt +++ b/composeApp/src/commonMain/kotlin/com/github/nsk90/composesample/App.kt @@ -1,25 +1,24 @@ package com.github.nsk90.composesample import androidx.compose.foundation.isSystemInDarkTheme -import androidx.compose.material.MaterialTheme -import androidx.compose.material.darkColors -import androidx.compose.material.lightColors +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.darkColorScheme +import androidx.compose.material3.lightColorScheme import androidx.compose.runtime.Composable import androidx.compose.ui.graphics.Color -import cafe.adriel.voyager.navigator.Navigator import androidx.compose.ui.tooling.preview.Preview -private val LightColors = lightColors( +private val LightColors = lightColorScheme( primary = Color(0xFF0277BD), - primaryVariant = Color(0xFF004C8C), + primaryContainer = Color(0xFF004C8C), secondary = Color(0xFF00897B), - secondaryVariant = Color(0xFF005B4F), + secondaryContainer = Color(0xFF005B4F), ) -private val DarkColors = darkColors( +private val DarkColors = darkColorScheme( primary = Color(0xFF4FC3F7), - primaryVariant = Color(0xFF0288D1), + primaryContainer = Color(0xFF0288D1), secondary = Color(0xFF4DB6AC), surface = Color(0xFF1E1E1E), background = Color(0xFF121212), @@ -28,7 +27,7 @@ private val DarkColors = darkColors( @Composable @Preview fun App() { - MaterialTheme(colors = if (isSystemInDarkTheme()) DarkColors else LightColors) { - Navigator(StickManGameScreen()) + MaterialTheme(colorScheme = if (isSystemInDarkTheme()) DarkColors else LightColors) { + StickManGameScreen() } } diff --git a/composeApp/src/commonMain/kotlin/com/github/nsk90/composesample/StickManGameScreen.kt b/composeApp/src/commonMain/kotlin/com/github/nsk90/composesample/StickManGameScreen.kt index 8a7bc83..4b04027 100644 --- a/composeApp/src/commonMain/kotlin/com/github/nsk90/composesample/StickManGameScreen.kt +++ b/composeApp/src/commonMain/kotlin/com/github/nsk90/composesample/StickManGameScreen.kt @@ -8,7 +8,7 @@ import androidx.compose.foundation.layout.* import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items import androidx.compose.foundation.shape.RoundedCornerShape -import androidx.compose.material.* +import androidx.compose.material3.* import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier @@ -19,8 +19,8 @@ import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp -import cafe.adriel.voyager.core.screen.Screen -import cafe.adriel.voyager.koin.getScreenModel +import androidx.lifecycle.compose.collectAsStateWithLifecycle +import androidx.lifecycle.viewmodel.compose.viewModel import com.github.nsk90.composesample.ControlEvent.* import com.github.nsk90.composesample.HeroState.* import kstatemachine_compose_sample.composeapp.generated.resources.* @@ -30,14 +30,6 @@ import org.jetbrains.compose.resources.painterResource private enum class LogTag { STATE, TRANSITION, EVENT } private data class LogEntry(val tag: LogTag, val text: String) -class StickManGameScreen : Screen { - @Composable - override fun Content() { - val viewModel = getScreenModel() - StickManGameScreenContent(viewModel) - } -} - private fun heroDrawable(states: List): DrawableResource = when { states.hasState() && states.hasState() -> Res.drawable.standing_shooting states.hasState() && states.hasState() -> Res.drawable.airattacking_shooting @@ -51,8 +43,8 @@ private fun heroDrawable(states: List): DrawableResource = when { } @Composable -fun StickManGameScreenContent(viewModel: StickManGameScreenModel) { - val uiState by viewModel.model.stateFlow.collectAsState() +fun StickManGameScreen(viewModel: StickManGameViewModel = viewModel { StickManGameViewModel() }) { + val uiState by viewModel.model.stateFlow.collectAsStateWithLifecycle() val logEntries = remember { mutableStateListOf() } val heroDrawableRes = remember(uiState.activeStates) { heroDrawable(uiState.activeStates) } @@ -88,6 +80,7 @@ fun StickManGameScreenContent(viewModel: StickManGameScreenModel) { BoxWithConstraints( modifier = Modifier .fillMaxSize() + .safeDrawingPadding() .padding(horizontal = 16.dp, vertical = 12.dp) ) { if (maxWidth > maxHeight) { @@ -166,13 +159,13 @@ private fun AmmoHeader(uiState: ModelData) { fontSize = 10.sp, fontWeight = FontWeight.Bold, letterSpacing = 1.5.sp, - color = MaterialTheme.colors.onSurface.copy(alpha = 0.45f) + color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.45f) ) Text( text = "${uiState.ammoLeft}", fontSize = 30.sp, fontWeight = FontWeight.Bold, - color = MaterialTheme.colors.primary + color = MaterialTheme.colorScheme.primary ) } Row(horizontalArrangement = Arrangement.spacedBy(6.dp)) { @@ -180,15 +173,15 @@ private fun AmmoHeader(uiState: ModelData) { Surface( modifier = Modifier.width(105.dp), shape = RoundedCornerShape(4.dp), - color = MaterialTheme.colors.primary.copy(alpha = 0.10f), - border = BorderStroke(1.dp, MaterialTheme.colors.primary.copy(alpha = 0.30f)) + color = MaterialTheme.colorScheme.primary.copy(alpha = 0.10f), + border = BorderStroke(1.dp, MaterialTheme.colorScheme.primary.copy(alpha = 0.30f)) ) { Text( text = state::class.simpleName ?: "", modifier = Modifier.padding(horizontal = 8.dp, vertical = 4.dp).fillMaxWidth(), fontSize = 11.sp, fontFamily = FontFamily.Monospace, - color = MaterialTheme.colors.primary, + color = MaterialTheme.colorScheme.primary, textAlign = TextAlign.Center ) } @@ -201,14 +194,16 @@ private fun AmmoHeader(uiState: ModelData) { private fun AmmoBar(uiState: ModelData) { val ammoFraction = uiState.ammoLeft.toFloat() / INITIAL_AMMO.toFloat() LinearProgressIndicator( - progress = ammoFraction, + progress = { ammoFraction }, modifier = Modifier.fillMaxWidth().height(3.dp), color = when { ammoFraction > 0.5f -> Color(0xFF4CAF50) ammoFraction > 0.2f -> Color(0xFFFF9800) else -> Color(0xFFF44336) }, - backgroundColor = MaterialTheme.colors.onSurface.copy(alpha = 0.10f) + trackColor = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.10f), + gapSize = 0.dp, + drawStopIndicator = {}, ) } @@ -217,8 +212,8 @@ private fun EventLog(entries: List, modifier: Modifier = Modifier) { Surface( modifier = modifier, shape = RoundedCornerShape(8.dp), - color = MaterialTheme.colors.onSurface.copy(alpha = 0.04f), - border = BorderStroke(1.dp, MaterialTheme.colors.onSurface.copy(alpha = 0.08f)) + color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.04f), + border = BorderStroke(1.dp, MaterialTheme.colorScheme.onSurface.copy(alpha = 0.08f)) ) { LazyColumn( modifier = Modifier.padding(horizontal = 10.dp, vertical = 6.dp), @@ -244,7 +239,7 @@ private fun EventLog(entries: List, modifier: Modifier = Modifier) { private fun ControlButtonRow( duckInteractionSource: MutableInteractionSource, fireInteractionSource: MutableInteractionSource, - viewModel: StickManGameScreenModel + viewModel: StickManGameViewModel ) { Row( modifier = Modifier.fillMaxWidth(), @@ -260,8 +255,8 @@ private fun ControlButtonRow( onClick = { viewModel.sendEvent(JumpPressEvent) }, modifier = Modifier.weight(1f), colors = ButtonDefaults.buttonColors( - backgroundColor = MaterialTheme.colors.secondary, - contentColor = MaterialTheme.colors.onSecondary + containerColor = MaterialTheme.colorScheme.secondary, + contentColor = MaterialTheme.colorScheme.onSecondary ) ) { Text("Jump", fontSize = 13.sp) } @@ -270,7 +265,7 @@ private fun ControlButtonRow( interactionSource = fireInteractionSource, modifier = Modifier.weight(1f), colors = ButtonDefaults.buttonColors( - backgroundColor = Color(0xFFB71C1C), + containerColor = Color(0xFFB71C1C), contentColor = Color.White ) ) { Text("Fire", fontSize = 13.sp) } diff --git a/composeApp/src/commonMain/kotlin/com/github/nsk90/composesample/StickmanGameScreenModel.kt b/composeApp/src/commonMain/kotlin/com/github/nsk90/composesample/StickManGameViewModel.kt similarity index 93% rename from composeApp/src/commonMain/kotlin/com/github/nsk90/composesample/StickmanGameScreenModel.kt rename to composeApp/src/commonMain/kotlin/com/github/nsk90/composesample/StickManGameViewModel.kt index 51747cd..ba8c2f4 100644 --- a/composeApp/src/commonMain/kotlin/com/github/nsk90/composesample/StickmanGameScreenModel.kt +++ b/composeApp/src/commonMain/kotlin/com/github/nsk90/composesample/StickManGameViewModel.kt @@ -1,7 +1,7 @@ package com.github.nsk90.composesample -import cafe.adriel.voyager.core.model.ScreenModel -import cafe.adriel.voyager.core.model.screenModelScope +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope import com.github.nsk90.composesample.ControlEvent.DuckPressEvent import com.github.nsk90.composesample.ControlEvent.DuckReleaseEvent import com.github.nsk90.composesample.ControlEvent.FirePressEvent @@ -32,15 +32,15 @@ import ru.nsk.kstatemachine.statemachine.onTransitionComplete import ru.nsk.kstatemachine.statemachine.onTransitionTriggered import ru.nsk.kstatemachine.transition.onTriggered -class StickManGameScreenModel : ScreenModel, MviModelHost { +class StickManGameViewModel : ViewModel(), MviModelHost { override val model = MviModel( - screenModelScope, ModelData( + viewModelScope, ModelData( INITIAL_AMMO, listOf(Standing) ) ) private val machine = createStateMachineBlocking( - screenModelScope, + viewModelScope, "Hero", ChildMode.PARALLEL, creationArguments = buildCreationArguments { doNotThrowOnMultipleTransitionsMatch = true } @@ -55,7 +55,7 @@ class StickManGameScreenModel : ScreenModel, MviModelHost(targetState = NotShooting) onEntry { - shootingTimer = screenModelScope.launch { + shootingTimer = viewModelScope.launch { tickerFlow(SHOOTING_INTERVAL_MS).collect { if (state.ammoLeft == 0u) sendEvent(OutOfAmmoEvent) diff --git a/gradle.properties b/gradle.properties index b8215f6..472247a 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,12 +1,5 @@ kotlin.code.style=official #Gradle -org.gradle.jvmargs=-Xmx2048M -Dfile.encoding=UTF-8 -Dkotlin.daemon.jvm.options\="-Xmx2048M" - -#Android -android.nonTransitiveRClass=true -android.useAndroidX=true - -#Kotlin Multiplatform -kotlin.mpp.enableCInteropCommonization=true -kotlin.native.ignoreDisabledTargets=true \ No newline at end of file +org.gradle.jvmargs=-Xmx4096M -Dfile.encoding=UTF-8 -Dkotlin.daemon.jvm.options\="-Xmx2048M" +org.gradle.configuration-cache=true diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 9eccff3..602264b 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,44 +1,35 @@ [versions] app = "1.3.0" -agp = "8.13.2" -android-compileSdk = "36" +agp = "9.4.1" +android-compileSdk = "37" android-minSdk = "28" -android-targetSdk = "34" +android-targetSdk = "37" androidx-activityCompose = "1.13.0" androidx-lifecycle = "2.11.0" -compose-plugin = "1.12.0" +compose-plugin = "1.12.2" +compose-material3 = "1.9.0" kotlin = "2.4.21" kstatemachine = "0.38.1" -koin = "4.2.2" -startupRuntime = "1.2.0" -voyager = "1.0.1" [libraries] kstatemachine = { module = "io.github.nsk90:kstatemachine", version.ref = "kstatemachine" } kstatemachine-coroutines = { module = "io.github.nsk90:kstatemachine-coroutines", version.ref = "kstatemachine" } -koin-core = { module = "io.insert-koin:koin-core", version.ref = "koin" } -koin-compose = { module = "io.insert-koin:koin-compose", version.ref = "koin" } -koin-android = { module = "io.insert-koin:koin-android", version.ref = "koin" } - -navigator-screen-model = { module = "cafe.adriel.voyager:voyager-screenmodel", version.ref = "voyager" } -navigator-koin = { module = "cafe.adriel.voyager:voyager-koin", version.ref = "voyager" } - compose-runtime = { module = "org.jetbrains.compose.runtime:runtime", version.ref = "compose-plugin" } compose-foundation = { module = "org.jetbrains.compose.foundation:foundation", version.ref = "compose-plugin" } -compose-material = { module = "org.jetbrains.compose.material:material", version.ref = "compose-plugin" } +compose-material3 = { module = "org.jetbrains.compose.material3:material3", version.ref = "compose-material3" } compose-ui = { module = "org.jetbrains.compose.ui:ui", version.ref = "compose-plugin" } compose-components-resources = { module = "org.jetbrains.compose.components:components-resources", version.ref = "compose-plugin" } compose-ui-tooling-preview = { module = "org.jetbrains.compose.ui:ui-tooling-preview", version.ref = "compose-plugin" } compose-ui-tooling = { module = "org.jetbrains.compose.ui:ui-tooling", version.ref = "compose-plugin" } androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "androidx-activityCompose" } -androidx-lifecycle-viewmodel = { group = "org.jetbrains.androidx.lifecycle", name = "lifecycle-viewmodel", version.ref = "androidx-lifecycle" } -androidx-lifecycle-runtime-compose = { group = "org.jetbrains.androidx.lifecycle", name = "lifecycle-runtime-compose", version.ref = "androidx-lifecycle" } -androidx-startup-runtime = { group = "androidx.startup", name = "startup-runtime", version.ref = "startupRuntime" } +androidx-lifecycle-viewmodel-compose = { module = "org.jetbrains.androidx.lifecycle:lifecycle-viewmodel-compose", version.ref = "androidx-lifecycle" } +androidx-lifecycle-runtime-compose = { module = "org.jetbrains.androidx.lifecycle:lifecycle-runtime-compose", version.ref = "androidx-lifecycle" } [plugins] androidApplication = { id = "com.android.application", version.ref = "agp" } +androidMultiplatformLibrary = { id = "com.android.kotlin.multiplatform.library", version.ref = "agp" } jetbrainsCompose = { id = "org.jetbrains.compose", version.ref = "compose-plugin" } compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" } -kotlinMultiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" } \ No newline at end of file +kotlinMultiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" } diff --git a/iosApp/Configuration/Config.xcconfig b/iosApp/Configuration/Config.xcconfig index d7a36bd..218aea2 100644 --- a/iosApp/Configuration/Config.xcconfig +++ b/iosApp/Configuration/Config.xcconfig @@ -1,3 +1,3 @@ TEAM_ID= -BUNDLE_ID=org.example.project.kstatemachine-compose-sample +BUNDLE_ID=com.github.nsk90.composesample APP_NAME=kstatemachine-compose-sample \ No newline at end of file diff --git a/iosApp/iosApp.xcodeproj/project.pbxproj b/iosApp/iosApp.xcodeproj/project.pbxproj index 52b8c01..5d3449b 100644 --- a/iosApp/iosApp.xcodeproj/project.pbxproj +++ b/iosApp/iosApp.xcodeproj/project.pbxproj @@ -323,7 +323,7 @@ ENABLE_PREVIEWS = YES; FRAMEWORK_SEARCH_PATHS = ( "$(inherited)", - "$(SRCROOT)/../shared/build/xcode-frameworks/$(CONFIGURATION)/$(SDK_NAME)\n$(SRCROOT)/../composeApp/build/xcode-frameworks/$(CONFIGURATION)/$(SDK_NAME)", + "$(SRCROOT)/../composeApp/build/xcode-frameworks/$(CONFIGURATION)/$(SDK_NAME)", ); INFOPLIST_FILE = iosApp/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 15.3; @@ -350,7 +350,7 @@ ENABLE_PREVIEWS = YES; FRAMEWORK_SEARCH_PATHS = ( "$(inherited)", - "$(SRCROOT)/../shared/build/xcode-frameworks/$(CONFIGURATION)/$(SDK_NAME)\n$(SRCROOT)/../composeApp/build/xcode-frameworks/$(CONFIGURATION)/$(SDK_NAME)", + "$(SRCROOT)/../composeApp/build/xcode-frameworks/$(CONFIGURATION)/$(SDK_NAME)", ); INFOPLIST_FILE = iosApp/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 15.3; diff --git a/settings.gradle.kts b/settings.gradle.kts index 28db8a2..d9a629c 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -28,4 +28,5 @@ dependencyResolutionManagement { } } -include(":composeApp") \ No newline at end of file +include(":composeApp") +include(":androidApp") \ No newline at end of file