Skip to content

Commit 702e06f

Browse files
added idle screen support for empty stack
1 parent fd73a0b commit 702e06f

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ import kotlinx.coroutines.flow.flowOf
1111

1212
/**
1313
* Creates a [BackStackWorkflow]. See the docs on [BackStackWorkflow.runBackStack] for more
14-
* information about what [block] can do.
14+
* information about what [runBackStack] can do.
1515
*/
1616
public inline fun <PropsT, OutputT> backStackWorkflow(
17-
crossinline block: suspend BackStackScope.(
17+
crossinline createIdleScreen: () -> Screen,
18+
crossinline runBackStack: suspend BackStackScope.(
1819
props: StateFlow<PropsT>,
1920
emitOutput: (OutputT) -> Unit
2021
) -> Unit
@@ -24,8 +25,10 @@ public inline fun <PropsT, OutputT> backStackWorkflow(
2425
props: StateFlow<PropsT>,
2526
emitOutput: (OutputT) -> Unit
2627
) {
27-
block(props, emitOutput)
28+
runBackStack(props, emitOutput)
2829
}
30+
31+
override fun createIdleScreen(): Screen = createIdleScreen()
2932
}
3033

3134
/**
@@ -96,6 +99,12 @@ public abstract class BackStackWorkflow<PropsT, OutputT> :
9699
emitOutput: (OutputT) -> Unit
97100
)
98101

102+
/**
103+
* Called to provide a screen to display when [runBackStack] has not shown anything yet, or when
104+
* a workflow's output handler is idle (not showing an active screen).
105+
*/
106+
abstract fun createIdleScreen(): Screen
107+
99108
final override fun asStatefulWorkflow():
100109
StatefulWorkflow<PropsT, *, OutputT, BackStackScreen<Screen>> =
101110
BackStackWorkflowImpl(this)

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,11 @@ internal class BackStackWorkflowImpl<PropsT, OutputT>(
9393
}
9494
}
9595

96-
// TODO show a loading screen if renderings is empty.
97-
return renderings.toBackStackScreen()
96+
return if (renderings.isEmpty()) {
97+
BackStackScreen(workflow.createIdleScreen())
98+
} else {
99+
renderings.toBackStackScreen()
100+
}
98101
}
99102

100103
override fun snapshotState(state: BackStackState): Snapshot? = null

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ data class RetryScreen(
1919
val onCancelClicked: () -> Unit,
2020
) : Screen
2121

22+
data object LoadingScreen : Screen
23+
2224
class MyWorkflow(
2325
private val child1: Workflow<Unit, String, Screen>,
2426
private val child2: Workflow<Unit, String, Screen>,
@@ -60,6 +62,8 @@ class MyWorkflow(
6062
}
6163
}
6264

65+
override fun createIdleScreen(): Screen = LoadingScreen
66+
6367
private suspend fun BackStackParentScope.networkCallWithRetry(
6468
request: String
6569
): String {

0 commit comments

Comments
 (0)