Skip to content

Commit 01eb576

Browse files
committed
WorkflowLayout.start needs a Lifecycle, BackStackScreen is supported.
On a lot of Samsung devices running Anroid 12 / API 30, the sketchy `WorkflowLayout.takeWhileAttached` method at the heart of `WorkflowLayout.start` was having surprising effects -- we'd see redundant calls to `OnAttachStateChangeListener.onViewAttachedToWindow`. The problem goes away if we get more conventional and use the new `repeatOnLifecycle` function as described in [this blog post](https://medium.com/androiddevelopers/a-safer-way-to-collect-flows-from-android-uis-23080b1f8bda). The old methods are deprecated and will be deleted soon. Making this change required making our `BackPressHandler` mechanism more robust, as it too was pretending that `OnAttachStateChangeListener` was an adequate lifecycle. Specifically, we were relying on `onViewAttachedToWindow` to be called in a predicable order, which stopped being the case with the move away from `WorkflowLayout.takeWhileAttached`. This PR changes things so that… - We register handlers immediately, so that we can count on them being stacked in the expected order - We lean on `OnAttachStateChangeListener` solely to enable and disable the `BackPressHandler` associated with a view - And take advantage of our recently added support for `ViewTreeLifecycleOwner` for clean up `ModalContainer`'s contribution to coping with the back button also had to be beefed up, for similar reasons. It needs to ensure that back button events are blocked by modal windows, just like any other event, which is tricky with the singleton `OnBackPressedDispatcher` that androidx provides. It does this by putting a no-op handler in place as a backstop for any set by modal renderings. Its bespoke code for doing this was also inadvertently relying on the order of `onViewAttachedToWindow` calls. We now achieve this by wrapping modal renderings with a reliable `BackStackScreen`, which is promoted from the sample container set.
1 parent be479f2 commit 01eb576

File tree

24 files changed

+171
-107
lines changed

24 files changed

+171
-107
lines changed

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@ class HelloBindingActivity : AppCompatActivity() {
3636
val model: HelloBindingModel by viewModels()
3737
setContentView(
3838
WorkflowLayout(this).apply {
39-
start(
40-
renderings = model.renderings,
41-
environment = viewEnvironment
42-
)
39+
start(lifecycle, model.renderings, viewEnvironment)
4340
}
4441
)
4542
}

samples/compose-samples/src/main/java/com/squareup/sample/compose/hellocomposeworkflow/HelloComposeWorkflowActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class HelloComposeWorkflowActivity : AppCompatActivity() {
1717
super.onCreate(savedInstanceState)
1818
val model: HelloComposeModel by viewModels()
1919
setContentView(
20-
WorkflowLayout(this).apply { start(model.renderings) }
20+
WorkflowLayout(this).apply { start(lifecycle, model.renderings) }
2121
)
2222
}
2323

samples/compose-samples/src/main/java/com/squareup/sample/compose/inlinerendering/InlineRenderingActivity.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ class InlineRenderingActivity : AppCompatActivity() {
2121

2222
val model: HelloBindingModel by viewModels()
2323
setContentView(
24-
WorkflowLayout(this).apply {
25-
start(renderings = model.renderings)
26-
}
24+
WorkflowLayout(this).apply { start(lifecycle, model.renderings) }
2725
)
2826
}
2927

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@ class NestedRenderingsActivity : AppCompatActivity() {
3838
val model: NestedRenderingsModel by viewModels()
3939
setContentView(
4040
WorkflowLayout(this).apply {
41-
start(
42-
renderings = model.renderings,
43-
environment = viewEnvironment
44-
)
41+
start(lifecycle, model.renderings, viewEnvironment)
4542
}
4643
)
4744
}

samples/containers/android/src/main/java/com/squareup/sample/container/BackButtonScreen.kt

Lines changed: 0 additions & 23 deletions
This file was deleted.

samples/containers/android/src/main/java/com/squareup/sample/container/BackButtonViewFactory.kt

Lines changed: 0 additions & 31 deletions
This file was deleted.

samples/containers/android/src/main/java/com/squareup/sample/container/SampleContainers.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
88

99
@OptIn(WorkflowUiExperimentalApi::class)
1010
val SampleContainers = ViewRegistry(
11-
BackButtonViewFactory, OverviewDetailContainer, PanelContainer, ScrimContainer
11+
OverviewDetailContainer, PanelContainer, ScrimContainer
1212
)

samples/containers/app-poetry/src/main/java/com/squareup/sample/poetryapp/PoetryActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class PoetryActivity : AppCompatActivity() {
2626

2727
val model: PoetryModel by viewModels()
2828
setContentView(
29-
WorkflowLayout(this).apply { start(model.renderings, viewRegistry) }
29+
WorkflowLayout(this).apply { start(lifecycle, model.renderings, viewRegistry) }
3030
)
3131
}
3232

samples/containers/app-raven/src/main/java/com/squareup/sample/ravenapp/RavenActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class RavenActivity : AppCompatActivity() {
3030

3131
val model: RavenModel by viewModels()
3232
setContentView(
33-
WorkflowLayout(this).apply { start(model.renderings, viewRegistry) }
33+
WorkflowLayout(this).apply { start(lifecycle, model.renderings, viewRegistry) }
3434
)
3535

3636
lifecycleScope.launch {

samples/containers/hello-back-button/src/main/java/com/squareup/sample/hellobackbutton/AreYouSureWorkflow.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.squareup.sample.hellobackbutton
22

33
import android.os.Parcelable
4-
import com.squareup.sample.container.BackButtonScreen
54
import com.squareup.sample.hellobackbutton.AreYouSureWorkflow.Finished
65
import com.squareup.sample.hellobackbutton.AreYouSureWorkflow.State
76
import com.squareup.sample.hellobackbutton.AreYouSureWorkflow.State.Quitting
@@ -10,6 +9,7 @@ import com.squareup.workflow1.Snapshot
109
import com.squareup.workflow1.StatefulWorkflow
1110
import com.squareup.workflow1.WorkflowAction.Companion.noAction
1211
import com.squareup.workflow1.action
12+
import com.squareup.workflow1.ui.BackButtonScreen
1313
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
1414
import com.squareup.workflow1.ui.modal.AlertContainerScreen
1515
import com.squareup.workflow1.ui.modal.AlertScreen

0 commit comments

Comments
 (0)