@@ -3,6 +3,9 @@ package com.squareup.workflow1.ui.container
3
3
import com.squareup.workflow1.ui.Container
4
4
import com.squareup.workflow1.ui.Screen
5
5
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
6
+ import com.squareup.workflow1.ui.container.BackStackScreen.Companion
7
+ import com.squareup.workflow1.ui.container.BackStackScreen.Companion.fromList
8
+ import com.squareup.workflow1.ui.container.BackStackScreen.Companion.fromListOrNull
6
9
7
10
/* *
8
11
* Represents an active screen ([top]), and a set of previously visited screens to which we may
@@ -13,25 +16,31 @@ import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
13
16
*
14
17
* UI kits are expected to provide handling for this class by default.
15
18
*
16
- * @param bottom the bottom-most entry in the stack
17
- * @param rest the rest of the stack, empty by default
19
+ * @see fromList
20
+ * @see fromListOrNull
18
21
*/
19
22
@WorkflowUiExperimentalApi
20
- public class BackStackScreen <StackedT : Screen >(
21
- bottom : StackedT ,
22
- rest : List <StackedT >
23
+ public class BackStackScreen <StackedT : Screen > private constructor(
24
+ public val frames : List <StackedT >
23
25
) : Screen, Container<Screen, StackedT> {
24
26
/* *
25
27
* Creates a screen with elements listed from the [bottom] to the top.
26
28
*/
27
29
public constructor (
28
30
bottom: StackedT ,
29
31
vararg rest: StackedT
30
- ) : this (bottom, rest.toList() )
32
+ ) : this (listOf ( bottom) + rest)
31
33
32
- override fun asSequence (): Sequence <StackedT > = frames.asSequence()
34
+ @Deprecated(
35
+ " Use fromList" ,
36
+ ReplaceWith (" BackStackScreen.fromList(listOf(bottom) + rest)" )
37
+ )
38
+ public constructor (
39
+ bottom: StackedT ,
40
+ rest: List <StackedT >
41
+ ) : this (listOf (bottom) + rest)
33
42
34
- public val frames : List <StackedT > = listOf (bottom) + rest
43
+ override fun asSequence (): Sequence <StackedT > = frames.asSequence()
35
44
36
45
/* *
37
46
* The active screen.
@@ -49,7 +58,7 @@ public class BackStackScreen<StackedT : Screen>(
49
58
return if (other == null ) {
50
59
this
51
60
} else {
52
- BackStackScreen (frames[ 0 ], frames.subList( 1 , frames.size) + other.frames)
61
+ BackStackScreen (frames + other.frames)
53
62
}
54
63
}
55
64
@@ -75,16 +84,37 @@ public class BackStackScreen<StackedT : Screen>(
75
84
override fun toString (): String {
76
85
return " ${this ::class .java.simpleName} ($frames )"
77
86
}
87
+
88
+ public companion object {
89
+ /* *
90
+ * Builds a [BackStackScreen] from a non-empty list of [frames].
91
+ *
92
+ * @throws IllegalArgumentException is [frames] is empty
93
+ */
94
+ public fun <T : Screen > fromList (frames : List <T >): BackStackScreen <T > {
95
+ require(frames.isNotEmpty()) {
96
+ " A BackStackScreen must have at least one frame."
97
+ }
98
+ return BackStackScreen (frames)
99
+ }
100
+
101
+ /* *
102
+ * Builds a [BackStackScreen] from a list of [frames], or returns `null`
103
+ * if [frames] is empty.
104
+ */
105
+ public fun <T : Screen > fromListOrNull (frames : List <T >): BackStackScreen <T >? {
106
+ return when {
107
+ frames.isEmpty() -> null
108
+ else -> BackStackScreen (frames)
109
+ }
110
+ }
111
+ }
78
112
}
79
113
80
114
@WorkflowUiExperimentalApi
81
- public fun <T : Screen > List<T>.toBackStackScreenOrNull (): BackStackScreen <T >? = when {
82
- isEmpty() -> null
83
- else -> toBackStackScreen()
84
- }
115
+ public fun <T : Screen > List<T>.toBackStackScreenOrNull (): BackStackScreen <T >? =
116
+ fromListOrNull(this )
85
117
86
118
@WorkflowUiExperimentalApi
87
- public fun <T : Screen > List<T>.toBackStackScreen (): BackStackScreen <T > {
88
- require(isNotEmpty())
89
- return BackStackScreen (first(), subList(1 , size))
90
- }
119
+ public fun <T : Screen > List<T>.toBackStackScreen (): BackStackScreen <T > =
120
+ Companion .fromList(this )
0 commit comments