Skip to content

Commit e466fac

Browse files
committed
Allow repeatOnLifecycle state to be configurable.
While most workflow use cases is likely to be inside an Activity and can specify renderings to be updated while in the Lifecycle.State.STARTED state, there are certain use cases where we want the workflow to depend on the application's lifecycle (using ProcessLifecycleOwner.) which will not work when calling repeatOnLifecycle with Lifecycle.State.STARTED as this state event is tied to Activities.
1 parent b264715 commit e466fac

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

samples/compose-samples/src/main/java/com/squareup/sample/compose/hellocomposebinding/HelloBindingActivity.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ class HelloBindingActivity : AppCompatActivity() {
3636
val model: HelloBindingModel by viewModels()
3737
setContentView(
3838
WorkflowLayout(this).apply {
39-
start(lifecycle, model.renderings, viewEnvironment)
39+
start(
40+
lifecycle = lifecycle,
41+
renderings = model.renderings,
42+
environment = viewEnvironment
43+
)
4044
}
4145
)
4246
}

samples/compose-samples/src/main/java/com/squareup/sample/compose/nestedrenderings/NestedRenderingsActivity.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ class NestedRenderingsActivity : AppCompatActivity() {
3838
val model: NestedRenderingsModel by viewModels()
3939
setContentView(
4040
WorkflowLayout(this).apply {
41-
start(lifecycle, model.renderings, viewEnvironment)
41+
start(
42+
lifecycle = lifecycle,
43+
renderings = model.renderings,
44+
environment = viewEnvironment
45+
)
4246
}
4347
)
4448
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,11 @@ public abstract interface class com/squareup/workflow1/ui/ViewStarter {
231231
public final class com/squareup/workflow1/ui/WorkflowLayout : android/widget/FrameLayout {
232232
public fun <init> (Landroid/content/Context;Landroid/util/AttributeSet;)V
233233
public synthetic fun <init> (Landroid/content/Context;Landroid/util/AttributeSet;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
234-
public final fun start (Landroidx/lifecycle/Lifecycle;Lkotlinx/coroutines/flow/Flow;Lcom/squareup/workflow1/ui/ViewEnvironment;)V
234+
public final fun start (Landroidx/lifecycle/Lifecycle;Lkotlinx/coroutines/flow/Flow;Landroidx/lifecycle/Lifecycle$State;Lcom/squareup/workflow1/ui/ViewEnvironment;)V
235235
public final fun start (Landroidx/lifecycle/Lifecycle;Lkotlinx/coroutines/flow/Flow;Lcom/squareup/workflow1/ui/ViewRegistry;)V
236236
public final fun start (Lkotlinx/coroutines/flow/Flow;Lcom/squareup/workflow1/ui/ViewEnvironment;)V
237237
public final fun start (Lkotlinx/coroutines/flow/Flow;Lcom/squareup/workflow1/ui/ViewRegistry;)V
238-
public static synthetic fun start$default (Lcom/squareup/workflow1/ui/WorkflowLayout;Landroidx/lifecycle/Lifecycle;Lkotlinx/coroutines/flow/Flow;Lcom/squareup/workflow1/ui/ViewEnvironment;ILjava/lang/Object;)V
238+
public static synthetic fun start$default (Lcom/squareup/workflow1/ui/WorkflowLayout;Landroidx/lifecycle/Lifecycle;Lkotlinx/coroutines/flow/Flow;Landroidx/lifecycle/Lifecycle$State;Lcom/squareup/workflow1/ui/ViewEnvironment;ILjava/lang/Object;)V
239239
public static synthetic fun start$default (Lcom/squareup/workflow1/ui/WorkflowLayout;Lkotlinx/coroutines/flow/Flow;Lcom/squareup/workflow1/ui/ViewEnvironment;ILjava/lang/Object;)V
240240
public final fun update (Ljava/lang/Object;Lcom/squareup/workflow1/ui/ViewEnvironment;)V
241241
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,18 @@ public class WorkflowLayout(
7373
*
7474
* @param [lifecycle] the lifecycle that defines when and how this view should be updated.
7575
* Typically this comes from `ComponentActivity.lifecycle` or `Fragment.lifecycle`.
76+
* @param [repeatOnLifecycle] the lifecycle state in which renderings should be actively
77+
* updated. Defaults to STARTED, which is appropriate for Activity and Fragment.
7678
*/
7779
public fun start(
7880
lifecycle: Lifecycle,
7981
renderings: Flow<Any>,
82+
repeatOnLifecycle: Lifecycle.State = Lifecycle.State.STARTED,
8083
environment: ViewEnvironment = ViewEnvironment()
8184
) {
8285
// Just like https://medium.com/androiddevelopers/a-safer-way-to-collect-flows-from-android-uis-23080b1f8bda
8386
lifecycle.coroutineScope.launch {
84-
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
87+
lifecycle.repeatOnLifecycle(repeatOnLifecycle) {
8588
renderings.collect { update(it, environment) }
8689
}
8790
}
@@ -96,7 +99,11 @@ public class WorkflowLayout(
9699
renderings: Flow<Any>,
97100
registry: ViewRegistry
98101
) {
99-
start(lifecycle, renderings, ViewEnvironment(mapOf(ViewRegistry to registry)))
102+
start(
103+
lifecycle = lifecycle,
104+
renderings = renderings,
105+
environment = ViewEnvironment(mapOf(ViewRegistry to registry))
106+
)
100107
}
101108

102109
@Deprecated(

0 commit comments

Comments
 (0)