-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It's time to get the Tutorial caught up with undeprecated API. This PR modifies only the final tutorial module. Once we're happy with the code, I'll back port the changes and update the prose in follow ups. - Use `AndroidScreen` and drop `ViewRegistry` - Better, more consistent use of `BackStackScreen` - Use `View.setBackHandler` - Use `TextController` - Delete a lot of `// Exactly what the function name and the code say` comments - More consistent naming, code style for actions and event handlers in `Screen` renderings - Event handler fields are named `onVerbPhrase`, like `onBackPressed` - Action names are verb phrases describing the action that is being taken, not the event that is being handled - Output names are generally in terms of the semantic event being reported to the parent, rather than describing what the parent will do Thus: ```kotlin return TodoListScreen( onRowPressed = { context.actionSink.send(reportSelection(it)) } ``` ```kotlin private fun reportSelection(index: Int) = action { // Tell our parent that a todo item was selected. setOutput(TodoSelected(index)) } ``` I did not introduce `RenderContext.eventHandler {}`, that seems like it would just be confusing to a newcomer. Also not introducing big new blocks of material, in particular not introducing `Overlay`. I do think we should do that, but for this release I just want to focus on getting the deprecated code deleted.
- Loading branch information
Showing
35 changed files
with
592 additions
and
645 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
samples/tutorial/tutorial-final/src/main/java/workflow/tutorial/RootNavigationWorkflow.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package workflow.tutorial | ||
|
||
import com.squareup.workflow1.Snapshot | ||
import com.squareup.workflow1.StatefulWorkflow | ||
import com.squareup.workflow1.action | ||
import com.squareup.workflow1.renderChild | ||
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi | ||
import com.squareup.workflow1.ui.container.BackStackScreen | ||
import com.squareup.workflow1.ui.container.plus | ||
import workflow.tutorial.RootNavigationWorkflow.State | ||
import workflow.tutorial.RootNavigationWorkflow.State.Todo | ||
import workflow.tutorial.RootNavigationWorkflow.State.Welcome | ||
import workflow.tutorial.TodoNavigationWorkflow.TodoProps | ||
|
||
@OptIn(WorkflowUiExperimentalApi::class) | ||
object RootNavigationWorkflow : StatefulWorkflow<Unit, State, Nothing, BackStackScreen<*>>() { | ||
|
||
sealed class State { | ||
object Welcome : State() | ||
data class Todo(val username: String) : State() | ||
} | ||
|
||
override fun initialState( | ||
props: Unit, | ||
snapshot: Snapshot? | ||
): State = Welcome | ||
|
||
@OptIn(WorkflowUiExperimentalApi::class) | ||
override fun render( | ||
renderProps: Unit, | ||
renderState: State, | ||
context: RenderContext | ||
): BackStackScreen<*> { | ||
// Render a child workflow of type WelcomeWorkflow. When renderChild is called, the | ||
// infrastructure will start a child workflow session if one is not already running. | ||
val welcomeScreen = context.renderChild(WelcomeWorkflow) { loggedIn -> | ||
// When WelcomeWorkflow emits LoggedIn, enqueue our log in action. | ||
logIn(loggedIn.username) | ||
} | ||
|
||
return when (renderState) { | ||
// When the state is Welcome, defer to the WelcomeWorkflow. | ||
is Welcome -> { | ||
BackStackScreen(welcomeScreen) | ||
} | ||
|
||
// When the state is Todo, defer to the TodoListWorkflow. | ||
is Todo -> { | ||
val todoBackStack = context.renderChild( | ||
child = TodoNavigationWorkflow, | ||
props = TodoProps(renderState.username) | ||
) { | ||
// When TodoNavigationWorkflow emits Back, enqueue our log out action. | ||
logOut | ||
} | ||
BackStackScreen(welcomeScreen) + todoBackStack | ||
} | ||
} | ||
} | ||
|
||
override fun snapshotState(state: State): Snapshot? = null | ||
|
||
private fun logIn(username: String) = action { | ||
state = Todo(username) | ||
} | ||
|
||
private val logOut = action { | ||
state = Welcome | ||
} | ||
} |
75 changes: 0 additions & 75 deletions
75
samples/tutorial/tutorial-final/src/main/java/workflow/tutorial/RootWorkflow.kt
This file was deleted.
Oops, something went wrong.
33 changes: 0 additions & 33 deletions
33
samples/tutorial/tutorial-final/src/main/java/workflow/tutorial/TodoEditLayoutRunner.kt
This file was deleted.
Oops, something went wrong.
24 changes: 15 additions & 9 deletions
24
samples/tutorial/tutorial-final/src/main/java/workflow/tutorial/TodoEditScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,21 @@ | ||
package workflow.tutorial | ||
|
||
import com.squareup.workflow1.ui.AndroidScreen | ||
import com.squareup.workflow1.ui.ScreenViewFactory | ||
import com.squareup.workflow1.ui.TextController | ||
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi | ||
import workflow.tutorial.views.databinding.TodoEditViewBinding | ||
|
||
@OptIn(WorkflowUiExperimentalApi::class) | ||
data class TodoEditScreen( | ||
/** The title of this todo item. */ | ||
val title: String, | ||
val title: TextController, | ||
/** The contents, or "note" of the todo. */ | ||
val note: String, | ||
|
||
/** Callbacks for when the title or note changes. */ | ||
val onTitleChanged: (String) -> Unit, | ||
val onNoteChanged: (String) -> Unit, | ||
val note: TextController, | ||
|
||
val discardChanges: () -> Unit, | ||
val saveChanges: () -> Unit | ||
) | ||
val onBackPressed: () -> Unit, | ||
val onSavePressed: () -> Unit | ||
) : AndroidScreen<TodoEditScreen> { | ||
override val viewFactory = | ||
ScreenViewFactory.fromViewBinding(TodoEditViewBinding::inflate, ::TodoEditScreenRunner) | ||
} |
24 changes: 24 additions & 0 deletions
24
samples/tutorial/tutorial-final/src/main/java/workflow/tutorial/TodoEditScreenRunner.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package workflow.tutorial | ||
|
||
import com.squareup.workflow1.ui.ScreenViewRunner | ||
import com.squareup.workflow1.ui.ViewEnvironment | ||
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi | ||
import com.squareup.workflow1.ui.control | ||
import com.squareup.workflow1.ui.setBackHandler | ||
import workflow.tutorial.views.databinding.TodoEditViewBinding | ||
|
||
@OptIn(WorkflowUiExperimentalApi::class) | ||
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 contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.