Skip to content

Commit 62a875e

Browse files
committed
Common flow demo
Refs: #11
1 parent 730992a commit 62a875e

102 files changed

Lines changed: 3261 additions & 75 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
/.idea/assetWizardSettings.xml
1111
/.idea/copilot
1212
/.idea/*.xd
13+
/.idea/deviceManager.xml
1314
.DS_Store
1415
/build
1516
/.kotlin

.idea/compiler.xml

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/deploymentTargetSelector.xml

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

.idea/gradle.xml

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

build.gradle.kts

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -105,36 +105,35 @@ tasks.register("runCommonflowTests") {
105105
description = "Run unit tests for the commonflow library."
106106
}
107107

108-
tasks.register("runRegisterUnitTests") {
109-
dependsOn(":examples:welcome:commonregister:allTests")
110-
description = "Run unit tests for the common register layer."
111-
}
112-
113-
tasks.register("runLoginUnitTests") {
114-
dependsOn(":examples:welcome:login:testDebugUnitTest")
115-
description = "Run unit tests for the login module."
116-
}
117-
118-
tasks.register("runLceUnitTests") {
108+
tasks.register("runLceExampleUnitTests") {
119109
dependsOn(":examples:lce:testDebugUnitTest")
120110
description = "Run unit tests for LCE app."
121111
}
122112

123-
tasks.register("runWelcomeUnitTests") {
113+
tasks.register("runWelcomeExampleUnitTests") {
114+
dependsOn(":examples:welcome:commonregister:allTests")
115+
dependsOn(":examples:welcome:login:testDebugUnitTest")
124116
dependsOn(":examples:welcome:welcome:testDebugUnitTest")
125117
description = "Run unit tests for welcome app."
126118
}
127119

128-
tasks.register("runTimerUnitTests") {
120+
tasks.register("runTimerExampleUnitTests") {
129121
dependsOn(":examples:timer:testAndroidHostTest")
130122
description = "Run unit tests for timer library."
131123
}
132124

133-
tasks.register("runDiUnitTests") {
125+
tasks.register("runDiExampleUnitTests") {
134126
dependsOn(":examples:di:login:testDebugUnitTest")
127+
dependsOn(":examples:di:social:testDebugUnitTest")
135128
description = "Run unit tests for di app."
136129
}
137130

131+
tasks.register("runBooksExampleUnitTests") {
132+
dependsOn(":examples:books:item:testDebugUnitTest")
133+
dependsOn(":examples:books:app:testDebugUnitTest")
134+
description = "Run unit tests for books app."
135+
}
136+
138137
tasks.register("displayVersion") {
139138
description = "Display application version name"
140139
doLast {
@@ -147,12 +146,11 @@ tasks.register("runUnitTests") {
147146
"runStateMachineTests",
148147
"runCoroutinesTests",
149148
"runCommonflowTests",
150-
"runLoginUnitTests",
151-
"runRegisterUnitTests",
152-
"runLceUnitTests",
153-
"runWelcomeUnitTests",
154-
"runTimerUnitTests",
155-
"runDiUnitTests"
149+
"runLceExampleUnitTests",
150+
"runWelcomeExampleUnitTests",
151+
"runTimerExampleUnitTests",
152+
"runDiExampleUnitTests",
153+
"runBooksExampleUnitTests"
156154
)
157155
group = "verification"
158156
description = "Run unit tests for all modules."

commonflow/data/src/commonMain/kotlin/com/motorro/commonstatemachine/flow/data/CommonFlowDataApi.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ interface CommonFlowDataApi<G: Any, U: Any, I, R> {
3030
/**
3131
* Creates flow
3232
*/
33-
fun init(flowHost: CommonFlowHost<R>, input: I? = null): CommonMachineState<G, U>
33+
fun init(flowHost: CommonFlowHost<R>, input: I): CommonMachineState<G, U>
3434

3535
/**
3636
* Returns default UI state

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ package com.motorro.commonstatemachine.flow.data
2020
* @see com.motorro.commonstatemachine.ProxyMachineState
2121
* @see CommonFlowDataApi
2222
*/
23-
interface CommonFlowHost<in R> {
23+
fun interface CommonFlowHost<in R> {
2424
/**
2525
* Completes common flow
26+
* @param result Optional result.
2627
*/
27-
fun onComplete(result: R? = null)
28+
fun onComplete(result: R)
2829
}

commonflow/viewmodel/src/androidMain/kotlin/com/motorro/commonstatemachine/flow/viewmodel/ViewModelComposition_android.kt

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package com.motorro.commonstatemachine.flow.viewmodel
1515

1616
import android.view.View
17+
import androidx.activity.ComponentActivity
1718
import androidx.activity.compose.BackHandler
1819
import androidx.activity.compose.setContent
1920
import androidx.activity.viewModels
@@ -23,26 +24,32 @@ import androidx.compose.ui.platform.ComposeView
2324
import androidx.compose.ui.platform.ViewCompositionStrategy
2425
import androidx.fragment.app.Fragment
2526
import androidx.fragment.app.viewModels
27+
import androidx.lifecycle.ViewModelProvider.Factory
2628
import androidx.lifecycle.viewmodel.CreationExtras
2729

2830
/**
2931
* Builds state machine composition
3032
* Ensure that [AppCompatActivity] has correct view model factory
3133
* @param extrasProducer optional extras producer
34+
* @param factoryProducer optional factory producer
3235
* @param setResult implement to set the activity result or do any result processing
3336
* @param navigationBackHandler optional back navigation handler. Implement to pass back navigation to the view model
3437
* @param content content block
3538
*/
36-
fun <G: Any, U: Any, R> AppCompatActivity.setStateMachineContent(
37-
extrasProducer: (() -> CreationExtras)? = null,
38-
setResult: (R?) -> Unit = { },
39-
navigationBackHandler: @Composable (Boolean, () -> Unit) -> Unit = { enabled, onBack ->
39+
inline fun <G: Any, U: Any, R, reified VM: CommonFlowViewModel<G, U, *, R>> ComponentActivity.setStateMachineContent(
40+
noinline extrasProducer: (() -> CreationExtras)? = null,
41+
noinline factoryProducer: (() -> Factory)? = null,
42+
noinline setResult: (R?) -> Unit = { },
43+
noinline navigationBackHandler: @Composable (Boolean, () -> Unit) -> Unit = { enabled, onBack ->
4044
BackHandler(enabled = enabled) { onBack() }
4145
},
42-
content: @Composable (U, (G) -> Unit) -> Unit
46+
noinline content: @Composable (U, (G) -> Unit) -> Unit
4347
) {
4448
setContent {
45-
val viewModel: CommonFlowViewModel<G, U, *, R> by viewModels(extrasProducer = extrasProducer)
49+
val viewModel: VM by viewModels(
50+
extrasProducer = extrasProducer,
51+
factoryProducer = factoryProducer
52+
)
4653
CommonFlowComposition(
4754
viewModel = viewModel,
4855
navigationBackHandler = navigationBackHandler,
@@ -62,21 +69,26 @@ fun <G: Any, U: Any, R> AppCompatActivity.setStateMachineContent(
6269
* Ensure that [Fragment] has correct view model factory
6370
* @param onFinish called when the flow is finished
6471
* @param extrasProducer optional extras producer
72+
* @param factoryProducer optional factory producer
6573
* @param navigationBackHandler optional back navigation handler. Implement to pass back navigation to the view model
6674
* @param content content block
6775
*/
68-
fun < G: Any, U: Any, R> Fragment.createStateMachineView(
69-
onFinish: (R?) -> Unit,
70-
extrasProducer: (() -> CreationExtras)? = null,
71-
navigationBackHandler: @Composable (Boolean, () -> Unit) -> Unit = { enabled, onBack ->
76+
inline fun <G: Any, U: Any, R, reified VM: CommonFlowViewModel<G, U, *, R>> Fragment.createStateMachineView(
77+
noinline onFinish: (R?) -> Unit,
78+
noinline extrasProducer: (() -> CreationExtras)? = null,
79+
noinline factoryProducer: (() -> Factory)? = null,
80+
noinline navigationBackHandler: @Composable (Boolean, () -> Unit) -> Unit = { enabled, onBack ->
7281
BackHandler(enabled = enabled) { onBack() }
7382
},
74-
content: @Composable (U, (G) -> Unit) -> Unit
83+
noinline content: @Composable (U, (G) -> Unit) -> Unit
7584
): View = ComposeView(requireContext()).apply {
7685
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
7786

7887
setContent {
79-
val viewModel: CommonFlowViewModel<G, U, *, R> by viewModels(extrasProducer = extrasProducer)
88+
val viewModel: VM by viewModels(
89+
extrasProducer = extrasProducer,
90+
factoryProducer = factoryProducer
91+
)
8092
CommonFlowComposition(
8193
viewModel = viewModel,
8294
navigationBackHandler = navigationBackHandler,

commonflow/viewmodel/src/commonMain/kotlin/com/motorro/commonstatemachine/flow/viewmodel/CommonFlowViewModel.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import kotlinx.coroutines.flow.StateFlow
2323
*/
2424
open class CommonFlowViewModel<G: Any, U: Any, I, R>(
2525
private val api: CommonFlowDataApi<G, U, I, R>,
26-
private val init: I? = null,
26+
private val init: I,
2727
vararg closeables: AutoCloseable
2828
) : ViewModel(*closeables) {
2929

@@ -46,12 +46,10 @@ open class CommonFlowViewModel<G: Any, U: Any, I, R>(
4646
object : ProxyMachineState<BaseFlowGesture<G>, BaseFlowUiState<U, R>, G, U>(api.getDefaultUiState()) {
4747

4848
private var terminated = false
49-
private val flowHost = object : CommonFlowHost<R> {
50-
override fun onComplete(result: R?) {
51-
if (terminated.not()) {
52-
terminated = true
53-
setUiState(BaseFlowUiState.Terminated(result))
54-
}
49+
private val flowHost = CommonFlowHost<R> { result ->
50+
if (terminated.not()) {
51+
terminated = true
52+
setUiState(BaseFlowUiState.Terminated(result))
5553
}
5654
}
5755

commonflow/viewmodel/src/commonTest/kotlin/com/motorro/commonstatemachine/flow/viewmodel/CommonFlowViewModelTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class CommonFlowViewModelTest {
3838

3939
private lateinit var dispatcher: TestDispatcher
4040
private lateinit var api: TestDataApi
41-
private lateinit var model: CommonFlowViewModel<TestGesture, TestUiState, Int, String>
41+
private lateinit var model: CommonFlowViewModel<TestGesture, TestUiState, Int, String?>
4242

4343
@BeforeTest
4444
fun init() {

0 commit comments

Comments
 (0)