I have an existing Android project which I am migr...
# kotlin-native
j
I have an existing Android project which I am migrating to K/N. First thing is to move the domain object layer to the K/N common module so the the to-be developed iOS can already start using this code. This approach seems to work. I have replaced a third-party DateTime library with the K/N Klock library (https://github.com/korlibs/klock). The existing Android app uses Java serialization to pass objects between activities. I created A an expect interface KSerializable with an actual typealias to Serializable in the Android source tree. This works, domain objects in the common can now be serialized using Java serialization in the Android app. The Klock DateTime object is, however, not serializable. this class does not implement KSerializable. Is there an approach to use Java serialization mechanism without much rewrite (this mechanism (passing serialized objects to activities) is used a lot in this application)? A possible solution could be creating Parcelable and pass the data as Parcelable, perhaps use a Json representation as the Parcelable object (using readString and writeString). Thanks!
😎 1
a
First, I would reccomend using something like https://github.com/erikc5000/island-time instead of Klock as it actually follows the java.time API
👌 1
second, if you need to serialize a date, why not just convert to milliseconds then rebuild the date. Otherwise you can used kotlinx serialization is your mpp library and then just deal with Strings in your interfaces
👍 1
💯 1
j
Thanks, I did not know about the island-time lib. Using milliseconds can be an option. I am integrating kotlinx serialization also and will take a look at the serialization mechanisms.
e
I'm the author of Island Time. Probably the easiest way is to just pass strings. You can also create custom parcelers using the Kotlin Android Extensions. At some point in the future, I'd like to add an artifact that has some ready to go, but they're a little more work. Aside from seeking feedback, I haven't been promoting the library really since it's still early days and the API is unstable. Consider yourself warned. 🙂 Shooting for an initial versioned release in the next couple weeks though, after which, I'll be a bit more careful about breaking things, but it'll still be "moving fast" for a while.
j
Thanks. I decided to leave the Klock library in it for now since that does what I need. It seems stable enough at the moment (coming from K/N 1.3.21 to 1.3.60). I kept the representations for the DateTime as objects and use a custom Parcelable which uses JSON to transform from and to the object representation. The Parcelable acts as a thin wrapper. This allows me to use existing code from K/N and pass it around activities with minimal extra work. See the following example (still uses Gson but will be converted to kotlinx serialization when the service layer is converted from Retrofit to Ktor):
class ParcelableWeekItem(val item: WeekItem): Parcelable {
override fun writeToParcel(dest: Parcel, flags: Int) {
dest.writeString(gson.toJson(item))
}
override fun describeContents() = 0
companion object CREATOR : Parcelable.Creator<ParcelableWeekItem> {
override fun createFromParcel(parcel: Parcel): ParcelableWeekItem? {
val json = parcel.readString()
return if (json != null) {
ParcelableWeekItem(gson.fromJson(json, WeekItem::class.java))
} else {
null
}
}
override fun newArray(size: Int) = arrayOfNulls<ParcelableWeekItem>(size)
}
}
s
FYI, to pass an object between view controllers in iOS only requires passing a reference, no serialising necessary.
👍 1