Skip to content

Commit

Permalink
Introduces WorkflowLayout.update.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rjrjr committed Jan 20, 2022
1 parent 1882f1d commit e67c4ec
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
1 change: 1 addition & 0 deletions workflow-ui/core-android/api/core-android.api
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ public final class com/squareup/workflow1/ui/WorkflowLayout : android/widget/Fra
public final fun start (Lkotlinx/coroutines/flow/Flow;Lcom/squareup/workflow1/ui/ViewEnvironment;)V
public final fun start (Lkotlinx/coroutines/flow/Flow;Lcom/squareup/workflow1/ui/ViewRegistry;)V
public static synthetic fun start$default (Lcom/squareup/workflow1/ui/WorkflowLayout;Lkotlinx/coroutines/flow/Flow;Lcom/squareup/workflow1/ui/ViewEnvironment;ILjava/lang/Object;)V
public final fun update (Ljava/lang/Object;Lcom/squareup/workflow1/ui/ViewEnvironment;)V
}

public abstract class com/squareup/workflow1/ui/WorkflowViewState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,38 +44,43 @@ public class WorkflowLayout(
private var restoredChildState: SparseArray<Parcelable>? = null

/**
* Subscribes to [renderings], and uses [registry] to
* [build a new view][ViewRegistry.buildView] each time a new type of rendering is received,
* making that view the only child of this one.
* Calls [WorkflowViewStub.update] on the [WorkflowViewStub] that is the only
* child of this view.
*
* This is the method called from [start]. It is exposed to allow clients to
* make their own choices about how exactly to consume a stream of renderings.
*/
public fun start(
renderings: Flow<Any>,
registry: ViewRegistry
public fun update(
newRendering: Any,
environment: ViewEnvironment
) {
start(renderings, ViewEnvironment(mapOf(ViewRegistry to registry)))
showing.update(newRendering, environment)
restoredChildState?.let { restoredState ->
restoredChildState = null
showing.actual.restoreHierarchyState(restoredState)
}
}

/**
* Subscribes to [renderings], and uses the [ViewRegistry] in the given [environment] to
* [build a new view][ViewRegistry.buildView] each time a new type of rendering is received,
* making that view the only child of this one.
* This is the most common way to bootstrap a [Workflow][com.squareup.workflow1.Workflow]
* driven UI. Collects [renderings], and calls [start] wih each that is received..
*/
public fun start(
renderings: Flow<Any>,
environment: ViewEnvironment = ViewEnvironment()
) {
takeWhileAttached(renderings) { show(it, environment) }
takeWhileAttached(renderings) { update(it, environment) }
}

private fun show(
newRendering: Any,
environment: ViewEnvironment
/**
* A convenience overload that builds a [ViewEnvironment] around [registry],
* for a bit less boilerplate.
*/
public fun start(
renderings: Flow<Any>,
registry: ViewRegistry
) {
showing.update(newRendering, environment)
restoredChildState?.let { restoredState ->
restoredChildState = null
showing.actual.restoreHierarchyState(restoredState)
}
start(renderings, ViewEnvironment(mapOf(ViewRegistry to registry)))
}

override fun onSaveInstanceState(): Parcelable {
Expand Down

0 comments on commit e67c4ec

Please sign in to comment.