Replies: 8 comments 2 replies
-
Hello! I never tried custom parcelers in multipaltform. But I think expect/actual should work fine. // commonMain
expect data class User(...) : Parcelable
// androidMain
@Parcelize
actual data class User(...) : Parcelable {
// Custom Parceler here
}
// Other source sets
actual data class User(...) : Parcelable Please let me know if there are any issues. |
Beta Was this translation helpful? Give feedback.
-
Closing this for now. Feel free to reopen if there are any questions. |
Beta Was this translation helpful? Give feedback.
-
Oh sorry, yes your suggestion worked 😄. From memory though, it involved a little bit of boilerplate, so a multiplatform option built into Essenty would be nice. |
Beta Was this translation helpful? Give feedback.
-
would be nice if we can support |
Beta Was this translation helpful? Give feedback.
-
@xxfast This would require expect/actual the whole // commonMain
expect interface Parceler<T>
@OptIn(ExperimentalMultiplatform::class)
@OptionalExpectation
@Target(AnnotationTarget.TYPE)
expect annotation class WriteWith<P : Parceler<*>>() // androidMain
actual typealias Parceler<T> = kotlinx.parcelize.Parceler<T>
actual typealias WriteWith<P> = kotlinx.parcelize.WriteWith<P> // nonAndroidMain - a shared source source for al targets except Android
actual interface Parceler<T> After that you can expect/actual your custom parcelers as follows. // commonMain
class ExternalClass(val value: Int)
expect object ExternalClassParceler : Parceler<ExternalClass>
@Parcelize
class MyClass(val external: @WriteWith<ExternalClassParceler> ExternalClass) : Parcelable // androidMain
actual object ExternalClassParceler : Parceler<ExternalClass> {
override fun create(parcel: Parcel): ExternalClass = ExternalClass(parcel.readInt())
override fun ExternalClass.write(parcel: Parcel, flags: Int) {
parcel.writeInt(value)
}
} // nonAndroidMain
actual object ExternalClassParceler : Parceler<ExternalClass> Yes, there is some boilerplate, but at least it works. It should be also possible to expect/actual |
Beta Was this translation helpful? Give feedback.
-
Converted this issue to a discussion for transparency. |
Beta Was this translation helpful? Give feedback.
-
I know this is a couple of months late - but just wanted to say that the above approach works 👍 thanks @arkivanov Just curious - why was |
Beta Was this translation helpful? Give feedback.
-
Version 0.4.0 is released with |
Beta Was this translation helpful? Give feedback.
-
I have an abstract class that I want to write a custom
Parceler
for per https://developer.android.com/kotlin/parcelizee.g.
Cheers.
Beta Was this translation helpful? Give feedback.
All reactions