From 4fd1ab3b070736ac425d63b146da3a05d43b6241 Mon Sep 17 00:00:00 2001 From: hoc081098 Date: Sat, 27 Apr 2024 13:32:42 +0700 Subject: [PATCH] Solivagant --- .idea/kotlinc.xml | 2 +- build.gradle.kts | 16 +++- .../kotlin_playground/solivagant/main.kt | 83 +++++++++++++++++++ 3 files changed, 97 insertions(+), 4 deletions(-) create mode 100644 src/main/kotlin/com/hoc081098/kotlin_playground/solivagant/main.kt diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml index 8d81632..fe63bb6 100644 --- a/.idea/kotlinc.xml +++ b/.idea/kotlinc.xml @@ -1,6 +1,6 @@ - \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index a413618..a0139c3 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -2,6 +2,7 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { kotlin("jvm") version "1.9.23" + id("org.jetbrains.compose") version "1.6.10-beta02" } group = "com.hoc.kotlin_playground" @@ -26,12 +27,21 @@ dependencies { testImplementation(kotlin("test")) implementation("io.github.hoc081098:FlowExt:0.8.0") - implementation("io.github.hoc081098:kmp-viewmodel:0.7.1") + val kmpViewModel = "0.7.1" + implementation("io.github.hoc081098:kmp-viewmodel:$kmpViewModel") + implementation("io.github.hoc081098:kmp-viewmodel-savedstate:$kmpViewModel") + implementation("io.github.hoc081098:kmp-viewmodel-compose:$kmpViewModel") implementation("io.github.hoc081098:channel-event-bus:0.0.2") implementation("io.github.hoc081098:solivagant-navigation:0.3.0") + implementation(compose.runtime) + implementation(compose.foundation) + implementation(compose.material3) + implementation(compose.materialIconsExtended) + implementation(compose.desktop.currentOs) - implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0") - implementation("org.jetbrains.kotlinx:kotlinx-coroutines-swing:1.8.0") + val coroutines = "1.8.0" + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines") + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-swing:$coroutines") implementation("org.jetbrains.kotlinx:kotlinx-collections-immutable:0.3.7") implementation("io.reactivex.rxjava3:rxjava:3.1.8") diff --git a/src/main/kotlin/com/hoc081098/kotlin_playground/solivagant/main.kt b/src/main/kotlin/com/hoc081098/kotlin_playground/solivagant/main.kt new file mode 100644 index 0000000..14b79ae --- /dev/null +++ b/src/main/kotlin/com/hoc081098/kotlin_playground/solivagant/main.kt @@ -0,0 +1,83 @@ +package com.hoc081098.kotlin_playground.solivagant + +import androidx.compose.foundation.background +import androidx.compose.foundation.layout.Box +import androidx.compose.material3.Button +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.Immutable +import androidx.compose.runtime.remember +import androidx.compose.ui.Alignment +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.window.Window +import androidx.compose.ui.window.application +import com.hoc081098.solivagant.lifecycle.LocalLifecycleOwner +import com.hoc081098.solivagant.navigation.ClearOnDispose +import com.hoc081098.solivagant.navigation.NavEventNavigator +import com.hoc081098.solivagant.navigation.NavHost +import com.hoc081098.solivagant.navigation.NavRoot +import com.hoc081098.solivagant.navigation.NavRoute +import com.hoc081098.solivagant.navigation.ProvideCompositionLocals +import com.hoc081098.solivagant.navigation.SavedStateSupport +import com.hoc081098.solivagant.navigation.ScreenDestination +import com.hoc081098.solivagant.navigation.rememberWindowLifecycleOwner +import kotlinx.collections.immutable.persistentSetOf + +fun main() { + val savedStateSupport = SavedStateSupport() + + application { + savedStateSupport.ClearOnDispose() + Window( + onCloseRequest = ::exitApplication, + title = "Solivagant", + ) { + val lifecycleOwner = checkNotNull(rememberWindowLifecycleOwner()) { "rememberWindowLifecycleOwner returns null" } + savedStateSupport.ProvideCompositionLocals(LocalLifecycleOwner provides lifecycleOwner) { MyApp() } + } + } +} + +val Navigator by lazy(LazyThreadSafetyMode.NONE) { NavEventNavigator() } + +@Composable +fun MyApp() = + NavHost( + startRoute = FirstRoute, + destinations = remember { + persistentSetOf( + FirstRouteDestination, + SecondRouteDestination, + ) + }, + navEventNavigator = Navigator, + ) + +@Immutable +data object FirstRoute : NavRoot + +val FirstRouteDestination = ScreenDestination { _, modifier -> + Box( + modifier = modifier.background(Color.Red.copy(alpha = 0.2f)), + contentAlignment = Alignment.Center, + ) { + Button(onClick = { Navigator.navigateTo(SecondRoute) }) { + Text("Go to second route") + } + } +} + + +@Immutable +data object SecondRoute : NavRoute + +val SecondRouteDestination = ScreenDestination { _, modifier -> + Box( + modifier = modifier.background(Color.Green.copy(alpha = 0.2f)), + contentAlignment = Alignment.Center, + ) { + Button(onClick = Navigator::navigateBack) { + Text("Back to first route") + } + } +}