@@ -3,6 +3,7 @@ package com.squareup.sample.thingy
33import com.squareup.workflow1.Workflow
44import com.squareup.workflow1.ui.Screen
55import kotlinx.coroutines.delay
6+ import kotlinx.coroutines.flow.StateFlow
67import kotlinx.coroutines.flow.flowOf
78import kotlinx.coroutines.launch
89import kotlin.time.Duration.Companion.seconds
@@ -12,41 +13,44 @@ enum class MyOutputs {
1213 Done ,
1314}
1415
15- fun MyWorkflow (
16- child1 : Workflow <Unit , String , Screen >,
17- child2 : Workflow <Unit , String , Screen >,
18- child3 : Workflow <String , Nothing , Screen >,
19- networkCall : suspend (String ) -> String
20- ) = backStackWorkflow <String , MyOutputs > {
16+ class MyWorkflow (
17+ private val child1 : Workflow <Unit , String , Screen >,
18+ private val child2 : Workflow <Unit , String , Screen >,
19+ private val child3 : Workflow <String , Nothing , Screen >,
20+ private val networkCall : suspend (String ) -> String
21+ ) : BackStackWorkflow <String, MyOutputs>() {
2122
22- // Step 1
23- showWorkflow(child1) { output ->
24- when (output) {
25- " back" -> emitOutput(MyOutputs .Back )
26- " next" -> {
27- // Step 2
28- val childResult = showWorkflow(child2) { output ->
29- if (output == " back" ) {
30- // Removes child2 from the stack, cancels the output handler from step 1, and just
31- // leaves child1 rendering.
32- goBack()
33- } else {
34- finishWith(output)
23+ override suspend fun BackStackScope<MyOutputs>.runBackStack (props : StateFlow <String >) {
24+ // Step 1
25+ // TODO clean this up
26+ val ignored: Unit = showWorkflow(child1) { output ->
27+ when (output) {
28+ " back" -> emitOutput(MyOutputs .Back )
29+ " next" -> {
30+ // Step 2
31+ val childResult = showWorkflow(child2) { output ->
32+ if (output == " back" ) {
33+ // Removes child2 from the stack, cancels the output handler from step 1, and just
34+ // leaves child1 rendering.
35+ goBack()
36+ } else {
37+ finishWith(output)
38+ }
3539 }
36- }
3740
38- // TODO: Show a loading screen automatically.
39- val networkResult = networkCall(childResult)
41+ // TODO: Show a loading screen automatically.
42+ val networkResult = networkCall(childResult)
4043
41- // Step 3: Show a workflow for 3 seconds then finish.
42- launch {
43- delay(3 .seconds)
44- emitOutput(MyOutputs .Done )
44+ // Step 3: Show a workflow for 3 seconds then finish.
45+ launch {
46+ delay(3 .seconds)
47+ emitOutput(MyOutputs .Done )
48+ }
49+ showWorkflow(child3, flowOf(networkResult))
4550 }
46- showWorkflow(child3, flowOf(networkResult))
47- }
4851
49- else -> {}
52+ else -> {}
53+ }
5054 }
5155 }
5256}
0 commit comments