Last active
March 2, 2022 21:24
-
-
Save AlexeyErofeev/0ee3a4592b3dd195b35381df4e9520ae to your computer and use it in GitHub Desktop.
"parcel hentai" - parcel with polymorphic field thrue parcelize
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.parcel_hentai | |
import android.app.Activity | |
import android.os.Bundle | |
import android.os.Parcel | |
import android.os.Parcelable | |
import android.util.Log | |
import kotlinx.android.parcel.Parceler | |
import kotlinx.android.parcel.Parcelize | |
import kotlinx.android.parcel.TypeParceler | |
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
val p = Parcel.obtain().apply { | |
writeList(listOf(Foo(Bar1(111)), Foo(Bar2("hello")), Foo(Bar()))) | |
} | |
val loader = Foo::class.java.classLoader | |
val result = mutableListOf<Any?>() | |
p.apply { | |
setDataPosition(0) | |
readList(result, loader) | |
recycle() | |
} | |
Log.d("FooResults", "[${result.joinToString(", ")}]") | |
} | |
} | |
@Parcelize | |
@TypeParceler<Bar, BarParceler>() | |
data class Foo(val bar: Bar) : Parcelable | |
@Parcelize | |
open class Bar : Parcelable { | |
override fun toString(): String = "undefinedBar" | |
} | |
@Parcelize | |
data class Bar1(val i: Int) : Bar() | |
@Parcelize | |
data class Bar2(val s: String) : Bar() | |
object BarParceler : Parceler<Bar> { | |
private const val TYPE_BAR_UNDEFINED = 0 | |
private const val TYPE_BAR1 = 1 | |
private const val TYPE_BAR2 = 2 | |
override fun create(parcel: Parcel): Bar { | |
val b = when (parcel.readInt()) { | |
TYPE_BAR1 -> Bar1(parcel.readInt()) | |
TYPE_BAR2 -> Bar2(parcel.readString()!!) | |
else -> Bar() | |
} | |
Log.d("BarParceler", "read $b") | |
return b | |
} | |
override fun Bar.write(parcel: Parcel, flags: Int) { | |
Log.d("BarParceler", "write $this") | |
when (this) { | |
is Bar1 -> { | |
parcel.writeInt(TYPE_BAR1) | |
parcel.writeInt(i) | |
} | |
is Bar2 -> { | |
parcel.writeInt(TYPE_BAR2) | |
parcel.writeString(s) | |
} | |
else -> { | |
parcel.writeInt(TYPE_BAR_UNDEFINED) | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2022-03-02 15:41:06.780 12219-12219/? D/BarParceler: write Bar1(i=111) | |
2022-03-02 15:41:06.780 12219-12219/? D/BarParceler: write Bar2(s='hello') | |
2022-03-02 15:41:06.780 12219-12219/? D/BarParceler: write undefinedBar | |
2022-03-02 15:41:06.780 12219-12219/? D/BarParceler: read Bar1(i=111) | |
2022-03-02 15:41:06.780 12219-12219/? D/BarParceler: read Bar2(s='hello') | |
2022-03-02 15:41:06.780 12219-12219/? D/BarParceler: read undefinedBar | |
2022-03-02 15:41:06.796 12219-12219/? D/FooResults: [Foo(bar=Bar1(i=111)), Foo(bar=Bar2(s='hello')), Foo(bar=undefinedBar)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment