Last active
October 17, 2022 22:55
-
-
Save realityexpander/19435e7b2b70ba24266fb2b049d36ca9 to your computer and use it in GitHub Desktop.
for KMM @TypeParceler article
This file contains hidden or 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
// androidMain/…/Platform.kt | |
import android.os.Parcel | |
import android.os.Parcelable | |
import kotlinx.datetime.LocalDateTime | |
import kotlinx.datetime.toLocalDateTime | |
import kotlinx.parcelize.Parceler // NOTE: kotlinx.parcelize.* | |
import kotlinx.parcelize.Parcelize | |
import kotlinx.parcelize.TypeParceler | |
actual typealias CommonParcelize = Parcelize // defined on Android, skipped on iOS | |
actual typealias CommonParcelable = Parcelable // defined on Android, skipped on iOS | |
actual typealias CommonParceler<T> = Parceler<T> // defined on Android, skipped on iOS | |
actual typealias CommonTypeParceler<T,P> = TypeParceler<T, P> // defined on Android, skipped on iOS | |
// Performs the type conversion to/from a Parcelable supported type (primitives only) | |
actual object LocalDateTimeParceler : Parceler<LocalDateTime> { // defined on Android, skipped on iOS | |
override fun create(parcel: Parcel): LocalDateTime { | |
val date = parcel.readString() | |
return date?.toLocalDateTime() | |
?: LocalDateTime(0, 0, 0, 0, 0) | |
} | |
override fun LocalDateTime.write(parcel: Parcel, flags: Int) { | |
parcel.writeString(this.toString()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See my sample project here: https://github.com/realityexpander/NoteAppKMM