Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Tutorial1.md #869

Merged
merged 2 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions samples/tutorial/Tutorial1.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ data class WelcomeScreen(
)
```

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
`. However if you would like to create and lay out the view yourself instead, feel free to do so!
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`. However if you would like to create and lay out the view yourself instead, feel free to do so!

```kotlin
@OptIn(WorkflowUiExperimentalApi::class)
class WelcomeLayoutRunner(
private val welcomeBinding: WelcomeViewBinding
) : LayoutRunner<WelcomeScreen> {
Expand Down Expand Up @@ -168,6 +168,10 @@ dependencies {
We'll update the `TutorialActivity` to set its content using a `ViewRegistry` that points to our `LayoutRunner`'s `ViewFactory`:

```kotlin
@file:OptIn(WorkflowUiExperimentalApi::class)
package workflow.tutorial
// ...

// This doesn't look like much right now, but we'll add more layout runners shortly.
private val viewRegistry = ViewRegistry(WelcomeLayoutRunner)

Expand All @@ -189,9 +193,9 @@ class TutorialActivity : AppCompatActivity() {
class TutorialViewModel(savedState: SavedStateHandle) : ViewModel() {
val renderings: StateFlow<WelcomeScreen> by lazy {
renderWorkflowIn(
workflow = WelcomeWorkflow,
scope = viewModelScope,
savedStateHandle = savedState
workflow = WelcomeWorkflow,
scope = viewModelScope,
savedStateHandle = savedState
)
}
}
Expand Down
Binary file modified samples/tutorial/images/layout-runner-name.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified samples/tutorial/images/workflow-name.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.