Skip to content

Commit 12bfb12

Browse files
authored
Merge pull request #81 from square/rjrjr/props-to-actions
Gives WorkflowAction access to props.
2 parents 77c9d21 + 935265b commit 12bfb12

File tree

67 files changed

+375
-363
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+375
-363
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ object PoemListWorkflow : StatelessWorkflow<List<Poem>, Int, PoemListRendering>(
2727

2828
override fun render(
2929
props: List<Poem>,
30-
context: RenderContext<Nothing, Int>
30+
context: RenderContext<List<Poem>, Nothing, Int>
3131
): PoemListRendering {
3232
// A sink that emits the given index as the result of this workflow.
3333
val sink = context.makeEventSink { index: Int -> setOutput(index) }

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ object PoemsBrowserWorkflow :
4040
override fun render(
4141
props: List<Poem>,
4242
state: SelectedPoem,
43-
context: RenderContext<SelectedPoem, Nothing>
43+
context: RenderContext<List<Poem>, SelectedPoem, Nothing>
4444
): OverviewDetailScreen {
4545
val poems: OverviewDetailScreen =
4646
context.renderChild(PoemListWorkflow, props) { selected -> choosePoem(selected) }

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ object AreYouSureWorkflow : StatefulWorkflow<Unit, State, Finished, AlertContain
5252
override fun render(
5353
props: Unit,
5454
state: State,
55-
context: RenderContext<State, Finished>
55+
context: RenderContext<Unit, State, Finished>
5656
): AlertContainerScreen<*> {
5757
val ableBakerCharlie = context.renderChild(HelloBackButtonWorkflow, Unit) { noAction() }
5858

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ object HelloBackButtonWorkflow : StatefulWorkflow<Unit, State, Nothing, Renderin
4646
override fun render(
4747
props: Unit,
4848
state: State,
49-
context: RenderContext<State, Nothing>
49+
context: RenderContext<Unit, State, Nothing>
5050
): Rendering {
5151
return Rendering(
5252
message = "$state",

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ object PoemWorkflow : StatefulWorkflow<Poem, Int, ClosePoem, OverviewDetailScree
5656
override fun render(
5757
props: Poem,
5858
state: Int,
59-
context: RenderContext<Int, ClosePoem>
59+
context: RenderContext<Poem, Int, ClosePoem>
6060
): OverviewDetailScreen {
6161
val previousStanzas: List<StanzaRendering> =
6262
if (state == -1) emptyList()
@@ -105,7 +105,7 @@ object PoemWorkflow : StatefulWorkflow<Poem, Int, ClosePoem, OverviewDetailScree
105105
sink.writeInt(state)
106106
}
107107

108-
private sealed class Action : WorkflowAction<Int, ClosePoem> {
108+
private sealed class Action : WorkflowAction<Poem, Int, ClosePoem> {
109109
object ClearSelection : Action()
110110
object SelectPrevious : Action()
111111
object SelectNext : Action()

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ object StanzaListWorkflow : StatelessWorkflow<Poem, Int, StanzaListRendering>()
2929

3030
override fun render(
3131
props: Poem,
32-
context: RenderContext<Nothing, Int>
32+
context: RenderContext<Poem, Nothing, Int>
3333
): StanzaListRendering {
3434
// A sink that emits the given index as the result of this workflow.
3535
val sink = context.makeEventSink { index: Int -> setOutput(index) }

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ object StanzaWorkflow : StatelessWorkflow<Props, Output, StanzaRendering>() {
4141

4242
override fun render(
4343
props: Props,
44-
context: RenderContext<Nothing, Output>
44+
context: RenderContext<Props, Nothing, Output>
4545
): StanzaRendering {
4646
with(props) {
4747
val sink: Sink<Output> = context.makeEventSink { setOutput(it) }

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class DungeonAppWorkflow(
5454
override fun render(
5555
props: Props,
5656
state: State,
57-
context: RenderContext<State, Nothing>
57+
context: RenderContext<Props, State, Nothing>
5858
): AlertContainerScreen<Any> = when (state) {
5959

6060
LoadingBoardList -> {

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class GameSessionWorkflow(
6565
override fun render(
6666
props: Props,
6767
state: State,
68-
context: RenderContext<State, Nothing>
68+
context: RenderContext<Props, State, Nothing>
6969
): AlertContainerScreen<Any> = when (state) {
7070

7171
Loading -> {
@@ -98,8 +98,8 @@ class GameSessionWorkflow(
9898

9999
override fun snapshotState(state: State): Snapshot = Snapshot.EMPTY
100100

101-
private class StartRunning(val board: Board) : WorkflowAction<State, Nothing> {
102-
override fun Updater<State, Nothing>.apply() {
101+
private class StartRunning(val board: Board) : WorkflowAction<Props, State, Nothing> {
102+
override fun Updater<Props, State, Nothing>.apply() {
103103
state = Running(board)
104104
}
105105
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class TimeMachineAppWorkflow(
4343

4444
override fun render(
4545
props: BoardPath,
46-
context: RenderContext<Nothing, Nothing>
46+
context: RenderContext<BoardPath, Nothing, Nothing>
4747
): ShakeableTimeMachineRendering {
4848
val propsFactory = PropsFactory { recording ->
4949
Props(paused = !recording)

samples/dungeon/common/src/main/java/com/squareup/sample/dungeon/AiWorkflow.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class AiWorkflow(
6767
override fun render(
6868
props: ActorProps,
6969
state: State,
70-
context: RenderContext<State, Nothing>
70+
context: RenderContext<ActorProps, State, Nothing>
7171
): ActorRendering {
7272
context.runningWorker(state.directionTicker) { updateDirection }
7373

samples/dungeon/common/src/main/java/com/squareup/sample/dungeon/GameWorkflow.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class GameWorkflow(
105105
override fun render(
106106
props: Props,
107107
state: State,
108-
context: RenderContext<State, Output>
108+
context: RenderContext<Props, State, Output>
109109
): GameRendering {
110110
val running = !props.paused && !state.game.isPlayerEaten
111111
// Stop actors from ticking if the game is paused or finished.

samples/dungeon/common/src/main/java/com/squareup/sample/dungeon/PlayerWorkflow.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,16 @@ class PlayerWorkflow(
3535
private val cellsPerSecond: Float = 15f
3636
) : StatefulWorkflow<ActorProps, Movement, Nothing, Rendering>() {
3737

38-
sealed class Action : WorkflowAction<Movement, Nothing> {
38+
sealed class Action : WorkflowAction<ActorProps, Movement, Nothing> {
3939

4040
class StartMoving(private val direction: Direction) : Action() {
41-
override fun Updater<Movement, Nothing>.apply() {
41+
override fun Updater<ActorProps, Movement, Nothing>.apply() {
4242
state += direction
4343
}
4444
}
4545

4646
class StopMoving(private val direction: Direction) : Action() {
47-
override fun Updater<Movement, Nothing>.apply() {
47+
override fun Updater<ActorProps, Movement, Nothing>.apply() {
4848
state -= direction
4949
}
5050
}
@@ -64,7 +64,7 @@ class PlayerWorkflow(
6464
override fun render(
6565
props: ActorProps,
6666
state: Movement,
67-
context: RenderContext<Movement, Nothing>
67+
context: RenderContext<ActorProps, Movement, Nothing>
6868
): Rendering = Rendering(
6969
actorRendering = ActorRendering(avatar = avatar, movement = state),
7070
onStartMoving = { context.actionSink.send(StartMoving(it)) },

samples/dungeon/timemachine-shakeable/src/main/java/com/squareup/sample/timemachine/shakeable/ShakeableTimeMachineWorkflow.kt

+5-5
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class ShakeableTimeMachineWorkflow<in P, O : Any, out R : Any>(
6969
override fun render(
7070
props: PropsFactory<P>,
7171
state: State,
72-
context: RenderContext<State, O>
72+
context: RenderContext<PropsFactory<P>, State, O>
7373
): ShakeableTimeMachineRendering {
7474
// Only listen to shakes when recording.
7575
if (state === Recording) context.runningWorker(shakeWorker) { onShake }
@@ -123,14 +123,14 @@ class ShakeableTimeMachineWorkflow<in P, O : Any, out R : Any>(
123123

124124
private inner class SeekAction(
125125
private val newPosition: Duration
126-
) : WorkflowAction<State, O> {
127-
override fun Updater<State, O>.apply() {
126+
) : WorkflowAction<PropsFactory<P>, State, O> {
127+
override fun Updater<PropsFactory<P>, State, O>.apply() {
128128
state = PlayingBack(newPosition)
129129
}
130130
}
131131

132-
private inner class ResumeRecordingAction : WorkflowAction<State, O> {
133-
override fun Updater<State, O>.apply() {
132+
private inner class ResumeRecordingAction : WorkflowAction<PropsFactory<P>, State, O> {
133+
override fun Updater<PropsFactory<P>, State, O>.apply() {
134134
state = Recording
135135
}
136136
}

samples/dungeon/timemachine/src/main/java/com/squareup/sample/timemachine/RecorderWorkflow.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ internal class RecorderWorkflow<T>(
9292
override fun render(
9393
props: RecorderProps<T>,
9494
state: Recording<T>,
95-
context: RenderContext<Recording<T>, Nothing>
95+
context: RenderContext<RecorderProps<T>, Recording<T>, Nothing>
9696
): TimeMachineRendering<T> {
9797
val value = when (props) {
9898
is RecordValue -> props.value

samples/dungeon/timemachine/src/main/java/com/squareup/sample/timemachine/TimeMachineWorkflow.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import kotlin.time.TimeSource
3838
*
3939
* @param delegateWorkflow The [Workflow] whose renderings to record. This workflow will be rendered
4040
* continuously as long as the `TimeMachineWorkflow` is being rendered.
41-
* @param clock The [Clock] to use to assign timestamps to recorded values.
41+
* @param clock The [TimeSource] to use to assign timestamps to recorded values.
4242
*/
4343
@ExperimentalTime
4444
class TimeMachineWorkflow<P, O : Any, out R>(
@@ -84,7 +84,7 @@ class TimeMachineWorkflow<P, O : Any, out R>(
8484

8585
override fun render(
8686
props: TimeMachineProps<P>,
87-
context: RenderContext<Nothing, O>
87+
context: RenderContext<TimeMachineProps<P>, Nothing, O>
8888
): TimeMachineRendering<R> {
8989
// Always render the delegate, even if in playback mode, to keep it alive.
9090
val delegateRendering =

samples/hello-terminal/hello-terminal-app/src/main/java/com/squareup/sample/helloterminal/BlinkingCursorWorkflow.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class BlinkingCursorWorkflow(
5050
override fun render(
5151
props: Unit,
5252
state: Boolean,
53-
context: RenderContext<Boolean, Nothing>
53+
context: RenderContext<Unit, Boolean, Nothing>
5454
): String {
5555
context.runningWorker(intervalWorker) { setCursorShowing(it) }
5656
return if (state) cursorString else ""

samples/hello-terminal/hello-terminal-app/src/main/java/com/squareup/sample/helloterminal/HelloTerminalWorkflow.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import com.squareup.workflow.WorkflowAction
3030
import com.squareup.workflow.action
3131
import com.squareup.workflow.renderChild
3232

33-
private typealias HelloTerminalAction = WorkflowAction<State, ExitCode>
33+
private typealias HelloTerminalAction = WorkflowAction<TerminalProps, State, ExitCode>
3434

3535
class HelloTerminalWorkflow : TerminalWorkflow,
3636
StatefulWorkflow<TerminalProps, State, ExitCode, TerminalRendering>() {
@@ -52,7 +52,7 @@ class HelloTerminalWorkflow : TerminalWorkflow,
5252
override fun render(
5353
props: TerminalProps,
5454
state: State,
55-
context: RenderContext<State, ExitCode>
55+
context: RenderContext<TerminalProps, State, ExitCode>
5656
): TerminalRendering {
5757
val (rows, columns) = props.size
5858
val header = """

samples/hello-terminal/todo-terminal-app/src/main/java/com/squareup/sample/hellotodo/EditTextWorkflow.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class EditTextWorkflow : StatefulWorkflow<EditTextProps, EditTextState, String,
4747
override fun render(
4848
props: EditTextProps,
4949
state: EditTextState,
50-
context: RenderContext<EditTextState, String>
50+
context: RenderContext<EditTextProps, EditTextState, String>
5151
): String {
5252
context.runningWorker(props.terminalProps.keyStrokes) { key -> onKeystroke(props, key) }
5353

samples/hello-terminal/todo-terminal-app/src/main/java/com/squareup/sample/hellotodo/TodoWorkflow.kt

+5-5
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import com.squareup.workflow.StatefulWorkflow
3232
import com.squareup.workflow.WorkflowAction
3333
import com.squareup.workflow.action
3434

35-
private typealias TodoAction = WorkflowAction<TodoList, Nothing>
35+
private typealias TodoAction = WorkflowAction<TerminalProps, TodoList, Nothing>
3636

3737
class TodoWorkflow : TerminalWorkflow,
3838
StatefulWorkflow<TerminalProps, TodoList, ExitCode, TerminalRendering>() {
@@ -75,14 +75,14 @@ class TodoWorkflow : TerminalWorkflow,
7575
override fun render(
7676
props: TerminalProps,
7777
state: TodoList,
78-
context: RenderContext<TodoList, ExitCode>
78+
context: RenderContext<TerminalProps, TodoList, ExitCode>
7979
): TerminalRendering {
8080

8181
context.runningWorker(props.keyStrokes) { onKeystroke(it) }
8282

8383
return TerminalRendering(buildString {
8484
@Suppress("UNCHECKED_CAST")
85-
appendln(state.renderTitle(props, context as RenderContext<TodoList, Nothing>))
85+
appendln(state.renderTitle(props, context as RenderContext<TerminalProps, TodoList, Nothing>))
8686
appendln(renderSelection(state.titleSeparator, false))
8787
appendln(state.renderItems(props, context))
8888
})
@@ -117,7 +117,7 @@ private fun setLabel(
117117

118118
private fun TodoList.renderTitle(
119119
props: TerminalProps,
120-
context: RenderContext<TodoList, Nothing>
120+
context: RenderContext<TerminalProps, TodoList, Nothing>
121121
): String {
122122
val isSelected = focusedField == TITLE_FIELD_INDEX
123123
val titleString = if (isSelected) {
@@ -136,7 +136,7 @@ private val TodoList.titleSeparator get() = "–".repeat(title.length + 1)
136136

137137
private fun TodoList.renderItems(
138138
props: TerminalProps,
139-
context: RenderContext<TodoList, Nothing>
139+
context: RenderContext<TerminalProps, TodoList, Nothing>
140140
): String =
141141
items
142142
.mapIndexed { index, item ->

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ object HelloWorkflow : StatefulWorkflow<Unit, State, Nothing, Rendering>() {
4545
override fun render(
4646
props: Unit,
4747
state: State,
48-
context: RenderContext<State, Nothing>
48+
context: RenderContext<Unit, State, Nothing>
4949
): Rendering {
5050
return Rendering(
5151
message = state.name,

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ object HelloWorkflow : StatefulWorkflow<Unit, State, Nothing, Rendering>() {
4545
override fun render(
4646
props: Unit,
4747
state: State,
48-
context: RenderContext<State, Nothing>
48+
context: RenderContext<Unit, State, Nothing>
4949
): Rendering {
5050
return Rendering(
5151
message = state.name,

samples/recyclerview/src/main/java/com/squareup/sample/recyclerview/AppWorkflow.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ object AppWorkflow : StatefulWorkflow<Unit, State, Nothing, Rendering>() {
7474
get() = listOfNotNull(popup)
7575
}
7676

77-
private sealed class Action : WorkflowAction<State, Nothing> {
77+
private sealed class Action : WorkflowAction<Unit, State, Nothing> {
7878
object ShowAddRowAction : Action()
7979
data class CommitNewRowAction(val index: Int) : Action()
8080

81-
override fun Updater<State, Nothing>.apply() {
81+
override fun Updater<Unit, State, Nothing>.apply() {
8282
state = when (this@Action) {
8383
ShowAddRowAction -> ChooseNewRow(state.rows)
8484
is CommitNewRowAction -> ShowList(state.rows + allRowTypes[index])
@@ -94,7 +94,7 @@ object AppWorkflow : StatefulWorkflow<Unit, State, Nothing, Rendering>() {
9494
override fun render(
9595
props: Unit,
9696
state: State,
97-
context: RenderContext<State, Nothing>
97+
context: RenderContext<Unit, State, Nothing>
9898
): Rendering {
9999
val listRendering = context.renderChild(EditableListWorkflow, Props(state.rows))
100100
val baseScreen = BaseScreen(

samples/recyclerview/src/main/java/com/squareup/sample/recyclerview/editablelistworkflow/EditableListWorkflow.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ object EditableListWorkflow : StatefulWorkflow<Props, State, Nothing, Rendering>
6161
override fun render(
6262
props: Props,
6363
state: State,
64-
context: RenderContext<State, Nothing>
64+
context: RenderContext<Props, State, Nothing>
6565
): Rendering {
6666
return Rendering(
6767
rowValues = state.rowValues,

samples/tictactoe/common/src/main/java/com/squareup/sample/authworkflow/AuthWorkflow.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ sealed class AuthResult {
6969
object Canceled : AuthResult()
7070
}
7171

72-
internal sealed class Action : WorkflowAction<AuthState, AuthResult> {
72+
internal sealed class Action : WorkflowAction<Unit, AuthState, AuthResult> {
7373
class SubmitLogin(
7474
val email: String,
7575
val password: String
@@ -91,7 +91,7 @@ internal sealed class Action : WorkflowAction<AuthState, AuthResult> {
9191
val response: AuthResponse
9292
) : Action()
9393

94-
final override fun Updater<AuthState, AuthResult>.apply() {
94+
final override fun Updater<Unit, AuthState, AuthResult>.apply() {
9595
when (this@Action) {
9696
is SubmitLogin -> {
9797
state = when {
@@ -146,7 +146,7 @@ class RealAuthWorkflow(private val authService: AuthService) : AuthWorkflow,
146146
override fun render(
147147
props: Unit,
148148
state: AuthState,
149-
context: RenderContext<AuthState, AuthResult>
149+
context: RenderContext<Unit, AuthState, AuthResult>
150150
): BackStackScreen<Any> = when (state) {
151151
is LoginPrompt -> {
152152
BackStackScreen(

samples/tictactoe/common/src/main/java/com/squareup/sample/gameworkflow/RunGameWorkflow.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ typealias RunGameScreen = AlertContainerScreen<PanelContainerScreen<Any, Any>>
6868
*/
6969
typealias RunGameWorkflow = Workflow<Unit, RunGameResult, RunGameScreen>
7070

71-
sealed class Action : WorkflowAction<RunGameState, RunGameResult> {
71+
sealed class Action : WorkflowAction<Unit, RunGameState, RunGameResult> {
7272
object CancelNewGame : Action()
7373

7474
class StartGame(
@@ -99,7 +99,7 @@ sealed class Action : WorkflowAction<RunGameState, RunGameResult> {
9999
// signal that this workflow is too big, and should be refactored into something
100100
// like one workflow per screen.
101101

102-
override fun Updater<RunGameState, RunGameResult>.apply() {
102+
override fun Updater<Unit, RunGameState, RunGameResult>.apply() {
103103

104104
when (this@Action) {
105105
CancelNewGame -> setOutput(CanceledStart)
@@ -178,7 +178,7 @@ class RealRunGameWorkflow(
178178
override fun render(
179179
props: Unit,
180180
state: RunGameState,
181-
context: RenderContext<RunGameState, RunGameResult>
181+
context: RenderContext<Unit, RunGameState, RunGameResult>
182182
): RunGameScreen = when (state) {
183183
is NewGame -> {
184184
val emptyGameScreen = GamePlayScreen()

0 commit comments

Comments
 (0)