Skip to content

Commit 4d8f5bc

Browse files
authoredMar 10, 2025
Merge pull request #1267 from square/ray/remember-event-handler
*BREAKING* Introduces `BaseRenderContext.remember` and stable `eventHandlers`.
2 parents 61b8111 + 60f7ea5 commit 4d8f5bc

File tree

36 files changed

+2532
-790
lines changed

36 files changed

+2532
-790
lines changed
 

‎benchmarks/performance-poetry/complex-poetry/src/main/java/com/squareup/benchmarks/performance/complex/poetry/instrumentation/ActionHandlingTracingInterceptor.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ import com.squareup.workflow1.WorkflowInterceptor.WorkflowSession
1212
* particular events.
1313
*
1414
* If you want to trace how long Workflow takes to process a UI event, then
15-
* annotate the [RenderContext.eventHandler] name argument with [keyForTrace]. That will cause
15+
* annotate the `RenderContext.eventHandler` name argument with [keyForTrace]. That will cause
1616
* this interceptor to pick it up when the action is sent into the sink and trace that main thread
1717
* message.
1818
*
19-
* If you want to trace how long Workflow takes to process the result of a [Worker], then
20-
* annotate the [Worker] using [TraceableWorker] which will set it up with a key such that when
19+
* If you want to trace how long Workflow takes to process the result of a `Worker`, then
20+
* annotate the `Worker` using [TraceableWorker] which will set it up with a key such that when
2121
* the action for the result is sent to the sink the main thread message will be traced.
2222
*/
2323
class ActionHandlingTracingInterceptor : WorkflowInterceptor, Resettable {

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

+11-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
package com.squareup.sample.compose.inlinerendering
44

55
import androidx.compose.animation.AnimatedContent
6-
import androidx.compose.animation.ExperimentalAnimationApi
76
import androidx.compose.animation.SizeTransform
87
import androidx.compose.animation.fadeIn
98
import androidx.compose.animation.fadeOut
@@ -38,12 +37,15 @@ object InlineRenderingWorkflow : StatefulWorkflow<Unit, Int, Nothing, Screen>()
3837
renderProps: Unit,
3938
renderState: Int,
4039
context: RenderContext
41-
) = ComposeScreen {
42-
Box {
43-
Button(onClick = context.eventHandler("increment") { state += 1 }) {
44-
Text("Counter: ")
45-
AnimatedCounter(renderState) { counterValue ->
46-
Text(counterValue.toString())
40+
): ComposeScreen {
41+
val onClick = context.eventHandler("increment") { state += 1 }
42+
return ComposeScreen {
43+
Box {
44+
Button(onClick = onClick) {
45+
Text("Counter: ")
46+
AnimatedCounter(renderState) { counterValue ->
47+
Text(counterValue.toString())
48+
}
4749
}
4850
}
4951
}
@@ -68,7 +70,6 @@ internal fun InlineRenderingWorkflowPreview() {
6870
InlineRenderingWorkflowRendering()
6971
}
7072

71-
@OptIn(ExperimentalAnimationApi::class)
7273
@Composable
7374
private fun AnimatedCounter(
7475
counterValue: Int,
@@ -79,6 +80,7 @@ private fun AnimatedCounter(
7980
transitionSpec = {
8081
((slideInVertically() + fadeIn()).togetherWith(slideOutVertically() + fadeOut()))
8182
.using(SizeTransform(clip = false))
82-
}
83+
},
84+
label = ""
8385
) { content(it) }
8486
}

‎samples/containers/poetry/src/main/java/com/squareup/sample/poetry/StanzaListWorkflow.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ object StanzaListWorkflow : StatelessWorkflow<Props, SelectedStanza, StanzaListS
1515

1616
data class Props(
1717
val poem: Poem,
18-
val eventHandlerTag: (String) -> String = { "" }
18+
val eventHandlerTag: (String) -> String = { it }
1919
)
2020

2121
const val NO_SELECTED_STANZA = -1

‎samples/containers/poetry/src/main/java/com/squareup/sample/poetry/StanzaWorkflow.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ object StanzaWorkflow : StatelessWorkflow<Props, Output, StanzaScreen>() {
1212
data class Props(
1313
val poem: Poem,
1414
val index: Int,
15-
val eventHandlerTag: (String) -> String = { "" }
15+
val eventHandlerTag: (String) -> String = { it }
1616
)
1717

1818
enum class Output {

‎samples/dungeon/timemachine/src/test/java/com/squareup/sample/timemachine/TimeMachineWorkflowTest.kt

+4-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ class TimeMachineWorkflowTest {
2525
val delegateWorkflow = Workflow.stateful<String, Nothing, DelegateRendering>(
2626
initialState = "initial",
2727
render = { renderState ->
28-
DelegateRendering(renderState, setState = eventHandler("") { s -> state = s })
28+
DelegateRendering(
29+
renderState,
30+
setState = eventHandler("setState") { s -> state = s }
31+
)
2932
}
3033
)
3134
val clock = TestTimeSource()

‎samples/nested-overlays/src/main/java/com/squareup/sample/nestedoverlays/NestedOverlaysWorkflow.kt

+37-11
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ object NestedOverlaysWorkflow : StatefulWorkflow<Unit, State, Nothing, Screen>()
6262
name = R.string.close,
6363
onClick = closeOuter
6464
),
65-
context.toggleInnerSheetButton(renderState),
65+
context.toggleInnerSheetButton(name = "inner", renderState),
6666
color = android.R.color.holo_green_light,
6767
showEditText = true,
6868
),
@@ -103,33 +103,59 @@ object NestedOverlaysWorkflow : StatefulWorkflow<Unit, State, Nothing, Screen>()
103103
name = "outer",
104104
overlays = listOfNotNull(outerSheet),
105105
body = TopAndBottomBarsScreen(
106-
topBar = if (!renderState.showTopBar) null else context.topBottomBar(renderState),
106+
topBar = if (!renderState.showTopBar) {
107+
null
108+
} else {
109+
context.topBottomBar(
110+
top = true,
111+
renderState
112+
)
113+
},
107114
content = BodyAndOverlaysScreen(
108115
name = "inner",
109116
body = bodyBarButtons,
110117
overlays = listOfNotNull(innerSheet)
111118
),
112-
bottomBar = if (!renderState.showBottomBar) null else context.topBottomBar(renderState)
119+
bottomBar = if (!renderState.showBottomBar) {
120+
null
121+
} else {
122+
context.topBottomBar(
123+
top = false,
124+
renderState
125+
)
126+
}
113127
)
114128
)
115129
}
116130

117131
override fun snapshotState(state: State) = null
118132

119133
private fun RenderContext.topBottomBar(
134+
top: Boolean,
120135
renderState: State
121-
) = ButtonBar(
122-
toggleInnerSheetButton(renderState),
123-
Button(
124-
name = R.string.cover_all,
125-
onClick = eventHandler("cover everything") { state = state.copy(showOuterSheet = true) }
136+
): ButtonBar {
137+
val name = if (top) "top" else "bottom"
138+
return ButtonBar(
139+
toggleInnerSheetButton(
140+
name = name,
141+
renderState = renderState,
142+
),
143+
Button(
144+
name = R.string.cover_all,
145+
onClick = eventHandler("$name cover everything") {
146+
state = state.copy(showOuterSheet = true)
147+
}
148+
)
126149
)
127-
)
150+
}
128151

129-
private fun RenderContext.toggleInnerSheetButton(renderState: State) =
152+
private fun RenderContext.toggleInnerSheetButton(
153+
name: String,
154+
renderState: State
155+
) =
130156
Button(
131157
name = if (renderState.showInnerSheet) R.string.reveal_body else R.string.cover_body,
132-
onClick = eventHandler("reveal / cover body") {
158+
onClick = eventHandler("$name: reveal / cover body") {
133159
state = state.copy(showInnerSheet = !state.showInnerSheet)
134160
}
135161
)

0 commit comments

Comments
 (0)
Please sign in to comment.