Skip to content

Commit 705d753

Browse files
committed
WorkflowLayout.start now requires a Lifecycle parameter.
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 `View.onAttachedToWindow`. 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.
1 parent c84b71a commit 705d753

File tree

15 files changed

+67
-31
lines changed

15 files changed

+67
-31
lines changed

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,19 @@ class HelloBindingActivity : AppCompatActivity() {
3535

3636
val model: HelloBindingModel by viewModels()
3737
setContentView(
38-
WorkflowLayout(this).apply {
39-
start(
40-
renderings = model.renderings,
41-
environment = viewEnvironment
42-
)
43-
}
38+
WorkflowLayout(this).apply {
39+
start(lifecycle, model.renderings, viewEnvironment)
40+
}
4441
)
4542
}
4643

4744
class HelloBindingModel(savedState: SavedStateHandle) : ViewModel() {
4845
@OptIn(WorkflowUiExperimentalApi::class)
4946
val renderings: StateFlow<Any> by lazy {
5047
renderWorkflowIn(
51-
workflow = HelloWorkflow,
52-
scope = viewModelScope,
53-
savedStateHandle = savedState
48+
workflow = HelloWorkflow,
49+
scope = viewModelScope,
50+
savedStateHandle = savedState
5451
)
5552
}
5653
}

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/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/HelloBackButtonActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class HelloBackButtonActivity : AppCompatActivity() {
2727

2828
val model: HelloBackButtonModel by viewModels()
2929
setContentView(
30-
WorkflowLayout(this).apply { start(model.renderings, viewRegistry) }
30+
WorkflowLayout(this).apply { start(lifecycle, model.renderings, viewRegistry) }
3131
)
3232

3333
lifecycleScope.launch {

samples/dungeon/app/src/main/java/com/squareup/sample/dungeon/DungeonActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class DungeonActivity : AppCompatActivity() {
1717
val model: TimeMachineModel by viewModels { component.timeMachineModelFactory }
1818

1919
setContentView(
20-
WorkflowLayout(this).apply { start(model.renderings, component.viewRegistry) }
20+
WorkflowLayout(this).apply { start(lifecycle, model.renderings, component.viewRegistry) }
2121
)
2222
}
2323
}

samples/hello-workflow-fragment/src/main/java/com/squareup/sample/helloworkflowfragment/HelloWorkflowFragment.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ class HelloWorkflowFragment : Fragment() {
2323
// This ViewModel will survive configuration changes. It's instantiated
2424
// by the first call to ViewModelProvider.get(), and that original instance is returned by
2525
// succeeding calls, until this Fragment session ends.
26-
val model: HelloViewModel = ViewModelProvider(this).get(HelloViewModel::class.java)
26+
val model: HelloViewModel = ViewModelProvider(this)[HelloViewModel::class.java]
2727

2828
return WorkflowLayout(inflater.context).apply {
29-
start(model.renderings)
29+
start(lifecycle, model.renderings)
3030
}
3131
}
3232
}

samples/hello-workflow/src/main/java/com/squareup/sample/helloworkflow/HelloWorkflowActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class HelloWorkflowActivity : AppCompatActivity() {
2121
// succeeding calls.
2222
val model: HelloViewModel by viewModels()
2323
setContentView(
24-
WorkflowLayout(this).apply { start(model.renderings) }
24+
WorkflowLayout(this).apply { start(lifecycle, model.renderings) }
2525
)
2626
}
2727
}

0 commit comments

Comments
 (0)