rename SavedState to MyParcelable

This commit is contained in:
tibbi 2017-06-23 21:50:28 +02:00
parent 5386ea8ffa
commit 0e111015bd
1 changed files with 15 additions and 15 deletions

View File

@ -216,13 +216,13 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
public override fun onSaveInstanceState(): Parcelable { public override fun onSaveInstanceState(): Parcelable {
val superState = super.onSaveInstanceState() val superState = super.onSaveInstanceState()
val savedState = SavedState(superState) val savedState = MyParcelable(superState)
savedState.paths = mPaths savedState.paths = mPaths
return savedState return savedState
} }
public override fun onRestoreInstanceState(state: Parcelable) { public override fun onRestoreInstanceState(state: Parcelable) {
if (state !is SavedState) { if (state !is MyParcelable) {
super.onRestoreInstanceState(state) super.onRestoreInstanceState(state)
return return
} }
@ -232,19 +232,20 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
pathsUpdated() pathsUpdated()
} }
internal class SavedState : View.BaseSavedState { internal class MyParcelable : View.BaseSavedState {
var paths = LinkedHashMap<MyPath, PaintOptions>() var paths = LinkedHashMap<MyPath, PaintOptions>()
companion object {
val CREATOR: Parcelable.Creator<SavedState> = object : Parcelable.Creator<SavedState> {
override fun newArray(size: Int) = arrayOf<SavedState>()
override fun createFromParcel(source: Parcel) = SavedState(source)
}
}
constructor(superState: Parcelable) : super(superState) constructor(superState: Parcelable) : super(superState)
constructor(parcel: Parcel) : super(parcel) {
val size = parcel.readInt()
for (i in 0..size - 1) {
val key = parcel.readSerializable() as MyPath
val paintOptions = PaintOptions(parcel.readInt(), parcel.readFloat())
paths.put(key, paintOptions)
}
}
override fun writeToParcel(out: Parcel, flags: Int) { override fun writeToParcel(out: Parcel, flags: Int) {
super.writeToParcel(out, flags) super.writeToParcel(out, flags)
out.writeInt(paths.size) out.writeInt(paths.size)
@ -255,12 +256,11 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
} }
} }
private constructor(parcel: Parcel) : super(parcel) { companion object {
val size = parcel.readInt() val CREATOR: Parcelable.Creator<MyParcelable> = object : Parcelable.Creator<MyParcelable> {
for (i in 0..size - 1) { override fun createFromParcel(source: Parcel) = MyParcelable(source)
val key = parcel.readSerializable() as MyPath
val paintOptions = PaintOptions(parcel.readInt(), parcel.readFloat()) override fun newArray(size: Int) = arrayOf<MyParcelable>()
paths.put(key, paintOptions)
} }
} }
} }