Skip to content

Commit

Permalink
Rename ContainerHints to ViewEnvironment to match Swift.
Browse files Browse the repository at this point in the history
Keeps `ContainerHints` around as a deprecated type alias with replacement action.

Closes #1000.
  • Loading branch information
zach-klippenstein committed Mar 6, 2020
1 parent 5540eef commit 38d2d59
Show file tree
Hide file tree
Showing 60 changed files with 283 additions and 273 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.suspendCancellableCoroutine
import org.junit.Test
import kotlin.coroutines.suspendCoroutine
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertFalse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ data class BackButtonScreen<W : Any>(
.also { view ->
val wrappedUpdater = view.getShowRendering<Any>()!!

view.bindShowRendering(initialRendering, initialHints) { rendering, hints ->
view.bindShowRendering(initialRendering, initialHints) { rendering, environment ->
if (!rendering.override) {
// Place our handler before invoking the wrapped updater, so that
// its later calls to view.backPressedHandler will take precedence
// over ours.
view.backPressedHandler = rendering.onBackPressed
}

wrappedUpdater.invoke(rendering.wrapped, hints)
wrappedUpdater.invoke(rendering.wrapped, environment)

if (rendering.override) {
// Place our handler after invoking the wrapped updater, so that ours
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
*/
package com.squareup.sample.container.masterdetail

import com.squareup.workflow.ui.ContainerHintKey
import com.squareup.workflow.ui.ViewEnvironmentKey

/**
* [com.squareup.workflow.ui.ContainerHints] value that informs views
* [com.squareup.workflow.ui.ViewEnvironment] value that informs views
* whether they're children of a [MasterDetailContainer], and if so
* in what configuration.
*/
Expand All @@ -43,7 +43,7 @@ enum class MasterDetailConfig {
*/
Single;

companion object : ContainerHintKey<MasterDetailConfig>(MasterDetailConfig::class) {
companion object : ViewEnvironmentKey<MasterDetailConfig>(MasterDetailConfig::class) {
override val default = None
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import com.squareup.sample.container.R
import com.squareup.sample.container.masterdetail.MasterDetailConfig.Detail
import com.squareup.sample.container.masterdetail.MasterDetailConfig.Master
import com.squareup.sample.container.masterdetail.MasterDetailConfig.Single
import com.squareup.workflow.ui.ContainerHints
import com.squareup.workflow.ui.LayoutRunner
import com.squareup.workflow.ui.ViewBinding
import com.squareup.workflow.ui.ViewEnvironment
import com.squareup.workflow.ui.WorkflowViewStub
import com.squareup.workflow.ui.backstack.BackStackScreen

Expand Down Expand Up @@ -52,29 +52,29 @@ class MasterDetailContainer(view: View) : LayoutRunner<MasterDetailScreen> {

override fun showRendering(
rendering: MasterDetailScreen,
containerHints: ContainerHints
viewEnvironment: ViewEnvironment
) {
if (singleStub == null) renderSplitView(rendering, containerHints)
else renderSingleView(rendering, containerHints, singleStub)
if (singleStub == null) renderSplitView(rendering, viewEnvironment)
else renderSingleView(rendering, viewEnvironment, singleStub)
}

private fun renderSplitView(
rendering: MasterDetailScreen,
containerHints: ContainerHints
viewEnvironment: ViewEnvironment
) {
if (rendering.detailRendering == null && rendering.selectDefault != null) {
rendering.selectDefault!!.invoke()
} else {
masterStub!!.update(
rendering.masterRendering,
containerHints + (MasterDetailConfig to Master)
viewEnvironment + (MasterDetailConfig to Master)
)
rendering.detailRendering
?.let { detail ->
detailStub!!.actual.visibility = VISIBLE
detailStub.update(
detail,
containerHints + (MasterDetailConfig to Detail)
viewEnvironment + (MasterDetailConfig to Detail)
)
}
?: run {
Expand All @@ -85,14 +85,14 @@ class MasterDetailContainer(view: View) : LayoutRunner<MasterDetailScreen> {

private fun renderSingleView(
rendering: MasterDetailScreen,
containerHints: ContainerHints,
viewEnvironment: ViewEnvironment,
stub: WorkflowViewStub
) {
val combined: BackStackScreen<*> = rendering.detailRendering
?.let { rendering.masterRendering + it }
?: rendering.masterRendering

stub.update(combined, containerHints + (MasterDetailConfig to Single))
stub.update(combined, viewEnvironment + (MasterDetailConfig to Single))
}

companion object : ViewBinding<MasterDetailScreen> by LayoutRunner.Binding(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/
package com.squareup.sample.container.panel

import android.animation.ValueAnimator
import android.content.Context
import android.util.AttributeSet
Expand Down Expand Up @@ -105,16 +106,18 @@ class ScrimContainer @JvmOverloads constructor(

companion object : ViewBinding<ScrimContainerScreen<*>> by BuilderBinding(
type = ScrimContainerScreen::class,
viewConstructor = { initialRendering, initialContainerHints, contextForNewView, _ ->
viewConstructor = { initialRendering, initialViewEnvironment, contextForNewView, _ ->
val stub = WorkflowViewStub(contextForNewView)

ScrimContainer(contextForNewView)
.apply {
layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
addView(stub)

bindShowRendering(initialRendering, initialContainerHints) { rendering, hints ->
stub.update(rendering.wrapped, hints)
bindShowRendering(
initialRendering, initialViewEnvironment
) { rendering, environment ->
stub.update(rendering.wrapped, environment)
isDimmed = rendering.dimmed
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import androidx.recyclerview.widget.RecyclerView
import com.squareup.sample.container.masterdetail.MasterDetailConfig
import com.squareup.sample.container.masterdetail.MasterDetailConfig.Master
import com.squareup.sample.container.poetryapp.R
import com.squareup.workflow.ui.ContainerHints
import com.squareup.workflow.ui.LayoutRunner
import com.squareup.workflow.ui.LayoutRunner.Companion.bind
import com.squareup.workflow.ui.ViewBinding
import com.squareup.workflow.ui.ViewEnvironment

class PoemListLayoutRunner(view: View) : LayoutRunner<PoemListRendering> {
init {
Expand All @@ -46,10 +46,10 @@ class PoemListLayoutRunner(view: View) : LayoutRunner<PoemListRendering> {

override fun showRendering(
rendering: PoemListRendering,
containerHints: ContainerHints
viewEnvironment: ViewEnvironment
) {
adapter.rendering = rendering
adapter.hints = containerHints
adapter.environment = viewEnvironment
adapter.notifyDataSetChanged()
if (recyclerView.adapter == null) recyclerView.adapter = adapter

Expand All @@ -60,13 +60,13 @@ class PoemListLayoutRunner(view: View) : LayoutRunner<PoemListRendering> {

private class Adapter : RecyclerView.Adapter<ViewHolder>() {
lateinit var rendering: PoemListRendering
lateinit var hints: ContainerHints
lateinit var environment: ViewEnvironment

override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): ViewHolder {
val selectable = hints[MasterDetailConfig] == Master
val selectable = environment[MasterDetailConfig] == Master
val layoutId = if (selectable) {
R.layout.list_row_selectable
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ package com.squareup.sample.hellobackbutton

import android.view.View
import android.widget.TextView
import com.squareup.sample.hellobackbutton.HelloBackButtonWorkflow.Rendering
import com.squareup.sample.hellobackbutton.R.id
import com.squareup.sample.hellobackbutton.R.layout
import com.squareup.sample.hellobackbutton.HelloBackButtonWorkflow.Rendering
import com.squareup.workflow.ui.ContainerHints
import com.squareup.workflow.ui.LayoutRunner
import com.squareup.workflow.ui.LayoutRunner.Companion.bind
import com.squareup.workflow.ui.ViewBinding
import com.squareup.workflow.ui.ViewEnvironment
import com.squareup.workflow.ui.backPressedHandler

class HelloBackButtonLayoutRunner(view: View) : LayoutRunner<Rendering> {
private val messageView: TextView = view.findViewById(id.hello_message)

override fun showRendering(
rendering: Rendering,
containerHints: ContainerHints
viewEnvironment: ViewEnvironment
) {
messageView.text = rendering.message
messageView.setOnClickListener { rendering.onClick() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import androidx.appcompat.widget.Toolbar
import com.squareup.sample.container.masterdetail.MasterDetailConfig
import com.squareup.sample.container.masterdetail.MasterDetailConfig.Detail
import com.squareup.sample.container.poetry.R
import com.squareup.workflow.ui.ContainerHints
import com.squareup.workflow.ui.LayoutRunner
import com.squareup.workflow.ui.ViewBinding
import com.squareup.workflow.ui.ViewEnvironment
import com.squareup.workflow.ui.backPressedHandler
import com.squareup.workflow.ui.backstack.BackStackConfig
import com.squareup.workflow.ui.backstack.BackStackConfig.None
Expand All @@ -47,9 +47,9 @@ class StanzaLayoutRunner(private val view: View) : LayoutRunner<StanzaRendering>

override fun showRendering(
rendering: StanzaRendering,
containerHints: ContainerHints
viewEnvironment: ViewEnvironment
) {
if (containerHints[MasterDetailConfig] == Detail) {
if (viewEnvironment[MasterDetailConfig] == Detail) {
toolbar.title = "Stanza ${rendering.stanzaNumber}"
toolbar.subtitle = null
} else {
Expand Down Expand Up @@ -81,14 +81,14 @@ class StanzaLayoutRunner(private val view: View) : LayoutRunner<StanzaRendering>
goBack.visibility = View.INVISIBLE
}

if (containerHints[MasterDetailConfig] != Detail && containerHints[BackStackConfig] != None) {
if (viewEnvironment[MasterDetailConfig] != Detail && viewEnvironment[BackStackConfig] != None) {
toolbar.setNavigationOnClickListener { rendering.onGoUp.invoke() }
} else {
toolbar.navigationIcon = null
}

view.backPressedHandler = rendering.onGoBack
?: rendering.onGoUp.takeIf { containerHints[MasterDetailConfig] != Detail }
?: rendering.onGoUp.takeIf { viewEnvironment[MasterDetailConfig] != Detail }
}

private fun TextView.setTabulatedText(lines: List<String>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import androidx.recyclerview.widget.RecyclerView
import com.squareup.sample.container.masterdetail.MasterDetailConfig
import com.squareup.sample.container.masterdetail.MasterDetailConfig.Master
import com.squareup.sample.container.poetry.R
import com.squareup.workflow.ui.ContainerHints
import com.squareup.workflow.ui.LayoutRunner
import com.squareup.workflow.ui.LayoutRunner.Companion.bind
import com.squareup.workflow.ui.ViewBinding
import com.squareup.workflow.ui.ViewEnvironment
import com.squareup.workflow.ui.backPressedHandler
import com.squareup.workflow.ui.backstack.BackStackConfig
import com.squareup.workflow.ui.backstack.BackStackConfig.Other
Expand All @@ -42,16 +42,16 @@ class StanzaListLayoutRunner(view: View) : LayoutRunner<StanzaListRendering> {

override fun showRendering(
rendering: StanzaListRendering,
containerHints: ContainerHints
viewEnvironment: ViewEnvironment
) {
adapter.rendering = rendering
adapter.hints = containerHints
adapter.environment = viewEnvironment
adapter.notifyDataSetChanged()
if (recyclerView.adapter == null) recyclerView.adapter = adapter
toolbar.title = rendering.title
toolbar.subtitle = rendering.subtitle

if (containerHints[BackStackConfig] == Other) {
if (viewEnvironment[BackStackConfig] == Other) {
toolbar.setNavigationOnClickListener { rendering.onExit() }
toolbar.backPressedHandler = rendering.onExit
} else {
Expand All @@ -66,13 +66,13 @@ class StanzaListLayoutRunner(view: View) : LayoutRunner<StanzaListRendering> {

private class Adapter : RecyclerView.Adapter<ViewHolder>() {
lateinit var rendering: StanzaListRendering
lateinit var hints: ContainerHints
lateinit var environment: ViewEnvironment

override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): ViewHolder {
val selectable = hints[MasterDetailConfig] == Master
val selectable = environment[MasterDetailConfig] == Master
val layoutId = if (selectable) {
R.layout.list_row_selectable
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ package com.squareup.sample.poetry

import com.squareup.sample.poetry.StanzaWorkflow.Output
import com.squareup.sample.poetry.StanzaWorkflow.Output.CloseStanzas
import com.squareup.sample.poetry.StanzaWorkflow.Output.ShowPreviousStanza
import com.squareup.sample.poetry.StanzaWorkflow.Output.ShowNextStanza
import com.squareup.sample.poetry.StanzaWorkflow.Output.ShowPreviousStanza
import com.squareup.sample.poetry.StanzaWorkflow.Props
import com.squareup.sample.poetry.model.Poem
import com.squareup.workflow.RenderContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import com.squareup.cycler.Recycler
import com.squareup.cycler.toDataSource
import com.squareup.sample.dungeon.DungeonAppWorkflow.DisplayBoardsListScreen
import com.squareup.sample.dungeon.board.Board
import com.squareup.workflow.ui.ContainerHints
import com.squareup.workflow.ui.LayoutRunner
import com.squareup.workflow.ui.LayoutRunner.Companion.bind
import com.squareup.workflow.ui.ViewBinding
import com.squareup.workflow.ui.ViewEnvironment
import com.squareup.workflow.ui.WorkflowViewStub

/**
Expand All @@ -39,14 +39,14 @@ import com.squareup.workflow.ui.WorkflowViewStub
class BoardsListLayoutRunner(rootView: View) : LayoutRunner<DisplayBoardsListScreen> {

/**
* Used to associate a single [ContainerHints] and [DisplayBoardsListScreen.onBoardSelected]
* Used to associate a single [ViewEnvironment] and [DisplayBoardsListScreen.onBoardSelected]
* event handler with every item of a [DisplayBoardsListScreen].
*
* @see toDataSource
*/
private data class BoardItem(
val board: Board,
val containerHints: ContainerHints,
val viewEnvironment: ViewEnvironment,
val onClicked: () -> Unit
)

Expand All @@ -66,7 +66,7 @@ class BoardsListLayoutRunner(rootView: View) : LayoutRunner<DisplayBoardsListScr
val boardPreviewView: WorkflowViewStub = view.findViewById(R.id.board_preview)

boardNameView.text = item.board.metadata.name
boardPreviewView.update(item.board, item.containerHints)
boardPreviewView.update(item.board, item.viewEnvironment)
card.setOnClickListener { item.onClicked() }
}
}
Expand All @@ -75,28 +75,28 @@ class BoardsListLayoutRunner(rootView: View) : LayoutRunner<DisplayBoardsListScr

override fun showRendering(
rendering: DisplayBoardsListScreen,
containerHints: ContainerHints
viewEnvironment: ViewEnvironment
) {
// Associate the containerHints and event handler to each item because it needs to be used when
// binding the RecyclerView item above.
// Recycler is configured with a DataSource, which effectively (and often in practice) a simple
// wrapper around a List.
recycler.data = rendering.toDataSource(containerHints)
recycler.data = rendering.toDataSource(viewEnvironment)
}

/**
* Converts this [DisplayBoardsListScreen] into a [DataSource] by lazily wrapping it in a
* [BoardItem] to associate it with the [ContainerHints] and selection event handler from the
* [BoardItem] to associate it with the [ViewEnvironment] and selection event handler from the
* rendering.
*/
private fun DisplayBoardsListScreen.toDataSource(
containerHints: ContainerHints
viewEnvironment: ViewEnvironment
): DataSource<BoardItem> = object : DataSource<BoardItem> {
override val size: Int get() = boards.size

override fun get(i: Int): BoardItem = BoardItem(
board = boards[i],
containerHints = containerHints,
viewEnvironment = viewEnvironment,
onClicked = { onBoardSelected(i) }
)
}
Expand Down

0 comments on commit 38d2d59

Please sign in to comment.