diff --git a/CHANGELOG.md b/CHANGELOG.md index 20f35b015b3..25e40580650 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,13 @@ * `Versions`: * `Klock`: `2.6.1` -> `2.6.2` +* `Coroutines`: + * `Compose`: + * Created :) + * New extensions and function: + * `Composition#linkWithJob` + * `Composition#linkWithContext` + * `renderComposableAndLinkToContext` ## 0.9.10 diff --git a/coroutines/compose/build.gradle b/coroutines/compose/build.gradle new file mode 100644 index 00000000000..ca8aa5b5fce --- /dev/null +++ b/coroutines/compose/build.gradle @@ -0,0 +1,18 @@ +plugins { + id "org.jetbrains.kotlin.multiplatform" + id "org.jetbrains.kotlin.plugin.serialization" + id "com.android.library" + alias(libs.plugins.jb.compose) +} + +apply from: "$mppProjectWithSerializationAndComposePresetPath" + +kotlin { + sourceSets { + commonMain { + dependencies { + api libs.kt.coroutines + } + } + } +} diff --git a/coroutines/compose/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/compose/LinkCompositionToJob.kt b/coroutines/compose/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/compose/LinkCompositionToJob.kt new file mode 100644 index 00000000000..5107a97b5bf --- /dev/null +++ b/coroutines/compose/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/compose/LinkCompositionToJob.kt @@ -0,0 +1,14 @@ +package dev.inmo.micro_utils.coroutines.compose + +import androidx.compose.runtime.* +import kotlinx.coroutines.Job +import kotlinx.coroutines.job +import kotlin.coroutines.CoroutineContext + +fun Composition.linkWithJob(job: Job) { + job.invokeOnCompletion { + this@linkWithJob.dispose() + } +} + +fun Composition.linkWithContext(coroutineContext: CoroutineContext) = linkWithJob(coroutineContext.job) diff --git a/coroutines/compose/src/jsMain/kotlin/dev/inmo/micro_utils/coroutines/compose/RenderComposableWithLinkToContext.kt b/coroutines/compose/src/jsMain/kotlin/dev/inmo/micro_utils/coroutines/compose/RenderComposableWithLinkToContext.kt new file mode 100644 index 00000000000..85d78e4011b --- /dev/null +++ b/coroutines/compose/src/jsMain/kotlin/dev/inmo/micro_utils/coroutines/compose/RenderComposableWithLinkToContext.kt @@ -0,0 +1,16 @@ +package dev.inmo.micro_utils.coroutines.compose + +import androidx.compose.runtime.* +import kotlinx.coroutines.* +import org.jetbrains.compose.web.dom.DOMScope +import org.w3c.dom.Element + +suspend fun renderComposableAndLinkToContext( + root: TElement, + monotonicFrameClock: MonotonicFrameClock = DefaultMonotonicFrameClock, + content: @Composable DOMScope.() -> Unit +): Composition = org.jetbrains.compose.web.renderComposable(root, monotonicFrameClock, content).apply { + linkWithContext( + currentCoroutineContext() + ) +} diff --git a/coroutines/compose/src/main/AndroidManifest.xml b/coroutines/compose/src/main/AndroidManifest.xml new file mode 100644 index 00000000000..3287860bd4c --- /dev/null +++ b/coroutines/compose/src/main/AndroidManifest.xml @@ -0,0 +1 @@ + diff --git a/extensions.gradle b/extensions.gradle index df5aa0c04e1..751d061e0d8 100644 --- a/extensions.gradle +++ b/extensions.gradle @@ -21,6 +21,7 @@ allprojects { releaseMode = (project.hasProperty('RELEASE_MODE') && project.property('RELEASE_MODE') == "true") || System.getenv('RELEASE_MODE') == "true" mppProjectWithSerializationPresetPath = "${rootProject.projectDir.absolutePath}/mppProjectWithSerialization.gradle" + mppProjectWithSerializationAndComposePresetPath = "${rootProject.projectDir.absolutePath}/mppProjectWithSerializationAndCompose.gradle" mppJavaProjectPresetPath = "${rootProject.projectDir.absolutePath}/mppJavaProject.gradle" mppAndroidProjectPresetPath = "${rootProject.projectDir.absolutePath}/mppAndroidProject.gradle" diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 5219d007752..bc669187942 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -74,5 +74,4 @@ buildscript-android-dexcount = { module = "com.getkeepsafe.dexcount:dexcount-gra [plugins] -kt-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kt" } jb-compose = { id = "org.jetbrains.compose", version.ref = "jb-compose" } diff --git a/mppProjectWithSerializationAndCompose.gradle b/mppProjectWithSerializationAndCompose.gradle new file mode 100644 index 00000000000..eff5f7fe0c0 --- /dev/null +++ b/mppProjectWithSerializationAndCompose.gradle @@ -0,0 +1,72 @@ +project.version = "$version" +project.group = "$group" + +apply from: "$publishGradlePath" + +kotlin { + jvm { + compilations.main { + kotlinOptions { + jvmTarget = "1.8" + } + } + } + js (IR) { + browser() + nodejs() + } + android { + publishAllLibraryVariants() + } + + sourceSets { + commonMain { + dependencies { + implementation kotlin('stdlib') + implementation libs.kt.serialization + implementation compose.runtime + } + } + commonTest { + dependencies { + implementation kotlin('test-common') + implementation kotlin('test-annotations-common') + } + } + jvmMain { + dependencies { + implementation compose.desktop.currentOs + } + } + jvmTest { + dependencies { + implementation kotlin('test-junit') + } + } + jsMain { + dependencies { + implementation compose.web.core + } + } + jsTest { + dependencies { + implementation kotlin('test-js') + implementation kotlin('test-junit') + } + } + androidTest { + dependencies { + implementation kotlin('test-junit') + implementation libs.android.test.junit + implementation libs.android.espresso + } + } + } +} + +apply from: "$defaultAndroidSettingsPresetPath" + +java { + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 +} diff --git a/settings.gradle b/settings.gradle index 0c7df486ea5..d22455ff9dd 100644 --- a/settings.gradle +++ b/settings.gradle @@ -23,6 +23,7 @@ String[] includes = [ ":ktor:common", ":ktor:client", ":coroutines", + ":coroutines:compose", ":android:recyclerview", ":android:alerts:common", ":android:alerts:recyclerview",