Skip to content

Commit

Permalink
Merge pull request #1158 from square/ray/tutorial-fileTemplates
Browse files Browse the repository at this point in the history
Fix tutorial fileTemplates
  • Loading branch information
rjrjr authored Jan 19, 2024
2 parents c8a0420 + 207762a commit 8c817e3
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 288 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.

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()
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ 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 workflow.tutorial.views.databinding.WelcomeViewBinding

@OptIn(WorkflowUiExperimentalApi::class)
Expand All @@ -13,6 +16,23 @@ data class WelcomeScreen(
/** Callback when the login button is tapped. */
val onLogInPressed: () -> Unit
) : AndroidScreen<WelcomeScreen> {

override val viewFactory =
ScreenViewFactory.fromViewBinding(WelcomeViewBinding::inflate, ::WelcomeScreenRunner)
}

@OptIn(WorkflowUiExperimentalApi::class)
private class WelcomeScreenRunner(
private val viewBinding: WelcomeViewBinding
) : ScreenViewRunner<WelcomeScreen> {

override fun showRendering(
rendering: WelcomeScreen,
environment: ViewEnvironment
) {
// TextController is a helper provided by the workflow library that takes
// care of the complexity of correctly interacting with EditTexts in a declarative manner.
rendering.username.control(viewBinding.username)
viewBinding.login.setOnClickListener { rendering.onLogInPressed() }
}
}
Loading

0 comments on commit 8c817e3

Please sign in to comment.