Slide 1

Slide 1 text

zsmb.co zsmb13 A Kotlin library design case study ft Krate Moshi Márton Braun

Slide 2

Slide 2 text

Krate AutSoft/Krate Moshi square/moshi

Slide 3

Slide 3 text

Krate AutSoft/Krate

Slide 4

Slide 4 text

Krate interface Krate { val sharedPreferences: SharedPreferences }

Slide 5

Slide 5 text

Krate interface Krate { val sharedPreferences: SharedPreferences } abstract class SimpleKrate(context: Context) : Krate { override val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) }

Slide 6

Slide 6 text

interface Krate { val sharedPreferences: SharedPreferences } Krate class MyKrate(context: Context) : SimpleKrate(context) { var onboarded by booleanPref("onboarded") var appOpenCount by intPref("appOpenCount") var username by stringPref("username") } abstract class SimpleKrate(context: Context) : Krate { override val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) }

Slide 7

Slide 7 text

interface Krate { val sharedPreferences: SharedPreferences } Krate myKrate.onboarded = true myKrate.appOpenCount++ myKrate.username = "t1gg3r" class MyKrate(context: Context) : SimpleKrate(context) { var onboarded by booleanPref("onboarded") var appOpenCount by intPref("appOpenCount") var username by stringPref("username") } abstract class SimpleKrate(context: Context) : Krate { override val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) }

Slide 8

Slide 8 text

Krate myKrate.onboarded = true myKrate.appOpenCount++ myKrate.username = "t1gg3r" var onboarded by booleanPref("onboarded") var username by stringPref("username") abstract class SimpleKrate(context: Context) : Krate { override val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) } } var appOpenCount by intPref("appOpenCount") class MyKrate(context: Context) : SimpleKrate(context) {

Slide 9

Slide 9 text

Krate class MyKrate(context: Context) : SimpleKrate(context) { var appOpenCount by intPref("appOpenCount") }

Slide 10

Slide 10 text

Krate class MyKrate(context: Context) : SimpleKrate(context) { var appOpenCount by IntDelegate("appOpenCount") }

Slide 11

Slide 11 text

Krate class MyKrate(context: Context) : SimpleKrate(context) { var appOpenCount by IntDelegate("appOpenCount") } class IntDelegate(private val key: String)

Slide 12

Slide 12 text

Krate class MyKrate(context: Context) : SimpleKrate(context) { var appOpenCount by IntDelegate("appOpenCount") } class IntDelegate(private val key: String) : ReadWriteProperty { }

Slide 13

Slide 13 text

Krate class MyKrate(context: Context) : SimpleKrate(context) { var appOpenCount by IntDelegate("appOpenCount") } class IntDelegate(private val key: String) : ReadWriteProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Int { } override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: Int) { } }

Slide 14

Slide 14 text

Krate class MyKrate(context: Context) : SimpleKrate(context) { var appOpenCount by IntDelegate("appOpenCount") } class IntDelegate(private val key: String) : ReadWriteProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Int { } override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: Int) { } }

Slide 15

Slide 15 text

Krate class MyKrate(context: Context) : SimpleKrate(context) { var appOpenCount: Int by IntDelegate("appOpenCount") } class IntDelegate(private val key: String) : ReadWriteProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Int { } override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: Int) { } }

Slide 16

Slide 16 text

Krate class IntDelegate(private val key: String) : ReadWriteProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Int { } override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: Int) { } } class MyKrate(context: Context) : SimpleKrate(context) { var appOpenCount by IntDelegate("appOpenCount") }

Slide 17

Slide 17 text

Krate class IntDelegate(private val key: String) : ReadWriteProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Int { return thisRef.sharedPreferences.getInt(key, 0) } override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: Int) { thisRef.sharedPreferences.edit().putInt(key, value).apply() } } class MyKrate(context: Context) : SimpleKrate(context) { var appOpenCount by IntDelegate("appOpenCount") }

Slide 18

Slide 18 text

Krate class MyKrate(context: Context) : SimpleKrate(context) { var appOpenCount by IntDelegate("appOpenCount") } class IntDelegate(private val key: String) : ReadWriteProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Int { return thisRef.sharedPreferences.getInt(key, 0) } override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: Int) { thisRef.sharedPreferences.edit().putInt(key, value).apply() } } fun Krate.intPref(key: String): ReadWriteProperty { return IntDelegate(key) }

Slide 19

Slide 19 text

Krate class MyKrate(context: Context) : SimpleKrate(context) { var appOpenCount by intPref("appOpenCount") } class IntDelegate(private val key: String) : ReadWriteProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Int { return thisRef.sharedPreferences.getInt(key, 0) } override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: Int) { thisRef.sharedPreferences.edit().putInt(key, value).apply() } } fun Krate.intPref(key: String): ReadWriteProperty { return IntDelegate(key) }

Slide 20

Slide 20 text

Krate AutSoft/Krate

Slide 21

Slide 21 text

Moshi square/moshi

Slide 22

Slide 22 text

Moshi

Slide 23

Slide 23 text

Moshi › Gson v3 in everything but name

Slide 24

Slide 24 text

Moshi › Gson v3 in everything but name › Part of the Ok libraries Okio Retrofit OkHttp Moshi

Slide 25

Slide 25 text

Moshi › Gson v3 in everything but name › Part of the Ok libraries › Streaming API Okio Retrofit OkHttp Moshi

Slide 26

Slide 26 text

Moshi › Gson v3 in everything but name › Part of the Ok libraries › Streaming API

Slide 27

Slide 27 text

Moshi › Gson v3 in everything but name › Part of the Ok libraries › Streaming API › Nullable values

Slide 28

Slide 28 text

Moshi › Gson v3 in everything but name › Part of the Ok libraries › Streaming API › Nullable values @JsonClass(generateAdapter = true) data class Burger( val name: String, val description: String, )

Slide 29

Slide 29 text

Moshi › Gson v3 in everything but name › Part of the Ok libraries › Streaming API › Nullable values @JsonClass(generateAdapter = true) data class Burger( val name: String, val description: String, ) val moshi = Moshi.Builder().build()

Slide 30

Slide 30 text

Moshi › Gson v3 in everything but name › Part of the Ok libraries › Streaming API › Nullable values @JsonClass(generateAdapter = true) data class Burger( val name: String, val description: String, ) val moshi = Moshi.Builder().build() val burgerAdapter = moshi.adapter(Burger::class.java)

Slide 31

Slide 31 text

Moshi › Gson v3 in everything but name › Part of the Ok libraries › Streaming API › Nullable values @JsonClass(generateAdapter = true) data class Burger( val name: String, val description: String, ) val moshi = Moshi.Builder().build() val burgerAdapter = moshi.adapter(Burger::class.java) val burger = burgerAdapter.fromJson( "{\"name\": \"Cheeseburger\"}" )

Slide 32

Slide 32 text

Moshi › Gson v3 in everything but name › Part of the Ok libraries › Streaming API › Nullable values @JsonClass(generateAdapter = true) data class Burger( val name: String, val description: String, ) val moshi = Moshi.Builder().build() val burgerAdapter = moshi.adapter(Burger::class.java) val burger = burgerAdapter.fromJson( "{\"name\": \"Cheeseburger\"}" ) Exception in thread "main" com.squareup.moshi.JsonDataException: Required value 'description' missing at $

Slide 33

Slide 33 text

Moshi › Gson v3 in everything but name › Part of the Ok libraries › Streaming API › Nullable values @JsonClass(generateAdapter = true) data class Burger( val name: String, val description: String?, ) val moshi = Moshi.Builder().build() val burgerAdapter = moshi.adapter(Burger::class.java) val burger = burgerAdapter.fromJson( "{\"name\": \"Cheeseburger\"}" )

Slide 34

Slide 34 text

Moshi › Gson v3 in everything but name › Part of the Ok libraries › Streaming API › Nullable values @JsonClass(generateAdapter = true) data class Burger( val name: String, val description: String ) val moshi = Moshi.Builder().build() val burgerAdapter = moshi.adapter(Burger::class.java) val burger = burgerAdapter.fromJson( "{\"name\": \"Cheeseburger\"}" ) Burger(name=Cheeseburger, description=null) ?,

Slide 35

Slide 35 text

Moshi › Gson v3 in everything but name › Part of the Ok libraries › Streaming API › Nullable values @JsonClass(generateAdapter = true) data class Burger( val name: String, val description: String ) val moshi = Moshi.Builder().build() val burgerAdapter = moshi.adapter(Burger::class.java) val burger = burgerAdapter.fromJson( "{\"name\": \"Cheeseburger\"}" ) › Default parameter values ?,

Slide 36

Slide 36 text

Moshi › Gson v3 in everything but name › Part of the Ok libraries › Streaming API › Nullable values › Default parameter values val moshi = Moshi.Builder().build() val burgerAdapter = moshi.adapter(Burger::class.java) val burger = burgerAdapter.fromJson( "{\"name\": \"Cheeseburger\"}" ) @JsonClass(generateAdapter = true) data class Burger( val name: String, val description: String ) , = "Yummy!"

Slide 37

Slide 37 text

Moshi › Gson v3 in everything but name › Part of the Ok libraries › Streaming API › Nullable values › Default parameter values val moshi = Moshi.Builder().build() val burgerAdapter = moshi.adapter(Burger::class.java) val burger = burgerAdapter.fromJson( "{\"name\": \"Cheeseburger\"}" ) Burger(name=Cheeseburger, description=Yummy!) @JsonClass(generateAdapter = true) data class Burger( val name: String, val description: String ) , = "Yummy!"

Slide 38

Slide 38 text

Moshi › Gson v3 in everything but name › Part of the Ok libraries › Streaming API › Nullable values › Default parameter values › Codegen, reflection, or both @JsonClass(generateAdapter = true) data class Burger( val name: String, val description: String = "Yummy!", ) val moshi = Moshi.Builder().build()

Slide 39

Slide 39 text

Moshi › Gson v3 in everything but name › Part of the Ok libraries › Streaming API › Nullable values › Default parameter values › Codegen, reflection, or both data class Burger( val name: String, val description: String = "Yummy!", ) val moshi = Moshi.Builder().build()

Slide 40

Slide 40 text

Moshi › Gson v3 in everything but name › Part of the Ok libraries › Streaming API › Nullable values › Default parameter values › Codegen, reflection, or both data class Burger( val name: String, val description: String = "Yummy!", ) val moshi = Moshi.Builder() .add(KotlinJsonAdapterFactory()) .build()

Slide 41

Slide 41 text

Moshi › Gson v3 in everything but name › Part of the Ok libraries › Streaming API › Nullable values › Default parameter values › Codegen, reflection, or both @JsonClass(generateAdapter = true) data class Burger( val name: String, val description: String = "Yummy!", ) val moshi = Moshi.Builder() .add(KotlinJsonAdapterFactory()) .build()

Slide 42

Slide 42 text

Moshi › Gson v3 in everything but name › Part of the Ok libraries › Streaming API › Nullable values › Default parameter values › Codegen, reflection, or both › Always calls constructors @JsonClass(generateAdapter = true) data class Burger( val name: String, val description: String = "Yummy!", ) { init { println("$name coming up...") } }

Slide 43

Slide 43 text

Moshi › Gson v3 in everything but name › Part of the Ok libraries › Streaming API › Nullable values › Default parameter values › Always calls constructors › Named after Jake’s dog › Codegen, reflection, or both

Slide 44

Slide 44 text

Moshi › Gson v3 in everything but name › Part of the Ok libraries › Streaming API › Nullable values › Default parameter values › Always calls constructors › Named after Jake’s dog › Codegen, reflection, or both

Slide 45

Slide 45 text

Storing complex data

Slide 46

Slide 46 text

DISCLAIMER Consider using a database before storing data like this in SharedPreferences

Slide 47

Slide 47 text

Gson delegate

Slide 48

Slide 48 text

Gson delegate fun Krate.gsonPref(key: String): ReadWriteProperty { return GsonDelegate(key) }

Slide 49

Slide 49 text

Gson delegate class GsonDelegate( private val key: String, ) : ReadWriteProperty fun Krate.gsonPref(key: String): ReadWriteProperty { return GsonDelegate(key) }

Slide 50

Slide 50 text

Gson delegate class GsonDelegate( private val key: String, ) : ReadWriteProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): T? { } override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: T?) { } } fun Krate.gsonPref(key: String): ReadWriteProperty { return GsonDelegate(key) }

Slide 51

Slide 51 text

Gson delegate class GsonDelegate( private val key: String, ) : ReadWriteProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): T? { val gson = Gson() val string = thisRef.sharedPreferences.getString(key, null) return gson.fromJson(string, object : TypeToken() {}.type) } override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: T?) { } } fun Krate.gsonPref(key: String): ReadWriteProperty { return GsonDelegate(key) }

Slide 52

Slide 52 text

Gson delegate class GsonDelegate( private val key: String, ) : ReadWriteProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): T? { val gson = Gson() val string = thisRef.sharedPreferences.getString(key, null) return gson.fromJson(string, object : TypeToken() {}.type) } override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: T?) { val gson = Gson() thisRef.sharedPreferences.edit() .putString(key, gson.toJson(value)) .apply() } } fun Krate.gsonPref(key: String): ReadWriteProperty { return GsonDelegate(key) }

Slide 53

Slide 53 text

Gson delegate class GsonDelegate( private val key: String, ) : ReadWriteProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): T? { val gson = Gson() val string = thisRef.sharedPreferences.getString(key, null) return gson.fromJson(string, object : TypeToken() {}.type) } override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: T?) { val gson = Gson() thisRef.sharedPreferences.edit() .putString(key, gson.toJson(value)) .apply() } } fun Krate.gsonPref(key: String): ReadWriteProperty { return GsonDelegate(key) } java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to hu.autsoft.krate.gson.TestModel

Slide 54

Slide 54 text

Gson delegate class GsonDelegate( private val key: String, ) : ReadWriteProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): T? { val gson = Gson() val string = thisRef.sharedPreferences.getString(key, null) return gson.fromJson(string, object : TypeToken() {}.type) } override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: T?) { val gson = Gson() thisRef.sharedPreferences.edit() .putString(key, gson.toJson(value)) .apply() } } fun Krate.gsonPref(key: String): ReadWriteProperty { return GsonDelegate(key) }

Slide 55

Slide 55 text

Gson delegate class GsonDelegate( private val key: String, ) : ReadWriteProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): T? { val gson = Gson() val string = thisRef.sharedPreferences.getString(key, null) return gson.fromJson(string, object : TypeToken() {}.type) } override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: T?) { val gson = Gson() thisRef.sharedPreferences.edit() .putString(key, gson.toJson(value)) .apply() } } fun Krate.gsonPref(key: String): ReadWriteProperty { return GsonDelegate(key) }

Slide 56

Slide 56 text

Gson delegate class GsonDelegate( private val key: String, ) : ReadWriteProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Object? { val gson = Gson() val string = thisRef.sharedPreferences.getString(key, null) return gson.fromJson(string, object : TypeToken() {}.type) } override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: Object?) { val gson = Gson() thisRef.sharedPreferences.edit() .putString(key, gson.toJson(value)) .apply() } } fun Krate.gsonPref(key: String): ReadWriteProperty { return GsonDelegate(key) }

Slide 57

Slide 57 text

Object T Gson delegate class GsonDelegate( private val key: String, ) : ReadWriteProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): T? { val gson = Gson() val string = thisRef.sharedPreferences.getString(key, null) return gson.fromJson(string, object : TypeToken() {}.type) } override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: T?) { val gson = Gson() thisRef.sharedPreferences.edit() .putString(key, gson.toJson(value)) .apply() } } inline fun ?> { return GsonDelegate(key) } Krate.gsonPref(key: String): ReadWriteProperty

Slide 58

Slide 58 text

inline fun ?> { return GsonDelegate(key) } Object T Gson delegate class GsonDelegate( private val key: String, ) : ReadWriteProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): T? { val gson = Gson() val string = thisRef.sharedPreferences.getString(key, null) return gson.fromJson(string, object : TypeToken() {}.type) } override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: T?) { val gson = Gson() thisRef.sharedPreferences.edit() .putString(key, gson.toJson(value)) .apply() } } Krate.gsonPref(key: String): ReadWriteProperty

Slide 59

Slide 59 text

Gson delegate class GsonDelegate( private val key: String, ) : ReadWriteProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): T? { val gson = Gson() val string = thisRef.sharedPreferences.getString(key, null) return gson.fromJson(string, object : TypeToken() {}.type) } override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: T?) { val gson = Gson() thisRef.sharedPreferences.edit() .putString(key, gson.toJson(value)) .apply() } } inline fun Krate.gsonPref(key: String): ReadWriteProperty { return GsonDelegate(key, object : TypeToken() {}.type) }

Slide 60

Slide 60 text

inline fun Krate.gsonPref(key: String): ReadWriteProperty { return GsonDelegate(key, object : TypeToken() {}.type) } Gson delegate } override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: T?) { val gson = Gson() thisRef.sharedPreferences.edit() .putString(key, gson.toJson(value)) .apply() } } class GsonDelegate( private val key: String, ) : ReadWriteProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): T? { val gson = Gson() val string = thisRef.sharedPreferences.getString(key, null) return gson.fromJson(string, ) private val type: Type, type

Slide 61

Slide 61 text

Testing Krate

Slide 62

Slide 62 text

Gson delegate tests data class TestModel(val x: Int, val y: Double, val z: String)

Slide 63

Slide 63 text

Gson delegate tests class GsonTestKrate(context: Context) : SimpleKrate(context) { var simpleValue: TestModel? by gsonPref("simpleValue") var listOfValues: List? by gsonPref("listOfValues") } data class TestModel(val x: Int, val y: Double, val z: String)

Slide 64

Slide 64 text

Gson delegate tests class GsonTestKrate(context: Context) : SimpleKrate(context) { var simpleValue: TestModel? by gsonPref("simpleValue") var listOfValues: List? by gsonPref("listOfValues") } data class TestModel(val x: Int, val y: Double, val z: String) val krate = GsonTestKrate(targetContext)

Slide 65

Slide 65 text

Gson delegate tests class GsonTestKrate(context: Context) : SimpleKrate(context) { var simpleValue: TestModel? by gsonPref("simpleValue") var listOfValues: List? by gsonPref("listOfValues") } @Test fun testSimpleValue() { val model = TestModel(x = 42, y = 3.141592, z = "shibboleth") krate.simpleValue = model assertEquals(model, krate.simpleValue) } data class TestModel(val x: Int, val y: Double, val z: String) val krate = GsonTestKrate(targetContext)

Slide 66

Slide 66 text

Gson delegate tests class GsonTestKrate(context: Context) : SimpleKrate(context) { var simpleValue: TestModel? by gsonPref("simpleValue") var listOfValues: List? by gsonPref("listOfValues") } @Test fun testSimpleValue() { val model = TestModel(x = 42, y = 3.141592, z = "shibboleth") krate.simpleValue = model assertEquals(model, krate.simpleValue) } data class TestModel(val x: Int, val y: Double, val z: String) val krate = GsonTestKrate(targetContext)

Slide 67

Slide 67 text

Gson delegate tests class GsonTestKrate(context: Context) : SimpleKrate(context) { var simpleValue: TestModel? by gsonPref("simpleValue") var listOfValues: List? by gsonPref("listOfValues") } @Test fun testListOfValues() { val list = listOf( TestModel(x = 42, y = 3.141592, z = "shibboleth"), TestModel(x = 13, y = 6.283185, z = "signal"), ) krate.listOfValues = list assertEquals(list, krate.listOfValues) } data class TestModel(val x: Int, val y: Double, val z: String) val krate = GsonTestKrate(targetContext)

Slide 68

Slide 68 text

Gson delegate tests class GsonTestKrate(context: Context) : SimpleKrate(context) { var simpleValue: TestModel? by gsonPref("simpleValue") var listOfValues: List? by gsonPref("listOfValues") } @Test fun testListOfValues() { val list = listOf( TestModel(x = 42, y = 3.141592, z = "shibboleth"), TestModel(x = 13, y = 6.283185, z = "signal"), ) krate.listOfValues = list assertEquals(list, krate.listOfValues) } data class TestModel(val x: Int, val y: Double, val z: String) val krate = GsonTestKrate(targetContext)

Slide 69

Slide 69 text

ft Krate Moshi

Slide 70

Slide 70 text

class GsonDelegate( private val key: String, private val type: Type, ) : ReadWriteProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): T? { val gson = Gson() val string = thisRef.sharedPreferences.getString(key, null) return gson.fromJson(string, type) } override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: T?) { val gson = Gson() thisRef.sharedPreferences.edit() .putString(key, gson.toJson(value)) .apply() } } inline fun Krate.gsonPref(key: String): ReadWriteProperty { return GsonDelegate(key, object : TypeToken() {}.type) } Moshi delegate

Slide 71

Slide 71 text

inline fun Krate.gsonPref(key: String): ReadWriteProperty { return GsonDelegate(key, object : TypeToken() {}.type) } Moshi delegate gson Gson() = .fromJson(string ) , type gson class MoshiDelegate( private val key: String, private val type: Type, ) : ReadWriteProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): T? { val val string = thisRef.sharedPreferences.getString(key, null) return } override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: T?) { val gson = Gson() thisRef.sharedPreferences.edit() .putString(key, gson.toJson(value)) .apply() } }

Slide 72

Slide 72 text

inline fun Krate.gsonPref(key: String): ReadWriteProperty { return GsonDelegate(key, object : TypeToken() {}.type) } Moshi delegate moshi.adapter(type) = adapter .fromJson(string) adapter class MoshiDelegate( private val key: String, private val type: Type, ) : ReadWriteProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): T? { val val string = thisRef.sharedPreferences.getString(key, null) return } override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: T?) { val gson = Gson() thisRef.sharedPreferences.edit() .putString(key, gson.toJson(value)) .apply() } }

Slide 73

Slide 73 text

class MoshiDelegate( private val key: String, private val type: Type, ) : ReadWriteProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): T? { val adapter = moshi.adapter(type) val string = thisRef.sharedPreferences.getString(key, null) return adapter.fromJson(string) } override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: T?) { val adapter = moshi.adapter(type) thisRef.sharedPreferences.edit() .putString(key, adapter.toJson(value)) .apply() } } inline fun Krate.gsonPref(key: String): ReadWriteProperty { return GsonDelegate(key, object : TypeToken() {}.type) } Moshi delegate

Slide 74

Slide 74 text

class MoshiDelegate( private val key: String, private val type: Type, ) : ReadWriteProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): T? { val adapter = moshi.adapter(type) val string = thisRef.sharedPreferences.getString(key, null) return adapter.fromJson(string) } override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: T?) { val adapter = moshi.adapter(type) thisRef.sharedPreferences.edit() .putString(key, adapter.toJson(value)) .apply() } } inline fun Krate.moshiPref(key: String): ReadWriteProperty { return MoshiDelegate(key, object : TypeToken() {}.type) } Moshi delegate

Slide 75

Slide 75 text

class MoshiDelegate( private val key: String, private val type: Type, ) : ReadWriteProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): T? { val adapter = moshi.adapter(type) val string = thisRef.sharedPreferences.getString(key, null) return adapter.fromJson(string) } override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: T?) { val adapter = moshi.adapter(type) thisRef.sharedPreferences.edit() .putString(key, adapter.toJson(value)) .apply() } } inline fun Krate.moshiPref(key: String): ReadWriteProperty { return MoshiDelegate(key, object : TypeToken() {}.type) } Moshi delegate

Slide 76

Slide 76 text

inline fun Krate.moshiPref(key: String): ReadWriteProperty { return MoshiDelegate(key, } class MoshiDelegate( private val key: String, private val type: Type, ) : ReadWriteProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): T? { val adapter = moshi.adapter(type) val string = thisRef.sharedPreferences.getString(key, null) return adapter.fromJson(string) } override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: T?) { val adapter = moshi.adapter(type) thisRef.sharedPreferences.edit() .putString(key, adapter.toJson(value)) .apply() } } Moshi delegate ???)

Slide 77

Slide 77 text

Class

Slide 78

Slide 78 text

java.lang.Class

Slide 79

Slide 79 text

java.lang.Class classes and interfaces in a running Java application

Slide 80

Slide 80 text

java.lang.Class classes and interfaces in a running Java application Class c1 = object.getClass();

Slide 81

Slide 81 text

java.lang.Class classes and interfaces in a running Java application Class c1 = object.getClass(); any.javaClass // Kotlin

Slide 82

Slide 82 text

java.lang.Class classes and interfaces in a running Java application Class c1 = object.getClass(); Class c2 = String.class; any.javaClass // Kotlin

Slide 83

Slide 83 text

java.lang.Class classes and interfaces in a running Java application Class c1 = object.getClass(); Class c2 = String.class; any.javaClass "hello world".getClass(); // class java.lang.String new ArrayList().getClass(); // class java.util.ArrayList new Serializable() {}.getClass(); // class com.example.Main$1 // Kotlin

Slide 84

Slide 84 text

java.lang.Class classes and interfaces in a running Java application Class c1 = object.getClass(); Class c2 = String.class; any.javaClass c.getName(); c.getSimpleName(); c.getCanonicalName(); c.getTypeName(); // Kotlin [I int[] int[] int[] java.lang.String String java.lang.String java.lang.String com.example.Main$1 null com.example.Main$1

Slide 85

Slide 85 text

java.lang.Class classes and interfaces in a running Java application Class c1 = object.getClass(); Class c2 = String.class; any.javaClass c.getName(); c.getSimpleName(); c.getCanonicalName(); c.getTypeName(); Annotation[] annotations = c.getAnnotations(); Class[] interfaces = c.getInterfaces(); Constructor[] constructors = c.getConstructors(); Field[] fields = c.getFields(); Method[] methods = c.getMethods(); // Kotlin [I int[] int[] int[] java.lang.String String java.lang.String java.lang.String com.example.Main$1 null com.example.Main$1

Slide 86

Slide 86 text

java.lang.reflect.Type

Slide 87

Slide 87 text

java.lang.reflect.Type all types in the Java programming language - raw types - parameterized types - array types - type variables - primitive types

Slide 88

Slide 88 text

java.lang.reflect.Type all types in the Java programming language - raw types - parameterized types - array types - type variables - primitive types String getTypeName();

Slide 89

Slide 89 text

java.lang.reflect.Type all types in the Java programming language - raw types - parameterized types - array types - type variables - primitive types Class String getTypeName();

Slide 90

Slide 90 text

java.lang.reflect.Type all types in the Java programming language - raw types - parameterized types - array types - type variables - primitive types Class ParameterizedType Type getRawType(); Type[] getActualTypeArguments(); String getTypeName();

Slide 91

Slide 91 text

Class Type No generics Supports generics Java reflection

Slide 92

Slide 92 text

Class Type No generics Supports generics Java reflection Kotlin reflection

Slide 93

Slide 93 text

Class Type No generics Supports generics Java reflection Kotlin reflection KClass

Slide 94

Slide 94 text

kotlin.reflect.KClass

Slide 95

Slide 95 text

kotlin.reflect.KClass val k1: KClass<*> = any::class val k2: KClass<*> = String::class

Slide 96

Slide 96 text

kotlin.reflect.KClass val simpleName: String? val qualifiedName: String? val members: Collection> val constructors: Collection> val nestedClasses: Collection> val typeParameters: List val supertypes: List val sealedSubclasses: List> val visibility: KVisibility? val k1: KClass<*> = any::class val k2: KClass<*> = String::class

Slide 97

Slide 97 text

kotlin.reflect.KClass val k1: KClass<*> = any::class val k2: KClass<*> = String::class val isFinal: Boolean val isOpen: Boolean val isAbstract: Boolean val isSealed: Boolean val isData: Boolean val isInner: Boolean val isCompanion: Boolean val isFun: Boolean

Slide 98

Slide 98 text

val c: Class<*> = String::class.java kotlin.reflect.KClass val k1: KClass<*> = any::class val k2: KClass<*> = String::class

Slide 99

Slide 99 text

val c: Class<*> = String::class.java kotlin.reflect. val k1: KClass<*> = any::class val k2: KClass<*> = String::class String.class // Java // Kotlin KClass

Slide 100

Slide 100 text

Class Type No generics Supports generics Java reflection Kotlin reflection KClass

Slide 101

Slide 101 text

Class Type No generics Supports generics Java reflection Kotlin reflection KClass KType ParameterizedType

Slide 102

Slide 102 text

Class Type No generics Supports generics Java reflection Kotlin reflection KClass KType ParameterizedType

Slide 103

Slide 103 text

Class Type No generics Supports generics Java reflection Kotlin reflection KClass KType rawType actualTypeArguments ParameterizedType

Slide 104

Slide 104 text

Class Type No generics Supports generics Java reflection Kotlin reflection KClass KType rawType actualTypeArguments ParameterizedType classifier arguments

Slide 105

Slide 105 text

rawType actualTypeArguments ParameterizedType classifier arguments isMarkedNullable Class Type No generics Supports generics Java reflection Kotlin reflection KClass KType

Slide 106

Slide 106 text

rawType actualTypeArguments ParameterizedType classifier arguments Class Type No generics Supports generics Java reflection Kotlin reflection KClass KType

Slide 107

Slide 107 text

Looking for a Type inline fun Krate.moshiPref(key: String): ReadWriteProperty { return MoshiDelegate(key, ???) } class MoshiDelegate( private val key: String, private val type: Type, ) : ReadWriteProperty { ... }

Slide 108

Slide 108 text

Looking for a Type inline fun Krate.moshiPref(key: String): ReadWriteProperty { return MoshiDelegate(key, T::class.java) } class MoshiDelegate( private val key: String, private val type: Type, ) : ReadWriteProperty { ... }

Slide 109

Slide 109 text

Looking for a Type inline fun Krate.moshiPref(key: String): ReadWriteProperty { return MoshiDelegate(key, T::class.java) } class MoshiDelegate( private val key: String, private val type: Type, ) : ReadWriteProperty { ... }

Slide 110

Slide 110 text

Moshi delegate tests class GsonTestKrate(context: Context) : SimpleKrate(context) { var simpleValue: TestModel? by gsonPref("simpleValue") var listOfValues: List? by gsonPref("listOfValues") } @Test fun testSimpleValue() { val model = TestModel(x = 42, y = 3.141592, z = "shibboleth") krate.simpleValue = model assertEquals(model, krate.simpleValue) } data class TestModel(val x: Int, val y: Double, val z: String) val krate = GsonTestKrate(targetContext)

Slide 111

Slide 111 text

Moshi delegate tests class MoshiTestKrate(context: Context) : SimpleKrate(context) { var simpleValue: TestModel? by moshiPref("simpleValue") var listOfValues: List? by moshiPref("listOfValues") } @Test fun testSimpleValue() { val model = TestModel(x = 42, y = 3.141592, z = "shibboleth") krate.simpleValue = model assertEquals(model, krate.simpleValue) } data class TestModel(val x: Int, val y: Double, val z: String) val krate = GsonTestKrate(targetContext)

Slide 112

Slide 112 text

Moshi delegate tests class MoshiTestKrate(context: Context) : SimpleKrate(context) { var simpleValue: TestModel? by moshiPref("simpleValue") var listOfValues: List? by moshiPref("listOfValues") } @Test fun testSimpleValue() { val model = TestModel(x = 42, y = 3.141592, z = "shibboleth") krate.simpleValue = model assertEquals(model, krate.simpleValue) } data class TestModel(val x: Int, val y: Double, val z: String) val krate = MoshiTestKrate(targetContext)

Slide 113

Slide 113 text

Moshi delegate tests class MoshiTestKrate(context: Context) : SimpleKrate(context) { var simpleValue: TestModel? by moshiPref("simpleValue") var listOfValues: List? by moshiPref("listOfValues") } @Test fun testSimpleValue() { val model = TestModel(x = 42, y = 3.141592, z = "shibboleth") krate.simpleValue = model assertEquals(model, krate.simpleValue) } data class TestModel(val x: Int, val y: Double, val z: String) val krate = MoshiTestKrate(targetContext)

Slide 114

Slide 114 text

Moshi delegate tests class MoshiTestKrate(context: Context) : SimpleKrate(context) { var simpleValue: TestModel? by moshiPref("simpleValue") var listOfValues: List? by moshiPref("listOfValues") } @Test fun testListOfValues() { val list = listOf( TestModel(x = 42, y = 3.141592, z = "shibboleth"), TestModel(x = 13, y = 6.283185, z = "signal"), ) krate.listOfValues = list assertEquals(list, krate.listOfValues) } data class TestModel(val x: Int, val y: Double, val z: String) val krate = MoshiTestKrate(targetContext)

Slide 115

Slide 115 text

Moshi delegate tests class MoshiTestKrate(context: Context) : SimpleKrate(context) { var simpleValue: TestModel? by moshiPref("simpleValue") var listOfValues: List? by moshiPref("listOfValues") } @Test fun testListOfValues() { val list = listOf( TestModel(x = 42, y = 3.141592, z = "shibboleth"), TestModel(x = 13, y = 6.283185, z = "signal"), ) krate.listOfValues = list assertEquals(list, krate.listOfValues) } data class TestModel(val x: Int, val y: Double, val z: String) val krate = MoshiTestKrate(targetContext)

Slide 116

Slide 116 text

class MoshiTestKrate(context: Context) : SimpleKrate(context) { var simpleValue: TestModel? by moshiPref("simpleValue") var listOfValues: List? by moshiPref("listOfValues") } @Test fun testListOfValues() { val list = listOf( TestModel(x = 42, y = 3.141592, z = "shibboleth"), TestModel(x = 13, y = 6.283185, z = "signal"), ) krate.listOfValues = list assertEquals(list, krate.listOfValues) } data class TestModel(val x: Int, val y: Double, val z: String) val krate = MoshiTestKrate(targetContext) Moshi delegate tests java.lang.AssertionError: Expected :[ TestModel(x=42, y=3.141592, z=shibboleth), TestModel(x=13, y=6.283185, z=signal) ] Actual :[ {x=42.0, y=3.141592, z=shibboleth}, {x=13.0, y=6.283185, z=signal} ]

Slide 117

Slide 117 text

java.lang.AssertionError: Expected :[ TestModel(x=42, y=3.141592, z=shibboleth), TestModel(x=13, y=6.283185, z=signal) ] Actual :[ {x=42.0, y=3.141592, z=shibboleth}, {x=13.0, y=6.283185, z=signal} ]

Slide 118

Slide 118 text

Actual :[ {x=42.0, y=3.141592, z=shibboleth}, {x=13.0, y=6.283185, z=signal} ]

Slide 119

Slide 119 text

inline fun Krate.moshiPref(key: String): ReadWriteProperty { return MoshiDelegate(key, T::class.java) } Moshi delegate tests

Slide 120

Slide 120 text

inline fun Krate.moshiPref(key: String): ReadWriteProperty { return MoshiDelegate(key, T::class.java) } Moshi delegate tests

Slide 121

Slide 121 text

inline fun Krate.moshiPref(key: String): ReadWriteProperty { return MoshiDelegate(key, T::class.java) } Moshi delegate tests

Slide 122

Slide 122 text

var simpleValue by moshiPref("simpleValue") var listOfValues by moshiPref>("listOfValues") inline fun Krate.moshiPref(key: String): ReadWriteProperty { return MoshiDelegate(key, T::class.java) } Moshi delegate tests

Slide 123

Slide 123 text

var simpleValue by moshiPref("simpleValue") var listOfValues by moshiPref>("listOfValues") inline fun Krate.moshiPref(key: String): ReadWriteProperty { return MoshiDelegate(key, T::class.java) } class hu.autsoft.krate.moshi.TestModel Moshi delegate tests

Slide 124

Slide 124 text

class hu.autsoft.krate.moshi.TestModel interface java.util.List var simpleValue by moshiPref("simpleValue") var listOfValues by moshiPref>("listOfValues") inline fun Krate.moshiPref(key: String): ReadWriteProperty { return MoshiDelegate(key, T::class.java) } Moshi delegate tests

Slide 125

Slide 125 text

inline fun Krate.moshiPref(key: String): ReadWriteProperty { return MoshiDelegate(key, T::class.java) } Looking for a Type

Slide 126

Slide 126 text

inline fun Krate.moshiPref(key: String): ReadWriteProperty { return MoshiDelegate(key, T::class.java) } Types.newParameterizedType(List::class.java, TestModel::class.java) Looking for a Type

Slide 127

Slide 127 text

inline fun Krate.moshiPref(key: String): ReadWriteProperty { return MoshiDelegate(key, T::class.java) } Types.newParameterizedType(List::class.java, TestModel::class.java) java.util.List Looking for a Type

Slide 128

Slide 128 text

inline fun Krate.moshiPref(key: String): ReadWriteProperty { return MoshiDelegate( key, Types.newParameterizedType(T::class.java, ???::class.java) ) } Types.newParameterizedType(List::class.java, TestModel::class.java) java.util.List Looking for a Type

Slide 129

Slide 129 text

Looking for a Type inline fun Krate.moshiPref(key: String): ReadWriteProperty { return MoshiDelegate(key, } ) ???

Slide 130

Slide 130 text

Looking for a Type inline fun Krate.moshiPref(key: String): ReadWriteProperty { return MoshiDelegate(key, object : TypeToken() {}.type) }

Slide 131

Slide 131 text

TypeToken object : TypeToken() {}.type

Slide 132

Slide 132 text

TypeToken public class TypeToken { final Type type = getSuperclassTypeParameter(getClass()); protected TypeToken() {} static Type getSuperclassTypeParameter(Class subclass) { Type superclass = subclass.getGenericSuperclass(); if (superclass instanceof Class) { throw new RuntimeException("Missing type parameter."); } ParameterizedType parameterized = (ParameterizedType) superclass; return canonicalize(parameterized.getActualTypeArguments()[0]); } /** * Returns a type that is functionally equal but not necessarily equal * according to Object.equals(). */ public static Type canonicalize(Type type) { ... } } object : TypeToken() {}.type

Slide 133

Slide 133 text

TypeToken public class TypeToken { final Type type = getSuperclassTypeParameter(getClass()); protected TypeToken() {} static Type getSuperclassTypeParameter(Class subclass) { Type superclass = subclass.getGenericSuperclass(); if (superclass instanceof Class) { throw new RuntimeException("Missing type parameter."); } ParameterizedType parameterized = (ParameterizedType) superclass; return canonicalize(parameterized.getActualTypeArguments()[0]); } /** * Returns a type that is functionally equal but not necessarily equal * according to Object.equals(). */ public static Type canonicalize(Type type) { ... } } object : TypeToken() {}.type

Slide 134

Slide 134 text

TypeToken public class TypeToken { final Type type = getSuperclassTypeParameter(getClass()); protected TypeToken() {} static Type getSuperclassTypeParameter(Class subclass) { Type superclass = subclass.getGenericSuperclass(); if (superclass instanceof Class) { throw new RuntimeException("Missing type parameter."); } ParameterizedType parameterized = (ParameterizedType) superclass; return canonicalize(parameterized.getActualTypeArguments()[0]); } /** * Returns a type that is functionally equal but not necessarily equal * according to Object.equals(). */ public static Type canonicalize(Type type) { ... } } object : TypeToken() {}.type

Slide 135

Slide 135 text

TypeToken public class TypeToken { final Type type = getSuperclassTypeParameter(getClass()); protected TypeToken() {} static Type getSuperclassTypeParameter(Class subclass) { Type superclass = subclass.getGenericSuperclass(); if (superclass instanceof Class) { throw new RuntimeException("Missing type parameter."); } ParameterizedType parameterized = (ParameterizedType) superclass; return canonicalize(parameterized.getActualTypeArguments()[0]); } /** * Returns a type that is functionally equal but not necessarily equal * according to Object.equals(). */ public static Type canonicalize(Type type) { ... } } object : TypeToken() {}.type

Slide 136

Slide 136 text

TypeToken public class TypeToken { final Type type = getSuperclassTypeParameter(getClass()); protected TypeToken() {} static Type getSuperclassTypeParameter(Class subclass) { Type superclass = subclass. if (superclass instanceof Class) { throw new RuntimeException("Missing type parameter."); } ParameterizedType parameterized = (ParameterizedType) superclass; return canonicalize(parameterized.getActualTypeArguments()[0]); } /** * Returns a type that is functionally equal but not necessarily equal * according to Object.equals(). */ public static Type canonicalize(Type type) { ... } } object : TypeToken() {}.type getGenericSuperclass();

Slide 137

Slide 137 text

getGenericSuperclass(); * ... */ public Type /** * Returns the Type representing the direct superclass of the entity * represented by this Class. *

Slide 138

Slide 138 text

* If the superclass is a parameterized type, the Type object returned must * accurately reflect the actual type parameters used in the source code. * getGenericSuperclass(); * ... */ public Type /** * Returns the Type representing the direct superclass of the entity * represented by this Class. *

Slide 139

Slide 139 text

* Non-parameterized -> Class * Parameterized -> ParameterizedType * If the superclass is a parameterized type, the Type object returned must * accurately reflect the actual type parameters used in the source code. * getGenericSuperclass(); * ... */ public Type /** * Returns the Type representing the direct superclass of the entity * represented by this Class. *

Slide 140

Slide 140 text

TypeToken public class TypeToken { final Type type = getSuperclassTypeParameter(getClass()); protected TypeToken() {} static Type getSuperclassTypeParameter(Class subclass) { Type superclass = subclass. if (superclass instanceof Class) { throw new RuntimeException("Missing type parameter."); } ParameterizedType parameterized = (ParameterizedType) superclass; return canonicalize(parameterized.getActualTypeArguments()[0]); } /** * Returns a type that is functionally equal but not necessarily equal * according to Object.equals(). */ public static Type canonicalize(Type type) { ... } } object : TypeToken() {}.type getGenericSuperclass();

Slide 141

Slide 141 text

TypeToken public class TypeToken { final Type type = getSuperclassTypeParameter(getClass()); protected TypeToken() {} static Type getSuperclassTypeParameter(Class subclass) { Type superclass = subclass.getGenericSuperclass(); if (superclass instanceof Class) { throw new RuntimeException("Missing type parameter."); } ParameterizedType parameterized = (ParameterizedType) superclass; return canonicalize(parameterized.getActualTypeArguments()[0]); } /** * Returns a type that is functionally equal but not necessarily equal * according to Object.equals(). */ public static Type canonicalize(Type type) { ... } } object : TypeToken() {}.type

Slide 142

Slide 142 text

TypeToken public class TypeToken { final Type type = getSuperclassTypeParameter(getClass()); protected TypeToken() {} static Type getSuperclassTypeParameter(Class subclass) { Type superclass = subclass.getGenericSuperclass(); if (superclass instanceof Class) { throw new RuntimeException("Missing type parameter."); } ParameterizedType parameterized = (ParameterizedType) superclass; return canonicalize(parameterized.getActualTypeArguments()[0]); } /** * Returns a type that is functionally equal but not necessarily equal * according to Object.equals(). */ public static Type canonicalize(Type type) { ... } } object : TypeToken() {}.type

Slide 143

Slide 143 text

TypeToken object : TypeToken() {}.type public class TypeToken { final Type type = getSuperclassTypeParameter(getClass()); protected TypeToken() {} static Type getSuperclassTypeParameter(Class subclass) { Type superclass = subclass.getGenericSuperclass(); if (superclass instanceof Class) { throw new RuntimeException("Missing type parameter."); } ParameterizedType parameterized = (ParameterizedType) superclass; parameterized.getActualTypeArguments()[0] canonicalize( ); return } } /** * Returns a type that is functionally equal but not necessarily equal * according to Object.equals(). */ public static Type canonicalize(Type type) { ... }

Slide 144

Slide 144 text

parameterized.getActualTypeArguments()[0] TypeToken object : TypeToken() {}.type ; public class TypeToken { final Type type = getSuperclassTypeParameter(getClass()); protected TypeToken() {} static Type getSuperclassTypeParameter(Class subclass) { Type superclass = subclass.getGenericSuperclass(); if (superclass instanceof Class) { throw new RuntimeException("Missing type parameter."); } ParameterizedType parameterized = (ParameterizedType) superclass; return } }

Slide 145

Slide 145 text

open constructor val fun : * : val is <*> val as getGenericSuperclass() getActualTypeArguments() TypeToken object : TypeToken() {}.type subclass getSuperclassTypeParameter Class< >) Type ( type = getSuperclassTypeParameter( ) class TypeToken { { { } } } return parameterized. parameterized = () protected javaClass if (superclass Class superclass = subclass. ) superclass ParameterizedType RuntimeException("Missing type parameter.") throw [0] actualTypeArguments genericSuperclass

Slide 146

Slide 146 text

: Type requireNotNull token::class.java if (superclass is Class<*>) { throw RuntimeException("Missing type parameter.") } val parameterized = superclass as ParameterizedType return parameterized.actualTypeArguments[0] } TypeToken object : TypeToken() {}.type abstract ): Type { .genericSuperclass ( ) class TypeToken val token = object : TypeToken() {} inline makeType = val superclass ( fun

Slide 147

Slide 147 text

TypeToken makeType() : Type requireNotNull token::class.java if (superclass is Class<*>) { throw RuntimeException("Missing type parameter.") } val parameterized = superclass as ParameterizedType return parameterized.actualTypeArguments[0] } abstract ): Type { .genericSuperclass ( ) class TypeToken val token = object : TypeToken() {} inline makeType = val superclass ( fun

Slide 148

Slide 148 text

TypeToken inline fun Krate.moshiPref(key: String): ReadWriteProperty { return MoshiDelegate(key, ) } makeType() : Type requireNotNull token::class.java if (superclass is Class<*>) { throw RuntimeException("Missing type parameter.") } val parameterized = superclass as ParameterizedType return parameterized.actualTypeArguments[0] } abstract ): Type { .genericSuperclass ( ) class TypeToken val token = object : TypeToken() {} inline makeType = val superclass ( fun

Slide 149

Slide 149 text

TypeToken

Slide 150

Slide 150 text

typeOf inline fun makeType(): Type inline fun Krate.moshiPref(key: String): ReadWriteProperty { return MoshiDelegate(key, makeType()) }

Slide 151

Slide 151 text

typeOf inline fun makeType(): Type /** * Returns a runtime representation of the given * reified type [T] as an instance of [KType]. */ inline fun typeOf(): KType inline fun Krate.moshiPref(key: String): ReadWriteProperty { return MoshiDelegate(key, makeType()) }

Slide 152

Slide 152 text

typeOf inline fun Krate.moshiPref(key: String): ReadWriteProperty { return MoshiDelegate(key, typeOf()) } /** * Returns a runtime representation of the given * reified type [T] as an instance of [KType]. */ inline fun typeOf(): KType

Slide 153

Slide 153 text

typeOf inline fun Krate.moshiPref(key: String): ReadWriteProperty { return MoshiDelegate(key, typeOf()) } /** * Returns a runtime representation of the given * reified type [T] as an instance of [KType]. */ inline fun typeOf(): KType

Slide 154

Slide 154 text

typeOf inline fun Krate.moshiPref(key: String): ReadWriteProperty { return MoshiDelegate(key, typeOf()) } /** * Returns a runtime representation of the given * reified type [T] as an instance of [KType]. */ inline fun typeOf(): KType /** * Returns a Java [Type] instance corresponding * to the given Kotlin type. */ val KType.javaType: Type

Slide 155

Slide 155 text

typeOf inline fun Krate.moshiPref(key: String): ReadWriteProperty { return MoshiDelegate(key, typeOf().javaType) } /** * Returns a runtime representation of the given * reified type [T] as an instance of [KType]. */ inline fun typeOf(): KType /** * Returns a Java [Type] instance corresponding * to the given Kotlin type. */ val KType.javaType: Type

Slide 156

Slide 156 text

typeOf inline fun Krate.moshiPref(key: String): ReadWriteProperty { return MoshiDelegate(key, typeOf().javaType) } /** * Returns a runtime representation of the given * reified type [T] as an instance of [KType]. */ inline fun typeOf(): KType /** * Returns a Java [Type] instance corresponding * to the given Kotlin type. */ val KType.javaType: Type NotImplementedError: Java type is not yet supported for types created with createType

Slide 157

Slide 157 text

typeOf inline fun Krate.moshiPref(key: String): ReadWriteProperty { return MoshiDelegate(key, typeOf().javaType) } /** * Returns a runtime representation of the given * reified type [T] as an instance of [KType]. */ /** * Returns a Java [Type] instance corresponding * to the given Kotlin type. */ val KType.javaType: Type inline fun typeOf(): KType NotImplementedError: Java type is not yet supported for types created with createType

Slide 158

Slide 158 text

makeType typeOf inline fun Krate.moshiPref(key: String): ReadWriteProperty { return MoshiDelegate(key, typeOf().javaType) } inline fun typeOf(): KType val KType.javaType: Type fun KClassifier.createType(...): KType { ... return KTypeImpl(kotlinType) { TODO("Java type is not yet supported for types created with createType") } }

Slide 159

Slide 159 text

typeOf inline fun typeOf(): KType val KType.javaType: Type inline fun Krate.moshiPref(key: String): ReadWriteProperty { return MoshiDelegate(key, makeType()) }

Slide 160

Slide 160 text

typeOf inline fun Krate.moshiPref(key: String): ReadWriteProperty { return MoshiDelegate(key, typeOf().javaType) } inline fun typeOf(): KType val KType.javaType: Type

Slide 161

Slide 161 text

typeOf @OptIn(ExperimentalStdlibApi::class) inline fun Krate.moshiPref(key: String): ReadWriteProperty { return MoshiDelegate(key, typeOf().javaType) } inline fun typeOf(): KType val KType.javaType: Type

Slide 162

Slide 162 text

typeOf @OptIn(ExperimentalStdlibApi::class) inline fun Krate.moshiPref(key: String): ReadWriteProperty { return MoshiDelegate(key, typeOf().javaType) } inline fun typeOf(): KType val KType.javaType: Type

Slide 163

Slide 163 text

Resources

Slide 164

Slide 164 text

• Krate  https://github.com/AutSoft/Krate • Moshi  https://github.com/square/moshi Resources

Slide 165

Slide 165 text

• Krate  https://github.com/AutSoft/Krate • Moshi  https://github.com/square/moshi • Delightful Delegate Design  https://www.droidcon.com/media-detail?video=481186381 • Say “hi” to Moshi!  https://www.youtube.com/watch?v=qadYVYc-8mc Resources

Slide 166

Slide 166 text

• Krate  https://github.com/AutSoft/Krate • Moshi  https://github.com/square/moshi • Delightful Delegate Design  https://www.droidcon.com/media-detail?video=481186381 • Say “hi” to Moshi!  https://www.youtube.com/watch?v=qadYVYc-8mc • Library design articles  https://zsmb.co/maintaining-compatibility-in-kotlin-libraries/  https://zsmb.co/mastering-api-visibility-in-kotlin/  https://zsmb.co/talks/how-to-build-awesome-android-libraries/ Resources

Slide 167

Slide 167 text

zsmb13 zsmb.co/talks

Slide 168

Slide 168 text

ft Krate Moshi A Kotlin library design case study zsmb.co/talks zsmb13 Márton Braun › Krate is great › Moshi is awesome › Type erasure is evil › Tests are your friend