Skip to content

Commit 69e4c2c

Browse files
Merge pull request #393 from tir38/tir38/update-templates-in-tutorial1
Update Tutorial1 after changes to templates
2 parents a71d61b + 825a849 commit 69e4c2c

File tree

4 files changed

+54
-7
lines changed

4 files changed

+54
-7
lines changed

samples/tutorial/Tutorial1.md

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,63 @@ Start by creating a new workflow and screen by creating a new file with the [fil
2323
![New Workflow](images/new-workflow.png)
2424
![Workflow Name](images/workflow-name.png)
2525

26-
Follow the same steps using the `Layout Runner (ViewBinding)` template.
26+
The template does not create everything needed. Manually add objects for `State` and `Output`. We'll define `WelcomeScreen` in a moment:
27+
28+
```kotlin
29+
object WelcomeWorkflow : StatefulWorkflow<Unit, State, Output, WelcomeScreen>() {
30+
31+
object State
32+
object Output
33+
34+
override fun initialState(
35+
props: Unit,
36+
snapshot: Snapshot?
37+
): State = TODO("Initialize state")
38+
39+
override fun render(
40+
props: Unit,
41+
state: State,
42+
context: RenderContext
43+
): WelcomeScreen {
44+
TODO("Render")
45+
}
46+
47+
override fun snapshotState(state: State): Snapshot? = Snapshot.write {
48+
TODO("Save state")
49+
}
50+
}
51+
```
52+
53+
Use the `Layout Runner (ViewBinding)` template to create a second file.
54+
55+
![Layout Runner Name](images/layout-runner-name.png)
56+
57+
```kotlin
58+
@OptIn(WorkflowUiExperimentalApi::class)
59+
class WelcomeLayoutRunner(
60+
private val binding: WelcomeViewBinding
61+
) : LayoutRunner<WelcomeScreen> {
62+
63+
override fun showRendering(
64+
rendering: WelcomeScreen,
65+
viewEnvironment: ViewEnvironment
66+
) {
67+
TODO("Update ViewBinding from rendering")
68+
}
69+
70+
companion object : ViewFactory<WelcomeScreen> by bind(
71+
WelcomeViewBinding::inflate, ::WelcomeLayoutRunner
72+
)
73+
}
74+
```
2775

2876
### Screens, View Factories, and Layout Runners
2977

3078
Let's start with what a "screen" is, and how it relates to views.
3179

3280
"Screen" is just the term we use to refer to a value type that represents the view model for a logical screen. Sometimes we'll even use the terms "screen" and "view model" interchangeably. It has no special type. Typically, a screen will be used as the rendering type for the workflow that is responsible for that screen. A screen is usually a data class, since that's the easiest way to make value type-like classes in Kotlin.
3381

34-
For our welcome screen, we'll define what it needs for a backing view model:
82+
For our welcome screen, we'll create a new class and define what it needs for a backing view model:
3583
```kotlin
3684
data class WelcomeScreen(
3785
/** The current name that has been entered. */
@@ -43,7 +91,8 @@ data class WelcomeScreen(
4391
)
4492
```
4593

46-
Then we need to create a `ViewFactory` that knows how to create an Android `View` to draw the actual screen. The easiest way to create a `ViewFactory` is to create a layout runner. A layout runner is a class that has a reference to the view and knows how to update the view given an instance of a screen. In a typical app, every screen will have a layout runner. Layout runners can also work with AndroidX `ViewBinding`s, which we'll use to define the `WelcomeLayoutRunner`. We have a pre-built `WelcomeViewBinding` that you can use. This binding will be autogenerated from layout files in `tutorials-views` when you first build the app. However if you would like to create and lay out the view yourself instead, feel free to do so!
94+
Then we need to create a `ViewFactory` that knows how to create an Android `View` to draw the actual screen. The easiest way to create a `ViewFactory` is to create a layout runner. A layout runner is a class that has a reference to the view and knows how to update the view given an instance of a screen. In a typical app, every screen will have a layout runner. Layout runners can also work with AndroidX `ViewBinding`s, which we'll use to define the `WelcomeLayoutRunner`. We have a pre-built `WelcomeViewBinding` that you can use. This binding will be autogenerated from layout files in `tutorials-views` when you first build the app. If Android Studio does not automatically find the file, you can manually import it `import workflow.tutorial.views.databinding.WelcomeViewBinding
95+
`. However if you would like to create and lay out the view yourself instead, feel free to do so!
4796

4897
```kotlin
4998
class WelcomeLayoutRunner(
@@ -87,8 +136,6 @@ object WelcomeWorkflow : StatefulWorkflow<Unit, State, Output, WelcomeScreen>()
87136

88137
//
89138

90-
object Output
91-
92139
override fun render(
93140
renderProps: Unit,
94141
renderState: State,
@@ -141,7 +188,7 @@ Right now, the workflow isn't handling any of the events from the UI. Let's upda
141188

142189
All workflows have a `State` type that represents the internal state of the workflow. This should be all of the data for which *this* workflow is _responsible_. It usually corresponds to the state for the UI.
143190

144-
Let's model the first part of state that we want to track: the login `username`. Update the `State` type to include a username property. We will also need to update `initialState` to give an initial value:
191+
Let's model the first part of state that we want to track: the login `username`. Update the `State` to a `data class` and include a username property. We will also need to update `initialState` to give an initial value:
145192

146193
```kotlin
147194
object WelcomeWorkflow : StatefulWorkflow<Unit, State, Output, WelcomeScreen>() {

samples/tutorial/Tutorial2.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ Workflows can only communicate with each other through their "properties" as inp
304304

305305
Our welcome workflow has a login button that doesn't do anything, and we'll now handle it and let our parent know that we've "logged in" so it can navigate to another screen.
306306

307-
Add an action for `onLogin` and define our `OutputT` type as a new `data class LoggedIn` to be able to message our parent:
307+
Add an action for `onLogin` and change our `OutputT` type from `Output` to a new `data class LoggedIn` to be able to message our parent:
308308

309309
```kotlin
310310

62.1 KB
Loading
-29.8 KB
Loading

0 commit comments

Comments
 (0)