Skip to content

Commit 4198e39

Browse files
wip
1 parent 2a7bf42 commit 4198e39

File tree

2 files changed

+41
-30
lines changed

2 files changed

+41
-30
lines changed

samples/containers/thingy/src/main/java/com/squareup/sample/thingy/BackStackWorkflow.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import kotlinx.coroutines.CoroutineScope
88
import kotlinx.coroutines.flow.Flow
99
import kotlinx.coroutines.flow.StateFlow
1010
import kotlinx.coroutines.flow.flowOf
11+
import kotlin.experimental.ExperimentalTypeInference
1112

1213
/**
1314
* Creates a [BackStackWorkflow]. See the docs on [BackStackWorkflow.runBackStack] for more
@@ -164,11 +165,17 @@ public sealed interface BackStackNestedScope<OutputT, R> : BackStackScope<Output
164165
suspend fun goBack(): Nothing
165166
}
166167

168+
@OptIn(ExperimentalTypeInference::class)
167169
public suspend inline fun <OutputT, ChildOutputT, R> BackStackScope<OutputT>.showWorkflow(
168170
workflow: Workflow<Unit, ChildOutputT, Screen>,
169-
noinline onOutput: suspend BackStackNestedScope<OutputT, R>.(output: ChildOutputT) -> Unit
171+
@BuilderInference noinline onOutput: suspend BackStackNestedScope<OutputT, R>.(output: ChildOutputT) -> Unit
170172
): R = showWorkflow(workflow, props = flowOf(Unit), onOutput)
171173

174+
// public suspend inline fun <OutputT, ChildOutputT> BackStackScope<OutputT>.showWorkflow(
175+
// workflow: Workflow<Unit, ChildOutputT, Screen>,
176+
// noinline onOutput: suspend BackStackNestedScope<OutputT, Unit>.(output: ChildOutputT) -> Unit
177+
// ): Unit = showWorkflow(workflow, props = flowOf(Unit), onOutput)
178+
172179
public suspend inline fun <ChildPropsT> BackStackScope<*>.showWorkflow(
173180
workflow: Workflow<ChildPropsT, Nothing, Screen>,
174181
props: Flow<ChildPropsT>,

samples/containers/thingy/src/main/java/com/squareup/sample/thingy/MyWorkflow.kt

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.squareup.sample.thingy
33
import com.squareup.workflow1.Workflow
44
import com.squareup.workflow1.ui.Screen
55
import kotlinx.coroutines.delay
6+
import kotlinx.coroutines.flow.StateFlow
67
import kotlinx.coroutines.flow.flowOf
78
import kotlinx.coroutines.launch
89
import 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

Comments
 (0)