Skip to content

Commit fe2ac61

Browse files
committed
Add Tutorial 4
1 parent 62b9d86 commit fe2ac61

File tree

5 files changed

+143
-84
lines changed

5 files changed

+143
-84
lines changed

samples/tutorial/tutorial-base/src/main/java/workflow/tutorial/RootWorkflow.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import workflow.tutorial.RootWorkflow.State
1212
import workflow.tutorial.RootWorkflow.State.Todo
1313
import workflow.tutorial.RootWorkflow.State.Welcome
1414
import workflow.tutorial.TodoListWorkflow.ListProps
15+
import workflow.tutorial.TodoWorkflow.TodoProps
1516

1617
@OptIn(WorkflowUiExperimentalApi::class)
1718
object RootWorkflow : StatefulWorkflow<Unit, State, Nothing, BackStackScreen<Screen>>() {
@@ -43,7 +44,7 @@ object RootWorkflow : StatefulWorkflow<Unit, State, Nothing, BackStackScreen<Scr
4344
when(renderState) {
4445
is Welcome -> {}
4546
is Todo -> {
46-
val todoScreens = context.renderChild(child = TodoListWorkflow, ListProps(username = renderState.username)) {
47+
val todoScreens = context.renderChild(child = TodoWorkflow, TodoProps(username = renderState.username)) {
4748
logout()
4849
}
4950
backStackScreens.addAll(todoScreens)

samples/tutorial/tutorial-base/src/main/java/workflow/tutorial/TodoEditWorkflow.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import workflow.tutorial.TodoEditWorkflow.Output
99
import workflow.tutorial.TodoEditWorkflow.Output.Discard
1010
import workflow.tutorial.TodoEditWorkflow.Output.Save
1111
import workflow.tutorial.TodoEditWorkflow.State
12-
import workflow.tutorial.TodoListWorkflow.TodoModel
1312

1413
object TodoEditWorkflow : StatefulWorkflow<EditProps, State, Output, TodoEditScreen>() {
1514

samples/tutorial/tutorial-base/src/main/java/workflow/tutorial/TodoListWorkflow.kt

Lines changed: 21 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -2,105 +2,44 @@ package workflow.tutorial
22

33
import com.squareup.workflow1.Snapshot
44
import com.squareup.workflow1.StatefulWorkflow
5+
import com.squareup.workflow1.StatelessWorkflow
56
import com.squareup.workflow1.action
6-
import com.squareup.workflow1.ui.Screen
77
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
8-
import workflow.tutorial.TodoEditWorkflow.EditProps
9-
import workflow.tutorial.TodoEditWorkflow.Output.Discard
10-
import workflow.tutorial.TodoEditWorkflow.Output.Save
11-
import workflow.tutorial.TodoListWorkflow.Back
128
import workflow.tutorial.TodoListWorkflow.ListProps
13-
import workflow.tutorial.TodoListWorkflow.State
14-
import workflow.tutorial.TodoListWorkflow.State.Step
9+
import workflow.tutorial.TodoListWorkflow.Output
1510

1611
@OptIn(WorkflowUiExperimentalApi::class)
17-
object TodoListWorkflow : StatefulWorkflow<ListProps, State, Back, List<Screen>>() {
12+
object TodoListWorkflow : StatelessWorkflow<ListProps, Output, TodoListScreen>() {
1813

19-
object Back
20-
data class ListProps(val username: String)
21-
data class TodoModel(
22-
val title: String,
23-
val note: String
14+
data class ListProps(
15+
val username: String,
16+
val todos: List<TodoModel>
2417
)
25-
data class State(
26-
val todos: List<TodoModel>,
27-
val step: Step
28-
) {
29-
sealed class Step {
30-
object List: Step()
3118

32-
data class Edit(val index: Int) : Step()
33-
}
19+
sealed class Output {
20+
object Back : Output()
21+
data class SelectTodo(val index: Int) : Output()
3422
}
3523

36-
override fun initialState(
37-
props: ListProps,
38-
snapshot: Snapshot?
39-
): State = State(
40-
todos = listOf(
41-
TodoModel(
42-
title = "Take the cat for a walk",
43-
note = "Cats really need their outside sunshine time. Don't forget to walk " +
44-
"Charlie. Hamilton is less excited about the prospect.",
45-
)
46-
),
47-
step = Step.List
48-
)
49-
50-
override fun render(
51-
renderProps: ListProps,
52-
renderState: State,
53-
context: RenderContext
54-
): List<Screen> {
55-
val titles = renderState.todos.map { it.title }
56-
val todoListScreen = TodoListScreen(
57-
username = renderProps.username,
58-
todoTitles = titles,
59-
onTodoSelected = {context.actionSink.send(selectTodo(it))},
60-
onBack = { context.actionSink.send(onBack()) }
61-
)
62-
63-
return when(val step = renderState.step) {
64-
Step.List -> listOf(todoListScreen)
65-
is Step.Edit -> {
66-
val todoEditScreen = context.renderChild(
67-
TodoEditWorkflow,
68-
props = EditProps(renderState.todos[step.index])
69-
) { output ->
70-
when(output) {
71-
Discard -> discardChanges()
72-
is Save -> saveChanges(output.todo, step.index)
73-
}
74-
}
75-
return listOf(todoListScreen, todoEditScreen)
76-
}
77-
}
78-
79-
}
8024

8125
private fun onBack() = action {
82-
setOutput(Back)
26+
setOutput(Output.Back)
8327
}
8428

8529
private fun selectTodo(index: Int) = action {
86-
state = state.copy(step = Step.Edit(index))
87-
}
88-
89-
private fun discardChanges() = action {
90-
// When a discard action is received, return to the list.
91-
state = state.copy(step = Step.List)
30+
setOutput(Output.SelectTodo(index))
9231
}
9332

94-
private fun saveChanges(
95-
todo: TodoModel,
96-
index: Int
97-
) = action {
98-
// When changes are saved, update the state of that todo item and return to the list.
99-
state = state.copy(
100-
todos = state.todos.toMutableList().also { it[index] = todo },
101-
step = Step.List
33+
override fun render(
34+
renderProps: ListProps,
35+
context: RenderContext
36+
): TodoListScreen {
37+
val titles = renderProps.todos.map { it.title }
38+
return TodoListScreen(
39+
username = renderProps.username,
40+
todoTitles = titles,
41+
onTodoSelected = { context.actionSink.send(selectTodo(it)) },
42+
onBack = { context.actionSink.send(onBack()) }
10243
)
10344
}
104-
105-
override fun snapshotState(state: State): Snapshot? = null
10645
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package workflow.tutorial
2+
3+
data class TodoModel(
4+
val title: String,
5+
val note: String
6+
)
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package workflow.tutorial
2+
3+
import com.squareup.workflow1.Snapshot
4+
import com.squareup.workflow1.StatefulWorkflow
5+
import com.squareup.workflow1.action
6+
import com.squareup.workflow1.ui.Screen
7+
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
8+
import workflow.tutorial.TodoEditWorkflow.EditProps
9+
import workflow.tutorial.TodoEditWorkflow.Output.Discard
10+
import workflow.tutorial.TodoEditWorkflow.Output.Save
11+
import workflow.tutorial.TodoListWorkflow.ListProps
12+
import workflow.tutorial.TodoListWorkflow.Output
13+
import workflow.tutorial.TodoListWorkflow.Output.SelectTodo
14+
import workflow.tutorial.TodoWorkflow.Back
15+
import workflow.tutorial.TodoWorkflow.State
16+
import workflow.tutorial.TodoWorkflow.State.Step
17+
import workflow.tutorial.TodoWorkflow.TodoProps
18+
19+
@OptIn(WorkflowUiExperimentalApi::class)
20+
object TodoWorkflow : StatefulWorkflow<TodoProps, State, Back, List<Screen>>() {
21+
22+
data class TodoProps(val username: String)
23+
object Back
24+
data class State(
25+
val todos: List<TodoModel>,
26+
val step: Step
27+
) {
28+
sealed class Step {
29+
object List: Step()
30+
31+
data class Edit(val index: Int) : Step()
32+
}
33+
}
34+
35+
override fun initialState(
36+
props: TodoProps,
37+
snapshot: Snapshot?
38+
): State = State(
39+
todos = listOf(
40+
TodoModel(
41+
title = "Take the cat for a walk",
42+
note = "Cats really need their outside sunshine time. Don't forget to walk " +
43+
"Charlie. Hamilton is less excited about the prospect.",
44+
)
45+
),
46+
step = Step.List
47+
)
48+
49+
override fun render(
50+
renderProps: TodoProps,
51+
renderState: State,
52+
context: RenderContext
53+
): List<Screen> {
54+
val todoListScreen = context.renderChild(
55+
TodoListWorkflow,
56+
props = ListProps(
57+
username = renderProps.username,
58+
todos = renderState.todos
59+
)
60+
) { output ->
61+
when (output) {
62+
Output.Back -> onBack()
63+
is SelectTodo -> editTodo(output.index)
64+
}
65+
}
66+
67+
return when (val step = renderState.step) {
68+
// On the "list" step, return just the list screen.
69+
Step.List -> listOf(todoListScreen)
70+
is Step.Edit -> {
71+
// On the "edit" step, return both the list and edit screens.
72+
val todoEditScreen = context.renderChild(
73+
TodoEditWorkflow,
74+
EditProps(renderState.todos[step.index])
75+
) { output ->
76+
when (output) {
77+
// Send the discardChanges action when the discard output is received.
78+
Discard -> discardChanges()
79+
// Send the saveChanges action when the save output is received.
80+
is Save -> saveChanges(output.todo, step.index)
81+
}
82+
}
83+
return listOf(todoListScreen, todoEditScreen)
84+
}
85+
}
86+
}
87+
88+
private fun discardChanges() = action {
89+
// When a discard action is received, return to the list.
90+
state = state.copy(step = Step.List)
91+
}
92+
93+
private fun saveChanges(
94+
todo: TodoModel,
95+
index: Int
96+
) = action {
97+
// When changes are saved, update the state of that todo item and return to the list.
98+
state = state.copy(
99+
todos = state.todos.toMutableList().also { it[index] = todo },
100+
step = Step.List
101+
)
102+
}
103+
104+
private fun onBack() = action {
105+
// When an onBack action is received, emit a Back output.
106+
setOutput(Back)
107+
}
108+
109+
private fun editTodo(index: Int) = action {
110+
// When a todo item is selected, edit it.
111+
state = state.copy(step = Step.Edit(index))
112+
}
113+
override fun snapshotState(state: State): Snapshot? = null
114+
}

0 commit comments

Comments
 (0)