Skip to content

Commit 4364a55

Browse files
committed
Introduces WorkflowLayout.update.
We make the cored update method for `WorkflowLayout` public to give apps complete control over how exactly they collect renderings. Doing this because the `takeWhileAttached` method used by `start` is causing problems. Opening this up lets those impacted by these problems avoid them. In a downstream release, we will upgrade `androidx.lifecycle:lifecycle-viewmodel` and update the convenient `start` to use its improved API. Details in #632.
1 parent 1882f1d commit 4364a55

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

workflow-ui/core-android/api/core-android.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ public final class com/squareup/workflow1/ui/WorkflowLayout : android/widget/Fra
206206
public final fun start (Lkotlinx/coroutines/flow/Flow;Lcom/squareup/workflow1/ui/ViewEnvironment;)V
207207
public final fun start (Lkotlinx/coroutines/flow/Flow;Lcom/squareup/workflow1/ui/ViewRegistry;)V
208208
public static synthetic fun start$default (Lcom/squareup/workflow1/ui/WorkflowLayout;Lkotlinx/coroutines/flow/Flow;Lcom/squareup/workflow1/ui/ViewEnvironment;ILjava/lang/Object;)V
209+
public final fun update (Ljava/lang/Object;Lcom/squareup/workflow1/ui/ViewEnvironment;)V
209210
}
210211

211212
public abstract class com/squareup/workflow1/ui/WorkflowViewState {

workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/WorkflowLayout.kt

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,38 +44,43 @@ public class WorkflowLayout(
4444
private var restoredChildState: SparseArray<Parcelable>? = null
4545

4646
/**
47-
* Subscribes to [renderings], and uses [registry] to
48-
* [build a new view][ViewRegistry.buildView] each time a new type of rendering is received,
49-
* making that view the only child of this one.
47+
* Calls [WorkflowViewStub.update] on the [WorkflowViewStub] that is the only
48+
* child of this view.
49+
*
50+
* This is the method called from [start]. It is exposed to allow client code
51+
* to make its own choices about how exactly to consume a stream of renderings.
5052
*/
51-
public fun start(
52-
renderings: Flow<Any>,
53-
registry: ViewRegistry
53+
public fun update(
54+
newRendering: Any,
55+
environment: ViewEnvironment
5456
) {
55-
start(renderings, ViewEnvironment(mapOf(ViewRegistry to registry)))
57+
showing.update(newRendering, environment)
58+
restoredChildState?.let { restoredState ->
59+
restoredChildState = null
60+
showing.actual.restoreHierarchyState(restoredState)
61+
}
5662
}
5763

5864
/**
59-
* Subscribes to [renderings], and uses the [ViewRegistry] in the given [environment] to
60-
* [build a new view][ViewRegistry.buildView] each time a new type of rendering is received,
61-
* making that view the only child of this one.
65+
* This is the most common way to bootstrap a [Workflow][com.squareup.workflow1.Workflow]
66+
* driven UI. Collects [renderings], and calls [start] wih each that is received..
6267
*/
6368
public fun start(
6469
renderings: Flow<Any>,
6570
environment: ViewEnvironment = ViewEnvironment()
6671
) {
67-
takeWhileAttached(renderings) { show(it, environment) }
72+
takeWhileAttached(renderings) { update(it, environment) }
6873
}
6974

70-
private fun show(
71-
newRendering: Any,
72-
environment: ViewEnvironment
75+
/**
76+
* A convenience overload that builds a [ViewEnvironment] around [registry],
77+
* for a bit less boilerplate.
78+
*/
79+
public fun start(
80+
renderings: Flow<Any>,
81+
registry: ViewRegistry
7382
) {
74-
showing.update(newRendering, environment)
75-
restoredChildState?.let { restoredState ->
76-
restoredChildState = null
77-
showing.actual.restoreHierarchyState(restoredState)
78-
}
83+
start(renderings, ViewEnvironment(mapOf(ViewRegistry to registry)))
7984
}
8085

8186
override fun onSaveInstanceState(): Parcelable {

0 commit comments

Comments
 (0)