Skip to content

Instantly share code, notes, and snippets.

@AdamSHurwitz
Last active Sep 25, 2018
Embed
What would you like to do?
Kotlin Parceable
package app.coinverse.content.models
import android.os.Parcel
import android.os.Parcelable
import androidx.room.Entity
import androidx.room.PrimaryKey
import app.coinverse.Enums.ContentType
import app.coinverse.Enums.FeedType
import java.util.*
@Entity(tableName = "content")
data class Content(@PrimaryKey var id: String, var qualityScore: Double,
var contentType: ContentType, var timestamp: Date, var isConsumed: Boolean) : Parcelable {
constructor(parcel: Parcel) : this(
parcel.readString()!!,
parcel.readDouble(),
ContentType.values()[parcel.readInt()],
Date(parcel.readLong()),
parcel.readBoolean()
) {
}
constructor() : this("", 0.0, ContentType.NONE, Date(), false)
override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeString(id)
parcel.writeDouble(qualityScore)
parcel.writeInt(contentType.ordinal)
parcel.writeLong(timestamp.time)
parcel.writeBoolean(isConsumed)
}
override fun describeContents(): Int {
return 0
}
companion object CREATOR : Parcelable.Creator<Content> {
override fun createFromParcel(parcel: Parcel): Content {
return Content(parcel)
}
override fun newArray(size: Int): Array<Content?> {
return arrayOfNulls(size)
}
fun Parcel.readBoolean() = readInt() != 0
fun Parcel.writeBoolean(value: Boolean) = writeInt(if (value) 1 else 0)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment