Skip to content

Commit fe8cf3d

Browse files
Collect renderings on context specified in WorkflowLayout.take
1 parent 14d657e commit fe8cf3d

File tree

2 files changed

+10
-26
lines changed

2 files changed

+10
-26
lines changed

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.squareup.workflow1.ui
22

33
import android.content.Context
44
import android.os.Build.VERSION
5+
import android.os.Looper
56
import android.os.Parcel
67
import android.os.Parcelable
78
import android.os.Parcelable.Creator
@@ -20,6 +21,7 @@ import kotlinx.coroutines.CoroutineDispatcher
2021
import kotlinx.coroutines.Job
2122
import kotlinx.coroutines.flow.Flow
2223
import kotlinx.coroutines.launch
24+
import kotlinx.coroutines.withContext
2325
import kotlin.coroutines.CoroutineContext
2426
import kotlin.coroutines.EmptyCoroutineContext
2527

@@ -89,8 +91,8 @@ public class WorkflowLayout(
8991
* @param [repeatOnLifecycle] the lifecycle state in which renderings should be actively
9092
* updated. Defaults to STARTED, which is appropriate for Activity and Fragment.
9193
* @param [collectionContext] additional [CoroutineContext] we want for the coroutine that is
92-
* launched to collect the renderings. This should not override the [CoroutineDispatcher][kotlinx.coroutines.CoroutineDispatcher]
93-
* but may include some other instrumentation elements.
94+
* launched to collect the renderings, can include a different dispatcher - but it should be
95+
* a main thread dispatcher!
9496
*
9597
* @return the [Job] started to collect [renderings], to give callers the option to
9698
* [cancel][Job.cancel] collection -- e.g., before calling [take] again with a new
@@ -104,16 +106,15 @@ public class WorkflowLayout(
104106
repeatOnLifecycle: State = STARTED,
105107
collectionContext: CoroutineContext = EmptyCoroutineContext
106108
): Job {
107-
// We remove the dispatcher as we want to use what is provided by the lifecycle.coroutineScope.
108-
val contextWithoutDispatcher = collectionContext.minusKey(CoroutineDispatcher.Key)
109-
val lifecycleDispatcher = lifecycle.coroutineScope.coroutineContext[CoroutineDispatcher.Key]
110109
// Just like https://medium.com/androiddevelopers/a-safer-way-to-collect-flows-from-android-uis-23080b1f8bda
111-
return lifecycle.coroutineScope.launch(contextWithoutDispatcher) {
110+
return lifecycle.coroutineScope.launch {
112111
lifecycle.repeatOnLifecycle(repeatOnLifecycle) {
113-
require(coroutineContext[CoroutineDispatcher.Key] == lifecycleDispatcher) {
114-
"Collection dispatch should happen on the lifecycle's dispatcher."
112+
withContext(collectionContext) {
113+
require(Looper.myLooper() == Looper.getMainLooper()) {
114+
"Collection dispatch should happen on the main thread!"
115+
}
116+
renderings.collect { show(it) }
115117
}
116-
renderings.collect { show(it) }
117118
}
118119
}
119120
}

workflow-ui/core-android/src/test/java/com/squareup/workflow1/ui/WorkflowLayoutTest.kt

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -92,23 +92,6 @@ internal class WorkflowLayoutTest {
9292
unoriginal.show(BScreen(), env)
9393
}
9494

95-
@Test fun usesLifecycleDispatcher() {
96-
val lifecycleDispatcher = UnconfinedTestDispatcher()
97-
val collectionContext: CoroutineContext = UnconfinedTestDispatcher()
98-
val testLifecycle = TestLifecycleOwner(
99-
Lifecycle.State.RESUMED,
100-
lifecycleDispatcher
101-
)
102-
103-
workflowLayout.take(
104-
lifecycle = testLifecycle.lifecycle,
105-
renderings = flowOf(WrappedScreen(), WrappedScreen()),
106-
collectionContext = collectionContext
107-
)
108-
109-
// No crash then we safely removed the dispatcher.
110-
}
111-
11295
@Test fun takes() {
11396
val lifecycleDispatcher = UnconfinedTestDispatcher()
11497
val testLifecycle = TestLifecycleOwner(

0 commit comments

Comments
 (0)