Skip to content

Commit

Permalink
Fix tutorial fileTemplates
Browse files Browse the repository at this point in the history
Our templates were out of date, and our `install.sh` script doesn't work any more.
Replaces it with an IDE-produced zip file that the IDE is willing to import.

Replaces `Layout Runner (ViewBinding)` with `Android Screen (ViewBinding)`:

```kotlin
package ${PACKAGE_NAME}

import com.squareup.workflow1.ui.AndroidScreen
import com.squareup.workflow1.ui.ScreenViewFactory
import com.squareup.workflow1.ui.ScreenViewFactory.Companion.fromViewBinding
import com.squareup.workflow1.ui.ScreenViewRunner
import com.squareup.workflow1.ui.ViewEnvironment
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi

@OptIn(WorkflowUiExperimentalApi::class)
data class $Name(
  // TODO: add properties needed to update $VIEW_BINDING_TYPE
) : AndroidScreen<$Name> {

  override val viewFactory: ScreenViewFactory<$Name> =
    fromViewBinding($VIEW_BINDING_TYPE::inflate, ::${Name}Runner)
}

@OptIn(WorkflowUiExperimentalApi::class)
private class ${Name}Runner(
  private val viewBinding: $VIEW_BINDING_TYPE
) : ScreenViewRunner<$Name> {

  override fun showRendering(
    rendering: $Name,
    environment: ViewEnvironment
  ) {
    TODO("Update viewBinding from rendering")
  }
}
```
  • Loading branch information
rjrjr committed Jan 19, 2024
1 parent 53668f3 commit 244ae9a
Show file tree
Hide file tree
Showing 14 changed files with 127 additions and 294 deletions.
8 changes: 8 additions & 0 deletions README-templates.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# File Templates

`workflow-file-templates.zip` can be imported into Android Studio / IntelliJ IDEA to add a few Workflow-specific file templates, via _File > Manage IDE Settings > Import Settings…_.

To update the templates:

* edit them in the IDE (_Settings > Editor > File and Code Templates_)
* export them (_File > Manage IDE Settings > Import Settings…_), taking care to clear every checkbox except that for File Templates.
27 changes: 0 additions & 27 deletions fileTemplates/Layout Runner (ViewBinding).kt

This file was deleted.

87 changes: 0 additions & 87 deletions fileTemplates/Stateful Workflow.kt

This file was deleted.

61 changes: 0 additions & 61 deletions fileTemplates/Stateless Workflow.kt

This file was deleted.

27 changes: 0 additions & 27 deletions install-templates.sh

This file was deleted.

7 changes: 3 additions & 4 deletions samples/tutorial/Tutorial1.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

_Let's get something on the screen..._

_Note that this tutorial differs slightly from the latest [documentation](https://square.github.io/workflow/). However the core concepts are the same, you can easily refactor your completed tutorial to reflect the latest API if you wish._

## Setup

To follow this tutorial, launch Android Studio and open this folder (`samples/tutorial`).
Expand All @@ -26,7 +24,8 @@ ___Note___

_Windows OS users: Please add the scripts from [file templates folder](../../fileTemplates) directly to Android Studio or Intellij Idea._

_Go to Settings --> Editor --> File and Code Templates --> Select Files Tab. Now click on the '+' icon and copy the the content of the Workflow file template to the editor and save it._
_Go to Settings --> Editor --> File and Code Templates --> Select Files Tab.
Now click on the '+' icon and copy the the content of the Workflow file template to the editor and save it._

![New Workflow](images/new-workflow.png)
![Workflow Name](images/workflow-name.png)
Expand Down Expand Up @@ -58,7 +57,7 @@ object WelcomeWorkflow : StatefulWorkflow<Unit, State, Output, WelcomeScreen>()
}
```

Use the `Layout Runner (ViewBinding)` template to create a second file.
Use the `Screen View Runner (ViewBinding)` template to create a second file.

![Layout Runner Name](images/layout-runner-name.png)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package workflow.tutorial

import com.squareup.workflow1.ui.LayoutRunner
import com.squareup.workflow1.ui.LayoutRunner.Companion.bind
import com.squareup.workflow1.ui.ViewEnvironment
import com.squareup.workflow1.ui.ViewFactory
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
import com.squareup.workflow1.ui.setTextChangedListener
import com.squareup.workflow1.ui.updateText
import workflow.tutorial.views.databinding.WelcomeViewBinding

@OptIn(WorkflowUiExperimentalApi::class)
class WelcomeScreenRunner(
private val welcomeBinding: WelcomeViewBinding
) : LayoutRunner<WelcomeScreen> {

override fun showRendering(
rendering: WelcomeScreen,
viewEnvironment: ViewEnvironment
) {
// updateText and setTextChangedListener are helpers provided by the workflow library that take
// care of the complexity of correctly interacting with EditTexts in a declarative manner.
welcomeBinding.username.updateText(rendering.username)
welcomeBinding.username.setTextChangedListener {
rendering.onUsernameChanged(it.toString())
}
welcomeBinding.login.setOnClickListener { rendering.onLoginTapped() }
}

/**
* Define a [ViewFactory] that will inflate an instance of [WelcomeViewBinding] and an instance
* of [WelcomeScreenRunner] when asked, then wire them up so that [showRendering] will be called
* whenever the workflow emits a new [WelcomeScreen].
*/
companion object : ViewFactory<WelcomeScreen> by bind(
WelcomeViewBinding::inflate, ::WelcomeScreenRunner
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package workflow.tutorial

import com.squareup.workflow1.ui.AndroidScreen
import com.squareup.workflow1.ui.ScreenViewFactory
import com.squareup.workflow1.ui.ScreenViewRunner
import com.squareup.workflow1.ui.TextController
import com.squareup.workflow1.ui.ViewEnvironment
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
import com.squareup.workflow1.ui.control
import com.squareup.workflow1.ui.navigation.setBackHandler
import workflow.tutorial.views.databinding.TodoEditViewBinding

@OptIn(WorkflowUiExperimentalApi::class)
Expand All @@ -19,3 +23,19 @@ data class TodoEditScreen(
override val viewFactory =
ScreenViewFactory.fromViewBinding(TodoEditViewBinding::inflate, ::TodoEditScreenRunner)
}

@OptIn(WorkflowUiExperimentalApi::class)
private class TodoEditScreenRunner(
private val binding: TodoEditViewBinding
) : ScreenViewRunner<TodoEditScreen> {

override fun showRendering(
rendering: TodoEditScreen,
environment: ViewEnvironment
) {
binding.root.setBackHandler(rendering.onBackPressed)
binding.save.setOnClickListener { rendering.onSavePressed() }
rendering.title.control(binding.todoTitle)
rendering.note.control(binding.todoNote)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package workflow.tutorial

import androidx.recyclerview.widget.LinearLayoutManager
import com.squareup.workflow1.ui.AndroidScreen
import com.squareup.workflow1.ui.ScreenViewFactory
import com.squareup.workflow1.ui.ScreenViewRunner
import com.squareup.workflow1.ui.ViewEnvironment
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
import com.squareup.workflow1.ui.navigation.setBackHandler
import workflow.tutorial.views.TodoListAdapter
import workflow.tutorial.views.databinding.TodoListViewBinding

/**
Expand All @@ -22,3 +27,33 @@ data class TodoListScreen(
override val viewFactory =
ScreenViewFactory.fromViewBinding(TodoListViewBinding::inflate, ::TodoListScreenRunner)
}

@OptIn(WorkflowUiExperimentalApi::class)
private class TodoListScreenRunner(
private val todoListBinding: TodoListViewBinding
) : ScreenViewRunner<TodoListScreen> {

private val adapter = TodoListAdapter()

init {
todoListBinding.todoList.layoutManager = LinearLayoutManager(todoListBinding.root.context)
todoListBinding.todoList.adapter = adapter
}

override fun showRendering(
rendering: TodoListScreen,
environment: ViewEnvironment
) {
todoListBinding.root.setBackHandler(rendering.onBackPressed)
todoListBinding.add.setOnClickListener { rendering.onAddPressed() }

with(todoListBinding.todoListWelcome) {
text =
resources.getString(workflow.tutorial.views.R.string.todo_list_welcome, rendering.username)
}

adapter.todoList = rendering.todoTitles
adapter.onTodoSelected = rendering.onRowPressed
adapter.notifyDataSetChanged()
}
}
Loading

0 comments on commit 244ae9a

Please sign in to comment.