Skip to content

Commit 168dc1d

Browse files
committed
Common compose api
Refs: #11
1 parent 709a3a4 commit 168dc1d

16 files changed

Lines changed: 306 additions & 61 deletions

File tree

.idea/gradle.xml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,13 +1179,26 @@ this interface by switching host machine to email or complete states in correspo
11791179
#### Common child flow API
11801180

11811181
For your convenience there are couple of ready-made interfaces to adopt child flow.
1182-
They are:
1182+
The interfaces are located in a separate libraries:
11831183

1184-
- [CommonFlowHost](commonstatemachine/src/commonMain/kotlin/com/motorro/commonstatemachine/flow/CommonFlowHost.kt) -
1184+
```groovy
1185+
dependencies {
1186+
// If you use it for state machines only
1187+
implementation "com.motorro.commonstatemachine:commonflow-data:x.x.x"
1188+
// If you go with compose
1189+
implementation "com.motorro.commonstatemachine:commonflow-compose:x.x.x"
1190+
}
1191+
```
1192+
1193+
The libraries include the following interfaces:
1194+
1195+
- [CommonFlowHost](commonflow/data/src/commonMain/kotlin/com/motorro/commonstatemachine/flow/data/CommonFlowHost.kt) -
11851196
the interface the proxy should provide to the child flow. Child uses this flow to terminate but could
11861197
also include any other callbacks
1187-
- [CommonFlowDataApi](commonstatemachine/src/commonMain/kotlin/com/motorro/commonstatemachine/flow/CommonFlowApi.kt) -
1198+
- [CommonFlowDataApi](commonflow/data/src/commonMain/kotlin/com/motorro/commonstatemachine/flow/data/CommonFlowApi.kt) -
11881199
contains methods to initiate the flow and to adapt it to the hosting flow.
1200+
- [CommonFlowUiApi](commonflow/compose/src/commonMain/kotlin/com/motorro/commonstatemachine/flow/compose/CommonFlowUiApi.kt) -
1201+
An interface with the single `Screen` method to inject and put to the composition
11891202

11901203
Check the [example](examples/di) that shows the use of this flow:
11911204

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/*
2+
* Copyright 2026 Nikolai Kotchetkov.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*/
13+
14+
@file:Suppress("unused")
15+
@file:OptIn(ExperimentalWasmDsl::class)
16+
17+
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
18+
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
19+
20+
plugins {
21+
alias(libs.plugins.kotlin.multiplatform)
22+
alias(libs.plugins.android.kotlin.multiplatform.library)
23+
alias(libs.plugins.compose)
24+
alias(libs.plugins.composeMultiplatform)
25+
alias(libs.plugins.kotlin.dokka)
26+
id("maven-publish")
27+
id("signing")
28+
}
29+
30+
val versionName: String by project.extra
31+
val androidMinSdkVersion: Int by project.extra
32+
val androidTargetSdkVersion: Int by project.extra
33+
val androidCompileSdkVersion: Int by project.extra
34+
35+
group = rootProject.group
36+
version = rootProject.version
37+
38+
println("== Project version: $versionName ==")
39+
40+
kotlin {
41+
jvmToolchain(17)
42+
43+
jvm()
44+
android {
45+
namespace = "com.motorro.commonstatemachine.commonflow.compose"
46+
compileSdk = androidCompileSdkVersion
47+
minSdk = androidMinSdkVersion
48+
49+
withHostTest {
50+
isIncludeAndroidResources = true
51+
}
52+
53+
compilerOptions {
54+
jvmTarget.set(JvmTarget.JVM_17)
55+
}
56+
}
57+
58+
js(IR) {
59+
binaries.library()
60+
useCommonJs()
61+
browser {
62+
testTask(Action {
63+
useMocha {
64+
timeout = "10s"
65+
}
66+
})
67+
}
68+
}
69+
70+
wasmJs {
71+
binaries.library()
72+
useCommonJs()
73+
browser {
74+
testTask(Action {
75+
useMocha {
76+
timeout = "10s"
77+
}
78+
})
79+
}
80+
}
81+
82+
listOf(
83+
iosX64(),
84+
iosArm64(),
85+
iosSimulatorArm64()
86+
).forEach {
87+
it.binaries.framework {
88+
baseName = "commonflow-compose"
89+
isStatic = true
90+
}
91+
}
92+
93+
sourceSets {
94+
commonMain.dependencies {
95+
api(project(":commonstatemachine"))
96+
api(project(":commonflow:data"))
97+
api(libs.composeMultiplatform.runtime)
98+
api(libs.composeMultiplatform.foundation)
99+
}
100+
}
101+
}
102+
val javadocJar by tasks.registering(Jar::class) {
103+
dependsOn(tasks.dokkaGenerate)
104+
group = "documentation"
105+
archiveClassifier.set("javadoc")
106+
from(tasks.dokkaGenerate)
107+
}
108+
109+
val libId = "commonflow-compose"
110+
val libName = "commonflow-compose"
111+
val libDesc = "Common compose flow for modularized state machines"
112+
val projectUrl: String by project.extra
113+
val projectScm: String by project.extra
114+
val ossrhUsername: String? by rootProject.extra
115+
val ossrhPassword: String? by rootProject.extra
116+
val developerId: String by project.extra
117+
val developerName: String by project.extra
118+
val developerEmail: String by project.extra
119+
val signingKey: String? by rootProject.extra
120+
val signingPassword: String? by rootProject.extra
121+
122+
publishing {
123+
publications.withType<MavenPublication> {
124+
artifact(javadocJar)
125+
pom {
126+
name.set(libName)
127+
description.set(libDesc)
128+
url.set(projectUrl)
129+
licenses {
130+
license {
131+
name.set("Apache-2.0")
132+
url.set("https://apache.org/licenses/LICENSE-2.0")
133+
}
134+
}
135+
developers {
136+
developer {
137+
id.set(developerId)
138+
name.set(developerName)
139+
email.set(developerEmail)
140+
}
141+
}
142+
scm {
143+
connection.set(projectScm)
144+
developerConnection.set(projectScm)
145+
url.set(projectUrl)
146+
}
147+
}
148+
}
149+
}
150+
151+
signing {
152+
useInMemoryPgpKeys(signingKey, signingPassword)
153+
sign(publishing.publications)
154+
}
155+
156+
val signingTasks = tasks.withType<Sign>()
157+
tasks.withType<AbstractPublishToMaven>().configureEach {
158+
dependsOn(signingTasks)
159+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2026 Nikolai Kotchetkov.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*/
13+
14+
package com.motorro.commonstatemachine.flow.compose
15+
16+
import androidx.compose.runtime.Composable
17+
import androidx.compose.ui.Modifier
18+
19+
/**
20+
* Common flow UI API. Used as a common interface between master and child flow using proxy.
21+
* @param G - gesture type
22+
* @param U - UI state type
23+
* @see com.motorro.commonstatemachine.ProxyMachineState
24+
* @see com.motorro.commonstatemachine.flow.data.CommonFlowHost
25+
* @see com.motorro.commonstatemachine.flow.data.CommonFlowDataApi
26+
*/
27+
interface CommonFlowUiApi<G: Any, U: Any> {
28+
/**
29+
* Provides composition
30+
*/
31+
@Composable
32+
fun Screen(
33+
state: U,
34+
onGesture: (G) -> Unit,
35+
modifier: Modifier = Modifier
36+
)
37+
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ kotlin {
4040

4141
jvm()
4242
android {
43-
namespace = "com.motorro.commonstatemachine.commonflow"
43+
namespace = "com.motorro.commonstatemachine.commonflow.data"
4444
compileSdk = androidCompileSdkVersion
4545
minSdk = androidMinSdkVersion
4646

@@ -83,7 +83,7 @@ kotlin {
8383
iosSimulatorArm64()
8484
).forEach {
8585
it.binaries.framework {
86-
baseName = "commonflow"
86+
baseName = "commonflow-data"
8787
isStatic = true
8888
}
8989
}
@@ -101,9 +101,9 @@ val javadocJar by tasks.registering(Jar::class) {
101101
from(tasks.dokkaGenerate)
102102
}
103103

104-
val libId = "commonflow"
105-
val libName = "commonflow"
106-
val libDesc = "Common flow for modularized state machines"
104+
val libId = "commonflow-data"
105+
val libName = "commonflow-data"
106+
val libDesc = "Common data flow for modularized state machines"
107107
val projectUrl: String by project.extra
108108
val projectScm: String by project.extra
109109
val ossrhUsername: String? by rootProject.extra
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2026 Nikolai Kotchetkov.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*/
13+
14+
package com.motorro.commonstatemachine.flow.data
15+
16+
import com.motorro.commonstatemachine.CommonMachineState
17+
18+
/**
19+
* Common flow data API. Used as a common interface between master and child flow using proxy.
20+
* The proxy provides [CommonFlowHost] to the child flow and the child calls its methods when finished
21+
* or otherwise needed
22+
* @param G - gesture type
23+
* @param U - UI state type
24+
* @param I - input type
25+
* @param R - result type
26+
* @see com.motorro.commonstatemachine.ProxyMachineState
27+
* @see CommonFlowHost
28+
*/
29+
interface CommonFlowDataApi<G: Any, U: Any, I, R, F : CommonFlowHost<R>> {
30+
/**
31+
* Creates flow
32+
*/
33+
fun init(flowHost: F, input: I? = null): CommonMachineState<G, U>
34+
35+
/**
36+
* Returns default UI state
37+
*/
38+
fun getDefaultUiState(): U
39+
40+
/**
41+
* Returns back gesture mapping
42+
*/
43+
fun getBackGesture(): G? = null
44+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2026 Nikolai Kotchetkov.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*/
13+
14+
package com.motorro.commonstatemachine.flow.data
15+
16+
/**
17+
* Common flow host.
18+
* Used as a common interface between master and child flow using proxy.
19+
* @param R - result type
20+
* @see com.motorro.commonstatemachine.ProxyMachineState
21+
* @see CommonFlowDataApi
22+
*/
23+
interface CommonFlowHost<in R> {
24+
/**
25+
* Completes common flow
26+
*/
27+
fun onComplete(result: R? = null)
28+
}

commonflow/src/commonMain/kotlin/com/motorro/commonstatemachine/flow/CommonFlowApi.kt

Lines changed: 0 additions & 23 deletions
This file was deleted.

commonflow/src/commonMain/kotlin/com/motorro/commonstatemachine/flow/CommonFlowHost.kt

Lines changed: 0 additions & 11 deletions
This file was deleted.

examples/di/api/build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ dependencies {
6060
api(libs.timber)
6161

6262
api(project(":commonstatemachine"))
63-
api(project(":commonflow"))
63+
api(project(":commonflow:data"))
64+
api(project(":commonflow:compose"))
6465

6566
coreLibraryDesugaring(libs.desugaring)
6667

0 commit comments

Comments
 (0)