0

I have already a Parcelable data class which is used across the module:

@Keep
data class TotalProductValue(val header: String?, val prices: Pricing?, val cta: Cta?) : Parcelable {

    constructor(parcel: Parcel) : this(
            parcel.readString(),
            parcel.readParcelable(Pricing::class.java.classLoader),
            parcel.readParcelable(Cta::class.java.classLoader),
    )

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeString(header)
        parcel.writeString(subHeader)
        parcel.writeParcelable(prices, flags)
        parcel.writeParcelable(cta, flags)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<TotalProductValue> {
        override fun createFromParcel(parcel: Parcel): TotalProductValue {
            return TotalProductValue(parcel)
        }

        override fun newArray(size: Int): Array<TotalProductValue?> {
            return arrayOfNulls(size)
        }
    }
}

Now I want to add one more parameter as below without impacting other places where it's used, I tried default value and parcel.dataAvail() below but it didn't work. Is there any work around without creating another data class ?

@Keep
data class TotalProductValue(val header: String?, val subHeader: String? = null, val prices: Pricing?, val cta: Cta?) : Parcelable {

    constructor(parcel: Parcel) : this(
            parcel.readString(),
            if (parcel.dataAvail() > 0) parcel.readString() else null,
            parcel.readParcelable(Pricing::class.java.classLoader),
            parcel.readParcelable(Cta::class.java.classLoader),
    )

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeString(header)
        parcel.writeString(subHeader)
        parcel.writeParcelable(prices, flags)
        parcel.writeParcelable(cta, flags)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<TotalProductValue> {
        override fun createFromParcel(parcel: Parcel): TotalProductValue {
            return TotalProductValue(parcel)
        }

        override fun newArray(size: Int): Array<TotalProductValue?> {
            return arrayOfNulls(size)
        }
    }
}
pandey_shubham
  • 423
  • 5
  • 14

0 Answers0