Skip to content

Commit 9bdeb99

Browse files
699: Cleanup and name Poetry components
1 parent 984358a commit 9bdeb99

File tree

6 files changed

+42
-16
lines changed

6 files changed

+42
-16
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public final class com/squareup/benchmarks/performance/poetry/PerformancePoetryActivity : androidx/appcompat/app/AppCompatActivity {
2+
public static final field Companion Lcom/squareup/benchmarks/performance/poetry/PerformancePoetryActivity$Companion;
3+
public fun <init> ()V
4+
}
5+
6+
public final class com/squareup/benchmarks/performance/poetry/PerformancePoetryActivity$Companion {
7+
}
8+
9+
public final class com/squareup/benchmarks/performance/poetry/PerformancePoetryActivityKt {
10+
}
11+
12+
public final class com/squareup/benchmarks/performance/poetry/PoetryModel : androidx/lifecycle/ViewModel {
13+
public fun <init> (Landroidx/lifecycle/SavedStateHandle;)V
14+
public final fun getRenderings ()Lkotlinx/coroutines/flow/StateFlow;
15+
}
16+

samples/containers/poetry/src/main/java/com/squareup/sample/poetry/PoemListRendering.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,16 @@ import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
2020
data class PoemListRendering(
2121
val poems: List<Poem>,
2222
val onPoemSelected: (Int) -> Unit,
23-
val selection: Int = -1
23+
val selection: Int = NO_POEM_SELECTED
2424
) : AndroidViewRendering<PoemListRendering> {
2525
override val viewFactory = LayoutRunner.bind(
2626
R.layout.list,
2727
::PoemListLayoutRunner
2828
)
29+
30+
companion object {
31+
const val NO_POEM_SELECTED: Int = -1
32+
}
2933
}
3034

3135
@OptIn(WorkflowUiExperimentalApi::class)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import com.squareup.sample.poetry.model.Poem
44
import com.squareup.workflow1.StatelessWorkflow
55

66
/**
7-
* Renders a given ordered list of [Poem]s. Reports the index of any that are clicked.
7+
* Renders a given ordered list of [Poem]s. Reports the index of any that are clicked via Output.
88
*/
9-
object PoemListWorkflow : StatelessWorkflow<List<Poem>, Int, PoemListRendering>() {
9+
object PoemListWorkflow : StatelessWorkflow<List<Poem>, SelectedPoem, PoemListRendering>() {
1010

1111
override fun render(
1212
renderProps: List<Poem>,

samples/containers/poetry/src/main/java/com/squareup/sample/poetry/PoemWorkflow.kt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,32 @@ import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
2020
import com.squareup.workflow1.ui.backstack.BackStackScreen
2121
import com.squareup.workflow1.ui.backstack.toBackStackScreen
2222

23+
typealias SelectedStanza = Int
24+
2325
/**
2426
* Renders a [Poem] as a [OverviewDetailScreen], whose overview is a [StanzaListRendering]
2527
* for the poem, and whose detail traverses through [StanzaRendering]s.
2628
*/
27-
object PoemWorkflow : StatefulWorkflow<Poem, Int, ClosePoem, OverviewDetailScreen>() {
29+
object PoemWorkflow : StatefulWorkflow<Poem, SelectedStanza, ClosePoem, OverviewDetailScreen>() {
2830
object ClosePoem
31+
public const val NO_SELECTED_STANZA = -1
2932

3033
override fun initialState(
3134
props: Poem,
3235
snapshot: Snapshot?
3336
): Int {
3437
return snapshot?.bytes?.parse { source -> source.readInt() }
35-
?: -1
38+
?: NO_SELECTED_STANZA
3639
}
3740

3841
@OptIn(WorkflowUiExperimentalApi::class)
3942
override fun render(
4043
renderProps: Poem,
41-
renderState: Int,
44+
renderState: SelectedStanza,
4245
context: RenderContext
4346
): OverviewDetailScreen {
4447
val previousStanzas: List<StanzaRendering> =
45-
if (renderState == -1) emptyList()
48+
if (renderState == NO_SELECTED_STANZA) emptyList()
4649
else renderProps.stanzas.subList(0, renderState)
4750
.mapIndexed { index, _ ->
4851
context.renderChild(StanzaWorkflow, Props(renderProps, index), "$index") {
@@ -88,7 +91,7 @@ object PoemWorkflow : StatefulWorkflow<Poem, Int, ClosePoem, OverviewDetailScree
8891
sink.writeInt(state)
8992
}
9093

91-
private sealed class Action : WorkflowAction<Poem, Int, ClosePoem>() {
94+
private sealed class Action : WorkflowAction<Poem, SelectedStanza, ClosePoem>() {
9295
object ClearSelection : Action()
9396
object SelectPrevious : Action()
9497
object SelectNext : Action()
@@ -97,11 +100,11 @@ object PoemWorkflow : StatefulWorkflow<Poem, Int, ClosePoem, OverviewDetailScree
97100

98101
override fun Updater.apply() {
99102
when (this@Action) {
100-
ClearSelection -> state = -1
103+
ClearSelection -> state = NO_SELECTED_STANZA
101104
SelectPrevious -> state -= 1
102105
SelectNext -> state += 1
103106
is HandleStanzaListOutput -> {
104-
if (selection == -1) setOutput(ClosePoem)
107+
if (selection == NO_SELECTED_STANZA) setOutput(ClosePoem)
105108
state = selection
106109
}
107110
ExitPoem -> setOutput(ClosePoem)

samples/containers/poetry/src/main/java/com/squareup/sample/poetry/PoemsBrowserWorkflow.kt

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

33
import com.squareup.sample.container.overviewdetail.OverviewDetailScreen
4+
import com.squareup.sample.poetry.PoemListRendering.Companion.NO_POEM_SELECTED
45
import com.squareup.sample.poetry.model.Poem
56
import com.squareup.workflow1.Snapshot
67
import com.squareup.workflow1.StatefulWorkflow
@@ -13,12 +14,13 @@ typealias SelectedPoem = Int
1314

1415
object PoemsBrowserWorkflow :
1516
StatefulWorkflow<List<Poem>, SelectedPoem, Nothing, OverviewDetailScreen>() {
17+
1618
override fun initialState(
1719
props: List<Poem>,
1820
snapshot: Snapshot?
1921
): SelectedPoem {
2022
return snapshot?.bytes?.parse { source -> source.readInt() }
21-
?: -1
23+
?: NO_POEM_SELECTED
2224
}
2325

2426
@OptIn(WorkflowUiExperimentalApi::class)
@@ -32,7 +34,7 @@ object PoemsBrowserWorkflow :
3234
.copy(selection = renderState)
3335
.let { OverviewDetailScreen(BackStackScreen(it)) }
3436

35-
return if (renderState == -1) {
37+
return if (renderState == NO_POEM_SELECTED) {
3638
poems
3739
} else {
3840
val poem: OverviewDetailScreen =
@@ -49,5 +51,5 @@ object PoemsBrowserWorkflow :
4951
state = index
5052
}
5153

52-
private val clearSelection = choosePoem(-1)
54+
private val clearSelection = choosePoem(NO_POEM_SELECTED)
5355
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package com.squareup.sample.poetry
22

3+
import com.squareup.sample.poetry.PoemWorkflow.NO_SELECTED_STANZA
34
import com.squareup.sample.poetry.model.Poem
45
import com.squareup.workflow1.StatelessWorkflow
56

67
/**
78
* Given a [Poem], renders a list of its [initialStanzas][Poem.initialStanzas].
89
*
9-
* Output is the index of a clicked stanza, or -1 on exit.
10+
* Output is the index of a clicked stanza, or [-1][NO_SELECTED_STANZA] on exit.
1011
*/
11-
object StanzaListWorkflow : StatelessWorkflow<Poem, Int, StanzaListRendering>() {
12+
object StanzaListWorkflow : StatelessWorkflow<Poem, SelectedStanza, StanzaListRendering>() {
1213

1314
override fun render(
1415
renderProps: Poem,
@@ -19,7 +20,7 @@ object StanzaListWorkflow : StatelessWorkflow<Poem, Int, StanzaListRendering>()
1920
subtitle = renderProps.poet.fullName,
2021
firstLines = renderProps.initialStanzas,
2122
onStanzaSelected = context.eventHandler { index -> setOutput(index) },
22-
onExit = context.eventHandler { setOutput(-1) }
23+
onExit = context.eventHandler { setOutput(NO_SELECTED_STANZA) }
2324
)
2425
}
2526
}

0 commit comments

Comments
 (0)