Skip to content

Commit e9cc34f

Browse files
committed
Introduces AndroidViewRendering, de-emphasizes ViewRegistry
`AndroidViewRendering` is implemented by renderings that can provide their own `ViewFactory`. `ViewRegistry.buildView` looks to see if a given rendering implements that method if it fails to find an entry for it -- note that we look for a registration first, so `ViewRegistry` is able override the default. Given this, it's no longer crucial for developers to define and provide a `ViewRegistry`, so we get rid of all the nannying that forced them to provide one. Updates the samples to use the new interface and drop their `ViewRegistry` instances where possible.
1 parent 92bb62a commit e9cc34f

File tree

45 files changed

+461
-819
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+461
-819
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,3 @@
1-
/*
2-
* Copyright 2019 Square Inc.
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
161
package com.squareup.sample.poetryapp
172

183
import android.view.LayoutInflater
@@ -25,29 +10,41 @@ import androidx.recyclerview.widget.RecyclerView
2510
import com.squareup.sample.container.overviewdetail.OverviewDetailConfig
2611
import com.squareup.sample.container.overviewdetail.OverviewDetailConfig.Overview
2712
import com.squareup.sample.container.poetryapp.R
28-
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
13+
import com.squareup.sample.poetry.model.Poem
14+
import com.squareup.workflow1.ui.AndroidViewRendering
2915
import com.squareup.workflow1.ui.LayoutRunner
30-
import com.squareup.workflow1.ui.LayoutRunner.Companion.bind
31-
import com.squareup.workflow1.ui.ViewFactory
3216
import com.squareup.workflow1.ui.ViewEnvironment
17+
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
3318

3419
@OptIn(WorkflowUiExperimentalApi::class)
35-
class PoemListLayoutRunner(view: View) : LayoutRunner<PoemListRendering> {
20+
data class PoemListView(
21+
val poems: List<Poem>,
22+
val onPoemSelected: (Int) -> Unit,
23+
val selection: Int = -1
24+
) : AndroidViewRendering<PoemListView> {
25+
override val viewFactory = LayoutRunner.bind(
26+
R.layout.list,
27+
::PoemListLayoutRunner
28+
)
29+
}
30+
31+
@OptIn(WorkflowUiExperimentalApi::class)
32+
private class PoemListLayoutRunner(view: View) : LayoutRunner<PoemListView> {
3633
init {
3734
view.findViewById<Toolbar>(R.id.list_toolbar)
38-
.apply {
39-
title = view.resources.getString(R.string.poems)
40-
navigationIcon = null
41-
}
35+
.apply {
36+
title = view.resources.getString(R.string.poems)
37+
navigationIcon = null
38+
}
4239
}
4340

4441
private val recyclerView = view.findViewById<RecyclerView>(R.id.list_body)
45-
.apply { layoutManager = LinearLayoutManager(context) }
42+
.apply { layoutManager = LinearLayoutManager(context) }
4643

4744
private val adapter = Adapter()
4845

4946
override fun showRendering(
50-
rendering: PoemListRendering,
47+
rendering: PoemListView,
5148
viewEnvironment: ViewEnvironment
5249
) {
5350
adapter.rendering = rendering
@@ -61,7 +58,7 @@ class PoemListLayoutRunner(view: View) : LayoutRunner<PoemListRendering> {
6158
private class ViewHolder(val view: TextView) : RecyclerView.ViewHolder(view)
6259

6360
private class Adapter : RecyclerView.Adapter<ViewHolder>() {
64-
lateinit var rendering: PoemListRendering
61+
lateinit var rendering: PoemListView
6562
lateinit var environment: ViewEnvironment
6663

6764
override fun onCreateViewHolder(
@@ -76,7 +73,7 @@ class PoemListLayoutRunner(view: View) : LayoutRunner<PoemListRendering> {
7673
}
7774

7875
return ViewHolder(
79-
LayoutInflater.from(parent.context).inflate(layoutId, parent, false) as TextView
76+
LayoutInflater.from(parent.context).inflate(layoutId, parent, false) as TextView
8077
)
8178
}
8279

@@ -95,10 +92,4 @@ class PoemListLayoutRunner(view: View) : LayoutRunner<PoemListRendering> {
9592
}
9693
}
9794
}
98-
99-
companion object : ViewFactory<PoemListRendering>
100-
by bind(
101-
R.layout.list,
102-
::PoemListLayoutRunner
103-
)
10495
}

samples/containers/app-poetry/src/main/java/com/squareup/sample/poetryapp/PoemListWorkflow.kt

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,15 @@ import com.squareup.workflow1.StatelessWorkflow
66
/**
77
* Renders a given ordered list of [Poem]s. Reports the index of any that are clicked.
88
*/
9-
object PoemListWorkflow : StatelessWorkflow<List<Poem>, Int, PoemListRendering>() {
9+
object PoemListWorkflow : StatelessWorkflow<List<Poem>, Int, PoemListView>() {
1010

1111
override fun render(
1212
props: List<Poem>,
1313
context: RenderContext
14-
): PoemListRendering {
15-
return PoemListRendering(
14+
): PoemListView {
15+
return PoemListView(
1616
poems = props,
1717
onPoemSelected = context.eventHandler { index -> setOutput(index) }
1818
)
1919
}
2020
}
21-
22-
data class PoemListRendering(
23-
val poems: List<Poem>,
24-
val onPoemSelected: (Int) -> Unit,
25-
val selection: Int = -1
26-
)

samples/containers/app-poetry/src/main/java/com/squareup/sample/poetryapp/PoetryActivity.kt

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,31 @@
1-
/*
2-
* Copyright 2019 Square Inc.
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
161
package com.squareup.sample.poetryapp
172

183
import android.os.Bundle
194
import androidx.appcompat.app.AppCompatActivity
205
import com.squareup.sample.container.SampleContainers
21-
import com.squareup.sample.poetry.PoetryViews
226
import com.squareup.sample.poetry.model.Poem
237
import com.squareup.workflow1.SimpleLoggingWorkflowInterceptor
24-
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
258
import com.squareup.workflow1.ui.WorkflowRunner
9+
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
2610
import com.squareup.workflow1.ui.backstack.BackStackContainer
2711
import com.squareup.workflow1.ui.plus
2812
import com.squareup.workflow1.ui.setContentWorkflow
2913
import timber.log.Timber
3014

3115
@OptIn(WorkflowUiExperimentalApi::class)
32-
private val viewRegistry =
33-
SampleContainers + PoetryViews + PoemListLayoutRunner + BackStackContainer
16+
private val viewRegistry = SampleContainers + BackStackContainer
3417

3518
class PoetryActivity : AppCompatActivity() {
3619
@OptIn(WorkflowUiExperimentalApi::class)
3720
override fun onCreate(savedInstanceState: Bundle?) {
3821
super.onCreate(savedInstanceState)
3922
setContentWorkflow(viewRegistry) {
4023
WorkflowRunner.Config(
41-
PoemsBrowserWorkflow,
42-
Poem.allPoems,
43-
interceptors = listOf(object : SimpleLoggingWorkflowInterceptor() {
44-
override fun log(text: String) = Timber.v(text)
45-
})
24+
PoemsBrowserWorkflow,
25+
Poem.allPoems,
26+
interceptors = listOf(object : SimpleLoggingWorkflowInterceptor() {
27+
override fun log(text: String) = Timber.v(text)
28+
})
4629
)
4730
}
4831
}

samples/containers/app-raven/src/main/java/com/squareup/sample/ravenapp/RavenActivity.kt

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,37 @@
1-
/*
2-
* Copyright 2019 Square Inc.
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
161
package com.squareup.sample.ravenapp
172

183
import android.os.Bundle
194
import androidx.appcompat.app.AppCompatActivity
205
import com.squareup.sample.container.SampleContainers
216
import com.squareup.sample.poetry.PoemWorkflow
22-
import com.squareup.sample.poetry.PoetryViews
237
import com.squareup.sample.poetry.model.Raven
248
import com.squareup.workflow1.SimpleLoggingWorkflowInterceptor
25-
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
269
import com.squareup.workflow1.ui.WorkflowRunner
10+
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
2711
import com.squareup.workflow1.ui.backstack.BackStackContainer
2812
import com.squareup.workflow1.ui.plus
2913
import com.squareup.workflow1.ui.setContentWorkflow
3014
import timber.log.Timber
3115

3216
@OptIn(WorkflowUiExperimentalApi::class)
33-
private val viewRegistry = SampleContainers + PoetryViews + BackStackContainer
17+
private val viewRegistry = SampleContainers + BackStackContainer
3418

3519
class RavenActivity : AppCompatActivity() {
3620
@OptIn(WorkflowUiExperimentalApi::class)
3721
override fun onCreate(savedInstanceState: Bundle?) {
3822
super.onCreate(savedInstanceState)
3923
setContentWorkflow(
40-
registry = viewRegistry,
41-
configure = {
42-
WorkflowRunner.Config(
43-
PoemWorkflow,
44-
Raven,
45-
interceptors = listOf(object : SimpleLoggingWorkflowInterceptor() {
46-
override fun log(text: String) = Timber.v(text)
47-
})
48-
)
49-
},
50-
onResult = { finish() }
24+
registry = viewRegistry,
25+
configure = {
26+
WorkflowRunner.Config(
27+
PoemWorkflow,
28+
Raven,
29+
interceptors = listOf(object : SimpleLoggingWorkflowInterceptor() {
30+
override fun log(text: String) = Timber.v(text)
31+
})
32+
)
33+
},
34+
onResult = { finish() }
5135
)
5236
}
5337

samples/containers/hello-back-button/src/main/java/com/squareup/sample/hellobackbutton/HelloBackButtonActivity.kt

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,17 @@
1-
/*
2-
* Copyright 2020 Square Inc.
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
161
package com.squareup.sample.hellobackbutton
172

183
import android.os.Bundle
194
import androidx.appcompat.app.AppCompatActivity
205
import com.squareup.sample.container.SampleContainers
216
import com.squareup.workflow1.SimpleLoggingWorkflowInterceptor
22-
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
23-
import com.squareup.workflow1.ui.ViewRegistry
247
import com.squareup.workflow1.ui.WorkflowRunner
8+
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
259
import com.squareup.workflow1.ui.modal.AlertContainer
2610
import com.squareup.workflow1.ui.plus
2711
import com.squareup.workflow1.ui.setContentWorkflow
2812

2913
@OptIn(WorkflowUiExperimentalApi::class)
30-
private val viewRegistry =
31-
ViewRegistry(HelloBackButtonLayoutRunner) + SampleContainers + AlertContainer
14+
private val viewRegistry = SampleContainers + AlertContainer
3215

3316
class HelloBackButtonActivity : AppCompatActivity() {
3417
@OptIn(WorkflowUiExperimentalApi::class)
Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,37 @@
1-
/*
2-
* Copyright 2020 Square Inc.
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
161
package com.squareup.sample.hellobackbutton
172

183
import android.view.View
194
import android.widget.TextView
20-
import com.squareup.sample.hellobackbutton.HelloBackButtonWorkflow.Rendering
215
import com.squareup.sample.hellobackbutton.R.id
226
import com.squareup.sample.hellobackbutton.R.layout
23-
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
7+
import com.squareup.workflow1.ui.AndroidViewRendering
248
import com.squareup.workflow1.ui.LayoutRunner
25-
import com.squareup.workflow1.ui.LayoutRunner.Companion.bind
26-
import com.squareup.workflow1.ui.ViewFactory
279
import com.squareup.workflow1.ui.ViewEnvironment
10+
import com.squareup.workflow1.ui.ViewFactory
11+
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
2812
import com.squareup.workflow1.ui.backPressedHandler
2913

3014
@OptIn(WorkflowUiExperimentalApi::class)
31-
class HelloBackButtonLayoutRunner(view: View) : LayoutRunner<Rendering> {
15+
data class HelloBackButtonView(
16+
val message: String,
17+
val onClick: () -> Unit,
18+
val onBackPressed: (() -> Unit)?
19+
) : AndroidViewRendering<HelloBackButtonView> {
20+
override val viewFactory: ViewFactory<HelloBackButtonView> = LayoutRunner.bind(
21+
layout.hello_back_button_layout, ::HelloBackButtonLayoutRunner
22+
)
23+
}
24+
25+
@OptIn(WorkflowUiExperimentalApi::class)
26+
private class HelloBackButtonLayoutRunner(view: View) : LayoutRunner<HelloBackButtonView> {
3227
private val messageView: TextView = view.findViewById(id.hello_message)
3328

3429
override fun showRendering(
35-
rendering: Rendering,
30+
rendering: HelloBackButtonView,
3631
viewEnvironment: ViewEnvironment
3732
) {
3833
messageView.text = rendering.message
3934
messageView.setOnClickListener { rendering.onClick() }
4035
messageView.backPressedHandler = rendering.onBackPressed
4136
}
42-
43-
companion object : ViewFactory<Rendering> by bind(
44-
layout.hello_back_button_layout, ::HelloBackButtonLayoutRunner
45-
)
4637
}

samples/containers/hello-back-button/src/main/java/com/squareup/sample/hellobackbutton/HelloBackButtonWorkflow.kt

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.squareup.sample.hellobackbutton
22

33
import android.os.Parcelable
4-
import com.squareup.sample.hellobackbutton.HelloBackButtonWorkflow.Rendering
54
import com.squareup.sample.hellobackbutton.HelloBackButtonWorkflow.State
65
import com.squareup.sample.hellobackbutton.HelloBackButtonWorkflow.State.Able
76
import com.squareup.sample.hellobackbutton.HelloBackButtonWorkflow.State.Baker
@@ -12,20 +11,14 @@ import com.squareup.workflow1.ui.toParcelable
1211
import com.squareup.workflow1.ui.toSnapshot
1312
import kotlinx.android.parcel.Parcelize
1413

15-
object HelloBackButtonWorkflow : StatefulWorkflow<Unit, State, Nothing, Rendering>() {
14+
object HelloBackButtonWorkflow : StatefulWorkflow<Unit, State, Nothing, HelloBackButtonView>() {
1615
@Parcelize
1716
enum class State : Parcelable {
1817
Able,
1918
Baker,
2019
Charlie;
2120
}
2221

23-
data class Rendering(
24-
val message: String,
25-
val onClick: () -> Unit,
26-
val onBackPressed: (() -> Unit)?
27-
)
28-
2922
override fun initialState(
3023
props: Unit,
3124
snapshot: Snapshot?
@@ -35,8 +28,8 @@ object HelloBackButtonWorkflow : StatefulWorkflow<Unit, State, Nothing, Renderin
3528
props: Unit,
3629
state: State,
3730
context: RenderContext
38-
): Rendering {
39-
return Rendering(
31+
): HelloBackButtonView {
32+
return HelloBackButtonView(
4033
message = "$state",
4134
onClick = context.eventHandler {
4235
this.state = when (this.state) {

0 commit comments

Comments
 (0)