@@ -5,26 +5,22 @@ import android.os.Parcelable
5
5
import android.os.Parcelable.Creator
6
6
import android.util.SparseArray
7
7
import android.view.View
8
- import android.view.View.BaseSavedState
9
8
import androidx.annotation.VisibleForTesting
10
9
import androidx.annotation.VisibleForTesting.PRIVATE
11
10
import androidx.savedstate.SavedStateRegistryOwner
12
11
import androidx.savedstate.ViewTreeSavedStateRegistryOwner
13
12
import com.squareup.workflow1.ui.Named
14
13
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
15
14
import com.squareup.workflow1.ui.androidx.WorkflowSavedStateRegistryAggregator
16
- import com.squareup.workflow1.ui.backstack.ViewStateCache.SavedState
17
15
import com.squareup.workflow1.ui.getRendering
18
16
19
17
/* *
20
18
* Handles persistence chores for container views that manage a set of [Named] renderings,
21
19
* showing a view for one at a time -- think back stacks or tab sets.
22
20
*
23
- * - This class implements [Parcelable] so that it can be preserved from
24
- * a container view's own [View.saveHierarchyState] method. A simple container can
25
- * return [SavedState] from that method rather than creating its own persistence class.
26
- *
27
- * - It also handles androidx [ViewTreeSavedStateRegistryOwner] duties, via
21
+ * - Provides [Parcelable]-based [save] and [restore] methods for use from a
22
+ * container's [View.onSaveInstanceState] and [View.onRestoreInstanceState] methods.
23
+ * - Also handles androidx [ViewTreeSavedStateRegistryOwner] duties, via
28
24
* a wrapped instance of [WorkflowSavedStateRegistryAggregator]. This means that container
29
25
* views using this class must call [attachToParentRegistryOwner] and
30
26
* [detachFromParentRegistry] when they are [attached][View.onAttachedToWindow] and
@@ -36,7 +32,7 @@ public class ViewStateCache
36
32
internal constructor (
37
33
@VisibleForTesting(otherwise = PRIVATE )
38
34
internal val viewStates: MutableMap <String , ViewStateFrame >
39
- ) : Parcelable {
35
+ ) {
40
36
public constructor () : this (mutableMapOf ())
41
37
42
38
private val stateRegistryAggregator = WorkflowSavedStateRegistryAggregator ()
@@ -134,77 +130,54 @@ internal constructor(
134
130
* Replaces the state of the receiver with that of [from]. Typical usage is to call this from
135
131
* a container view's [View.onRestoreInstanceState].
136
132
*/
137
- public fun restore (from : ViewStateCache ) {
133
+ public fun restore (from : Saved ) {
138
134
viewStates.clear()
139
135
viewStates + = from.viewStates
140
136
}
141
137
142
138
/* *
143
- * Convenience for use in [View.onSaveInstanceState] and [View.onRestoreInstanceState]
144
- * methods of container views that have no other state of their own to save.
145
- *
146
- * More interesting containers should create their own subclass of [BaseSavedState]
147
- * rather than trying to extend this one.
139
+ * Returns a [Parcelable] copy of the internal state of the receiver, for use with
140
+ * a container view's [View.onSaveInstanceState].
148
141
*/
149
- public class SavedState : BaseSavedState {
150
- public constructor (
151
- superState: Parcelable ? ,
152
- viewStateCache: ViewStateCache
153
- ) : super (superState) {
154
- this .viewStateCache = viewStateCache
142
+ public fun save (): Saved {
143
+ return Saved (this )
144
+ }
145
+
146
+ public class Saved : Parcelable {
147
+ internal constructor (viewStateCache: ViewStateCache ) {
148
+ this .viewStates = viewStateCache.viewStates.toMap()
155
149
}
156
150
157
- public constructor (source: Parcel ) : super (source) {
158
- this .viewStateCache = source.readParcelable(SavedState ::class .java.classLoader)!!
151
+ public constructor (source: Parcel ) {
152
+ this .viewStates = mutableMapOf<String , ViewStateFrame >()
153
+ .apply {
154
+ @Suppress(" UNCHECKED_CAST" )
155
+ source.readMap(
156
+ this as MutableMap <Any ?, Any ?>,
157
+ ViewStateCache ::class .java.classLoader
158
+ )
159
+ }
160
+ .toMap()
159
161
}
160
162
161
- public val viewStateCache: ViewStateCache
163
+ internal val viewStates: Map <String , ViewStateFrame >
164
+
165
+ override fun describeContents (): Int = 0
162
166
163
167
override fun writeToParcel (
164
168
out : Parcel ,
165
169
flags : Int
166
170
) {
167
- super .writeToParcel(out , flags)
168
- out .writeParcelable(viewStateCache, flags)
171
+ out .writeMap(viewStates)
169
172
}
170
173
171
- public companion object CREATOR : Creator<SavedState > {
172
- override fun createFromParcel (source : Parcel ): SavedState =
173
- SavedState (source)
174
+ public companion object CREATOR : Creator<Saved > {
175
+ override fun createFromParcel (source : Parcel ): Saved =
176
+ Saved (source)
174
177
175
- override fun newArray (size : Int ): Array <SavedState ?> = arrayOfNulls(size)
178
+ override fun newArray (size : Int ): Array <Saved ?> = arrayOfNulls(size)
176
179
}
177
180
}
178
-
179
- // region Parcelable
180
-
181
- override fun describeContents (): Int = 0
182
-
183
- override fun writeToParcel (
184
- parcel : Parcel ,
185
- flags : Int
186
- ) {
187
- @Suppress(" UNCHECKED_CAST" )
188
- parcel.writeMap(viewStates as MutableMap <Any ?, Any ?>)
189
- }
190
-
191
- public companion object CREATOR : Creator<ViewStateCache> {
192
- override fun createFromParcel (parcel : Parcel ): ViewStateCache {
193
- @Suppress(" UNCHECKED_CAST" )
194
- return mutableMapOf<String , ViewStateFrame >()
195
- .apply {
196
- parcel.readMap(
197
- this as MutableMap <Any ?, Any ?>,
198
- ViewStateCache ::class .java.classLoader
199
- )
200
- }
201
- .let { ViewStateCache (it) }
202
- }
203
-
204
- override fun newArray (size : Int ): Array <ViewStateCache ?> = arrayOfNulls(size)
205
- }
206
-
207
- // endregion
208
181
}
209
182
210
183
@WorkflowUiExperimentalApi
0 commit comments