From 48b51acf3763342fb89fd63a6dfc98ca66ce99d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1dy=20Istv=C3=A1n?= Date: Thu, 23 Sep 2021 17:49:19 +0200 Subject: [PATCH 01/31] Introduce KeyDelegate and KeyDelegateProvider --- .../kotlin/hu/autsoft/krate/gson/Functions.kt | 13 +++--- .../gson/default/GsonDelegateWithDefault.kt | 17 +++++--- .../krate/gson/optional/GsonDelegate.kt | 17 +++++--- .../hu/autsoft/krate/kotlinx/Functions.kt | 13 +++--- .../default/KotlinxDelegateWithDefault.kt | 17 +++++--- .../krate/kotlinx/optional/KotlinxDelegate.kt | 17 +++++--- .../hu/autsoft/krate/moshi/Functions.kt | 13 +++--- .../moshi/default/MoshiDelegateWithDefault.kt | 17 +++++--- .../krate/moshi/optional/MoshiDelegate.kt | 17 +++++--- .../main/kotlin/hu/autsoft/krate/Functions.kt | 43 ++++++++----------- .../hu/autsoft/krate/base/KeyDelegate.kt | 8 ++++ .../autsoft/krate/base/KeyDelegateProvider.kt | 8 ++++ .../default/BooleanDelegateWithDefault.kt | 9 ++-- .../krate/default/FloatDelegateWithDefault.kt | 9 ++-- .../krate/default/IntDelegateWithDefault.kt | 9 ++-- .../krate/default/LongDelegateWithDefault.kt | 9 ++-- .../default/StringDelegateWithDefault.kt | 9 ++-- .../default/StringSetDelegateWithDefault.kt | 9 ++-- .../autsoft/krate/optional/BooleanDelegate.kt | 9 ++-- .../autsoft/krate/optional/FloatDelegate.kt | 9 ++-- .../hu/autsoft/krate/optional/IntDelegate.kt | 9 ++-- .../hu/autsoft/krate/optional/LongDelegate.kt | 9 ++-- .../autsoft/krate/optional/StringDelegate.kt | 9 ++-- .../krate/optional/StringSetDelegate.kt | 9 ++-- 24 files changed, 186 insertions(+), 122 deletions(-) create mode 100644 krate/src/main/kotlin/hu/autsoft/krate/base/KeyDelegate.kt create mode 100644 krate/src/main/kotlin/hu/autsoft/krate/base/KeyDelegateProvider.kt diff --git a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/Functions.kt b/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/Functions.kt index 6719f75..1544412 100644 --- a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/Functions.kt +++ b/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/Functions.kt @@ -1,14 +1,15 @@ -@file:Suppress("unused") +@file:[Suppress("unused") OptIn(InternalKrateApi::class)] package hu.autsoft.krate.gson import com.google.gson.reflect.TypeToken import hu.autsoft.krate.Krate +import hu.autsoft.krate.base.KeyDelegate import hu.autsoft.krate.gson.default.GsonDelegateWithDefaultFactory import hu.autsoft.krate.gson.optional.GsonDelegateFactory +import hu.autsoft.krate.internal.InternalKrateApi import java.lang.reflect.Type import kotlin.properties.PropertyDelegateProvider -import kotlin.properties.ReadWriteProperty /** * Creates an optional preference of type T with the given [key] in this [Krate] instance. @@ -16,7 +17,7 @@ import kotlin.properties.ReadWriteProperty */ public inline fun Krate.gsonPref( key: String, -): PropertyDelegateProvider> { +): PropertyDelegateProvider> { return gsonPrefImpl(key, object : TypeToken() {}.type) } @@ -24,7 +25,7 @@ public inline fun Krate.gsonPref( internal fun Krate.gsonPrefImpl( key: String, type: Type, -): PropertyDelegateProvider> { +): PropertyDelegateProvider> { return GsonDelegateFactory(key, type) } @@ -35,7 +36,7 @@ internal fun Krate.gsonPrefImpl( public inline fun Krate.gsonPref( key: String, defaultValue: T, -): PropertyDelegateProvider> { +): PropertyDelegateProvider> { return gsonPrefImpl(key, defaultValue, object : TypeToken() {}.type) } @@ -44,6 +45,6 @@ internal fun Krate.gsonPrefImpl( key: String, defaultValue: T, type: Type, -): PropertyDelegateProvider> { +): PropertyDelegateProvider> { return GsonDelegateWithDefaultFactory(key, defaultValue, type) } diff --git a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/default/GsonDelegateWithDefault.kt b/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/default/GsonDelegateWithDefault.kt index febc78d..a52d387 100644 --- a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/default/GsonDelegateWithDefault.kt +++ b/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/default/GsonDelegateWithDefault.kt @@ -1,20 +1,24 @@ +@file:OptIn(InternalKrateApi::class) + package hu.autsoft.krate.gson.default import com.google.gson.TypeAdapter import com.google.gson.reflect.TypeToken import hu.autsoft.krate.Krate +import hu.autsoft.krate.base.KeyDelegate +import hu.autsoft.krate.base.KeyDelegateProvider import hu.autsoft.krate.gson.internalGson import hu.autsoft.krate.gson.util.edit +import hu.autsoft.krate.internal.InternalKrateApi import java.lang.reflect.Type -import kotlin.properties.PropertyDelegateProvider -import kotlin.properties.ReadWriteProperty import kotlin.reflect.KProperty private class GsonDelegateWithDefault( - private val key: String, + key: String, private val default: T, private val adapter: TypeAdapter, -) : ReadWriteProperty { +) : KeyDelegate(key) { + override operator fun getValue(thisRef: Krate, property: KProperty<*>): T { if (!thisRef.sharedPreferences.contains(key)) { return default @@ -35,8 +39,9 @@ internal class GsonDelegateWithDefaultFactory( private val key: String, private val default: T, private val type: Type, -) : PropertyDelegateProvider> { - override fun provideDelegate(thisRef: Krate, property: KProperty<*>): ReadWriteProperty { +) : KeyDelegateProvider() { + + override fun provideDelegate(thisRef: Krate, property: KProperty<*>): KeyDelegate { @Suppress("UNCHECKED_CAST") val adapter: TypeAdapter = thisRef.internalGson.getAdapter(TypeToken.get(type)) as TypeAdapter return GsonDelegateWithDefault(key, default, adapter) diff --git a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/optional/GsonDelegate.kt b/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/optional/GsonDelegate.kt index 01dde64..2191d33 100644 --- a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/optional/GsonDelegate.kt +++ b/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/optional/GsonDelegate.kt @@ -1,19 +1,23 @@ +@file:OptIn(InternalKrateApi::class) + package hu.autsoft.krate.gson.optional import com.google.gson.TypeAdapter import com.google.gson.reflect.TypeToken import hu.autsoft.krate.Krate +import hu.autsoft.krate.base.KeyDelegate +import hu.autsoft.krate.base.KeyDelegateProvider import hu.autsoft.krate.gson.internalGson import hu.autsoft.krate.gson.util.edit +import hu.autsoft.krate.internal.InternalKrateApi import java.lang.reflect.Type -import kotlin.properties.PropertyDelegateProvider -import kotlin.properties.ReadWriteProperty import kotlin.reflect.KProperty private class GsonDelegate( - private val key: String, + key: String, private val adapter: TypeAdapter, -) : ReadWriteProperty { +) : KeyDelegate(key) { + override operator fun getValue(thisRef: Krate, property: KProperty<*>): T? { return if (!thisRef.sharedPreferences.contains(key)) { null @@ -39,8 +43,9 @@ private class GsonDelegate( internal class GsonDelegateFactory( private val key: String, private val type: Type, -) : PropertyDelegateProvider> { - override fun provideDelegate(thisRef: Krate, property: KProperty<*>): ReadWriteProperty { +) : KeyDelegateProvider() { + + override fun provideDelegate(thisRef: Krate, property: KProperty<*>): KeyDelegate { @Suppress("UNCHECKED_CAST") val adapter: TypeAdapter = thisRef.internalGson.getAdapter(TypeToken.get(type)) as TypeAdapter return GsonDelegate(key, adapter) diff --git a/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/Functions.kt b/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/Functions.kt index c128239..fad0afe 100644 --- a/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/Functions.kt +++ b/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/Functions.kt @@ -1,12 +1,13 @@ -@file:Suppress("unused") +@file:[Suppress("unused") OptIn(InternalKrateApi::class)] package hu.autsoft.krate.kotlinx import hu.autsoft.krate.Krate +import hu.autsoft.krate.base.KeyDelegate +import hu.autsoft.krate.internal.InternalKrateApi import hu.autsoft.krate.kotlinx.default.KotlinxDelegateWithDefaultFactory import hu.autsoft.krate.kotlinx.optional.KotlinxDelegateFactory import kotlin.properties.PropertyDelegateProvider -import kotlin.properties.ReadWriteProperty import kotlin.reflect.KType import kotlin.reflect.typeOf @@ -17,7 +18,7 @@ import kotlin.reflect.typeOf @OptIn(ExperimentalStdlibApi::class) public inline fun Krate.kotlinxPref( key: String, -): PropertyDelegateProvider> { +): PropertyDelegateProvider> { return kotlinxPrefImpl(key, typeOf()) } @@ -25,7 +26,7 @@ public inline fun Krate.kotlinxPref( internal fun Krate.kotlinxPrefImpl( key: String, type: KType, -): PropertyDelegateProvider> { +): PropertyDelegateProvider> { return KotlinxDelegateFactory(key, type) } @@ -37,7 +38,7 @@ internal fun Krate.kotlinxPrefImpl( public inline fun Krate.kotlinxPref( key: String, defaultValue: T, -): PropertyDelegateProvider> { +): PropertyDelegateProvider> { return kotlinxPrefImpl(key, defaultValue, typeOf()) } @@ -46,6 +47,6 @@ internal fun Krate.kotlinxPrefImpl( key: String, defaultValue: T, type: KType, -): PropertyDelegateProvider> { +): PropertyDelegateProvider> { return KotlinxDelegateWithDefaultFactory(key, defaultValue, type) } diff --git a/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/default/KotlinxDelegateWithDefault.kt b/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/default/KotlinxDelegateWithDefault.kt index 0a467c1..decedd4 100644 --- a/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/default/KotlinxDelegateWithDefault.kt +++ b/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/default/KotlinxDelegateWithDefault.kt @@ -1,20 +1,24 @@ +@file:OptIn(InternalKrateApi::class) + package hu.autsoft.krate.kotlinx.default import hu.autsoft.krate.Krate +import hu.autsoft.krate.base.KeyDelegate +import hu.autsoft.krate.base.KeyDelegateProvider +import hu.autsoft.krate.internal.InternalKrateApi import hu.autsoft.krate.kotlinx.internalJson import hu.autsoft.krate.kotlinx.util.edit import kotlinx.serialization.KSerializer import kotlinx.serialization.serializer -import kotlin.properties.PropertyDelegateProvider -import kotlin.properties.ReadWriteProperty import kotlin.reflect.KProperty import kotlin.reflect.KType private class KotlinxDelegateWithDefault( - private val key: String, + key: String, private val default: T, private val serializer: KSerializer, -) : ReadWriteProperty { +) : KeyDelegate(key) { + override operator fun getValue(thisRef: Krate, property: KProperty<*>): T { if (!thisRef.sharedPreferences.contains(key)) { return default @@ -34,8 +38,9 @@ internal class KotlinxDelegateWithDefaultFactory( private val key: String, private val default: T, private val type: KType, -) : PropertyDelegateProvider> { - override fun provideDelegate(thisRef: Krate, property: KProperty<*>): ReadWriteProperty { +) : KeyDelegateProvider() { + + override fun provideDelegate(thisRef: Krate, property: KProperty<*>): KeyDelegate { @Suppress("UNCHECKED_CAST") val serializer = thisRef.internalJson.serializersModule.serializer(type) as KSerializer return KotlinxDelegateWithDefault(key, default, serializer) diff --git a/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/optional/KotlinxDelegate.kt b/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/optional/KotlinxDelegate.kt index bbf269f..2f6155b 100644 --- a/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/optional/KotlinxDelegate.kt +++ b/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/optional/KotlinxDelegate.kt @@ -1,20 +1,24 @@ +@file:OptIn(InternalKrateApi::class) + package hu.autsoft.krate.kotlinx.optional import hu.autsoft.krate.Krate +import hu.autsoft.krate.base.KeyDelegate +import hu.autsoft.krate.base.KeyDelegateProvider +import hu.autsoft.krate.internal.InternalKrateApi import hu.autsoft.krate.kotlinx.internalJson import hu.autsoft.krate.kotlinx.util.edit import kotlinx.serialization.KSerializer import kotlinx.serialization.serializer -import kotlin.properties.PropertyDelegateProvider -import kotlin.properties.ReadWriteProperty import kotlin.reflect.KProperty import kotlin.reflect.KType private class KotlinxDelegate( - private val key: String, + key: String, private val serializer: KSerializer, -) : ReadWriteProperty { +) : KeyDelegate(key) { + override operator fun getValue(thisRef: Krate, property: KProperty<*>): T? { return if (!thisRef.sharedPreferences.contains(key)) { null @@ -40,8 +44,9 @@ private class KotlinxDelegate( internal class KotlinxDelegateFactory( private val key: String, private val type: KType, -) : PropertyDelegateProvider> { - override fun provideDelegate(thisRef: Krate, property: KProperty<*>): ReadWriteProperty { +) : KeyDelegateProvider() { + + override fun provideDelegate(thisRef: Krate, property: KProperty<*>): KeyDelegate { @Suppress("UNCHECKED_CAST") val serializer = thisRef.internalJson.serializersModule.serializer(type) as KSerializer return KotlinxDelegate(key, serializer) diff --git a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/Functions.kt b/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/Functions.kt index fc18755..3301103 100644 --- a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/Functions.kt +++ b/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/Functions.kt @@ -1,13 +1,14 @@ -@file:Suppress("unused") +@file:[Suppress("unused") OptIn(InternalKrateApi::class)] package hu.autsoft.krate.moshi import hu.autsoft.krate.Krate +import hu.autsoft.krate.base.KeyDelegate +import hu.autsoft.krate.internal.InternalKrateApi import hu.autsoft.krate.moshi.default.MoshiDelegateWithDefaultFactory import hu.autsoft.krate.moshi.optional.MoshiDelegateFactory import java.lang.reflect.Type import kotlin.properties.PropertyDelegateProvider -import kotlin.properties.ReadWriteProperty import kotlin.reflect.javaType import kotlin.reflect.typeOf @@ -18,7 +19,7 @@ import kotlin.reflect.typeOf @OptIn(ExperimentalStdlibApi::class) public inline fun Krate.moshiPref( key: String, -): PropertyDelegateProvider> { +): PropertyDelegateProvider> { return moshiPrefImpl(key, typeOf().javaType) } @@ -26,7 +27,7 @@ public inline fun Krate.moshiPref( internal fun Krate.moshiPrefImpl( key: String, type: Type, -): PropertyDelegateProvider> { +): PropertyDelegateProvider> { return MoshiDelegateFactory(key, type) } @@ -38,7 +39,7 @@ internal fun Krate.moshiPrefImpl( public inline fun Krate.moshiPref( key: String, defaultValue: T, -): PropertyDelegateProvider> { +): PropertyDelegateProvider> { return moshiPrefImpl(key, defaultValue, typeOf().javaType) } @@ -47,6 +48,6 @@ internal fun Krate.moshiPrefImpl( key: String, defaultValue: T, type: Type, -): PropertyDelegateProvider> { +): PropertyDelegateProvider> { return MoshiDelegateWithDefaultFactory(key, defaultValue, type) } diff --git a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/default/MoshiDelegateWithDefault.kt b/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/default/MoshiDelegateWithDefault.kt index 7ebbd06..54e5f0c 100644 --- a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/default/MoshiDelegateWithDefault.kt +++ b/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/default/MoshiDelegateWithDefault.kt @@ -1,20 +1,24 @@ +@file:OptIn(InternalKrateApi::class) + package hu.autsoft.krate.moshi.default import com.squareup.moshi.JsonAdapter import hu.autsoft.krate.Krate +import hu.autsoft.krate.base.KeyDelegate +import hu.autsoft.krate.base.KeyDelegateProvider +import hu.autsoft.krate.internal.InternalKrateApi import hu.autsoft.krate.moshi.realMoshiInstance import hu.autsoft.krate.moshi.util.edit import java.lang.reflect.Type -import kotlin.properties.PropertyDelegateProvider -import kotlin.properties.ReadWriteProperty import kotlin.reflect.KProperty private class MoshiDelegateWithDefault( - private val key: String, + key: String, private val default: T, private val adapter: JsonAdapter, -) : ReadWriteProperty { +) : KeyDelegate(key) { + override operator fun getValue(thisRef: Krate, property: KProperty<*>): T { if (!thisRef.sharedPreferences.contains(key)) { return default @@ -34,8 +38,9 @@ internal class MoshiDelegateWithDefaultFactory( private val key: String, private val default: T, private val type: Type, -) : PropertyDelegateProvider> { - override fun provideDelegate(thisRef: Krate, property: KProperty<*>): ReadWriteProperty { +) : KeyDelegateProvider() { + + override fun provideDelegate(thisRef: Krate, property: KProperty<*>): KeyDelegate { val adapter = thisRef.realMoshiInstance.adapter(type) return MoshiDelegateWithDefault(key, default, adapter) } diff --git a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/optional/MoshiDelegate.kt b/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/optional/MoshiDelegate.kt index ee70320..c39213e 100644 --- a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/optional/MoshiDelegate.kt +++ b/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/optional/MoshiDelegate.kt @@ -1,18 +1,22 @@ +@file:OptIn(InternalKrateApi::class) + package hu.autsoft.krate.moshi.optional import com.squareup.moshi.JsonAdapter import hu.autsoft.krate.Krate +import hu.autsoft.krate.base.KeyDelegate +import hu.autsoft.krate.base.KeyDelegateProvider +import hu.autsoft.krate.internal.InternalKrateApi import hu.autsoft.krate.moshi.realMoshiInstance import hu.autsoft.krate.moshi.util.edit import java.lang.reflect.Type -import kotlin.properties.PropertyDelegateProvider -import kotlin.properties.ReadWriteProperty import kotlin.reflect.KProperty private class MoshiDelegate( - private val key: String, + key: String, private val adapter: JsonAdapter, -) : ReadWriteProperty { +) : KeyDelegate(key) { + override fun getValue(thisRef: Krate, property: KProperty<*>): T? { return if (!thisRef.sharedPreferences.contains(key)) { null @@ -38,8 +42,9 @@ private class MoshiDelegate( internal class MoshiDelegateFactory( private val key: String, private val type: Type, -) : PropertyDelegateProvider> { - override fun provideDelegate(thisRef: Krate, property: KProperty<*>): ReadWriteProperty { +) : KeyDelegateProvider() { + + override fun provideDelegate(thisRef: Krate, property: KProperty<*>): KeyDelegate { val adapter = thisRef.realMoshiInstance.adapter(type) return MoshiDelegate(key, adapter) } diff --git a/krate/src/main/kotlin/hu/autsoft/krate/Functions.kt b/krate/src/main/kotlin/hu/autsoft/krate/Functions.kt index 8f3f2c9..5865e11 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/Functions.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/Functions.kt @@ -1,60 +1,51 @@ -@file:Suppress("unused") +@file:[Suppress("unused") OptIn(InternalKrateApi::class)] package hu.autsoft.krate -import hu.autsoft.krate.default.BooleanDelegateWithDefault -import hu.autsoft.krate.default.FloatDelegateWithDefault -import hu.autsoft.krate.default.IntDelegateWithDefault -import hu.autsoft.krate.default.LongDelegateWithDefault -import hu.autsoft.krate.default.StringDelegateWithDefault -import hu.autsoft.krate.default.StringSetDelegateWithDefault -import hu.autsoft.krate.optional.BooleanDelegate -import hu.autsoft.krate.optional.FloatDelegate -import hu.autsoft.krate.optional.IntDelegate -import hu.autsoft.krate.optional.LongDelegate -import hu.autsoft.krate.optional.StringDelegate -import hu.autsoft.krate.optional.StringSetDelegate -import kotlin.properties.ReadWriteProperty +import hu.autsoft.krate.base.KeyDelegate +import hu.autsoft.krate.default.* +import hu.autsoft.krate.internal.InternalKrateApi +import hu.autsoft.krate.optional.* /** * Creates an optional preference of type [Boolean] with the given [key] in this [Krate] instance. */ -public fun Krate.booleanPref(key: String): ReadWriteProperty { +public fun Krate.booleanPref(key: String): KeyDelegate { return BooleanDelegate(key) } /** * Creates an optional preference of type [Float] with the given [key] in this [Krate] instance. */ -public fun Krate.floatPref(key: String): ReadWriteProperty { +public fun Krate.floatPref(key: String): KeyDelegate { return FloatDelegate(key) } /** * Creates an optional preference of type [Int] with the given [key] in this [Krate] instance. */ -public fun Krate.intPref(key: String): ReadWriteProperty { +public fun Krate.intPref(key: String): KeyDelegate { return IntDelegate(key) } /** * Creates an optional preference of type [Long] with the given [key] in this [Krate] instance. */ -public fun Krate.longPref(key: String): ReadWriteProperty { +public fun Krate.longPref(key: String): KeyDelegate { return LongDelegate(key) } /** * Creates an optional preference of type [String] with the given [key] in this [Krate] instance. */ -public fun Krate.stringPref(key: String): ReadWriteProperty { +public fun Krate.stringPref(key: String): KeyDelegate { return StringDelegate(key) } /** * Creates an optional preference of type Set with the given [key] in this [Krate] instance. */ -public fun Krate.stringSetPref(key: String): ReadWriteProperty?> { +public fun Krate.stringSetPref(key: String): KeyDelegate?> { return StringSetDelegate(key) } @@ -62,41 +53,41 @@ public fun Krate.stringSetPref(key: String): ReadWriteProperty { +public fun Krate.booleanPref(key: String, defaultValue: Boolean): KeyDelegate { return BooleanDelegateWithDefault(key, defaultValue) } /** * Creates a non-optional preference of type [Float] with the given [key] and [defaultValue] in this [Krate] instance. */ -public fun Krate.floatPref(key: String, defaultValue: Float): ReadWriteProperty { +public fun Krate.floatPref(key: String, defaultValue: Float): KeyDelegate { return FloatDelegateWithDefault(key, defaultValue) } /** * Creates a non-optional preference of type [Int] with the given [key] and [defaultValue] in this [Krate] instance. */ -public fun Krate.intPref(key: String, defaultValue: Int): ReadWriteProperty { +public fun Krate.intPref(key: String, defaultValue: Int): KeyDelegate { return IntDelegateWithDefault(key, defaultValue) } /** * Creates a non-optional preference of type [Long] with the given [key] and [defaultValue] in this [Krate] instance. */ -public fun Krate.longPref(key: String, defaultValue: Long): ReadWriteProperty { +public fun Krate.longPref(key: String, defaultValue: Long): KeyDelegate { return LongDelegateWithDefault(key, defaultValue) } /** * Creates a non-optional preference of type [String] with the given [key] and [defaultValue] in this [Krate] instance. */ -public fun Krate.stringPref(key: String, defaultValue: String): ReadWriteProperty { +public fun Krate.stringPref(key: String, defaultValue: String): KeyDelegate { return StringDelegateWithDefault(key, defaultValue) } /** * Creates a non-optional preference of type Set with the given [key] and [defaultValue] in this [Krate] instance. */ -public fun Krate.stringSetPref(key: String, defaultValue: Set): ReadWriteProperty> { +public fun Krate.stringSetPref(key: String, defaultValue: Set): KeyDelegate> { return StringSetDelegateWithDefault(key, defaultValue) } diff --git a/krate/src/main/kotlin/hu/autsoft/krate/base/KeyDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/base/KeyDelegate.kt new file mode 100644 index 0000000..6b023a5 --- /dev/null +++ b/krate/src/main/kotlin/hu/autsoft/krate/base/KeyDelegate.kt @@ -0,0 +1,8 @@ +package hu.autsoft.krate.base + +import hu.autsoft.krate.Krate +import hu.autsoft.krate.internal.InternalKrateApi +import kotlin.properties.ReadWriteProperty + +@InternalKrateApi +public abstract class KeyDelegate(public val key: String) : ReadWriteProperty \ No newline at end of file diff --git a/krate/src/main/kotlin/hu/autsoft/krate/base/KeyDelegateProvider.kt b/krate/src/main/kotlin/hu/autsoft/krate/base/KeyDelegateProvider.kt new file mode 100644 index 0000000..2a88791 --- /dev/null +++ b/krate/src/main/kotlin/hu/autsoft/krate/base/KeyDelegateProvider.kt @@ -0,0 +1,8 @@ +package hu.autsoft.krate.base + +import hu.autsoft.krate.Krate +import hu.autsoft.krate.internal.InternalKrateApi +import kotlin.properties.PropertyDelegateProvider + +@InternalKrateApi +public abstract class KeyDelegateProvider : PropertyDelegateProvider> \ No newline at end of file diff --git a/krate/src/main/kotlin/hu/autsoft/krate/default/BooleanDelegateWithDefault.kt b/krate/src/main/kotlin/hu/autsoft/krate/default/BooleanDelegateWithDefault.kt index 2586b44..dd7e138 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/default/BooleanDelegateWithDefault.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/default/BooleanDelegateWithDefault.kt @@ -1,14 +1,17 @@ +@file:OptIn(InternalKrateApi::class) + package hu.autsoft.krate.default import hu.autsoft.krate.Krate +import hu.autsoft.krate.base.KeyDelegate +import hu.autsoft.krate.internal.InternalKrateApi import hu.autsoft.krate.util.edit -import kotlin.properties.ReadWriteProperty import kotlin.reflect.KProperty internal class BooleanDelegateWithDefault( - private val key: String, + key: String, private val default: Boolean, -) : ReadWriteProperty { +) : KeyDelegate(key) { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Boolean { return thisRef.sharedPreferences.getBoolean(key, default) diff --git a/krate/src/main/kotlin/hu/autsoft/krate/default/FloatDelegateWithDefault.kt b/krate/src/main/kotlin/hu/autsoft/krate/default/FloatDelegateWithDefault.kt index f7498d0..3eda9e6 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/default/FloatDelegateWithDefault.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/default/FloatDelegateWithDefault.kt @@ -1,14 +1,17 @@ +@file:OptIn(InternalKrateApi::class) + package hu.autsoft.krate.default import hu.autsoft.krate.Krate +import hu.autsoft.krate.base.KeyDelegate +import hu.autsoft.krate.internal.InternalKrateApi import hu.autsoft.krate.util.edit -import kotlin.properties.ReadWriteProperty import kotlin.reflect.KProperty internal class FloatDelegateWithDefault( - private val key: String, + key: String, private val default: Float, -) : ReadWriteProperty { +) : KeyDelegate(key) { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Float { return thisRef.sharedPreferences.getFloat(key, default) diff --git a/krate/src/main/kotlin/hu/autsoft/krate/default/IntDelegateWithDefault.kt b/krate/src/main/kotlin/hu/autsoft/krate/default/IntDelegateWithDefault.kt index 2eea0d7..3313fc5 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/default/IntDelegateWithDefault.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/default/IntDelegateWithDefault.kt @@ -1,14 +1,17 @@ +@file:OptIn(InternalKrateApi::class) + package hu.autsoft.krate.default import hu.autsoft.krate.Krate +import hu.autsoft.krate.base.KeyDelegate +import hu.autsoft.krate.internal.InternalKrateApi import hu.autsoft.krate.util.edit -import kotlin.properties.ReadWriteProperty import kotlin.reflect.KProperty internal class IntDelegateWithDefault( - private val key: String, + key: String, private val default: Int, -) : ReadWriteProperty { +) : KeyDelegate(key) { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Int { return thisRef.sharedPreferences.getInt(key, default) diff --git a/krate/src/main/kotlin/hu/autsoft/krate/default/LongDelegateWithDefault.kt b/krate/src/main/kotlin/hu/autsoft/krate/default/LongDelegateWithDefault.kt index 3ec5562..cb1abc6 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/default/LongDelegateWithDefault.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/default/LongDelegateWithDefault.kt @@ -1,14 +1,17 @@ +@file:OptIn(InternalKrateApi::class) + package hu.autsoft.krate.default import hu.autsoft.krate.Krate +import hu.autsoft.krate.base.KeyDelegate +import hu.autsoft.krate.internal.InternalKrateApi import hu.autsoft.krate.util.edit -import kotlin.properties.ReadWriteProperty import kotlin.reflect.KProperty internal class LongDelegateWithDefault( - private val key: String, + key: String, private val default: Long, -) : ReadWriteProperty { +) : KeyDelegate(key) { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Long { return thisRef.sharedPreferences.getLong(key, default) diff --git a/krate/src/main/kotlin/hu/autsoft/krate/default/StringDelegateWithDefault.kt b/krate/src/main/kotlin/hu/autsoft/krate/default/StringDelegateWithDefault.kt index 721fa1c..ef5c0d0 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/default/StringDelegateWithDefault.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/default/StringDelegateWithDefault.kt @@ -1,14 +1,17 @@ +@file:OptIn(InternalKrateApi::class) + package hu.autsoft.krate.default import hu.autsoft.krate.Krate +import hu.autsoft.krate.base.KeyDelegate +import hu.autsoft.krate.internal.InternalKrateApi import hu.autsoft.krate.util.edit -import kotlin.properties.ReadWriteProperty import kotlin.reflect.KProperty internal class StringDelegateWithDefault( - private val key: String, + key: String, private val default: String, -) : ReadWriteProperty { +) : KeyDelegate(key) { @Suppress("NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS") override operator fun getValue(thisRef: Krate, property: KProperty<*>): String { diff --git a/krate/src/main/kotlin/hu/autsoft/krate/default/StringSetDelegateWithDefault.kt b/krate/src/main/kotlin/hu/autsoft/krate/default/StringSetDelegateWithDefault.kt index 3bd5a96..ccaef75 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/default/StringSetDelegateWithDefault.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/default/StringSetDelegateWithDefault.kt @@ -1,14 +1,17 @@ +@file:OptIn(InternalKrateApi::class) + package hu.autsoft.krate.default import hu.autsoft.krate.Krate +import hu.autsoft.krate.base.KeyDelegate +import hu.autsoft.krate.internal.InternalKrateApi import hu.autsoft.krate.util.edit -import kotlin.properties.ReadWriteProperty import kotlin.reflect.KProperty internal class StringSetDelegateWithDefault( - private val key: String, + key: String, private val default: Set, -) : ReadWriteProperty> { +) : KeyDelegate>(key) { @Suppress("NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS") override operator fun getValue(thisRef: Krate, property: KProperty<*>): Set { diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/BooleanDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/BooleanDelegate.kt index 0e54d1c..043db2a 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/BooleanDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/BooleanDelegate.kt @@ -1,13 +1,14 @@ +@file:OptIn(InternalKrateApi::class) + package hu.autsoft.krate.optional import hu.autsoft.krate.Krate +import hu.autsoft.krate.base.KeyDelegate +import hu.autsoft.krate.internal.InternalKrateApi import hu.autsoft.krate.util.edit -import kotlin.properties.ReadWriteProperty import kotlin.reflect.KProperty -internal class BooleanDelegate( - private val key: String, -) : ReadWriteProperty { +internal class BooleanDelegate(key: String) : KeyDelegate(key) { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Boolean? { return if (!thisRef.sharedPreferences.contains(key)) { diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/FloatDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/FloatDelegate.kt index 3d1285c..49f31b4 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/FloatDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/FloatDelegate.kt @@ -1,13 +1,14 @@ +@file:OptIn(InternalKrateApi::class) + package hu.autsoft.krate.optional import hu.autsoft.krate.Krate +import hu.autsoft.krate.base.KeyDelegate +import hu.autsoft.krate.internal.InternalKrateApi import hu.autsoft.krate.util.edit -import kotlin.properties.ReadWriteProperty import kotlin.reflect.KProperty -internal class FloatDelegate( - private val key: String, -) : ReadWriteProperty { +internal class FloatDelegate(key: String) : KeyDelegate(key) { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Float? { return if (!thisRef.sharedPreferences.contains(key)) { diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/IntDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/IntDelegate.kt index 7797cdb..3d2ea14 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/IntDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/IntDelegate.kt @@ -1,13 +1,14 @@ +@file:OptIn(InternalKrateApi::class) + package hu.autsoft.krate.optional import hu.autsoft.krate.Krate +import hu.autsoft.krate.base.KeyDelegate +import hu.autsoft.krate.internal.InternalKrateApi import hu.autsoft.krate.util.edit -import kotlin.properties.ReadWriteProperty import kotlin.reflect.KProperty -internal class IntDelegate( - private val key: String, -) : ReadWriteProperty { +internal class IntDelegate(key: String) : KeyDelegate(key) { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Int? { return if (!thisRef.sharedPreferences.contains(key)) { diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/LongDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/LongDelegate.kt index fab65f3..eb00561 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/LongDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/LongDelegate.kt @@ -1,13 +1,14 @@ +@file:OptIn(InternalKrateApi::class) + package hu.autsoft.krate.optional import hu.autsoft.krate.Krate +import hu.autsoft.krate.base.KeyDelegate +import hu.autsoft.krate.internal.InternalKrateApi import hu.autsoft.krate.util.edit -import kotlin.properties.ReadWriteProperty import kotlin.reflect.KProperty -internal class LongDelegate( - private val key: String, -) : ReadWriteProperty { +internal class LongDelegate(key: String) : KeyDelegate(key) { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Long? { return if (!thisRef.sharedPreferences.contains(key)) { diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/StringDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/StringDelegate.kt index 46fce62..bde73ba 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/StringDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/StringDelegate.kt @@ -1,13 +1,14 @@ +@file:OptIn(InternalKrateApi::class) + package hu.autsoft.krate.optional import hu.autsoft.krate.Krate +import hu.autsoft.krate.base.KeyDelegate +import hu.autsoft.krate.internal.InternalKrateApi import hu.autsoft.krate.util.edit -import kotlin.properties.ReadWriteProperty import kotlin.reflect.KProperty -internal class StringDelegate( - private val key: String, -) : ReadWriteProperty { +internal class StringDelegate(key: String) : KeyDelegate(key) { override operator fun getValue(thisRef: Krate, property: KProperty<*>): String? { return if (!thisRef.sharedPreferences.contains(key)) { diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/StringSetDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/StringSetDelegate.kt index 2f58ab5..bd37ae5 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/StringSetDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/StringSetDelegate.kt @@ -1,13 +1,14 @@ +@file:OptIn(InternalKrateApi::class) + package hu.autsoft.krate.optional import hu.autsoft.krate.Krate +import hu.autsoft.krate.base.KeyDelegate +import hu.autsoft.krate.internal.InternalKrateApi import hu.autsoft.krate.util.edit -import kotlin.properties.ReadWriteProperty import kotlin.reflect.KProperty -internal class StringSetDelegate( - private val key: String, -) : ReadWriteProperty?> { +internal class StringSetDelegate(key: String) : KeyDelegate?>(key) { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Set? { return if (!thisRef.sharedPreferences.contains(key)) { From 5d962eccc4683246b73f69ef0d0cf58fd767825c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1dy=20Istv=C3=A1n?= Date: Fri, 24 Sep 2021 12:40:09 +0200 Subject: [PATCH 02/31] Introduce withDefault() extension instead of using default property --- .../kotlin/hu/autsoft/krate/gson/Functions.kt | 26 +++++-- .../autsoft/krate/gson/ValidatedFunctions.kt | 4 +- .../gson/default/GsonDelegateWithDefault.kt | 49 ------------ .../krate/gson/optional/GsonDelegate.kt | 5 +- .../hu/autsoft/krate/kotlinx/Functions.kt | 26 +++++-- .../default/KotlinxDelegateWithDefault.kt | 48 ------------ .../krate/kotlinx/optional/KotlinxDelegate.kt | 3 - .../hu/autsoft/krate/moshi/Functions.kt | 26 +++++-- .../autsoft/krate/moshi/ValidatedFunctions.kt | 4 +- .../moshi/default/MoshiDelegateWithDefault.kt | 47 ----------- .../krate/moshi/optional/MoshiDelegate.kt | 3 - .../main/kotlin/hu/autsoft/krate/Functions.kt | 78 +++++++++++++++---- .../hu/autsoft/krate/ValidatedFunctions.kt | 42 ++++------ .../hu/autsoft/krate/base/KeyDelegate.kt | 2 - .../autsoft/krate/base/KeyDelegateProvider.kt | 2 - .../default/BooleanDelegateWithDefault.kt | 24 ------ .../krate/default/DelegateWithDefault.kt | 76 ++++++++++++++++++ .../krate/default/FloatDelegateWithDefault.kt | 24 ------ .../krate/default/IntDelegateWithDefault.kt | 24 ------ .../krate/default/LongDelegateWithDefault.kt | 24 ------ .../default/StringDelegateWithDefault.kt | 25 ------ .../default/StringSetDelegateWithDefault.kt | 25 ------ .../autsoft/krate/optional/BooleanDelegate.kt | 3 - .../autsoft/krate/optional/FloatDelegate.kt | 3 - .../hu/autsoft/krate/optional/IntDelegate.kt | 3 - .../hu/autsoft/krate/optional/LongDelegate.kt | 3 - .../autsoft/krate/optional/StringDelegate.kt | 3 - .../krate/optional/StringSetDelegate.kt | 3 - .../test/java/hu/autsoft/krate/TestKrate.kt | 4 +- 29 files changed, 218 insertions(+), 391 deletions(-) delete mode 100644 krate-gson/src/main/kotlin/hu/autsoft/krate/gson/default/GsonDelegateWithDefault.kt delete mode 100644 krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/default/KotlinxDelegateWithDefault.kt delete mode 100644 krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/default/MoshiDelegateWithDefault.kt delete mode 100644 krate/src/main/kotlin/hu/autsoft/krate/default/BooleanDelegateWithDefault.kt create mode 100644 krate/src/main/kotlin/hu/autsoft/krate/default/DelegateWithDefault.kt delete mode 100644 krate/src/main/kotlin/hu/autsoft/krate/default/FloatDelegateWithDefault.kt delete mode 100644 krate/src/main/kotlin/hu/autsoft/krate/default/IntDelegateWithDefault.kt delete mode 100644 krate/src/main/kotlin/hu/autsoft/krate/default/LongDelegateWithDefault.kt delete mode 100644 krate/src/main/kotlin/hu/autsoft/krate/default/StringDelegateWithDefault.kt delete mode 100644 krate/src/main/kotlin/hu/autsoft/krate/default/StringSetDelegateWithDefault.kt diff --git a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/Functions.kt b/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/Functions.kt index 1544412..a72a45b 100644 --- a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/Functions.kt +++ b/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/Functions.kt @@ -1,15 +1,16 @@ -@file:[Suppress("unused") OptIn(InternalKrateApi::class)] +@file:Suppress("unused") package hu.autsoft.krate.gson import com.google.gson.reflect.TypeToken import hu.autsoft.krate.Krate -import hu.autsoft.krate.base.KeyDelegate -import hu.autsoft.krate.gson.default.GsonDelegateWithDefaultFactory +import hu.autsoft.krate.base.KeyDelegateProvider +import hu.autsoft.krate.default.DelegateWithDefaultFactory import hu.autsoft.krate.gson.optional.GsonDelegateFactory import hu.autsoft.krate.internal.InternalKrateApi import java.lang.reflect.Type import kotlin.properties.PropertyDelegateProvider +import kotlin.properties.ReadWriteProperty /** * Creates an optional preference of type T with the given [key] in this [Krate] instance. @@ -17,7 +18,7 @@ import kotlin.properties.PropertyDelegateProvider */ public inline fun Krate.gsonPref( key: String, -): PropertyDelegateProvider> { +): KeyDelegateProvider { return gsonPrefImpl(key, object : TypeToken() {}.type) } @@ -25,7 +26,7 @@ public inline fun Krate.gsonPref( internal fun Krate.gsonPrefImpl( key: String, type: Type, -): PropertyDelegateProvider> { +): KeyDelegateProvider { return GsonDelegateFactory(key, type) } @@ -33,10 +34,18 @@ internal fun Krate.gsonPrefImpl( * Creates a non-optional preference of type T with the given [key] and [defaultValue] in this [Krate] instance. * This instance will be serialized using Gson. */ +@Deprecated( + message = "Use .withDefault() on a gsonPref instead", + level = DeprecationLevel.WARNING, + replaceWith = ReplaceWith( + "this.gsonPref(key).withDefault(defaultValue)", + imports = arrayOf("hu.autsoft.krate.default.withDefault"), + ), +) public inline fun Krate.gsonPref( key: String, defaultValue: T, -): PropertyDelegateProvider> { +): PropertyDelegateProvider> { return gsonPrefImpl(key, defaultValue, object : TypeToken() {}.type) } @@ -45,6 +54,7 @@ internal fun Krate.gsonPrefImpl( key: String, defaultValue: T, type: Type, -): PropertyDelegateProvider> { - return GsonDelegateWithDefaultFactory(key, defaultValue, type) +): PropertyDelegateProvider> { + @OptIn(InternalKrateApi::class) + return DelegateWithDefaultFactory(GsonDelegateFactory(key, type), defaultValue) } diff --git a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/ValidatedFunctions.kt b/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/ValidatedFunctions.kt index 4dd05b3..6b8115e 100644 --- a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/ValidatedFunctions.kt +++ b/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/ValidatedFunctions.kt @@ -4,7 +4,7 @@ package hu.autsoft.krate.gson import com.google.gson.reflect.TypeToken import hu.autsoft.krate.Krate -import hu.autsoft.krate.gson.default.GsonDelegateWithDefaultFactory +import hu.autsoft.krate.default.DelegateWithDefaultFactory import hu.autsoft.krate.gson.optional.GsonDelegateFactory import hu.autsoft.krate.internal.InternalKrateApi import hu.autsoft.krate.validation.ValidatedPreferenceDelegateFactory @@ -68,5 +68,5 @@ internal fun Krate.gsonPrefImpl( type: Type, isValid: (newValue: T) -> Boolean, ): PropertyDelegateProvider> { - return ValidatedPreferenceDelegateFactory(GsonDelegateWithDefaultFactory(key, defaultValue, type), isValid) + return ValidatedPreferenceDelegateFactory(DelegateWithDefaultFactory(GsonDelegateFactory(key, type), defaultValue), isValid) } diff --git a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/default/GsonDelegateWithDefault.kt b/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/default/GsonDelegateWithDefault.kt deleted file mode 100644 index a52d387..0000000 --- a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/default/GsonDelegateWithDefault.kt +++ /dev/null @@ -1,49 +0,0 @@ -@file:OptIn(InternalKrateApi::class) - -package hu.autsoft.krate.gson.default - -import com.google.gson.TypeAdapter -import com.google.gson.reflect.TypeToken -import hu.autsoft.krate.Krate -import hu.autsoft.krate.base.KeyDelegate -import hu.autsoft.krate.base.KeyDelegateProvider -import hu.autsoft.krate.gson.internalGson -import hu.autsoft.krate.gson.util.edit -import hu.autsoft.krate.internal.InternalKrateApi -import java.lang.reflect.Type -import kotlin.reflect.KProperty - -private class GsonDelegateWithDefault( - key: String, - private val default: T, - private val adapter: TypeAdapter, -) : KeyDelegate(key) { - - override operator fun getValue(thisRef: Krate, property: KProperty<*>): T { - if (!thisRef.sharedPreferences.contains(key)) { - return default - } - - val string = thisRef.sharedPreferences.getString(key, null) - return adapter.fromJson(string) - } - - override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: T) { - thisRef.sharedPreferences.edit { - putString(key, adapter.toJson(value)) - } - } -} - -internal class GsonDelegateWithDefaultFactory( - private val key: String, - private val default: T, - private val type: Type, -) : KeyDelegateProvider() { - - override fun provideDelegate(thisRef: Krate, property: KProperty<*>): KeyDelegate { - @Suppress("UNCHECKED_CAST") - val adapter: TypeAdapter = thisRef.internalGson.getAdapter(TypeToken.get(type)) as TypeAdapter - return GsonDelegateWithDefault(key, default, adapter) - } -} diff --git a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/optional/GsonDelegate.kt b/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/optional/GsonDelegate.kt index 2191d33..65991ca 100644 --- a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/optional/GsonDelegate.kt +++ b/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/optional/GsonDelegate.kt @@ -1,5 +1,3 @@ -@file:OptIn(InternalKrateApi::class) - package hu.autsoft.krate.gson.optional import com.google.gson.TypeAdapter @@ -9,7 +7,6 @@ import hu.autsoft.krate.base.KeyDelegate import hu.autsoft.krate.base.KeyDelegateProvider import hu.autsoft.krate.gson.internalGson import hu.autsoft.krate.gson.util.edit -import hu.autsoft.krate.internal.InternalKrateApi import java.lang.reflect.Type import kotlin.reflect.KProperty @@ -22,7 +19,7 @@ private class GsonDelegate( return if (!thisRef.sharedPreferences.contains(key)) { null } else { - val string = thisRef.sharedPreferences.getString(key, null) + val string = requireNotNull(thisRef.sharedPreferences.getString(key, null)) adapter.fromJson(string) } } diff --git a/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/Functions.kt b/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/Functions.kt index fad0afe..44ee4d6 100644 --- a/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/Functions.kt +++ b/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/Functions.kt @@ -1,13 +1,14 @@ -@file:[Suppress("unused") OptIn(InternalKrateApi::class)] +@file:Suppress("unused") package hu.autsoft.krate.kotlinx import hu.autsoft.krate.Krate -import hu.autsoft.krate.base.KeyDelegate +import hu.autsoft.krate.base.KeyDelegateProvider +import hu.autsoft.krate.default.DelegateWithDefaultFactory import hu.autsoft.krate.internal.InternalKrateApi -import hu.autsoft.krate.kotlinx.default.KotlinxDelegateWithDefaultFactory import hu.autsoft.krate.kotlinx.optional.KotlinxDelegateFactory import kotlin.properties.PropertyDelegateProvider +import kotlin.properties.ReadWriteProperty import kotlin.reflect.KType import kotlin.reflect.typeOf @@ -18,7 +19,7 @@ import kotlin.reflect.typeOf @OptIn(ExperimentalStdlibApi::class) public inline fun Krate.kotlinxPref( key: String, -): PropertyDelegateProvider> { +): KeyDelegateProvider { return kotlinxPrefImpl(key, typeOf()) } @@ -26,7 +27,7 @@ public inline fun Krate.kotlinxPref( internal fun Krate.kotlinxPrefImpl( key: String, type: KType, -): PropertyDelegateProvider> { +): KeyDelegateProvider { return KotlinxDelegateFactory(key, type) } @@ -34,11 +35,19 @@ internal fun Krate.kotlinxPrefImpl( * Creates a non-optional preference of type T with the given [key] and [defaultValue] in this [Krate] instance. * This instance will be serialized using kotlinx.serialization. */ +@Deprecated( + message = "Use .withDefault() on a kotlinxPref instead", + level = DeprecationLevel.WARNING, + replaceWith = ReplaceWith( + "this.kotlinxPref(key).withDefault(defaultValue)", + imports = arrayOf("hu.autsoft.krate.default.withDefault"), + ), +) @OptIn(ExperimentalStdlibApi::class) public inline fun Krate.kotlinxPref( key: String, defaultValue: T, -): PropertyDelegateProvider> { +): PropertyDelegateProvider> { return kotlinxPrefImpl(key, defaultValue, typeOf()) } @@ -47,6 +56,7 @@ internal fun Krate.kotlinxPrefImpl( key: String, defaultValue: T, type: KType, -): PropertyDelegateProvider> { - return KotlinxDelegateWithDefaultFactory(key, defaultValue, type) +): PropertyDelegateProvider> { + @OptIn(InternalKrateApi::class) + return DelegateWithDefaultFactory(KotlinxDelegateFactory(key, type), defaultValue) } diff --git a/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/default/KotlinxDelegateWithDefault.kt b/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/default/KotlinxDelegateWithDefault.kt deleted file mode 100644 index decedd4..0000000 --- a/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/default/KotlinxDelegateWithDefault.kt +++ /dev/null @@ -1,48 +0,0 @@ -@file:OptIn(InternalKrateApi::class) - -package hu.autsoft.krate.kotlinx.default - -import hu.autsoft.krate.Krate -import hu.autsoft.krate.base.KeyDelegate -import hu.autsoft.krate.base.KeyDelegateProvider -import hu.autsoft.krate.internal.InternalKrateApi -import hu.autsoft.krate.kotlinx.internalJson -import hu.autsoft.krate.kotlinx.util.edit -import kotlinx.serialization.KSerializer -import kotlinx.serialization.serializer -import kotlin.reflect.KProperty -import kotlin.reflect.KType - -private class KotlinxDelegateWithDefault( - key: String, - private val default: T, - private val serializer: KSerializer, -) : KeyDelegate(key) { - - override operator fun getValue(thisRef: Krate, property: KProperty<*>): T { - if (!thisRef.sharedPreferences.contains(key)) { - return default - } - val string = requireNotNull(thisRef.sharedPreferences.getString(key, null)) - return thisRef.internalJson.decodeFromString(serializer, string) - } - - override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: T) { - thisRef.sharedPreferences.edit { - putString(key, thisRef.internalJson.encodeToString(serializer, value)) - } - } -} - -internal class KotlinxDelegateWithDefaultFactory( - private val key: String, - private val default: T, - private val type: KType, -) : KeyDelegateProvider() { - - override fun provideDelegate(thisRef: Krate, property: KProperty<*>): KeyDelegate { - @Suppress("UNCHECKED_CAST") - val serializer = thisRef.internalJson.serializersModule.serializer(type) as KSerializer - return KotlinxDelegateWithDefault(key, default, serializer) - } -} diff --git a/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/optional/KotlinxDelegate.kt b/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/optional/KotlinxDelegate.kt index 2f6155b..325688d 100644 --- a/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/optional/KotlinxDelegate.kt +++ b/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/optional/KotlinxDelegate.kt @@ -1,11 +1,8 @@ -@file:OptIn(InternalKrateApi::class) - package hu.autsoft.krate.kotlinx.optional import hu.autsoft.krate.Krate import hu.autsoft.krate.base.KeyDelegate import hu.autsoft.krate.base.KeyDelegateProvider -import hu.autsoft.krate.internal.InternalKrateApi import hu.autsoft.krate.kotlinx.internalJson import hu.autsoft.krate.kotlinx.util.edit import kotlinx.serialization.KSerializer diff --git a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/Functions.kt b/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/Functions.kt index 3301103..a2d4a2e 100644 --- a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/Functions.kt +++ b/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/Functions.kt @@ -1,14 +1,15 @@ -@file:[Suppress("unused") OptIn(InternalKrateApi::class)] +@file:Suppress("unused") package hu.autsoft.krate.moshi import hu.autsoft.krate.Krate -import hu.autsoft.krate.base.KeyDelegate +import hu.autsoft.krate.base.KeyDelegateProvider +import hu.autsoft.krate.default.DelegateWithDefaultFactory import hu.autsoft.krate.internal.InternalKrateApi -import hu.autsoft.krate.moshi.default.MoshiDelegateWithDefaultFactory import hu.autsoft.krate.moshi.optional.MoshiDelegateFactory import java.lang.reflect.Type import kotlin.properties.PropertyDelegateProvider +import kotlin.properties.ReadWriteProperty import kotlin.reflect.javaType import kotlin.reflect.typeOf @@ -19,7 +20,7 @@ import kotlin.reflect.typeOf @OptIn(ExperimentalStdlibApi::class) public inline fun Krate.moshiPref( key: String, -): PropertyDelegateProvider> { +): KeyDelegateProvider { return moshiPrefImpl(key, typeOf().javaType) } @@ -27,7 +28,7 @@ public inline fun Krate.moshiPref( internal fun Krate.moshiPrefImpl( key: String, type: Type, -): PropertyDelegateProvider> { +): KeyDelegateProvider { return MoshiDelegateFactory(key, type) } @@ -35,11 +36,19 @@ internal fun Krate.moshiPrefImpl( * Creates a non-optional preference of type T with the given [key] and [defaultValue] in this [Krate] instance. * This instance will be serialized using Moshi. */ +@Deprecated( + message = "Use .withDefault() on a moshiPref instead", + level = DeprecationLevel.WARNING, + replaceWith = ReplaceWith( + "this.moshiPref(key).withDefault(defaultValue)", + imports = arrayOf("hu.autsoft.krate.default.withDefault"), + ), +) @OptIn(ExperimentalStdlibApi::class) public inline fun Krate.moshiPref( key: String, defaultValue: T, -): PropertyDelegateProvider> { +): PropertyDelegateProvider> { return moshiPrefImpl(key, defaultValue, typeOf().javaType) } @@ -48,6 +57,7 @@ internal fun Krate.moshiPrefImpl( key: String, defaultValue: T, type: Type, -): PropertyDelegateProvider> { - return MoshiDelegateWithDefaultFactory(key, defaultValue, type) +): PropertyDelegateProvider> { + @OptIn(InternalKrateApi::class) + return DelegateWithDefaultFactory(MoshiDelegateFactory(key, type), defaultValue) } diff --git a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/ValidatedFunctions.kt b/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/ValidatedFunctions.kt index 009736d..34c8a33 100644 --- a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/ValidatedFunctions.kt +++ b/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/ValidatedFunctions.kt @@ -3,8 +3,8 @@ package hu.autsoft.krate.moshi import hu.autsoft.krate.Krate +import hu.autsoft.krate.default.DelegateWithDefaultFactory import hu.autsoft.krate.internal.InternalKrateApi -import hu.autsoft.krate.moshi.default.MoshiDelegateWithDefaultFactory import hu.autsoft.krate.moshi.optional.MoshiDelegateFactory import hu.autsoft.krate.validation.ValidatedPreferenceDelegateFactory import java.lang.reflect.Type @@ -71,5 +71,5 @@ internal fun Krate.moshiPrefImpl( type: Type, isValid: (newValue: T) -> Boolean, ): PropertyDelegateProvider> { - return ValidatedPreferenceDelegateFactory(MoshiDelegateWithDefaultFactory(key, defaultValue, type), isValid) + return ValidatedPreferenceDelegateFactory(DelegateWithDefaultFactory(MoshiDelegateFactory(key, type), defaultValue), isValid) } diff --git a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/default/MoshiDelegateWithDefault.kt b/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/default/MoshiDelegateWithDefault.kt deleted file mode 100644 index 54e5f0c..0000000 --- a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/default/MoshiDelegateWithDefault.kt +++ /dev/null @@ -1,47 +0,0 @@ -@file:OptIn(InternalKrateApi::class) - -package hu.autsoft.krate.moshi.default - -import com.squareup.moshi.JsonAdapter -import hu.autsoft.krate.Krate -import hu.autsoft.krate.base.KeyDelegate -import hu.autsoft.krate.base.KeyDelegateProvider -import hu.autsoft.krate.internal.InternalKrateApi -import hu.autsoft.krate.moshi.realMoshiInstance -import hu.autsoft.krate.moshi.util.edit -import java.lang.reflect.Type -import kotlin.reflect.KProperty - - -private class MoshiDelegateWithDefault( - key: String, - private val default: T, - private val adapter: JsonAdapter, -) : KeyDelegate(key) { - - override operator fun getValue(thisRef: Krate, property: KProperty<*>): T { - if (!thisRef.sharedPreferences.contains(key)) { - return default - } - val string = requireNotNull(thisRef.sharedPreferences.getString(key, null)) - return requireNotNull(adapter.fromJson(string)) - } - - override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: T) { - thisRef.sharedPreferences.edit { - putString(key, adapter.toJson(value)) - } - } -} - -internal class MoshiDelegateWithDefaultFactory( - private val key: String, - private val default: T, - private val type: Type, -) : KeyDelegateProvider() { - - override fun provideDelegate(thisRef: Krate, property: KProperty<*>): KeyDelegate { - val adapter = thisRef.realMoshiInstance.adapter(type) - return MoshiDelegateWithDefault(key, default, adapter) - } -} diff --git a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/optional/MoshiDelegate.kt b/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/optional/MoshiDelegate.kt index c39213e..f38ffcb 100644 --- a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/optional/MoshiDelegate.kt +++ b/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/optional/MoshiDelegate.kt @@ -1,12 +1,9 @@ -@file:OptIn(InternalKrateApi::class) - package hu.autsoft.krate.moshi.optional import com.squareup.moshi.JsonAdapter import hu.autsoft.krate.Krate import hu.autsoft.krate.base.KeyDelegate import hu.autsoft.krate.base.KeyDelegateProvider -import hu.autsoft.krate.internal.InternalKrateApi import hu.autsoft.krate.moshi.realMoshiInstance import hu.autsoft.krate.moshi.util.edit import java.lang.reflect.Type diff --git a/krate/src/main/kotlin/hu/autsoft/krate/Functions.kt b/krate/src/main/kotlin/hu/autsoft/krate/Functions.kt index 5865e11..6ea8436 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/Functions.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/Functions.kt @@ -1,11 +1,11 @@ -@file:[Suppress("unused") OptIn(InternalKrateApi::class)] +@file:Suppress("unused") package hu.autsoft.krate import hu.autsoft.krate.base.KeyDelegate -import hu.autsoft.krate.default.* -import hu.autsoft.krate.internal.InternalKrateApi +import hu.autsoft.krate.default.withDefault import hu.autsoft.krate.optional.* +import kotlin.properties.ReadWriteProperty /** * Creates an optional preference of type [Boolean] with the given [key] in this [Krate] instance. @@ -53,41 +53,89 @@ public fun Krate.stringSetPref(key: String): KeyDelegate?> { /** * Creates a non-optional preference of type [Boolean] with the given [key] and [defaultValue] in this [Krate] instance. */ -public fun Krate.booleanPref(key: String, defaultValue: Boolean): KeyDelegate { - return BooleanDelegateWithDefault(key, defaultValue) +@Deprecated( + message = "Use .withDefault() on a booleanPref instead", + level = DeprecationLevel.WARNING, + replaceWith = ReplaceWith( + "this.booleanPref(key).withDefault(defaultValue)", + imports = arrayOf("hu.autsoft.krate.default.withDefault"), + ), +) +public fun Krate.booleanPref(key: String, defaultValue: Boolean): ReadWriteProperty { + return booleanPref(key).withDefault(defaultValue) } /** * Creates a non-optional preference of type [Float] with the given [key] and [defaultValue] in this [Krate] instance. */ -public fun Krate.floatPref(key: String, defaultValue: Float): KeyDelegate { - return FloatDelegateWithDefault(key, defaultValue) +@Deprecated( + message = "Use .withDefault() on a floatPref instead", + level = DeprecationLevel.WARNING, + replaceWith = ReplaceWith( + "this.floatPref(key).withDefault(defaultValue)", + imports = arrayOf("hu.autsoft.krate.default.withDefault"), + ), +) +public fun Krate.floatPref(key: String, defaultValue: Float): ReadWriteProperty { + return floatPref(key).withDefault(defaultValue) } /** * Creates a non-optional preference of type [Int] with the given [key] and [defaultValue] in this [Krate] instance. */ -public fun Krate.intPref(key: String, defaultValue: Int): KeyDelegate { - return IntDelegateWithDefault(key, defaultValue) +@Deprecated( + message = "Use .withDefault() on an intPref instead", + level = DeprecationLevel.ERROR, + replaceWith = ReplaceWith( + "this.intPref(key).withDefault(defaultValue)", + imports = arrayOf("hu.autsoft.krate.default.withDefault"), + ), +) +public fun Krate.intPref(key: String, defaultValue: Int): ReadWriteProperty { + return intPref(key).withDefault(defaultValue) } /** * Creates a non-optional preference of type [Long] with the given [key] and [defaultValue] in this [Krate] instance. */ -public fun Krate.longPref(key: String, defaultValue: Long): KeyDelegate { - return LongDelegateWithDefault(key, defaultValue) +@Deprecated( + message = "Use .withDefault() on a longPref instead", + level = DeprecationLevel.WARNING, + replaceWith = ReplaceWith( + "this.longPref(key).withDefault(defaultValue)", + imports = arrayOf("hu.autsoft.krate.default.withDefault"), + ), +) +public fun Krate.longPref(key: String, defaultValue: Long): ReadWriteProperty { + return longPref(key).withDefault(defaultValue) } /** * Creates a non-optional preference of type [String] with the given [key] and [defaultValue] in this [Krate] instance. */ -public fun Krate.stringPref(key: String, defaultValue: String): KeyDelegate { - return StringDelegateWithDefault(key, defaultValue) +@Deprecated( + message = "Use .withDefault() on a stringPref instead", + level = DeprecationLevel.WARNING, + replaceWith = ReplaceWith( + "this.stringPref(key).withDefault(defaultValue)", + imports = arrayOf("hu.autsoft.krate.default.withDefault"), + ), +) +public fun Krate.stringPref(key: String, defaultValue: String): ReadWriteProperty { + return stringPref(key).withDefault(defaultValue) } /** * Creates a non-optional preference of type Set with the given [key] and [defaultValue] in this [Krate] instance. */ -public fun Krate.stringSetPref(key: String, defaultValue: Set): KeyDelegate> { - return StringSetDelegateWithDefault(key, defaultValue) +@Deprecated( + message = "Use .withDefault() on a stringSetPref instead", + level = DeprecationLevel.WARNING, + replaceWith = ReplaceWith( + "this.stringSetPref(key).withDefault(defaultValue)", + imports = arrayOf("hu.autsoft.krate.default.withDefault"), + ), +) +public fun Krate.stringSetPref(key: String, defaultValue: Set): ReadWriteProperty> { + return stringSetPref(key).withDefault(defaultValue) } diff --git a/krate/src/main/kotlin/hu/autsoft/krate/ValidatedFunctions.kt b/krate/src/main/kotlin/hu/autsoft/krate/ValidatedFunctions.kt index 1e1d27c..b53e3c9 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/ValidatedFunctions.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/ValidatedFunctions.kt @@ -2,17 +2,9 @@ package hu.autsoft.krate -import hu.autsoft.krate.default.FloatDelegateWithDefault -import hu.autsoft.krate.default.IntDelegateWithDefault -import hu.autsoft.krate.default.LongDelegateWithDefault -import hu.autsoft.krate.default.StringDelegateWithDefault -import hu.autsoft.krate.default.StringSetDelegateWithDefault +import hu.autsoft.krate.default.withDefault import hu.autsoft.krate.internal.InternalKrateApi -import hu.autsoft.krate.optional.FloatDelegate -import hu.autsoft.krate.optional.IntDelegate -import hu.autsoft.krate.optional.LongDelegate -import hu.autsoft.krate.optional.StringDelegate -import hu.autsoft.krate.optional.StringSetDelegate +import hu.autsoft.krate.optional.* import hu.autsoft.krate.validation.ValidatedPreferenceDelegate import kotlin.properties.ReadWriteProperty @@ -47,8 +39,8 @@ public fun Krate.floatPref( message = "Use .validate {} on a floatPref instead", level = DeprecationLevel.ERROR, replaceWith = ReplaceWith( - "this.floatPref(key, defaultValue).validate(isValid)", - imports = arrayOf("hu.autsoft.krate.validation.validate"), + "this.floatPref(key).withDefault(defaultValue).validate(isValid)", + imports = arrayOf("hu.autsoft.krate.default.withDefault", "hu.autsoft.krate.validation.validate"), ), ) public fun Krate.floatPref( @@ -56,7 +48,7 @@ public fun Krate.floatPref( defaultValue: Float, isValid: (newValue: Float) -> Boolean, ): ReadWriteProperty { - return ValidatedPreferenceDelegate(FloatDelegateWithDefault(key, defaultValue), isValid) + return ValidatedPreferenceDelegate(FloatDelegate(key).withDefault(defaultValue), isValid) } /** @@ -90,8 +82,8 @@ public fun Krate.intPref( message = "Use .validate {} on an intPref instead", level = DeprecationLevel.ERROR, replaceWith = ReplaceWith( - "this.intPref(key, defaultValue).validate(isValid)", - imports = arrayOf("hu.autsoft.krate.validation.validate"), + "this.intPref(key).withDefault(defaultValue).validate(isValid)", + imports = arrayOf("hu.autsoft.krate.default.withDefault", "hu.autsoft.krate.validation.validate"), ), ) public fun Krate.intPref( @@ -99,7 +91,7 @@ public fun Krate.intPref( defaultValue: Int, isValid: (newValue: Int) -> Boolean, ): ReadWriteProperty { - return ValidatedPreferenceDelegate(IntDelegateWithDefault(key, defaultValue), isValid) + return ValidatedPreferenceDelegate(IntDelegate(key).withDefault(defaultValue), isValid) } /** @@ -133,8 +125,8 @@ public fun Krate.longPref( message = "Use .validate {} on a longPref instead", level = DeprecationLevel.ERROR, replaceWith = ReplaceWith( - "this.longPref(key, defaultValue).validate(isValid)", - imports = arrayOf("hu.autsoft.krate.validation.validate"), + "this.longPref(key).withDefault(defaultValue).validate(isValid)", + imports = arrayOf("hu.autsoft.krate.default.withDefault", "hu.autsoft.krate.validation.validate"), ), ) public fun Krate.longPref( @@ -142,7 +134,7 @@ public fun Krate.longPref( defaultValue: Long, isValid: (newValue: Long) -> Boolean, ): ReadWriteProperty { - return ValidatedPreferenceDelegate(LongDelegateWithDefault(key, defaultValue), isValid) + return ValidatedPreferenceDelegate(LongDelegate(key).withDefault(defaultValue), isValid) } /** @@ -176,8 +168,8 @@ public fun Krate.stringPref( message = "Use .validate {} on a stringPref instead", level = DeprecationLevel.ERROR, replaceWith = ReplaceWith( - "this.stringPref(key, defaultValue).validate(isValid)", - imports = arrayOf("hu.autsoft.krate.validation.validate"), + "this.stringPref(key).withDefault(defaultValue).validate(isValid)", + imports = arrayOf("hu.autsoft.krate.default.withDefault", "hu.autsoft.krate.validation.validate"), ), ) public fun Krate.stringPref( @@ -185,7 +177,7 @@ public fun Krate.stringPref( defaultValue: String, isValid: (newValue: String) -> Boolean, ): ReadWriteProperty { - return ValidatedPreferenceDelegate(StringDelegateWithDefault(key, defaultValue), isValid) + return ValidatedPreferenceDelegate(StringDelegate(key).withDefault(defaultValue), isValid) } /** @@ -219,8 +211,8 @@ public fun Krate.stringSetPref( message = "Use .validate {} on a stringSetPref instead", level = DeprecationLevel.ERROR, replaceWith = ReplaceWith( - "this.stringSetPref(key, defaultValue).validate(isValid)", - imports = arrayOf("hu.autsoft.krate.validation.validate"), + "this.stringSetPref(key).withDefault(defaultValue).validate(isValid)", + imports = arrayOf("hu.autsoft.krate.default.withDefault", "hu.autsoft.krate.validation.validate"), ), ) public fun Krate.stringSetPref( @@ -228,5 +220,5 @@ public fun Krate.stringSetPref( defaultValue: Set, isValid: (newValue: Set) -> Boolean, ): ReadWriteProperty> { - return ValidatedPreferenceDelegate(StringSetDelegateWithDefault(key, defaultValue), isValid) + return ValidatedPreferenceDelegate(StringSetDelegate(key).withDefault(defaultValue), isValid) } diff --git a/krate/src/main/kotlin/hu/autsoft/krate/base/KeyDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/base/KeyDelegate.kt index 6b023a5..30a720d 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/base/KeyDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/base/KeyDelegate.kt @@ -1,8 +1,6 @@ package hu.autsoft.krate.base import hu.autsoft.krate.Krate -import hu.autsoft.krate.internal.InternalKrateApi import kotlin.properties.ReadWriteProperty -@InternalKrateApi public abstract class KeyDelegate(public val key: String) : ReadWriteProperty \ No newline at end of file diff --git a/krate/src/main/kotlin/hu/autsoft/krate/base/KeyDelegateProvider.kt b/krate/src/main/kotlin/hu/autsoft/krate/base/KeyDelegateProvider.kt index 2a88791..5ba8dce 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/base/KeyDelegateProvider.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/base/KeyDelegateProvider.kt @@ -1,8 +1,6 @@ package hu.autsoft.krate.base import hu.autsoft.krate.Krate -import hu.autsoft.krate.internal.InternalKrateApi import kotlin.properties.PropertyDelegateProvider -@InternalKrateApi public abstract class KeyDelegateProvider : PropertyDelegateProvider> \ No newline at end of file diff --git a/krate/src/main/kotlin/hu/autsoft/krate/default/BooleanDelegateWithDefault.kt b/krate/src/main/kotlin/hu/autsoft/krate/default/BooleanDelegateWithDefault.kt deleted file mode 100644 index dd7e138..0000000 --- a/krate/src/main/kotlin/hu/autsoft/krate/default/BooleanDelegateWithDefault.kt +++ /dev/null @@ -1,24 +0,0 @@ -@file:OptIn(InternalKrateApi::class) - -package hu.autsoft.krate.default - -import hu.autsoft.krate.Krate -import hu.autsoft.krate.base.KeyDelegate -import hu.autsoft.krate.internal.InternalKrateApi -import hu.autsoft.krate.util.edit -import kotlin.reflect.KProperty - -internal class BooleanDelegateWithDefault( - key: String, - private val default: Boolean, -) : KeyDelegate(key) { - - override operator fun getValue(thisRef: Krate, property: KProperty<*>): Boolean { - return thisRef.sharedPreferences.getBoolean(key, default) - } - - override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: Boolean) { - thisRef.sharedPreferences.edit { putBoolean(key, value) } - } - -} diff --git a/krate/src/main/kotlin/hu/autsoft/krate/default/DelegateWithDefault.kt b/krate/src/main/kotlin/hu/autsoft/krate/default/DelegateWithDefault.kt new file mode 100644 index 0000000..1e8dc68 --- /dev/null +++ b/krate/src/main/kotlin/hu/autsoft/krate/default/DelegateWithDefault.kt @@ -0,0 +1,76 @@ +@file:Suppress("unused") + +package hu.autsoft.krate.default + +import hu.autsoft.krate.Krate +import hu.autsoft.krate.base.KeyDelegate +import hu.autsoft.krate.base.KeyDelegateProvider +import hu.autsoft.krate.internal.InternalKrateApi +import kotlin.properties.PropertyDelegateProvider +import kotlin.properties.ReadWriteProperty +import kotlin.reflect.KProperty + +@InternalKrateApi +public class DelegateWithDefault( + private val delegate: KeyDelegate, + private val default: T, +) : ReadWriteProperty { + + override fun setValue(thisRef: Krate, property: KProperty<*>, value: T) { + delegate.setValue(thisRef, property, value) + } + + override fun getValue(thisRef: Krate, property: KProperty<*>): T { + return if (!thisRef.sharedPreferences.contains(delegate.key)) default else delegate.getValue(thisRef, property)!! + } +} + +@InternalKrateApi +public class DelegateWithDefaultFactory( + private val propertyDelegateProvider: PropertyDelegateProvider>, + private val default: T, +) : PropertyDelegateProvider> { + + override fun provideDelegate(thisRef: Krate, property: KProperty<*>): ReadWriteProperty { + return DelegateWithDefault( + delegate = propertyDelegateProvider.provideDelegate(thisRef, property), + default = default + ) + } +} + +/** + * Adds a default value to a Krate delegate. + * + * If a delegate returns with null because it have not been set before, then this returns with [default] + * + * Example property using this function: + * + * ```kotlin + * var defaultString by stringPref("validatedString").withDefault("default") + * ``` + */ +public fun KeyDelegate.withDefault( + default: T +): ReadWriteProperty { + @OptIn(InternalKrateApi::class) + return DelegateWithDefault(this, default) +} + +/** + * Adds a default value to a Krate delegate factory. + * + * If a delegate returns with null because it have not been set before, then this returns with [default] + * + * Example property using this function: + * + * ```kotlin + * var defaultModel by kotlinxPref("defaultModel").withDefault(DefaultModel()) + * ``` + */ +public fun KeyDelegateProvider.withDefault( + default: T +): PropertyDelegateProvider> { + @OptIn(InternalKrateApi::class) + return DelegateWithDefaultFactory(this, default) +} diff --git a/krate/src/main/kotlin/hu/autsoft/krate/default/FloatDelegateWithDefault.kt b/krate/src/main/kotlin/hu/autsoft/krate/default/FloatDelegateWithDefault.kt deleted file mode 100644 index 3eda9e6..0000000 --- a/krate/src/main/kotlin/hu/autsoft/krate/default/FloatDelegateWithDefault.kt +++ /dev/null @@ -1,24 +0,0 @@ -@file:OptIn(InternalKrateApi::class) - -package hu.autsoft.krate.default - -import hu.autsoft.krate.Krate -import hu.autsoft.krate.base.KeyDelegate -import hu.autsoft.krate.internal.InternalKrateApi -import hu.autsoft.krate.util.edit -import kotlin.reflect.KProperty - -internal class FloatDelegateWithDefault( - key: String, - private val default: Float, -) : KeyDelegate(key) { - - override operator fun getValue(thisRef: Krate, property: KProperty<*>): Float { - return thisRef.sharedPreferences.getFloat(key, default) - } - - override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: Float) { - thisRef.sharedPreferences.edit { putFloat(key, value) } - } - -} diff --git a/krate/src/main/kotlin/hu/autsoft/krate/default/IntDelegateWithDefault.kt b/krate/src/main/kotlin/hu/autsoft/krate/default/IntDelegateWithDefault.kt deleted file mode 100644 index 3313fc5..0000000 --- a/krate/src/main/kotlin/hu/autsoft/krate/default/IntDelegateWithDefault.kt +++ /dev/null @@ -1,24 +0,0 @@ -@file:OptIn(InternalKrateApi::class) - -package hu.autsoft.krate.default - -import hu.autsoft.krate.Krate -import hu.autsoft.krate.base.KeyDelegate -import hu.autsoft.krate.internal.InternalKrateApi -import hu.autsoft.krate.util.edit -import kotlin.reflect.KProperty - -internal class IntDelegateWithDefault( - key: String, - private val default: Int, -) : KeyDelegate(key) { - - override operator fun getValue(thisRef: Krate, property: KProperty<*>): Int { - return thisRef.sharedPreferences.getInt(key, default) - } - - override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: Int) { - thisRef.sharedPreferences.edit { putInt(key, value) } - } - -} diff --git a/krate/src/main/kotlin/hu/autsoft/krate/default/LongDelegateWithDefault.kt b/krate/src/main/kotlin/hu/autsoft/krate/default/LongDelegateWithDefault.kt deleted file mode 100644 index cb1abc6..0000000 --- a/krate/src/main/kotlin/hu/autsoft/krate/default/LongDelegateWithDefault.kt +++ /dev/null @@ -1,24 +0,0 @@ -@file:OptIn(InternalKrateApi::class) - -package hu.autsoft.krate.default - -import hu.autsoft.krate.Krate -import hu.autsoft.krate.base.KeyDelegate -import hu.autsoft.krate.internal.InternalKrateApi -import hu.autsoft.krate.util.edit -import kotlin.reflect.KProperty - -internal class LongDelegateWithDefault( - key: String, - private val default: Long, -) : KeyDelegate(key) { - - override operator fun getValue(thisRef: Krate, property: KProperty<*>): Long { - return thisRef.sharedPreferences.getLong(key, default) - } - - override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: Long) { - thisRef.sharedPreferences.edit { putLong(key, value) } - } - -} diff --git a/krate/src/main/kotlin/hu/autsoft/krate/default/StringDelegateWithDefault.kt b/krate/src/main/kotlin/hu/autsoft/krate/default/StringDelegateWithDefault.kt deleted file mode 100644 index ef5c0d0..0000000 --- a/krate/src/main/kotlin/hu/autsoft/krate/default/StringDelegateWithDefault.kt +++ /dev/null @@ -1,25 +0,0 @@ -@file:OptIn(InternalKrateApi::class) - -package hu.autsoft.krate.default - -import hu.autsoft.krate.Krate -import hu.autsoft.krate.base.KeyDelegate -import hu.autsoft.krate.internal.InternalKrateApi -import hu.autsoft.krate.util.edit -import kotlin.reflect.KProperty - -internal class StringDelegateWithDefault( - key: String, - private val default: String, -) : KeyDelegate(key) { - - @Suppress("NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS") - override operator fun getValue(thisRef: Krate, property: KProperty<*>): String { - return thisRef.sharedPreferences.getString(key, default)!! - } - - override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: String) { - thisRef.sharedPreferences.edit { putString(key, value) } - } - -} diff --git a/krate/src/main/kotlin/hu/autsoft/krate/default/StringSetDelegateWithDefault.kt b/krate/src/main/kotlin/hu/autsoft/krate/default/StringSetDelegateWithDefault.kt deleted file mode 100644 index ccaef75..0000000 --- a/krate/src/main/kotlin/hu/autsoft/krate/default/StringSetDelegateWithDefault.kt +++ /dev/null @@ -1,25 +0,0 @@ -@file:OptIn(InternalKrateApi::class) - -package hu.autsoft.krate.default - -import hu.autsoft.krate.Krate -import hu.autsoft.krate.base.KeyDelegate -import hu.autsoft.krate.internal.InternalKrateApi -import hu.autsoft.krate.util.edit -import kotlin.reflect.KProperty - -internal class StringSetDelegateWithDefault( - key: String, - private val default: Set, -) : KeyDelegate>(key) { - - @Suppress("NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS") - override operator fun getValue(thisRef: Krate, property: KProperty<*>): Set { - return thisRef.sharedPreferences.getStringSet(key, default)!! - } - - override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: Set) { - thisRef.sharedPreferences.edit { putStringSet(key, value) } - } - -} diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/BooleanDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/BooleanDelegate.kt index 043db2a..50557fa 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/BooleanDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/BooleanDelegate.kt @@ -1,10 +1,7 @@ -@file:OptIn(InternalKrateApi::class) - package hu.autsoft.krate.optional import hu.autsoft.krate.Krate import hu.autsoft.krate.base.KeyDelegate -import hu.autsoft.krate.internal.InternalKrateApi import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/FloatDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/FloatDelegate.kt index 49f31b4..d33a116 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/FloatDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/FloatDelegate.kt @@ -1,10 +1,7 @@ -@file:OptIn(InternalKrateApi::class) - package hu.autsoft.krate.optional import hu.autsoft.krate.Krate import hu.autsoft.krate.base.KeyDelegate -import hu.autsoft.krate.internal.InternalKrateApi import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/IntDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/IntDelegate.kt index 3d2ea14..60e33cc 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/IntDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/IntDelegate.kt @@ -1,10 +1,7 @@ -@file:OptIn(InternalKrateApi::class) - package hu.autsoft.krate.optional import hu.autsoft.krate.Krate import hu.autsoft.krate.base.KeyDelegate -import hu.autsoft.krate.internal.InternalKrateApi import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/LongDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/LongDelegate.kt index eb00561..085ffab 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/LongDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/LongDelegate.kt @@ -1,10 +1,7 @@ -@file:OptIn(InternalKrateApi::class) - package hu.autsoft.krate.optional import hu.autsoft.krate.Krate import hu.autsoft.krate.base.KeyDelegate -import hu.autsoft.krate.internal.InternalKrateApi import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/StringDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/StringDelegate.kt index bde73ba..8dce3c1 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/StringDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/StringDelegate.kt @@ -1,10 +1,7 @@ -@file:OptIn(InternalKrateApi::class) - package hu.autsoft.krate.optional import hu.autsoft.krate.Krate import hu.autsoft.krate.base.KeyDelegate -import hu.autsoft.krate.internal.InternalKrateApi import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/StringSetDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/StringSetDelegate.kt index bd37ae5..48c0093 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/StringSetDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/StringSetDelegate.kt @@ -1,10 +1,7 @@ -@file:OptIn(InternalKrateApi::class) - package hu.autsoft.krate.optional import hu.autsoft.krate.Krate import hu.autsoft.krate.base.KeyDelegate -import hu.autsoft.krate.internal.InternalKrateApi import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty diff --git a/krate/src/test/java/hu/autsoft/krate/TestKrate.kt b/krate/src/test/java/hu/autsoft/krate/TestKrate.kt index f9a11fc..9e30613 100644 --- a/krate/src/test/java/hu/autsoft/krate/TestKrate.kt +++ b/krate/src/test/java/hu/autsoft/krate/TestKrate.kt @@ -1,6 +1,7 @@ package hu.autsoft.krate import android.content.Context +import hu.autsoft.krate.default.withDefault import hu.autsoft.krate.validation.validate internal class TestKrate(context: Context) : SimpleKrate(context) { @@ -17,7 +18,8 @@ internal class TestKrate(context: Context) : SimpleKrate(context) { var optionalStringSet by stringSetPref("optionalStringSet") var optionalValidatedString by stringPref("optionalValidatedString") .validate { it?.length ?: 5 == 5 } - var defaultValidatedFloat by floatPref("defaultValidatedFloat", 0.0f) + var defaultValidatedFloat by floatPref("defaultValidatedFloat") + .withDefault(0.0f) .validate { it in 0.0f..1.0f } } From 1936c249ce3657afa89882b08fc1dd094f89461b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1dy=20Istv=C3=A1n?= Date: Fri, 24 Sep 2021 13:34:24 +0200 Subject: [PATCH 03/31] Update Example app to follow API changes --- app/build.gradle | 28 ++++- .../autsoft/krateexample/ExampleActivity.kt | 19 +++ .../krateexample/krates/ExampleCustomKrate.kt | 28 +++-- .../krateexample/krates/ExampleSettings.kt | 5 + .../krateexample/krates/ExampleSimpleKrate.kt | 29 ++--- .../krates/ExampleThirdPartyKrates.kt | 5 +- .../hu/autsoft/krateexample/models/User.kt | 11 ++ app/src/main/res/layout/activity_example.xml | 117 ++++++++++++++++++ 8 files changed, 208 insertions(+), 34 deletions(-) create mode 100644 app/src/main/java/hu/autsoft/krateexample/models/User.kt diff --git a/app/build.gradle b/app/build.gradle index 3d85abb..d611400 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,5 +1,9 @@ -apply plugin: 'com.android.application' -apply plugin: 'kotlin-android' +plugins { + id 'com.android.application' + id 'kotlin-android' + id 'kotlin-kapt' + id 'org.jetbrains.kotlin.plugin.serialization' +} android { compileSdkVersion compile_sdk @@ -22,14 +26,28 @@ android { } dependencies { + + // Krate + implementation project(path: ':krate') + implementation project(path: ':krate-gson') + implementation project(path: ':krate-kotlinx') + implementation project(path: ':krate-moshi-codegen') + + // Android implementation 'com.google.android.material:material:1.4.0' implementation 'androidx.appcompat:appcompat:1.3.1' implementation 'androidx.constraintlayout:constraintlayout:2.1.0' + // KotlinX + implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.0-RC' + + // Moshi + def moshiVersion = "1.12.0" + implementation "com.squareup.moshi:moshi-kotlin:$moshiVersion" + kapt "com.squareup.moshi:moshi-kotlin-codegen:$moshiVersion" + implementation "com.squareup.moshi:moshi-adapters:$moshiVersion" + // Third-party SharedPreferences implementations implementation "androidx.security:security-crypto:1.1.0-alpha03" implementation 'com.frybits.harmony:harmony:1.1.2' - - implementation project(path: ':krate') - implementation project(path: ':krate-gson') } diff --git a/app/src/main/java/hu/autsoft/krateexample/ExampleActivity.kt b/app/src/main/java/hu/autsoft/krateexample/ExampleActivity.kt index 7af8757..4a774c5 100644 --- a/app/src/main/java/hu/autsoft/krateexample/ExampleActivity.kt +++ b/app/src/main/java/hu/autsoft/krateexample/ExampleActivity.kt @@ -6,6 +6,7 @@ import hu.autsoft.krateexample.databinding.ActivityExampleBinding import hu.autsoft.krateexample.krates.ExampleCustomKrate import hu.autsoft.krateexample.krates.ExampleSettings import hu.autsoft.krateexample.krates.ExampleSimpleKrate +import hu.autsoft.krateexample.models.User class ExampleActivity : AppCompatActivity() { @@ -47,6 +48,12 @@ class ExampleActivity : AppCompatActivity() { binding.longPreferenceInput.setText(exampleSettings.exampleLong.toString()) binding.stringPreferenceInput.setText(exampleSettings.exampleString) binding.stringSetPreferenceInput.setText(exampleSettings.exampleStringSet.joinToString(separator = ", ")) + binding.gsonPreferenceFirstInput.setText(exampleSettings.exampleUserGson.firstName) + binding.gsonPreferenceLastInput.setText(exampleSettings.exampleUserGson.lastName) + binding.kotlinxPreferenceFirstInput.setText(exampleSettings.exampleUserKotlinX.firstName) + binding.kotlinxPreferenceLastInput.setText(exampleSettings.exampleUserKotlinX.lastName) + binding.moshiPreferenceFirstInput.setText(exampleSettings.exampleUserMoshi.firstName) + binding.moshiPreferenceLastInput.setText(exampleSettings.exampleUserMoshi.lastName) } override fun onPause() { @@ -59,6 +66,18 @@ class ExampleActivity : AppCompatActivity() { exampleSettings.exampleString = binding.stringPreferenceInput.text.toString() exampleSettings.exampleStringSet = binding.stringSetPreferenceInput.text.toString().split(",").map(String::trim).toSet() + exampleSettings.exampleUserGson = User( + binding.gsonPreferenceFirstInput.text.toString(), + binding.gsonPreferenceLastInput.text.toString() + ) + exampleSettings.exampleUserKotlinX = User( + binding.kotlinxPreferenceFirstInput.text.toString(), + binding.kotlinxPreferenceLastInput.text.toString() + ) + exampleSettings.exampleUserMoshi = User( + binding.moshiPreferenceFirstInput.text.toString(), + binding.moshiPreferenceLastInput.text.toString() + ) } } diff --git a/app/src/main/java/hu/autsoft/krateexample/krates/ExampleCustomKrate.kt b/app/src/main/java/hu/autsoft/krateexample/krates/ExampleCustomKrate.kt index ab43afa..cb5700a 100644 --- a/app/src/main/java/hu/autsoft/krateexample/krates/ExampleCustomKrate.kt +++ b/app/src/main/java/hu/autsoft/krateexample/krates/ExampleCustomKrate.kt @@ -4,13 +4,12 @@ package hu.autsoft.krateexample.krates import android.content.Context import android.content.SharedPreferences -import hu.autsoft.krate.Krate -import hu.autsoft.krate.booleanPref -import hu.autsoft.krate.floatPref -import hu.autsoft.krate.intPref -import hu.autsoft.krate.longPref -import hu.autsoft.krate.stringPref -import hu.autsoft.krate.stringSetPref +import hu.autsoft.krate.* +import hu.autsoft.krate.default.withDefault +import hu.autsoft.krate.gson.gsonPref +import hu.autsoft.krate.kotlinx.kotlinxPref +import hu.autsoft.krate.moshi.moshiPref +import hu.autsoft.krateexample.models.User class ExampleCustomKrate(context: Context) : Krate, ExampleSettings { @@ -20,11 +19,14 @@ class ExampleCustomKrate(context: Context) : Krate, ExampleSettings { sharedPreferences = context.applicationContext.getSharedPreferences("custom_krate_prefs", Context.MODE_PRIVATE) } - override var exampleBoolean by booleanPref("exampleBoolean", false) - override var exampleFloat by floatPref("exampleFloat", 0f) - override var exampleInt by intPref("exampleInt", 0) - override var exampleLong by longPref("exampleLong", 0L) - override var exampleString by stringPref("exampleString", "") - override var exampleStringSet by stringSetPref("exampleStringSet", setOf()) + override var exampleBoolean by booleanPref("exampleBoolean").withDefault(false) + override var exampleFloat by floatPref("exampleFloat").withDefault(0f) + override var exampleInt by intPref("exampleInt").withDefault(0) + override var exampleLong by longPref("exampleLong").withDefault(0L) + override var exampleString by stringPref("exampleString").withDefault("") + override var exampleStringSet by stringSetPref("exampleStringSet").withDefault(setOf()) + override var exampleUserGson by gsonPref("user_gson").withDefault(User("Gson", "Smith")) + override var exampleUserKotlinX by kotlinxPref("user_kotlinx").withDefault(User("KotlinX", "Smith")) + override var exampleUserMoshi by moshiPref("user_moshi").withDefault(User("Moshi", "Smith")) } diff --git a/app/src/main/java/hu/autsoft/krateexample/krates/ExampleSettings.kt b/app/src/main/java/hu/autsoft/krateexample/krates/ExampleSettings.kt index 1252ce7..c922892 100644 --- a/app/src/main/java/hu/autsoft/krateexample/krates/ExampleSettings.kt +++ b/app/src/main/java/hu/autsoft/krateexample/krates/ExampleSettings.kt @@ -1,5 +1,7 @@ package hu.autsoft.krateexample.krates +import hu.autsoft.krateexample.models.User + interface ExampleSettings { var exampleBoolean: Boolean var exampleFloat: Float @@ -7,4 +9,7 @@ interface ExampleSettings { var exampleLong: Long var exampleString: String var exampleStringSet: Set + var exampleUserGson: User + var exampleUserKotlinX: User + var exampleUserMoshi: User } diff --git a/app/src/main/java/hu/autsoft/krateexample/krates/ExampleSimpleKrate.kt b/app/src/main/java/hu/autsoft/krateexample/krates/ExampleSimpleKrate.kt index 62e9def..cee1fcb 100644 --- a/app/src/main/java/hu/autsoft/krateexample/krates/ExampleSimpleKrate.kt +++ b/app/src/main/java/hu/autsoft/krateexample/krates/ExampleSimpleKrate.kt @@ -1,13 +1,12 @@ package hu.autsoft.krateexample.krates import android.content.Context -import hu.autsoft.krate.SimpleKrate -import hu.autsoft.krate.booleanPref -import hu.autsoft.krate.floatPref -import hu.autsoft.krate.intPref -import hu.autsoft.krate.longPref -import hu.autsoft.krate.stringPref -import hu.autsoft.krate.stringSetPref +import hu.autsoft.krate.* +import hu.autsoft.krate.default.withDefault +import hu.autsoft.krate.gson.gsonPref +import hu.autsoft.krate.kotlinx.kotlinxPref +import hu.autsoft.krate.moshi.moshiPref +import hu.autsoft.krateexample.models.User class ExampleSimpleKrate(context: Context) : SimpleKrate(context, NAME), ExampleSettings { @@ -15,11 +14,13 @@ class ExampleSimpleKrate(context: Context) : SimpleKrate(context, NAME), Example private const val NAME = "example" } - override var exampleBoolean by booleanPref("exampleBoolean", false) - override var exampleFloat by floatPref("exampleFloat", 0f) - override var exampleInt by intPref("exampleInt", 0) - override var exampleLong by longPref("exampleLong", 0L) - override var exampleString by stringPref("exampleString", "") - override var exampleStringSet by stringSetPref("exampleStringSet", setOf()) - + override var exampleBoolean by booleanPref("exampleBoolean").withDefault(false) + override var exampleFloat by floatPref("exampleFloat").withDefault(0f) + override var exampleInt by intPref("exampleInt").withDefault(0) + override var exampleLong by longPref("exampleLong").withDefault(0L) + override var exampleString by stringPref("exampleString").withDefault("") + override var exampleStringSet by stringSetPref("exampleStringSet").withDefault(setOf()) + override var exampleUserGson by gsonPref("user_gson").withDefault(User("Gson", "Smith")) + override var exampleUserKotlinX by kotlinxPref("user_kotlinx").withDefault(User("KotlinX", "Smith")) + override var exampleUserMoshi by moshiPref("user_moshi").withDefault(User("Moshi", "Smith")) } diff --git a/app/src/main/java/hu/autsoft/krateexample/krates/ExampleThirdPartyKrates.kt b/app/src/main/java/hu/autsoft/krateexample/krates/ExampleThirdPartyKrates.kt index fcca564..b4aa8a0 100644 --- a/app/src/main/java/hu/autsoft/krateexample/krates/ExampleThirdPartyKrates.kt +++ b/app/src/main/java/hu/autsoft/krateexample/krates/ExampleThirdPartyKrates.kt @@ -6,6 +6,7 @@ import androidx.security.crypto.EncryptedSharedPreferences import androidx.security.crypto.MasterKey import com.frybits.harmony.getHarmonySharedPreferences import hu.autsoft.krate.Krate +import hu.autsoft.krate.default.withDefault import hu.autsoft.krate.stringPref /** @@ -32,7 +33,7 @@ class EncryptedKrate(applicationContext: Context) : Krate { ) } - val myStringValue: String by stringPref("my_string_value", "") + val myStringValue: String by stringPref("my_string_value").withDefault("") } /** @@ -42,5 +43,5 @@ class EncryptedKrate(applicationContext: Context) : Krate { class HarmonyKrate(context: Context) : Krate { override val sharedPreferences: SharedPreferences = context.getHarmonySharedPreferences("PREF_NAME") - val myStringValue: String by stringPref("my_string_value", "") + val myStringValue: String by stringPref("my_string_value").withDefault("") } diff --git a/app/src/main/java/hu/autsoft/krateexample/models/User.kt b/app/src/main/java/hu/autsoft/krateexample/models/User.kt new file mode 100644 index 0000000..ede4265 --- /dev/null +++ b/app/src/main/java/hu/autsoft/krateexample/models/User.kt @@ -0,0 +1,11 @@ +package hu.autsoft.krateexample.models + +import com.squareup.moshi.JsonClass +import kotlinx.serialization.Serializable + +@JsonClass(generateAdapter = true) +@Serializable +data class User( + val firstName: String, + val lastName: String +) diff --git a/app/src/main/res/layout/activity_example.xml b/app/src/main/res/layout/activity_example.xml index c0bf635..57b6bbb 100644 --- a/app/src/main/res/layout/activity_example.xml +++ b/app/src/main/res/layout/activity_example.xml @@ -114,6 +114,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 2e4666a272df47863068df79d55a440889bfedfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1dy=20Istv=C3=A1n?= Date: Fri, 24 Sep 2021 14:09:17 +0200 Subject: [PATCH 04/31] Update README.md to follow the default value API changes --- README.md | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 530f808..d045a83 100644 --- a/README.md +++ b/README.md @@ -11,9 +11,9 @@ Here's what its basic usage looks like, extending the provided `SimpleKrate` cla ```kotlin class UserSettings(context: Context) : SimpleKrate(context) { - var notificationsEnabled by booleanPref("notifications_enabled", false) - var loginCount by intPref("login_count", 0) - var nickname by stringPref("nickname", "") + var notificationsEnabled by booleanPref("notifications_enabled").withDefault(false) + var loginCount by intPref("login_count").withDefault(0) + var nickname by stringPref("nickname").withDefault("") } @@ -44,10 +44,12 @@ var username: String? by stringPref("username") ### Default values: -A property declared with the two-argument delegate function takes its default value as the second argument, and it will have a non-nullable type. Reading from this property will return either the value it was last set to or the default value. Setting this property will update the value stored in `SharedPreferences`. Note that there's no way to remove these values from `SharedPreferences` (although you could set it explicitly to the default value). +An extension function declared takes the default value as argument, and it will have a non-nullable type. Reading from this property will +return either the value it was last set to or the default value. Setting this property will update the value stored in `SharedPreferences`. +Note that there's no way to remove these values from `SharedPreferences` (although you could set it explicitly to the default value). ```kotlin -var username: String by stringPref("username", defaultValue = "admin") +var username: String by stringPref("username").withDefault("admin") ``` # Custom Krate implementations @@ -65,7 +67,7 @@ class ExampleCustomKrate(context: Context) : Krate { sharedPreferences = context.applicationContext.getSharedPreferences("custom_krate_prefs", Context.MODE_PRIVATE) } - var exampleBoolean by booleanPref("exampleBoolean", false) + var exampleBoolean by booleanPref("exampleBoolean").withDefault(false) } ``` @@ -79,7 +81,7 @@ class MainActivity : AppCompatActivity(), Krate { getPreferences(Context.MODE_PRIVATE) // Could also fetch a named or default SharedPrefs } - var username by stringPref("username", "") + var username by stringPref("username").withDefault("") } ``` @@ -99,7 +101,7 @@ class EncryptedKrate(applicationContext: Context) : Krate { sharedPreferences = EncryptedSharedPreferences.create(applicationContext, ...) } - val myStringValue: String by stringPref("my_string_value", "") + val myStringValue: String by stringPref("my_string_value").withDefault("") } ``` @@ -108,10 +110,9 @@ class EncryptedKrate(applicationContext: Context) : Krate { You can add validation rules to your Krate properties by calling `validate` on any of Krate's delegate functions: ```kotlin -var percentage: Int by intPref( - key = "percentage", - defaultValue = 0, -).validate { it in 0..100 } +var percentage: Int by intPref("percentage") + .withDefault(0) + .validate { it in 0..100 } ``` If this validation fails, an `IllegalArgumentException` will be thrown. From 6cdcad3f9199488239d9aae960c90c9da7a93828 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1dy=20Istv=C3=A1n?= Date: Fri, 24 Sep 2021 21:16:18 +0200 Subject: [PATCH 05/31] Make key arguments of the delegate function nullable and use property name as key by default --- README.md | 53 ++++++++++++------- .../krateexample/krates/ExampleCustomKrate.kt | 18 +++---- .../krateexample/krates/ExampleSimpleKrate.kt | 6 +-- .../kotlin/hu/autsoft/krate/gson/Functions.kt | 10 ++-- .../autsoft/krate/gson/ValidatedFunctions.kt | 10 ++-- .../krate/gson/optional/GsonDelegate.kt | 14 ++--- .../hu/autsoft/krate/kotlinx/Functions.kt | 10 ++-- .../krate/kotlinx/optional/KotlinxDelegate.kt | 14 ++--- .../hu/autsoft/krate/moshi/Functions.kt | 10 ++-- .../autsoft/krate/moshi/ValidatedFunctions.kt | 10 ++-- .../krate/moshi/optional/MoshiDelegate.kt | 14 ++--- .../main/kotlin/hu/autsoft/krate/Functions.kt | 36 ++++++++----- .../hu/autsoft/krate/ValidatedFunctions.kt | 20 +++---- .../hu/autsoft/krate/base/KeyDelegate.kt | 4 +- .../krate/default/DelegateWithDefault.kt | 2 +- .../autsoft/krate/optional/BooleanDelegate.kt | 10 ++-- .../autsoft/krate/optional/FloatDelegate.kt | 10 ++-- .../hu/autsoft/krate/optional/IntDelegate.kt | 10 ++-- .../hu/autsoft/krate/optional/LongDelegate.kt | 10 ++-- .../autsoft/krate/optional/StringDelegate.kt | 10 ++-- .../krate/optional/StringSetDelegate.kt | 10 ++-- 21 files changed, 165 insertions(+), 126 deletions(-) diff --git a/README.md b/README.md index d045a83..b7724ce 100644 --- a/README.md +++ b/README.md @@ -11,9 +11,9 @@ Here's what its basic usage looks like, extending the provided `SimpleKrate` cla ```kotlin class UserSettings(context: Context) : SimpleKrate(context) { - var notificationsEnabled by booleanPref("notifications_enabled").withDefault(false) - var loginCount by intPref("login_count").withDefault(0) - var nickname by stringPref("nickname").withDefault("") + var notificationsEnabled by booleanPref().withDefault(false) + var loginCount by intPref().withDefault(0) + var nickname by stringPref().withDefault("") } @@ -36,10 +36,11 @@ Each stored property can be declared with or without a default value. Here's how ### Optional values: -A property declared with the one-argument delegate function will have a nullable type. It will have a `null` value if no value has been set for this property yet, and its current value can be erased from `SharedPreferences` completely by setting it to `null`. +A property declared with the delegate function will have a nullable type. It will have a `null` value if no value has been set for this +property yet, and its current value can be erased from `SharedPreferences` completely by setting it to `null`. ```kotlin -var username: String? by stringPref("username") +var username: String? by stringPref() ``` ### Default values: @@ -48,15 +49,29 @@ An extension function declared takes the default value as argument, and it will return either the value it was last set to or the default value. Setting this property will update the value stored in `SharedPreferences`. Note that there's no way to remove these values from `SharedPreferences` (although you could set it explicitly to the default value). +```kotlin +var username: String by stringPref().withDefault("admin") +``` + +# Custom keys + +By default the the property will be stored under the key of the property's name in the `SharedPreferences`. + +You can change this behaviour by explicitly give the key in the argument of the delegate function. + ```kotlin var username: String by stringPref("username").withDefault("admin") ``` # Custom Krate implementations -You can usually get away with extending `SimpleKrate`, as it does allow you to pass in a custom name for the `SharedPreferences` to be used to store your values in its constructor as an optional parameter. (If you pass in no `name` parameter to its constructor, it will default to using the instance returned by `PreferenceManager.getDefaultSharedPreferences(context)`.) +You can usually get away with extending `SimpleKrate`, as it does allow you to pass in a custom name for the `SharedPreferences` to be used +to store your values in its constructor as an optional parameter. (If you pass in no `name` parameter to its constructor, it will default to +using the instance returned by `PreferenceManager.getDefaultSharedPreferences(context)`.) -However, you can also implement the `Krate` interface directly if you want to manage the `SharedPreferences` instance yourself for whatever reason - all this interface requires is a property that holds a `SharedPreferences` instance. With that, you can use the delegate functions the same way as shown above: +However, you can also implement the `Krate` interface directly if you want to manage the `SharedPreferences` instance yourself for whatever +reason - all this interface requires is a property that holds a `SharedPreferences` instance. With that, you can use the delegate functions +the same way as shown above: ```kotlin class ExampleCustomKrate(context: Context) : Krate { @@ -67,7 +82,7 @@ class ExampleCustomKrate(context: Context) : Krate { sharedPreferences = context.applicationContext.getSharedPreferences("custom_krate_prefs", Context.MODE_PRIVATE) } - var exampleBoolean by booleanPref("exampleBoolean").withDefault(false) + var exampleBoolean by booleanPref().withDefault(false) } ``` @@ -81,7 +96,7 @@ class MainActivity : AppCompatActivity(), Krate { getPreferences(Context.MODE_PRIVATE) // Could also fetch a named or default SharedPrefs } - var username by stringPref("username").withDefault("") + var username by stringPref().withDefault("") } ``` @@ -101,7 +116,7 @@ class EncryptedKrate(applicationContext: Context) : Krate { sharedPreferences = EncryptedSharedPreferences.create(applicationContext, ...) } - val myStringValue: String by stringPref("my_string_value").withDefault("") + val myStringValue: String by stringPref().withDefault("") } ``` @@ -110,7 +125,7 @@ class EncryptedKrate(applicationContext: Context) : Krate { You can add validation rules to your Krate properties by calling `validate` on any of Krate's delegate functions: ```kotlin -var percentage: Int by intPref("percentage") +var percentage: Int by intPref() .withDefault(0) .validate { it in 0..100 } ``` @@ -133,8 +148,8 @@ The usage of the Krate integration is the same for both setups: ```kotlin class MoshiKrate(context: Context) : SimpleKrate(context) { - var user: User? by moshiPref("user") - var savedArticles: List
? by moshiPref("articles") + var user: User? by moshiPref() + var savedArticles: List
? by moshiPref() } ``` @@ -182,8 +197,8 @@ Its usage is the same as with any of the base library's delegates: ```kotlin class KotlinxKrate(context: Context) : SimpleKrate(context) { - var user: User? by kotlinxPref("user") - var savedArticles: List
? by kotlinxPref("articles") + var user: User? by kotlinxPref() + var savedArticles: List
? by kotlinxPref() } ``` @@ -198,7 +213,7 @@ class CustomKotlinxKrate(context: Context) : SimpleKrate(context) { } } - var user: User? by kotlinxPref("user") + var user: User? by kotlinxPref() } ``` @@ -214,8 +229,8 @@ Its basic usage is the same as with any of the base library's delegates: ```kotlin class GsonKrate(context: Context) : SimpleKrate(context) { - var user: User? by gsonPref("user") - var savedArticles: List
? by gsonPref("articles") + var user: User? by gsonPref() + var savedArticles: List
? by gsonPref() } ``` @@ -227,7 +242,7 @@ class CustomGsonKrate(context: Context) : SimpleKrate(context) { gson = GsonBuilder().create() } - var user: User? by gsonPref("user") + var user: User? by gsonPref() } ``` diff --git a/app/src/main/java/hu/autsoft/krateexample/krates/ExampleCustomKrate.kt b/app/src/main/java/hu/autsoft/krateexample/krates/ExampleCustomKrate.kt index cb5700a..6956d7c 100644 --- a/app/src/main/java/hu/autsoft/krateexample/krates/ExampleCustomKrate.kt +++ b/app/src/main/java/hu/autsoft/krateexample/krates/ExampleCustomKrate.kt @@ -19,14 +19,14 @@ class ExampleCustomKrate(context: Context) : Krate, ExampleSettings { sharedPreferences = context.applicationContext.getSharedPreferences("custom_krate_prefs", Context.MODE_PRIVATE) } - override var exampleBoolean by booleanPref("exampleBoolean").withDefault(false) - override var exampleFloat by floatPref("exampleFloat").withDefault(0f) - override var exampleInt by intPref("exampleInt").withDefault(0) - override var exampleLong by longPref("exampleLong").withDefault(0L) - override var exampleString by stringPref("exampleString").withDefault("") - override var exampleStringSet by stringSetPref("exampleStringSet").withDefault(setOf()) - override var exampleUserGson by gsonPref("user_gson").withDefault(User("Gson", "Smith")) - override var exampleUserKotlinX by kotlinxPref("user_kotlinx").withDefault(User("KotlinX", "Smith")) - override var exampleUserMoshi by moshiPref("user_moshi").withDefault(User("Moshi", "Smith")) + override var exampleBoolean by booleanPref().withDefault(false) + override var exampleFloat by floatPref().withDefault(0f) + override var exampleInt by intPref().withDefault(0) + override var exampleLong by longPref().withDefault(0L) + override var exampleString by stringPref().withDefault("") + override var exampleStringSet by stringSetPref().withDefault(setOf()) + override var exampleUserGson by gsonPref().withDefault(User("Gson", "Smith")) + override var exampleUserKotlinX by kotlinxPref().withDefault(User("KotlinX", "Smith")) + override var exampleUserMoshi by moshiPref().withDefault(User("Moshi", "Smith")) } diff --git a/app/src/main/java/hu/autsoft/krateexample/krates/ExampleSimpleKrate.kt b/app/src/main/java/hu/autsoft/krateexample/krates/ExampleSimpleKrate.kt index cee1fcb..ec5e589 100644 --- a/app/src/main/java/hu/autsoft/krateexample/krates/ExampleSimpleKrate.kt +++ b/app/src/main/java/hu/autsoft/krateexample/krates/ExampleSimpleKrate.kt @@ -20,7 +20,7 @@ class ExampleSimpleKrate(context: Context) : SimpleKrate(context, NAME), Example override var exampleLong by longPref("exampleLong").withDefault(0L) override var exampleString by stringPref("exampleString").withDefault("") override var exampleStringSet by stringSetPref("exampleStringSet").withDefault(setOf()) - override var exampleUserGson by gsonPref("user_gson").withDefault(User("Gson", "Smith")) - override var exampleUserKotlinX by kotlinxPref("user_kotlinx").withDefault(User("KotlinX", "Smith")) - override var exampleUserMoshi by moshiPref("user_moshi").withDefault(User("Moshi", "Smith")) + override var exampleUserGson by gsonPref("exampleUserGson").withDefault(User("Gson", "Smith")) + override var exampleUserKotlinX by kotlinxPref("exampleUserKotlinx").withDefault(User("KotlinX", "Smith")) + override var exampleUserMoshi by moshiPref("exampleUserMoshi").withDefault(User("Moshi", "Smith")) } diff --git a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/Functions.kt b/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/Functions.kt index a72a45b..8838de0 100644 --- a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/Functions.kt +++ b/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/Functions.kt @@ -14,17 +14,18 @@ import kotlin.properties.ReadWriteProperty /** * Creates an optional preference of type T with the given [key] in this [Krate] instance. + * If [key] is `null` then the property name will be used as key. * This instance will be serialized using Gson. */ public inline fun Krate.gsonPref( - key: String, + key: String? = null, ): KeyDelegateProvider { return gsonPrefImpl(key, object : TypeToken() {}.type) } @PublishedApi internal fun Krate.gsonPrefImpl( - key: String, + key: String? = null, type: Type, ): KeyDelegateProvider { return GsonDelegateFactory(key, type) @@ -32,6 +33,7 @@ internal fun Krate.gsonPrefImpl( /** * Creates a non-optional preference of type T with the given [key] and [defaultValue] in this [Krate] instance. + * If [key] is `null` then the property name will be used as key. * This instance will be serialized using Gson. */ @Deprecated( @@ -43,7 +45,7 @@ internal fun Krate.gsonPrefImpl( ), ) public inline fun Krate.gsonPref( - key: String, + key: String? = null, defaultValue: T, ): PropertyDelegateProvider> { return gsonPrefImpl(key, defaultValue, object : TypeToken() {}.type) @@ -51,7 +53,7 @@ public inline fun Krate.gsonPref( @PublishedApi internal fun Krate.gsonPrefImpl( - key: String, + key: String? = null, defaultValue: T, type: Type, ): PropertyDelegateProvider> { diff --git a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/ValidatedFunctions.kt b/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/ValidatedFunctions.kt index 6b8115e..5670618 100644 --- a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/ValidatedFunctions.kt +++ b/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/ValidatedFunctions.kt @@ -14,6 +14,7 @@ import kotlin.properties.ReadWriteProperty /** * Creates a validated, optional preference of type T with the given [key] in this [Krate] instance. + * If [key] is `null` then the property name will be used as key. * This value will be serialized using Gson. */ @Deprecated( @@ -25,7 +26,7 @@ import kotlin.properties.ReadWriteProperty ), ) public inline fun Krate.gsonPref( - key: String, + key: String? = null, noinline isValid: (newValue: T?) -> Boolean, ): PropertyDelegateProvider> { return gsonPrefImpl(key, object : TypeToken() {}.type, isValid) @@ -33,7 +34,7 @@ public inline fun Krate.gsonPref( @PublishedApi internal fun Krate.gsonPrefImpl( - key: String, + key: String? = null, type: Type, isValid: (newValue: T?) -> Boolean, ): PropertyDelegateProvider> { @@ -43,6 +44,7 @@ internal fun Krate.gsonPrefImpl( /** * Creates a validated, non-optional preference of type T with the given [key] and [defaultValue] * in this [Krate] instance. + * If [key] is `null` then the property name will be used as key. * This value will be serialized using Gson. */ @Deprecated( @@ -54,7 +56,7 @@ internal fun Krate.gsonPrefImpl( ), ) public inline fun Krate.gsonPref( - key: String, + key: String? = null, defaultValue: T, noinline isValid: (newValue: T) -> Boolean, ): PropertyDelegateProvider> { @@ -63,7 +65,7 @@ public inline fun Krate.gsonPref( @PublishedApi internal fun Krate.gsonPrefImpl( - key: String, + key: String? = null, defaultValue: T, type: Type, isValid: (newValue: T) -> Boolean, diff --git a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/optional/GsonDelegate.kt b/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/optional/GsonDelegate.kt index 65991ca..a51f495 100644 --- a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/optional/GsonDelegate.kt +++ b/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/optional/GsonDelegate.kt @@ -11,15 +11,15 @@ import java.lang.reflect.Type import kotlin.reflect.KProperty private class GsonDelegate( - key: String, + key: String?, private val adapter: TypeAdapter, ) : KeyDelegate(key) { override operator fun getValue(thisRef: Krate, property: KProperty<*>): T? { - return if (!thisRef.sharedPreferences.contains(key)) { + return if (!thisRef.sharedPreferences.contains(key ?: property.name)) { null } else { - val string = requireNotNull(thisRef.sharedPreferences.getString(key, null)) + val string = requireNotNull(thisRef.sharedPreferences.getString(key ?: property.name, null)) adapter.fromJson(string) } } @@ -27,24 +27,24 @@ private class GsonDelegate( override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: T?) { if (value == null) { thisRef.sharedPreferences.edit { - remove(key) + remove(key ?: property.name) } } else { thisRef.sharedPreferences.edit { - putString(key, adapter.toJson(value)) + putString(key ?: property.name, adapter.toJson(value)) } } } } internal class GsonDelegateFactory( - private val key: String, + private val key: String?, private val type: Type, ) : KeyDelegateProvider() { override fun provideDelegate(thisRef: Krate, property: KProperty<*>): KeyDelegate { @Suppress("UNCHECKED_CAST") val adapter: TypeAdapter = thisRef.internalGson.getAdapter(TypeToken.get(type)) as TypeAdapter - return GsonDelegate(key, adapter) + return GsonDelegate(key ?: property.name, adapter) } } diff --git a/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/Functions.kt b/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/Functions.kt index 44ee4d6..cab0be7 100644 --- a/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/Functions.kt +++ b/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/Functions.kt @@ -14,18 +14,19 @@ import kotlin.reflect.typeOf /** * Creates an optional preference of type T with the given [key] in this [Krate] instance. + * If [key] is `null` then the property name will be used as key. * This instance will be serialized using kotlinx.serialization. */ @OptIn(ExperimentalStdlibApi::class) public inline fun Krate.kotlinxPref( - key: String, + key: String? = null, ): KeyDelegateProvider { return kotlinxPrefImpl(key, typeOf()) } @PublishedApi internal fun Krate.kotlinxPrefImpl( - key: String, + key: String? = null, type: KType, ): KeyDelegateProvider { return KotlinxDelegateFactory(key, type) @@ -33,6 +34,7 @@ internal fun Krate.kotlinxPrefImpl( /** * Creates a non-optional preference of type T with the given [key] and [defaultValue] in this [Krate] instance. + * If [key] is `null` then the property name will be used as key. * This instance will be serialized using kotlinx.serialization. */ @Deprecated( @@ -45,7 +47,7 @@ internal fun Krate.kotlinxPrefImpl( ) @OptIn(ExperimentalStdlibApi::class) public inline fun Krate.kotlinxPref( - key: String, + key: String? = null, defaultValue: T, ): PropertyDelegateProvider> { return kotlinxPrefImpl(key, defaultValue, typeOf()) @@ -53,7 +55,7 @@ public inline fun Krate.kotlinxPref( @PublishedApi internal fun Krate.kotlinxPrefImpl( - key: String, + key: String? = null, defaultValue: T, type: KType, ): PropertyDelegateProvider> { diff --git a/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/optional/KotlinxDelegate.kt b/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/optional/KotlinxDelegate.kt index 325688d..1430811 100644 --- a/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/optional/KotlinxDelegate.kt +++ b/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/optional/KotlinxDelegate.kt @@ -12,15 +12,15 @@ import kotlin.reflect.KType private class KotlinxDelegate( - key: String, + key: String?, private val serializer: KSerializer, ) : KeyDelegate(key) { override operator fun getValue(thisRef: Krate, property: KProperty<*>): T? { - return if (!thisRef.sharedPreferences.contains(key)) { + return if (!thisRef.sharedPreferences.contains(key ?: property.name)) { null } else { - val string = requireNotNull(thisRef.sharedPreferences.getString(key, null)) + val string = requireNotNull(thisRef.sharedPreferences.getString(key ?: property.name, null)) thisRef.internalJson.decodeFromString(serializer, string) } } @@ -28,24 +28,24 @@ private class KotlinxDelegate( override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: T?) { if (value == null) { thisRef.sharedPreferences.edit { - remove(key) + remove(key ?: property.name) } } else { thisRef.sharedPreferences.edit { - putString(key, thisRef.internalJson.encodeToString(serializer, value)) + putString(key ?: property.name, thisRef.internalJson.encodeToString(serializer, value)) } } } } internal class KotlinxDelegateFactory( - private val key: String, + private val key: String?, private val type: KType, ) : KeyDelegateProvider() { override fun provideDelegate(thisRef: Krate, property: KProperty<*>): KeyDelegate { @Suppress("UNCHECKED_CAST") val serializer = thisRef.internalJson.serializersModule.serializer(type) as KSerializer - return KotlinxDelegate(key, serializer) + return KotlinxDelegate(key ?: property.name, serializer) } } diff --git a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/Functions.kt b/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/Functions.kt index a2d4a2e..55796fb 100644 --- a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/Functions.kt +++ b/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/Functions.kt @@ -15,18 +15,19 @@ import kotlin.reflect.typeOf /** * Creates an optional preference of type T with the given [key] in this [Krate] instance. + * If [key] is `null` then the property name will be used as key. * This instance will be serialized using Moshi. */ @OptIn(ExperimentalStdlibApi::class) public inline fun Krate.moshiPref( - key: String, + key: String? = null, ): KeyDelegateProvider { return moshiPrefImpl(key, typeOf().javaType) } @PublishedApi internal fun Krate.moshiPrefImpl( - key: String, + key: String? = null, type: Type, ): KeyDelegateProvider { return MoshiDelegateFactory(key, type) @@ -34,6 +35,7 @@ internal fun Krate.moshiPrefImpl( /** * Creates a non-optional preference of type T with the given [key] and [defaultValue] in this [Krate] instance. + * If [key] is `null` then the property name will be used as key. * This instance will be serialized using Moshi. */ @Deprecated( @@ -46,7 +48,7 @@ internal fun Krate.moshiPrefImpl( ) @OptIn(ExperimentalStdlibApi::class) public inline fun Krate.moshiPref( - key: String, + key: String? = null, defaultValue: T, ): PropertyDelegateProvider> { return moshiPrefImpl(key, defaultValue, typeOf().javaType) @@ -54,7 +56,7 @@ public inline fun Krate.moshiPref( @PublishedApi internal fun Krate.moshiPrefImpl( - key: String, + key: String? = null, defaultValue: T, type: Type, ): PropertyDelegateProvider> { diff --git a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/ValidatedFunctions.kt b/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/ValidatedFunctions.kt index 34c8a33..907f6c6 100644 --- a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/ValidatedFunctions.kt +++ b/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/ValidatedFunctions.kt @@ -15,6 +15,7 @@ import kotlin.reflect.typeOf /** * Creates a validated, optional preference of type T with the given [key] in this [Krate] instance. + * If [key] is `null` then the property name will be used as key. * This value will be serialized using Moshi. */ @OptIn(ExperimentalStdlibApi::class) @@ -27,7 +28,7 @@ import kotlin.reflect.typeOf ), ) public inline fun Krate.moshiPref( - key: String, + key: String? = null, noinline isValid: (newValue: T?) -> Boolean, ): PropertyDelegateProvider> { return moshiPrefImpl(key, typeOf().javaType, isValid) @@ -35,7 +36,7 @@ public inline fun Krate.moshiPref( @PublishedApi internal fun Krate.moshiPrefImpl( - key: String, + key: String? = null, type: Type, isValid: (newValue: T?) -> Boolean, ): PropertyDelegateProvider> { @@ -45,6 +46,7 @@ internal fun Krate.moshiPrefImpl( /** * Creates a validated, non-optional preference of type T with the given [key] and [defaultValue] * in this [Krate] instance. + * If [key] is `null` then the property name will be used as key. * This value will be serialized using Moshi. */ @OptIn(ExperimentalStdlibApi::class) @@ -57,7 +59,7 @@ internal fun Krate.moshiPrefImpl( ), ) public inline fun Krate.moshiPref( - key: String, + key: String? = null, defaultValue: T, noinline isValid: (newValue: T) -> Boolean, ): PropertyDelegateProvider> { @@ -66,7 +68,7 @@ public inline fun Krate.moshiPref( @PublishedApi internal fun Krate.moshiPrefImpl( - key: String, + key: String? = null, defaultValue: T, type: Type, isValid: (newValue: T) -> Boolean, diff --git a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/optional/MoshiDelegate.kt b/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/optional/MoshiDelegate.kt index f38ffcb..03a997d 100644 --- a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/optional/MoshiDelegate.kt +++ b/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/optional/MoshiDelegate.kt @@ -10,15 +10,15 @@ import java.lang.reflect.Type import kotlin.reflect.KProperty private class MoshiDelegate( - key: String, + key: String?, private val adapter: JsonAdapter, ) : KeyDelegate(key) { override fun getValue(thisRef: Krate, property: KProperty<*>): T? { - return if (!thisRef.sharedPreferences.contains(key)) { + return if (!thisRef.sharedPreferences.contains(key ?: property.name)) { null } else { - val string = requireNotNull(thisRef.sharedPreferences.getString(key, null)) + val string = requireNotNull(thisRef.sharedPreferences.getString(key ?: property.name, null)) adapter.fromJson(string) } } @@ -26,23 +26,23 @@ private class MoshiDelegate( override fun setValue(thisRef: Krate, property: KProperty<*>, value: T?) { if (value == null) { thisRef.sharedPreferences.edit { - remove(key) + remove(key ?: property.name) } } else { thisRef.sharedPreferences.edit { - putString(key, adapter.toJson(value)) + putString(key ?: property.name, adapter.toJson(value)) } } } } internal class MoshiDelegateFactory( - private val key: String, + private val key: String?, private val type: Type, ) : KeyDelegateProvider() { override fun provideDelegate(thisRef: Krate, property: KProperty<*>): KeyDelegate { val adapter = thisRef.realMoshiInstance.adapter(type) - return MoshiDelegate(key, adapter) + return MoshiDelegate(key ?: property.name, adapter) } } diff --git a/krate/src/main/kotlin/hu/autsoft/krate/Functions.kt b/krate/src/main/kotlin/hu/autsoft/krate/Functions.kt index 6ea8436..7fda4a1 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/Functions.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/Functions.kt @@ -9,49 +9,56 @@ import kotlin.properties.ReadWriteProperty /** * Creates an optional preference of type [Boolean] with the given [key] in this [Krate] instance. + * If [key] is `null` then the property name will be used as key. */ -public fun Krate.booleanPref(key: String): KeyDelegate { +public fun Krate.booleanPref(key: String? = null): KeyDelegate { return BooleanDelegate(key) } /** * Creates an optional preference of type [Float] with the given [key] in this [Krate] instance. + * If [key] is `null` then the property name will be used as key. */ -public fun Krate.floatPref(key: String): KeyDelegate { +public fun Krate.floatPref(key: String? = null): KeyDelegate { return FloatDelegate(key) } /** * Creates an optional preference of type [Int] with the given [key] in this [Krate] instance. + * If [key] is `null` then the property name will be used as key. */ -public fun Krate.intPref(key: String): KeyDelegate { +public fun Krate.intPref(key: String? = null): KeyDelegate { return IntDelegate(key) } /** * Creates an optional preference of type [Long] with the given [key] in this [Krate] instance. + * If [key] is `null` then the property name will be used as key. */ -public fun Krate.longPref(key: String): KeyDelegate { +public fun Krate.longPref(key: String? = null): KeyDelegate { return LongDelegate(key) } /** * Creates an optional preference of type [String] with the given [key] in this [Krate] instance. + * If [key] is `null` then the property name will be used as key. */ -public fun Krate.stringPref(key: String): KeyDelegate { +public fun Krate.stringPref(key: String? = null): KeyDelegate { return StringDelegate(key) } /** * Creates an optional preference of type Set with the given [key] in this [Krate] instance. + * If [key] is `null` then the property name will be used as key. */ -public fun Krate.stringSetPref(key: String): KeyDelegate?> { +public fun Krate.stringSetPref(key: String? = null): KeyDelegate?> { return StringSetDelegate(key) } /** * Creates a non-optional preference of type [Boolean] with the given [key] and [defaultValue] in this [Krate] instance. + * If [key] is `null` then the property name will be used as key. */ @Deprecated( message = "Use .withDefault() on a booleanPref instead", @@ -61,12 +68,13 @@ public fun Krate.stringSetPref(key: String): KeyDelegate?> { imports = arrayOf("hu.autsoft.krate.default.withDefault"), ), ) -public fun Krate.booleanPref(key: String, defaultValue: Boolean): ReadWriteProperty { +public fun Krate.booleanPref(key: String? = null, defaultValue: Boolean): ReadWriteProperty { return booleanPref(key).withDefault(defaultValue) } /** * Creates a non-optional preference of type [Float] with the given [key] and [defaultValue] in this [Krate] instance. + * If [key] is `null` then the property name will be used as key. */ @Deprecated( message = "Use .withDefault() on a floatPref instead", @@ -76,12 +84,13 @@ public fun Krate.booleanPref(key: String, defaultValue: Boolean): ReadWritePrope imports = arrayOf("hu.autsoft.krate.default.withDefault"), ), ) -public fun Krate.floatPref(key: String, defaultValue: Float): ReadWriteProperty { +public fun Krate.floatPref(key: String? = null, defaultValue: Float): ReadWriteProperty { return floatPref(key).withDefault(defaultValue) } /** * Creates a non-optional preference of type [Int] with the given [key] and [defaultValue] in this [Krate] instance. + * If [key] is `null` then the property name will be used as key. */ @Deprecated( message = "Use .withDefault() on an intPref instead", @@ -91,12 +100,13 @@ public fun Krate.floatPref(key: String, defaultValue: Float): ReadWriteProperty< imports = arrayOf("hu.autsoft.krate.default.withDefault"), ), ) -public fun Krate.intPref(key: String, defaultValue: Int): ReadWriteProperty { +public fun Krate.intPref(key: String? = null, defaultValue: Int): ReadWriteProperty { return intPref(key).withDefault(defaultValue) } /** * Creates a non-optional preference of type [Long] with the given [key] and [defaultValue] in this [Krate] instance. + * If [key] is `null` then the property name will be used as key. */ @Deprecated( message = "Use .withDefault() on a longPref instead", @@ -106,12 +116,13 @@ public fun Krate.intPref(key: String, defaultValue: Int): ReadWriteProperty { +public fun Krate.longPref(key: String? = null, defaultValue: Long): ReadWriteProperty { return longPref(key).withDefault(defaultValue) } /** * Creates a non-optional preference of type [String] with the given [key] and [defaultValue] in this [Krate] instance. + * If [key] is `null` then the property name will be used as key. */ @Deprecated( message = "Use .withDefault() on a stringPref instead", @@ -121,12 +132,13 @@ public fun Krate.longPref(key: String, defaultValue: Long): ReadWriteProperty { +public fun Krate.stringPref(key: String? = null, defaultValue: String): ReadWriteProperty { return stringPref(key).withDefault(defaultValue) } /** * Creates a non-optional preference of type Set with the given [key] and [defaultValue] in this [Krate] instance. + * If [key] is `null` then the property name will be used as key. */ @Deprecated( message = "Use .withDefault() on a stringSetPref instead", @@ -136,6 +148,6 @@ public fun Krate.stringPref(key: String, defaultValue: String): ReadWritePropert imports = arrayOf("hu.autsoft.krate.default.withDefault"), ), ) -public fun Krate.stringSetPref(key: String, defaultValue: Set): ReadWriteProperty> { +public fun Krate.stringSetPref(key: String? = null, defaultValue: Set): ReadWriteProperty> { return stringSetPref(key).withDefault(defaultValue) } diff --git a/krate/src/main/kotlin/hu/autsoft/krate/ValidatedFunctions.kt b/krate/src/main/kotlin/hu/autsoft/krate/ValidatedFunctions.kt index b53e3c9..8e4c30c 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/ValidatedFunctions.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/ValidatedFunctions.kt @@ -23,7 +23,7 @@ import kotlin.properties.ReadWriteProperty ), ) public fun Krate.floatPref( - key: String, + key: String? = null, isValid: (newValue: Float?) -> Boolean, ): ReadWriteProperty { return ValidatedPreferenceDelegate(FloatDelegate(key), isValid) @@ -44,7 +44,7 @@ public fun Krate.floatPref( ), ) public fun Krate.floatPref( - key: String, + key: String? = null, defaultValue: Float, isValid: (newValue: Float) -> Boolean, ): ReadWriteProperty { @@ -66,7 +66,7 @@ public fun Krate.floatPref( ), ) public fun Krate.intPref( - key: String, + key: String? = null, isValid: (newValue: Int?) -> Boolean, ): ReadWriteProperty { return ValidatedPreferenceDelegate(IntDelegate(key), isValid) @@ -87,7 +87,7 @@ public fun Krate.intPref( ), ) public fun Krate.intPref( - key: String, + key: String? = null, defaultValue: Int, isValid: (newValue: Int) -> Boolean, ): ReadWriteProperty { @@ -109,7 +109,7 @@ public fun Krate.intPref( ), ) public fun Krate.longPref( - key: String, + key: String? = null, isValid: (newValue: Long?) -> Boolean, ): ReadWriteProperty { return ValidatedPreferenceDelegate(LongDelegate(key), isValid) @@ -130,7 +130,7 @@ public fun Krate.longPref( ), ) public fun Krate.longPref( - key: String, + key: String? = null, defaultValue: Long, isValid: (newValue: Long) -> Boolean, ): ReadWriteProperty { @@ -152,7 +152,7 @@ public fun Krate.longPref( ), ) public fun Krate.stringPref( - key: String, + key: String? = null, isValid: (newValue: String?) -> Boolean, ): ReadWriteProperty { return ValidatedPreferenceDelegate(StringDelegate(key), isValid) @@ -173,7 +173,7 @@ public fun Krate.stringPref( ), ) public fun Krate.stringPref( - key: String, + key: String? = null, defaultValue: String, isValid: (newValue: String) -> Boolean, ): ReadWriteProperty { @@ -195,7 +195,7 @@ public fun Krate.stringPref( ), ) public fun Krate.stringSetPref( - key: String, + key: String? = null, isValid: (newValue: Set?) -> Boolean, ): ReadWriteProperty?> { return ValidatedPreferenceDelegate(StringSetDelegate(key), isValid) @@ -216,7 +216,7 @@ public fun Krate.stringSetPref( ), ) public fun Krate.stringSetPref( - key: String, + key: String? = null, defaultValue: Set, isValid: (newValue: Set) -> Boolean, ): ReadWriteProperty> { diff --git a/krate/src/main/kotlin/hu/autsoft/krate/base/KeyDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/base/KeyDelegate.kt index 30a720d..15b46b0 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/base/KeyDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/base/KeyDelegate.kt @@ -3,4 +3,6 @@ package hu.autsoft.krate.base import hu.autsoft.krate.Krate import kotlin.properties.ReadWriteProperty -public abstract class KeyDelegate(public val key: String) : ReadWriteProperty \ No newline at end of file +public abstract class KeyDelegate( + public val key: String? +) : ReadWriteProperty \ No newline at end of file diff --git a/krate/src/main/kotlin/hu/autsoft/krate/default/DelegateWithDefault.kt b/krate/src/main/kotlin/hu/autsoft/krate/default/DelegateWithDefault.kt index 1e8dc68..282c1e1 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/default/DelegateWithDefault.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/default/DelegateWithDefault.kt @@ -21,7 +21,7 @@ public class DelegateWithDefault( } override fun getValue(thisRef: Krate, property: KProperty<*>): T { - return if (!thisRef.sharedPreferences.contains(delegate.key)) default else delegate.getValue(thisRef, property)!! + return if (!thisRef.sharedPreferences.contains(delegate.key ?: property.name)) default else delegate.getValue(thisRef, property)!! } } diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/BooleanDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/BooleanDelegate.kt index 50557fa..303743b 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/BooleanDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/BooleanDelegate.kt @@ -5,21 +5,21 @@ import hu.autsoft.krate.base.KeyDelegate import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty -internal class BooleanDelegate(key: String) : KeyDelegate(key) { +internal class BooleanDelegate(key: String?) : KeyDelegate(key) { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Boolean? { - return if (!thisRef.sharedPreferences.contains(key)) { + return if (!thisRef.sharedPreferences.contains(key ?: property.name)) { null } else { - thisRef.sharedPreferences.getBoolean(key, false) + thisRef.sharedPreferences.getBoolean(key ?: property.name, false) } } override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: Boolean?) { if (value == null) { - thisRef.sharedPreferences.edit { remove(key) } + thisRef.sharedPreferences.edit { remove(key ?: property.name) } } else { - thisRef.sharedPreferences.edit { putBoolean(key, value) } + thisRef.sharedPreferences.edit { putBoolean(key ?: property.name, value) } } } diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/FloatDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/FloatDelegate.kt index d33a116..cefc088 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/FloatDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/FloatDelegate.kt @@ -5,21 +5,21 @@ import hu.autsoft.krate.base.KeyDelegate import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty -internal class FloatDelegate(key: String) : KeyDelegate(key) { +internal class FloatDelegate(key: String?) : KeyDelegate(key) { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Float? { - return if (!thisRef.sharedPreferences.contains(key)) { + return if (!thisRef.sharedPreferences.contains(key ?: property.name)) { null } else { - thisRef.sharedPreferences.getFloat(key, 0f) + thisRef.sharedPreferences.getFloat(key ?: property.name, 0f) } } override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: Float?) { if (value == null) { - thisRef.sharedPreferences.edit { remove(key) } + thisRef.sharedPreferences.edit { remove(key ?: property.name) } } else { - thisRef.sharedPreferences.edit { putFloat(key, value) } + thisRef.sharedPreferences.edit { putFloat(key ?: property.name, value) } } } diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/IntDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/IntDelegate.kt index 60e33cc..d4a1f14 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/IntDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/IntDelegate.kt @@ -5,21 +5,21 @@ import hu.autsoft.krate.base.KeyDelegate import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty -internal class IntDelegate(key: String) : KeyDelegate(key) { +internal class IntDelegate(key: String?) : KeyDelegate(key) { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Int? { - return if (!thisRef.sharedPreferences.contains(key)) { + return if (!thisRef.sharedPreferences.contains(key ?: property.name)) { null } else { - thisRef.sharedPreferences.getInt(key, 0) + thisRef.sharedPreferences.getInt(key ?: property.name, 0) } } override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: Int?) { if (value == null) { - thisRef.sharedPreferences.edit { remove(key) } + thisRef.sharedPreferences.edit { remove(key ?: property.name) } } else { - thisRef.sharedPreferences.edit { putInt(key, value) } + thisRef.sharedPreferences.edit { putInt(key ?: property.name, value) } } } diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/LongDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/LongDelegate.kt index 085ffab..f042da1 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/LongDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/LongDelegate.kt @@ -5,21 +5,21 @@ import hu.autsoft.krate.base.KeyDelegate import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty -internal class LongDelegate(key: String) : KeyDelegate(key) { +internal class LongDelegate(key: String?) : KeyDelegate(key) { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Long? { - return if (!thisRef.sharedPreferences.contains(key)) { + return if (!thisRef.sharedPreferences.contains(key ?: property.name)) { null } else { - thisRef.sharedPreferences.getLong(key, 0L) + thisRef.sharedPreferences.getLong(key ?: property.name, 0L) } } override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: Long?) { if (value == null) { - thisRef.sharedPreferences.edit { remove(key) } + thisRef.sharedPreferences.edit { remove(key ?: property.name) } } else { - thisRef.sharedPreferences.edit { putLong(key, value) } + thisRef.sharedPreferences.edit { putLong(key ?: property.name, value) } } } diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/StringDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/StringDelegate.kt index 8dce3c1..88647f3 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/StringDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/StringDelegate.kt @@ -5,21 +5,21 @@ import hu.autsoft.krate.base.KeyDelegate import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty -internal class StringDelegate(key: String) : KeyDelegate(key) { +internal class StringDelegate(key: String?) : KeyDelegate(key) { override operator fun getValue(thisRef: Krate, property: KProperty<*>): String? { - return if (!thisRef.sharedPreferences.contains(key)) { + return if (!thisRef.sharedPreferences.contains(key ?: property.name)) { null } else { - thisRef.sharedPreferences.getString(key, null) + thisRef.sharedPreferences.getString(key ?: property.name, null) } } override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: String?) { if (value == null) { - thisRef.sharedPreferences.edit { remove(key) } + thisRef.sharedPreferences.edit { remove(key ?: property.name) } } else { - thisRef.sharedPreferences.edit { putString(key, value) } + thisRef.sharedPreferences.edit { putString(key ?: property.name, value) } } } diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/StringSetDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/StringSetDelegate.kt index 48c0093..6fce73d 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/StringSetDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/StringSetDelegate.kt @@ -5,21 +5,21 @@ import hu.autsoft.krate.base.KeyDelegate import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty -internal class StringSetDelegate(key: String) : KeyDelegate?>(key) { +internal class StringSetDelegate(key: String?) : KeyDelegate?>(key) { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Set? { - return if (!thisRef.sharedPreferences.contains(key)) { + return if (!thisRef.sharedPreferences.contains(key ?: property.name)) { null } else { - thisRef.sharedPreferences.getStringSet(key, emptySet()) + thisRef.sharedPreferences.getStringSet(key ?: property.name, emptySet()) } } override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: Set?) { if (value == null) { - thisRef.sharedPreferences.edit { remove(key) } + thisRef.sharedPreferences.edit { remove(key ?: property.name) } } else { - thisRef.sharedPreferences.edit { putStringSet(key, value) } + thisRef.sharedPreferences.edit { putStringSet(key ?: property.name, value) } } } From 844c2a6634156e837688e4750d800af1a6df35fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Braun?= Date: Wed, 17 Nov 2021 18:17:39 +0100 Subject: [PATCH 06/31] Remove deprecated validated functions --- .../autsoft/krate/gson/ValidatedFunctions.kt | 72 ------ .../autsoft/krate/moshi/ValidatedFunctions.kt | 75 ------ .../hu/autsoft/krate/ValidatedFunctions.kt | 232 ------------------ 3 files changed, 379 deletions(-) delete mode 100644 krate-gson/src/main/kotlin/hu/autsoft/krate/gson/ValidatedFunctions.kt delete mode 100644 krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/ValidatedFunctions.kt delete mode 100644 krate/src/main/kotlin/hu/autsoft/krate/ValidatedFunctions.kt diff --git a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/ValidatedFunctions.kt b/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/ValidatedFunctions.kt deleted file mode 100644 index 4dd05b3..0000000 --- a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/ValidatedFunctions.kt +++ /dev/null @@ -1,72 +0,0 @@ -@file:[Suppress("unused") OptIn(InternalKrateApi::class)] - -package hu.autsoft.krate.gson - -import com.google.gson.reflect.TypeToken -import hu.autsoft.krate.Krate -import hu.autsoft.krate.gson.default.GsonDelegateWithDefaultFactory -import hu.autsoft.krate.gson.optional.GsonDelegateFactory -import hu.autsoft.krate.internal.InternalKrateApi -import hu.autsoft.krate.validation.ValidatedPreferenceDelegateFactory -import java.lang.reflect.Type -import kotlin.properties.PropertyDelegateProvider -import kotlin.properties.ReadWriteProperty - -/** - * Creates a validated, optional preference of type T with the given [key] in this [Krate] instance. - * This value will be serialized using Gson. - */ -@Deprecated( - message = "Use .validate {} on a gsonPref instead", - level = DeprecationLevel.ERROR, - replaceWith = ReplaceWith( - "this.gsonPref(key).validate(isValid)", - imports = arrayOf("hu.autsoft.krate.validation.validate"), - ), -) -public inline fun Krate.gsonPref( - key: String, - noinline isValid: (newValue: T?) -> Boolean, -): PropertyDelegateProvider> { - return gsonPrefImpl(key, object : TypeToken() {}.type, isValid) -} - -@PublishedApi -internal fun Krate.gsonPrefImpl( - key: String, - type: Type, - isValid: (newValue: T?) -> Boolean, -): PropertyDelegateProvider> { - return ValidatedPreferenceDelegateFactory(GsonDelegateFactory(key, type), isValid) -} - -/** - * Creates a validated, non-optional preference of type T with the given [key] and [defaultValue] - * in this [Krate] instance. - * This value will be serialized using Gson. - */ -@Deprecated( - message = "Use .validate {} on a gsonPref instead", - level = DeprecationLevel.ERROR, - replaceWith = ReplaceWith( - "this.gsonPref(key, defaultValue).validate(isValid)", - imports = arrayOf("hu.autsoft.krate.validation.validate"), - ), -) -public inline fun Krate.gsonPref( - key: String, - defaultValue: T, - noinline isValid: (newValue: T) -> Boolean, -): PropertyDelegateProvider> { - return gsonPrefImpl(key, defaultValue, object : TypeToken() {}.type, isValid) -} - -@PublishedApi -internal fun Krate.gsonPrefImpl( - key: String, - defaultValue: T, - type: Type, - isValid: (newValue: T) -> Boolean, -): PropertyDelegateProvider> { - return ValidatedPreferenceDelegateFactory(GsonDelegateWithDefaultFactory(key, defaultValue, type), isValid) -} diff --git a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/ValidatedFunctions.kt b/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/ValidatedFunctions.kt deleted file mode 100644 index 009736d..0000000 --- a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/ValidatedFunctions.kt +++ /dev/null @@ -1,75 +0,0 @@ -@file:[Suppress("unused") OptIn(InternalKrateApi::class)] - -package hu.autsoft.krate.moshi - -import hu.autsoft.krate.Krate -import hu.autsoft.krate.internal.InternalKrateApi -import hu.autsoft.krate.moshi.default.MoshiDelegateWithDefaultFactory -import hu.autsoft.krate.moshi.optional.MoshiDelegateFactory -import hu.autsoft.krate.validation.ValidatedPreferenceDelegateFactory -import java.lang.reflect.Type -import kotlin.properties.PropertyDelegateProvider -import kotlin.properties.ReadWriteProperty -import kotlin.reflect.javaType -import kotlin.reflect.typeOf - -/** - * Creates a validated, optional preference of type T with the given [key] in this [Krate] instance. - * This value will be serialized using Moshi. - */ -@OptIn(ExperimentalStdlibApi::class) -@Deprecated( - message = "Use .validate {} on a moshiPref instead", - level = DeprecationLevel.ERROR, - replaceWith = ReplaceWith( - "this.moshiPref(key).validate(isValid)", - imports = arrayOf("hu.autsoft.krate.validation.validate"), - ), -) -public inline fun Krate.moshiPref( - key: String, - noinline isValid: (newValue: T?) -> Boolean, -): PropertyDelegateProvider> { - return moshiPrefImpl(key, typeOf().javaType, isValid) -} - -@PublishedApi -internal fun Krate.moshiPrefImpl( - key: String, - type: Type, - isValid: (newValue: T?) -> Boolean, -): PropertyDelegateProvider> { - return ValidatedPreferenceDelegateFactory(MoshiDelegateFactory(key, type), isValid) -} - -/** - * Creates a validated, non-optional preference of type T with the given [key] and [defaultValue] - * in this [Krate] instance. - * This value will be serialized using Moshi. - */ -@OptIn(ExperimentalStdlibApi::class) -@Deprecated( - message = "Use .validate {} on a moshiPref instead", - level = DeprecationLevel.ERROR, - replaceWith = ReplaceWith( - "this.moshiPref(key, defaultValue).validate(isValid)", - imports = arrayOf("hu.autsoft.krate.validation.validate"), - ), -) -public inline fun Krate.moshiPref( - key: String, - defaultValue: T, - noinline isValid: (newValue: T) -> Boolean, -): PropertyDelegateProvider> { - return moshiPrefImpl(key, defaultValue, typeOf().javaType, isValid) -} - -@PublishedApi -internal fun Krate.moshiPrefImpl( - key: String, - defaultValue: T, - type: Type, - isValid: (newValue: T) -> Boolean, -): PropertyDelegateProvider> { - return ValidatedPreferenceDelegateFactory(MoshiDelegateWithDefaultFactory(key, defaultValue, type), isValid) -} diff --git a/krate/src/main/kotlin/hu/autsoft/krate/ValidatedFunctions.kt b/krate/src/main/kotlin/hu/autsoft/krate/ValidatedFunctions.kt deleted file mode 100644 index 1e1d27c..0000000 --- a/krate/src/main/kotlin/hu/autsoft/krate/ValidatedFunctions.kt +++ /dev/null @@ -1,232 +0,0 @@ -@file:[Suppress("unused") OptIn(InternalKrateApi::class)] - -package hu.autsoft.krate - -import hu.autsoft.krate.default.FloatDelegateWithDefault -import hu.autsoft.krate.default.IntDelegateWithDefault -import hu.autsoft.krate.default.LongDelegateWithDefault -import hu.autsoft.krate.default.StringDelegateWithDefault -import hu.autsoft.krate.default.StringSetDelegateWithDefault -import hu.autsoft.krate.internal.InternalKrateApi -import hu.autsoft.krate.optional.FloatDelegate -import hu.autsoft.krate.optional.IntDelegate -import hu.autsoft.krate.optional.LongDelegate -import hu.autsoft.krate.optional.StringDelegate -import hu.autsoft.krate.optional.StringSetDelegate -import hu.autsoft.krate.validation.ValidatedPreferenceDelegate -import kotlin.properties.ReadWriteProperty - -/** - * Creates a validated, optional preference of type [Float] with the given [key]. - * - * If a value being set to this preference returns `false` when checked by [isValid], - * an [IllegalArgumentException] will be thrown. - */ -@Deprecated( - message = "Use .validate {} on a floatPref instead", - level = DeprecationLevel.ERROR, - replaceWith = ReplaceWith( - "this.floatPref(key).validate(isValid)", - imports = arrayOf("hu.autsoft.krate.validation.validate"), - ), -) -public fun Krate.floatPref( - key: String, - isValid: (newValue: Float?) -> Boolean, -): ReadWriteProperty { - return ValidatedPreferenceDelegate(FloatDelegate(key), isValid) -} - -/** - * Creates a validated, non-optional preference of type [Float] with the given [key]. - * - * If a value being set to this preference returns `false` when checked by [isValid], - * an [IllegalArgumentException] will be thrown. - */ -@Deprecated( - message = "Use .validate {} on a floatPref instead", - level = DeprecationLevel.ERROR, - replaceWith = ReplaceWith( - "this.floatPref(key, defaultValue).validate(isValid)", - imports = arrayOf("hu.autsoft.krate.validation.validate"), - ), -) -public fun Krate.floatPref( - key: String, - defaultValue: Float, - isValid: (newValue: Float) -> Boolean, -): ReadWriteProperty { - return ValidatedPreferenceDelegate(FloatDelegateWithDefault(key, defaultValue), isValid) -} - -/** - * Creates a validated, optional preference of type [Int] with the given [key]. - * - * If a value being set to this preference returns `false` when checked by [isValid], - * an [IllegalArgumentException] will be thrown. - */ -@Deprecated( - message = "Use .validate {} on an intPref instead", - level = DeprecationLevel.ERROR, - replaceWith = ReplaceWith( - "this.intPref(key).validate(isValid)", - imports = arrayOf("hu.autsoft.krate.validation.validate"), - ), -) -public fun Krate.intPref( - key: String, - isValid: (newValue: Int?) -> Boolean, -): ReadWriteProperty { - return ValidatedPreferenceDelegate(IntDelegate(key), isValid) -} - -/** - * Creates a validated, non-optional preference of type [Int] with the given [key]. - * - * If a value being set to this preference returns `false` when checked by [isValid], - * an [IllegalArgumentException] will be thrown. - */ -@Deprecated( - message = "Use .validate {} on an intPref instead", - level = DeprecationLevel.ERROR, - replaceWith = ReplaceWith( - "this.intPref(key, defaultValue).validate(isValid)", - imports = arrayOf("hu.autsoft.krate.validation.validate"), - ), -) -public fun Krate.intPref( - key: String, - defaultValue: Int, - isValid: (newValue: Int) -> Boolean, -): ReadWriteProperty { - return ValidatedPreferenceDelegate(IntDelegateWithDefault(key, defaultValue), isValid) -} - -/** - * Creates a validated, optional preference of type [Long] with the given [key]. - * - * If a value being set to this preference returns `false` when checked by [isValid], - * an [IllegalArgumentException] will be thrown. - */ -@Deprecated( - message = "Use .validate {} on a longPref instead", - level = DeprecationLevel.ERROR, - replaceWith = ReplaceWith( - "this.longPref(key).validate(isValid)", - imports = arrayOf("hu.autsoft.krate.validation.validate"), - ), -) -public fun Krate.longPref( - key: String, - isValid: (newValue: Long?) -> Boolean, -): ReadWriteProperty { - return ValidatedPreferenceDelegate(LongDelegate(key), isValid) -} - -/** - * Creates a validated, non-optional preference of type [Long] with the given [key]. - * - * If a value being set to this preference returns `false` when checked by [isValid], - * an [IllegalArgumentException] will be thrown. - */ -@Deprecated( - message = "Use .validate {} on a longPref instead", - level = DeprecationLevel.ERROR, - replaceWith = ReplaceWith( - "this.longPref(key, defaultValue).validate(isValid)", - imports = arrayOf("hu.autsoft.krate.validation.validate"), - ), -) -public fun Krate.longPref( - key: String, - defaultValue: Long, - isValid: (newValue: Long) -> Boolean, -): ReadWriteProperty { - return ValidatedPreferenceDelegate(LongDelegateWithDefault(key, defaultValue), isValid) -} - -/** - * Creates a validated, optional preference of type [String] with the given [key]. - * - * If a value being set to this preference returns `false` when checked by [isValid], - * an [IllegalArgumentException] will be thrown. - */ -@Deprecated( - message = "Use .validate {} on a stringPref instead", - level = DeprecationLevel.ERROR, - replaceWith = ReplaceWith( - "this.stringPref(key).validate(isValid)", - imports = arrayOf("hu.autsoft.krate.validation.validate"), - ), -) -public fun Krate.stringPref( - key: String, - isValid: (newValue: String?) -> Boolean, -): ReadWriteProperty { - return ValidatedPreferenceDelegate(StringDelegate(key), isValid) -} - -/** - * Creates a validated, non-optional preference of type [String] with the given [key]. - * - * If a value being set to this preference returns `false` when checked by [isValid], - * an [IllegalArgumentException] will be thrown. - */ -@Deprecated( - message = "Use .validate {} on a stringPref instead", - level = DeprecationLevel.ERROR, - replaceWith = ReplaceWith( - "this.stringPref(key, defaultValue).validate(isValid)", - imports = arrayOf("hu.autsoft.krate.validation.validate"), - ), -) -public fun Krate.stringPref( - key: String, - defaultValue: String, - isValid: (newValue: String) -> Boolean, -): ReadWriteProperty { - return ValidatedPreferenceDelegate(StringDelegateWithDefault(key, defaultValue), isValid) -} - -/** - * Creates a validated, optional preference of type Set with the given [key]. - * - * If a value being set to this preference returns `false` when checked by [isValid], - * an [IllegalArgumentException] will be thrown. - */ -@Deprecated( - message = "Use .validate {} on a stringSetPref instead", - level = DeprecationLevel.ERROR, - replaceWith = ReplaceWith( - "this.stringSetPref(key).validate(isValid)", - imports = arrayOf("hu.autsoft.krate.validation.validate"), - ), -) -public fun Krate.stringSetPref( - key: String, - isValid: (newValue: Set?) -> Boolean, -): ReadWriteProperty?> { - return ValidatedPreferenceDelegate(StringSetDelegate(key), isValid) -} - -/** - * Creates a validated, non-optional preference of type Set with the given [key]. - * - * If a value being set to this preference returns `false` when checked by [isValid], - * an [IllegalArgumentException] will be thrown. - */ -@Deprecated( - message = "Use .validate {} on a stringSetPref instead", - level = DeprecationLevel.ERROR, - replaceWith = ReplaceWith( - "this.stringSetPref(key, defaultValue).validate(isValid)", - imports = arrayOf("hu.autsoft.krate.validation.validate"), - ), -) -public fun Krate.stringSetPref( - key: String, - defaultValue: Set, - isValid: (newValue: Set) -> Boolean, -): ReadWriteProperty> { - return ValidatedPreferenceDelegate(StringSetDelegateWithDefault(key, defaultValue), isValid) -} From 5fb34fb50700188166bad18f27f2c0f7a2984cef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Braun?= Date: Wed, 17 Nov 2021 19:10:56 +0100 Subject: [PATCH 07/31] Convert KeyDelegate to an interface --- .../kotlin/hu/autsoft/krate/gson/optional/GsonDelegate.kt | 6 +++--- .../hu/autsoft/krate/kotlinx/optional/KotlinxDelegate.kt | 6 +++--- .../kotlin/hu/autsoft/krate/moshi/optional/MoshiDelegate.kt | 6 +++--- krate/src/main/kotlin/hu/autsoft/krate/base/KeyDelegate.kt | 4 +++- .../kotlin/hu/autsoft/krate/optional/BooleanDelegate.kt | 2 +- .../main/kotlin/hu/autsoft/krate/optional/FloatDelegate.kt | 2 +- .../main/kotlin/hu/autsoft/krate/optional/IntDelegate.kt | 2 +- .../main/kotlin/hu/autsoft/krate/optional/LongDelegate.kt | 2 +- .../main/kotlin/hu/autsoft/krate/optional/StringDelegate.kt | 2 +- .../kotlin/hu/autsoft/krate/optional/StringSetDelegate.kt | 2 +- 10 files changed, 18 insertions(+), 16 deletions(-) diff --git a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/optional/GsonDelegate.kt b/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/optional/GsonDelegate.kt index 65991ca..8883a24 100644 --- a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/optional/GsonDelegate.kt +++ b/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/optional/GsonDelegate.kt @@ -11,9 +11,9 @@ import java.lang.reflect.Type import kotlin.reflect.KProperty private class GsonDelegate( - key: String, - private val adapter: TypeAdapter, -) : KeyDelegate(key) { + override val key: String, + private val adapter: TypeAdapter, +) : KeyDelegate { override operator fun getValue(thisRef: Krate, property: KProperty<*>): T? { return if (!thisRef.sharedPreferences.contains(key)) { diff --git a/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/optional/KotlinxDelegate.kt b/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/optional/KotlinxDelegate.kt index 325688d..0f3a595 100644 --- a/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/optional/KotlinxDelegate.kt +++ b/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/optional/KotlinxDelegate.kt @@ -12,9 +12,9 @@ import kotlin.reflect.KType private class KotlinxDelegate( - key: String, - private val serializer: KSerializer, -) : KeyDelegate(key) { + override val key: String, + private val serializer: KSerializer, +) : KeyDelegate { override operator fun getValue(thisRef: Krate, property: KProperty<*>): T? { return if (!thisRef.sharedPreferences.contains(key)) { diff --git a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/optional/MoshiDelegate.kt b/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/optional/MoshiDelegate.kt index f38ffcb..c0cdc68 100644 --- a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/optional/MoshiDelegate.kt +++ b/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/optional/MoshiDelegate.kt @@ -10,9 +10,9 @@ import java.lang.reflect.Type import kotlin.reflect.KProperty private class MoshiDelegate( - key: String, - private val adapter: JsonAdapter, -) : KeyDelegate(key) { + override val key: String, + private val adapter: JsonAdapter, +) : KeyDelegate { override fun getValue(thisRef: Krate, property: KProperty<*>): T? { return if (!thisRef.sharedPreferences.contains(key)) { diff --git a/krate/src/main/kotlin/hu/autsoft/krate/base/KeyDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/base/KeyDelegate.kt index 30a720d..8c38d5b 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/base/KeyDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/base/KeyDelegate.kt @@ -3,4 +3,6 @@ package hu.autsoft.krate.base import hu.autsoft.krate.Krate import kotlin.properties.ReadWriteProperty -public abstract class KeyDelegate(public val key: String) : ReadWriteProperty \ No newline at end of file +public interface KeyDelegate : ReadWriteProperty { + public val key: String +} diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/BooleanDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/BooleanDelegate.kt index 50557fa..ffc672f 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/BooleanDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/BooleanDelegate.kt @@ -5,7 +5,7 @@ import hu.autsoft.krate.base.KeyDelegate import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty -internal class BooleanDelegate(key: String) : KeyDelegate(key) { +internal class BooleanDelegate(override val key: String) : KeyDelegate { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Boolean? { return if (!thisRef.sharedPreferences.contains(key)) { diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/FloatDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/FloatDelegate.kt index d33a116..7e5541d 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/FloatDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/FloatDelegate.kt @@ -5,7 +5,7 @@ import hu.autsoft.krate.base.KeyDelegate import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty -internal class FloatDelegate(key: String) : KeyDelegate(key) { +internal class FloatDelegate(override val key: String) : KeyDelegate { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Float? { return if (!thisRef.sharedPreferences.contains(key)) { diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/IntDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/IntDelegate.kt index 60e33cc..250b2de 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/IntDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/IntDelegate.kt @@ -5,7 +5,7 @@ import hu.autsoft.krate.base.KeyDelegate import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty -internal class IntDelegate(key: String) : KeyDelegate(key) { +internal class IntDelegate(override val key: String) : KeyDelegate { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Int? { return if (!thisRef.sharedPreferences.contains(key)) { diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/LongDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/LongDelegate.kt index 085ffab..01371a0 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/LongDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/LongDelegate.kt @@ -5,7 +5,7 @@ import hu.autsoft.krate.base.KeyDelegate import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty -internal class LongDelegate(key: String) : KeyDelegate(key) { +internal class LongDelegate(override val key: String) : KeyDelegate { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Long? { return if (!thisRef.sharedPreferences.contains(key)) { diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/StringDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/StringDelegate.kt index 8dce3c1..d3d0f96 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/StringDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/StringDelegate.kt @@ -5,7 +5,7 @@ import hu.autsoft.krate.base.KeyDelegate import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty -internal class StringDelegate(key: String) : KeyDelegate(key) { +internal class StringDelegate(override val key: String) : KeyDelegate { override operator fun getValue(thisRef: Krate, property: KProperty<*>): String? { return if (!thisRef.sharedPreferences.contains(key)) { diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/StringSetDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/StringSetDelegate.kt index 48c0093..92c482d 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/StringSetDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/StringSetDelegate.kt @@ -5,7 +5,7 @@ import hu.autsoft.krate.base.KeyDelegate import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty -internal class StringSetDelegate(key: String) : KeyDelegate?>(key) { +internal class StringSetDelegate(override val key: String) : KeyDelegate?> { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Set? { return if (!thisRef.sharedPreferences.contains(key)) { From aa8037fb110c48fc19328c833055a613f04a9175 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Braun?= Date: Wed, 17 Nov 2021 19:12:18 +0100 Subject: [PATCH 08/31] Convert KeyDelegateProvider to an interface --- .../kotlin/hu/autsoft/krate/gson/optional/GsonDelegate.kt | 6 +++--- .../hu/autsoft/krate/kotlinx/optional/KotlinxDelegate.kt | 6 +++--- .../kotlin/hu/autsoft/krate/moshi/optional/MoshiDelegate.kt | 6 +++--- .../kotlin/hu/autsoft/krate/base/KeyDelegateProvider.kt | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/optional/GsonDelegate.kt b/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/optional/GsonDelegate.kt index 8883a24..6bba9ee 100644 --- a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/optional/GsonDelegate.kt +++ b/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/optional/GsonDelegate.kt @@ -38,9 +38,9 @@ private class GsonDelegate( } internal class GsonDelegateFactory( - private val key: String, - private val type: Type, -) : KeyDelegateProvider() { + private val key: String, + private val type: Type, +) : KeyDelegateProvider { override fun provideDelegate(thisRef: Krate, property: KProperty<*>): KeyDelegate { @Suppress("UNCHECKED_CAST") diff --git a/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/optional/KotlinxDelegate.kt b/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/optional/KotlinxDelegate.kt index 0f3a595..ad064b4 100644 --- a/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/optional/KotlinxDelegate.kt +++ b/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/optional/KotlinxDelegate.kt @@ -39,9 +39,9 @@ private class KotlinxDelegate( } internal class KotlinxDelegateFactory( - private val key: String, - private val type: KType, -) : KeyDelegateProvider() { + private val key: String, + private val type: KType, +) : KeyDelegateProvider { override fun provideDelegate(thisRef: Krate, property: KProperty<*>): KeyDelegate { @Suppress("UNCHECKED_CAST") diff --git a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/optional/MoshiDelegate.kt b/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/optional/MoshiDelegate.kt index c0cdc68..5a2f989 100644 --- a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/optional/MoshiDelegate.kt +++ b/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/optional/MoshiDelegate.kt @@ -37,9 +37,9 @@ private class MoshiDelegate( } internal class MoshiDelegateFactory( - private val key: String, - private val type: Type, -) : KeyDelegateProvider() { + private val key: String, + private val type: Type, +) : KeyDelegateProvider { override fun provideDelegate(thisRef: Krate, property: KProperty<*>): KeyDelegate { val adapter = thisRef.realMoshiInstance.adapter(type) diff --git a/krate/src/main/kotlin/hu/autsoft/krate/base/KeyDelegateProvider.kt b/krate/src/main/kotlin/hu/autsoft/krate/base/KeyDelegateProvider.kt index 5ba8dce..597b860 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/base/KeyDelegateProvider.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/base/KeyDelegateProvider.kt @@ -3,4 +3,4 @@ package hu.autsoft.krate.base import hu.autsoft.krate.Krate import kotlin.properties.PropertyDelegateProvider -public abstract class KeyDelegateProvider : PropertyDelegateProvider> \ No newline at end of file +public interface KeyDelegateProvider : PropertyDelegateProvider> From 994103d93576f55b4553a55a00fc208d09947ce9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Braun?= Date: Wed, 17 Nov 2021 19:13:26 +0100 Subject: [PATCH 09/31] Rename KeyedKrateProperty and KeyedKratePropertyProvider --- .../kotlin/hu/autsoft/krate/gson/Functions.kt | 12 +++++------ .../krate/gson/optional/GsonDelegate.kt | 10 +++++----- .../hu/autsoft/krate/kotlinx/Functions.kt | 12 +++++------ .../krate/kotlinx/optional/KotlinxDelegate.kt | 10 +++++----- .../hu/autsoft/krate/moshi/Functions.kt | 12 +++++------ .../krate/moshi/optional/MoshiDelegate.kt | 10 +++++----- .../main/kotlin/hu/autsoft/krate/Functions.kt | 14 ++++++------- .../{KeyDelegate.kt => KeyedKrateProperty.kt} | 2 +- ...vider.kt => KeyedKratePropertyProvider.kt} | 2 +- .../krate/default/DelegateWithDefault.kt | 20 +++++++++---------- .../autsoft/krate/optional/BooleanDelegate.kt | 4 ++-- .../autsoft/krate/optional/FloatDelegate.kt | 4 ++-- .../hu/autsoft/krate/optional/IntDelegate.kt | 4 ++-- .../hu/autsoft/krate/optional/LongDelegate.kt | 4 ++-- .../autsoft/krate/optional/StringDelegate.kt | 4 ++-- .../krate/optional/StringSetDelegate.kt | 4 ++-- 16 files changed, 64 insertions(+), 64 deletions(-) rename krate/src/main/kotlin/hu/autsoft/krate/base/{KeyDelegate.kt => KeyedKrateProperty.kt} (65%) rename krate/src/main/kotlin/hu/autsoft/krate/base/{KeyDelegateProvider.kt => KeyedKratePropertyProvider.kt} (51%) diff --git a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/Functions.kt b/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/Functions.kt index a72a45b..3553665 100644 --- a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/Functions.kt +++ b/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/Functions.kt @@ -4,7 +4,7 @@ package hu.autsoft.krate.gson import com.google.gson.reflect.TypeToken import hu.autsoft.krate.Krate -import hu.autsoft.krate.base.KeyDelegateProvider +import hu.autsoft.krate.base.KeyedKratePropertyProvider import hu.autsoft.krate.default.DelegateWithDefaultFactory import hu.autsoft.krate.gson.optional.GsonDelegateFactory import hu.autsoft.krate.internal.InternalKrateApi @@ -17,16 +17,16 @@ import kotlin.properties.ReadWriteProperty * This instance will be serialized using Gson. */ public inline fun Krate.gsonPref( - key: String, -): KeyDelegateProvider { + key: String, +): KeyedKratePropertyProvider { return gsonPrefImpl(key, object : TypeToken() {}.type) } @PublishedApi internal fun Krate.gsonPrefImpl( - key: String, - type: Type, -): KeyDelegateProvider { + key: String, + type: Type, +): KeyedKratePropertyProvider { return GsonDelegateFactory(key, type) } diff --git a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/optional/GsonDelegate.kt b/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/optional/GsonDelegate.kt index 6bba9ee..0062a66 100644 --- a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/optional/GsonDelegate.kt +++ b/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/optional/GsonDelegate.kt @@ -3,8 +3,8 @@ package hu.autsoft.krate.gson.optional import com.google.gson.TypeAdapter import com.google.gson.reflect.TypeToken import hu.autsoft.krate.Krate -import hu.autsoft.krate.base.KeyDelegate -import hu.autsoft.krate.base.KeyDelegateProvider +import hu.autsoft.krate.base.KeyedKrateProperty +import hu.autsoft.krate.base.KeyedKratePropertyProvider import hu.autsoft.krate.gson.internalGson import hu.autsoft.krate.gson.util.edit import java.lang.reflect.Type @@ -13,7 +13,7 @@ import kotlin.reflect.KProperty private class GsonDelegate( override val key: String, private val adapter: TypeAdapter, -) : KeyDelegate { +) : KeyedKrateProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): T? { return if (!thisRef.sharedPreferences.contains(key)) { @@ -40,9 +40,9 @@ private class GsonDelegate( internal class GsonDelegateFactory( private val key: String, private val type: Type, -) : KeyDelegateProvider { +) : KeyedKratePropertyProvider { - override fun provideDelegate(thisRef: Krate, property: KProperty<*>): KeyDelegate { + override fun provideDelegate(thisRef: Krate, property: KProperty<*>): KeyedKrateProperty { @Suppress("UNCHECKED_CAST") val adapter: TypeAdapter = thisRef.internalGson.getAdapter(TypeToken.get(type)) as TypeAdapter return GsonDelegate(key, adapter) diff --git a/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/Functions.kt b/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/Functions.kt index 44ee4d6..4cd77e6 100644 --- a/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/Functions.kt +++ b/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/Functions.kt @@ -3,7 +3,7 @@ package hu.autsoft.krate.kotlinx import hu.autsoft.krate.Krate -import hu.autsoft.krate.base.KeyDelegateProvider +import hu.autsoft.krate.base.KeyedKratePropertyProvider import hu.autsoft.krate.default.DelegateWithDefaultFactory import hu.autsoft.krate.internal.InternalKrateApi import hu.autsoft.krate.kotlinx.optional.KotlinxDelegateFactory @@ -18,16 +18,16 @@ import kotlin.reflect.typeOf */ @OptIn(ExperimentalStdlibApi::class) public inline fun Krate.kotlinxPref( - key: String, -): KeyDelegateProvider { + key: String, +): KeyedKratePropertyProvider { return kotlinxPrefImpl(key, typeOf()) } @PublishedApi internal fun Krate.kotlinxPrefImpl( - key: String, - type: KType, -): KeyDelegateProvider { + key: String, + type: KType, +): KeyedKratePropertyProvider { return KotlinxDelegateFactory(key, type) } diff --git a/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/optional/KotlinxDelegate.kt b/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/optional/KotlinxDelegate.kt index ad064b4..7ce3838 100644 --- a/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/optional/KotlinxDelegate.kt +++ b/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/optional/KotlinxDelegate.kt @@ -1,8 +1,8 @@ package hu.autsoft.krate.kotlinx.optional import hu.autsoft.krate.Krate -import hu.autsoft.krate.base.KeyDelegate -import hu.autsoft.krate.base.KeyDelegateProvider +import hu.autsoft.krate.base.KeyedKrateProperty +import hu.autsoft.krate.base.KeyedKratePropertyProvider import hu.autsoft.krate.kotlinx.internalJson import hu.autsoft.krate.kotlinx.util.edit import kotlinx.serialization.KSerializer @@ -14,7 +14,7 @@ import kotlin.reflect.KType private class KotlinxDelegate( override val key: String, private val serializer: KSerializer, -) : KeyDelegate { +) : KeyedKrateProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): T? { return if (!thisRef.sharedPreferences.contains(key)) { @@ -41,9 +41,9 @@ private class KotlinxDelegate( internal class KotlinxDelegateFactory( private val key: String, private val type: KType, -) : KeyDelegateProvider { +) : KeyedKratePropertyProvider { - override fun provideDelegate(thisRef: Krate, property: KProperty<*>): KeyDelegate { + override fun provideDelegate(thisRef: Krate, property: KProperty<*>): KeyedKrateProperty { @Suppress("UNCHECKED_CAST") val serializer = thisRef.internalJson.serializersModule.serializer(type) as KSerializer return KotlinxDelegate(key, serializer) diff --git a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/Functions.kt b/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/Functions.kt index a2d4a2e..b0bc3f6 100644 --- a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/Functions.kt +++ b/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/Functions.kt @@ -3,7 +3,7 @@ package hu.autsoft.krate.moshi import hu.autsoft.krate.Krate -import hu.autsoft.krate.base.KeyDelegateProvider +import hu.autsoft.krate.base.KeyedKratePropertyProvider import hu.autsoft.krate.default.DelegateWithDefaultFactory import hu.autsoft.krate.internal.InternalKrateApi import hu.autsoft.krate.moshi.optional.MoshiDelegateFactory @@ -19,16 +19,16 @@ import kotlin.reflect.typeOf */ @OptIn(ExperimentalStdlibApi::class) public inline fun Krate.moshiPref( - key: String, -): KeyDelegateProvider { + key: String, +): KeyedKratePropertyProvider { return moshiPrefImpl(key, typeOf().javaType) } @PublishedApi internal fun Krate.moshiPrefImpl( - key: String, - type: Type, -): KeyDelegateProvider { + key: String, + type: Type, +): KeyedKratePropertyProvider { return MoshiDelegateFactory(key, type) } diff --git a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/optional/MoshiDelegate.kt b/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/optional/MoshiDelegate.kt index 5a2f989..0c4a1ef 100644 --- a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/optional/MoshiDelegate.kt +++ b/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/optional/MoshiDelegate.kt @@ -2,8 +2,8 @@ package hu.autsoft.krate.moshi.optional import com.squareup.moshi.JsonAdapter import hu.autsoft.krate.Krate -import hu.autsoft.krate.base.KeyDelegate -import hu.autsoft.krate.base.KeyDelegateProvider +import hu.autsoft.krate.base.KeyedKrateProperty +import hu.autsoft.krate.base.KeyedKratePropertyProvider import hu.autsoft.krate.moshi.realMoshiInstance import hu.autsoft.krate.moshi.util.edit import java.lang.reflect.Type @@ -12,7 +12,7 @@ import kotlin.reflect.KProperty private class MoshiDelegate( override val key: String, private val adapter: JsonAdapter, -) : KeyDelegate { +) : KeyedKrateProperty { override fun getValue(thisRef: Krate, property: KProperty<*>): T? { return if (!thisRef.sharedPreferences.contains(key)) { @@ -39,9 +39,9 @@ private class MoshiDelegate( internal class MoshiDelegateFactory( private val key: String, private val type: Type, -) : KeyDelegateProvider { +) : KeyedKratePropertyProvider { - override fun provideDelegate(thisRef: Krate, property: KProperty<*>): KeyDelegate { + override fun provideDelegate(thisRef: Krate, property: KProperty<*>): KeyedKrateProperty { val adapter = thisRef.realMoshiInstance.adapter(type) return MoshiDelegate(key, adapter) } diff --git a/krate/src/main/kotlin/hu/autsoft/krate/Functions.kt b/krate/src/main/kotlin/hu/autsoft/krate/Functions.kt index 6ea8436..94b727d 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/Functions.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/Functions.kt @@ -2,7 +2,7 @@ package hu.autsoft.krate -import hu.autsoft.krate.base.KeyDelegate +import hu.autsoft.krate.base.KeyedKrateProperty import hu.autsoft.krate.default.withDefault import hu.autsoft.krate.optional.* import kotlin.properties.ReadWriteProperty @@ -10,42 +10,42 @@ import kotlin.properties.ReadWriteProperty /** * Creates an optional preference of type [Boolean] with the given [key] in this [Krate] instance. */ -public fun Krate.booleanPref(key: String): KeyDelegate { +public fun Krate.booleanPref(key: String): KeyedKrateProperty { return BooleanDelegate(key) } /** * Creates an optional preference of type [Float] with the given [key] in this [Krate] instance. */ -public fun Krate.floatPref(key: String): KeyDelegate { +public fun Krate.floatPref(key: String): KeyedKrateProperty { return FloatDelegate(key) } /** * Creates an optional preference of type [Int] with the given [key] in this [Krate] instance. */ -public fun Krate.intPref(key: String): KeyDelegate { +public fun Krate.intPref(key: String): KeyedKrateProperty { return IntDelegate(key) } /** * Creates an optional preference of type [Long] with the given [key] in this [Krate] instance. */ -public fun Krate.longPref(key: String): KeyDelegate { +public fun Krate.longPref(key: String): KeyedKrateProperty { return LongDelegate(key) } /** * Creates an optional preference of type [String] with the given [key] in this [Krate] instance. */ -public fun Krate.stringPref(key: String): KeyDelegate { +public fun Krate.stringPref(key: String): KeyedKrateProperty { return StringDelegate(key) } /** * Creates an optional preference of type Set with the given [key] in this [Krate] instance. */ -public fun Krate.stringSetPref(key: String): KeyDelegate?> { +public fun Krate.stringSetPref(key: String): KeyedKrateProperty?> { return StringSetDelegate(key) } diff --git a/krate/src/main/kotlin/hu/autsoft/krate/base/KeyDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/base/KeyedKrateProperty.kt similarity index 65% rename from krate/src/main/kotlin/hu/autsoft/krate/base/KeyDelegate.kt rename to krate/src/main/kotlin/hu/autsoft/krate/base/KeyedKrateProperty.kt index 8c38d5b..c7d8640 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/base/KeyDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/base/KeyedKrateProperty.kt @@ -3,6 +3,6 @@ package hu.autsoft.krate.base import hu.autsoft.krate.Krate import kotlin.properties.ReadWriteProperty -public interface KeyDelegate : ReadWriteProperty { +public interface KeyedKrateProperty : ReadWriteProperty { public val key: String } diff --git a/krate/src/main/kotlin/hu/autsoft/krate/base/KeyDelegateProvider.kt b/krate/src/main/kotlin/hu/autsoft/krate/base/KeyedKratePropertyProvider.kt similarity index 51% rename from krate/src/main/kotlin/hu/autsoft/krate/base/KeyDelegateProvider.kt rename to krate/src/main/kotlin/hu/autsoft/krate/base/KeyedKratePropertyProvider.kt index 597b860..b2e79ae 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/base/KeyDelegateProvider.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/base/KeyedKratePropertyProvider.kt @@ -3,4 +3,4 @@ package hu.autsoft.krate.base import hu.autsoft.krate.Krate import kotlin.properties.PropertyDelegateProvider -public interface KeyDelegateProvider : PropertyDelegateProvider> +public interface KeyedKratePropertyProvider : PropertyDelegateProvider> diff --git a/krate/src/main/kotlin/hu/autsoft/krate/default/DelegateWithDefault.kt b/krate/src/main/kotlin/hu/autsoft/krate/default/DelegateWithDefault.kt index 1e8dc68..66c994d 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/default/DelegateWithDefault.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/default/DelegateWithDefault.kt @@ -3,8 +3,8 @@ package hu.autsoft.krate.default import hu.autsoft.krate.Krate -import hu.autsoft.krate.base.KeyDelegate -import hu.autsoft.krate.base.KeyDelegateProvider +import hu.autsoft.krate.base.KeyedKrateProperty +import hu.autsoft.krate.base.KeyedKratePropertyProvider import hu.autsoft.krate.internal.InternalKrateApi import kotlin.properties.PropertyDelegateProvider import kotlin.properties.ReadWriteProperty @@ -12,8 +12,8 @@ import kotlin.reflect.KProperty @InternalKrateApi public class DelegateWithDefault( - private val delegate: KeyDelegate, - private val default: T, + private val delegate: KeyedKrateProperty, + private val default: T, ) : ReadWriteProperty { override fun setValue(thisRef: Krate, property: KProperty<*>, value: T) { @@ -27,8 +27,8 @@ public class DelegateWithDefault( @InternalKrateApi public class DelegateWithDefaultFactory( - private val propertyDelegateProvider: PropertyDelegateProvider>, - private val default: T, + private val propertyDelegateProvider: PropertyDelegateProvider>, + private val default: T, ) : PropertyDelegateProvider> { override fun provideDelegate(thisRef: Krate, property: KProperty<*>): ReadWriteProperty { @@ -50,8 +50,8 @@ public class DelegateWithDefaultFactory( * var defaultString by stringPref("validatedString").withDefault("default") * ``` */ -public fun KeyDelegate.withDefault( - default: T +public fun KeyedKrateProperty.withDefault( + default: T ): ReadWriteProperty { @OptIn(InternalKrateApi::class) return DelegateWithDefault(this, default) @@ -68,8 +68,8 @@ public fun KeyDelegate.withDefault( * var defaultModel by kotlinxPref("defaultModel").withDefault(DefaultModel()) * ``` */ -public fun KeyDelegateProvider.withDefault( - default: T +public fun KeyedKratePropertyProvider.withDefault( + default: T ): PropertyDelegateProvider> { @OptIn(InternalKrateApi::class) return DelegateWithDefaultFactory(this, default) diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/BooleanDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/BooleanDelegate.kt index ffc672f..a378668 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/BooleanDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/BooleanDelegate.kt @@ -1,11 +1,11 @@ package hu.autsoft.krate.optional import hu.autsoft.krate.Krate -import hu.autsoft.krate.base.KeyDelegate +import hu.autsoft.krate.base.KeyedKrateProperty import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty -internal class BooleanDelegate(override val key: String) : KeyDelegate { +internal class BooleanDelegate(override val key: String) : KeyedKrateProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Boolean? { return if (!thisRef.sharedPreferences.contains(key)) { diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/FloatDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/FloatDelegate.kt index 7e5541d..d81a8ab 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/FloatDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/FloatDelegate.kt @@ -1,11 +1,11 @@ package hu.autsoft.krate.optional import hu.autsoft.krate.Krate -import hu.autsoft.krate.base.KeyDelegate +import hu.autsoft.krate.base.KeyedKrateProperty import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty -internal class FloatDelegate(override val key: String) : KeyDelegate { +internal class FloatDelegate(override val key: String) : KeyedKrateProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Float? { return if (!thisRef.sharedPreferences.contains(key)) { diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/IntDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/IntDelegate.kt index 250b2de..876a30f 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/IntDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/IntDelegate.kt @@ -1,11 +1,11 @@ package hu.autsoft.krate.optional import hu.autsoft.krate.Krate -import hu.autsoft.krate.base.KeyDelegate +import hu.autsoft.krate.base.KeyedKrateProperty import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty -internal class IntDelegate(override val key: String) : KeyDelegate { +internal class IntDelegate(override val key: String) : KeyedKrateProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Int? { return if (!thisRef.sharedPreferences.contains(key)) { diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/LongDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/LongDelegate.kt index 01371a0..abad7b9 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/LongDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/LongDelegate.kt @@ -1,11 +1,11 @@ package hu.autsoft.krate.optional import hu.autsoft.krate.Krate -import hu.autsoft.krate.base.KeyDelegate +import hu.autsoft.krate.base.KeyedKrateProperty import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty -internal class LongDelegate(override val key: String) : KeyDelegate { +internal class LongDelegate(override val key: String) : KeyedKrateProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Long? { return if (!thisRef.sharedPreferences.contains(key)) { diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/StringDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/StringDelegate.kt index d3d0f96..e8b6ff6 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/StringDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/StringDelegate.kt @@ -1,11 +1,11 @@ package hu.autsoft.krate.optional import hu.autsoft.krate.Krate -import hu.autsoft.krate.base.KeyDelegate +import hu.autsoft.krate.base.KeyedKrateProperty import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty -internal class StringDelegate(override val key: String) : KeyDelegate { +internal class StringDelegate(override val key: String) : KeyedKrateProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): String? { return if (!thisRef.sharedPreferences.contains(key)) { diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/StringSetDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/StringSetDelegate.kt index 92c482d..3dab962 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/StringSetDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/StringSetDelegate.kt @@ -1,11 +1,11 @@ package hu.autsoft.krate.optional import hu.autsoft.krate.Krate -import hu.autsoft.krate.base.KeyDelegate +import hu.autsoft.krate.base.KeyedKrateProperty import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty -internal class StringSetDelegate(override val key: String) : KeyDelegate?> { +internal class StringSetDelegate(override val key: String) : KeyedKrateProperty?> { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Set? { return if (!thisRef.sharedPreferences.contains(key)) { From 687c6745bc3870a87aeac2861e5f3e466646f3e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Braun?= Date: Wed, 17 Nov 2021 19:17:35 +0100 Subject: [PATCH 10/31] Format code --- .../kotlin/hu/autsoft/krate/gson/Functions.kt | 6 +++--- .../krate/gson/optional/GsonDelegate.kt | 8 ++++---- .../hu/autsoft/krate/gson/GsonTestKrate.kt | 18 ++++++++--------- .../hu/autsoft/krate/kotlinx/Functions.kt | 6 +++--- .../krate/kotlinx/optional/KotlinxDelegate.kt | 8 ++++---- .../autsoft/krate/kotlinx/KotlinxTestKrate.kt | 18 ++++++++--------- .../hu/autsoft/krate/moshi/MoshiTestKrate.kt | 20 +++++++++---------- .../hu/autsoft/krate/moshi/MoshiInstances.kt | 8 ++++---- .../krate/moshi/optional/MoshiDelegate.kt | 8 ++++---- .../hu/autsoft/krate/moshi/MoshiTestKrate.kt | 20 +++++++++---------- .../main/kotlin/hu/autsoft/krate/Functions.kt | 7 ++++++- .../krate/default/DelegateWithDefault.kt | 12 +++++------ .../krate/internal/InternalKrateApi.kt | 6 +++--- 13 files changed, 75 insertions(+), 70 deletions(-) diff --git a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/Functions.kt b/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/Functions.kt index 3553665..09c1aeb 100644 --- a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/Functions.kt +++ b/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/Functions.kt @@ -17,15 +17,15 @@ import kotlin.properties.ReadWriteProperty * This instance will be serialized using Gson. */ public inline fun Krate.gsonPref( - key: String, + key: String, ): KeyedKratePropertyProvider { return gsonPrefImpl(key, object : TypeToken() {}.type) } @PublishedApi internal fun Krate.gsonPrefImpl( - key: String, - type: Type, + key: String, + type: Type, ): KeyedKratePropertyProvider { return GsonDelegateFactory(key, type) } diff --git a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/optional/GsonDelegate.kt b/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/optional/GsonDelegate.kt index 0062a66..f70e21c 100644 --- a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/optional/GsonDelegate.kt +++ b/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/optional/GsonDelegate.kt @@ -11,8 +11,8 @@ import java.lang.reflect.Type import kotlin.reflect.KProperty private class GsonDelegate( - override val key: String, - private val adapter: TypeAdapter, + override val key: String, + private val adapter: TypeAdapter, ) : KeyedKrateProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): T? { @@ -38,8 +38,8 @@ private class GsonDelegate( } internal class GsonDelegateFactory( - private val key: String, - private val type: Type, + private val key: String, + private val type: Type, ) : KeyedKratePropertyProvider { override fun provideDelegate(thisRef: Krate, property: KProperty<*>): KeyedKrateProperty { diff --git a/krate-gson/src/test/java/hu/autsoft/krate/gson/GsonTestKrate.kt b/krate-gson/src/test/java/hu/autsoft/krate/gson/GsonTestKrate.kt index ee46f64..e7909fb 100644 --- a/krate-gson/src/test/java/hu/autsoft/krate/gson/GsonTestKrate.kt +++ b/krate-gson/src/test/java/hu/autsoft/krate/gson/GsonTestKrate.kt @@ -21,21 +21,21 @@ internal class GsonTestKrate(context: Context) : SimpleKrate(context) { var listOfValues: List? by gsonPref("listOfValues") var simpleValueWithDefault: TestModel - by gsonPref("simpleValueWithDefault", DEFAULT_SIMPLE_VALUE) + by gsonPref("simpleValueWithDefault").withDefault(DEFAULT_SIMPLE_VALUE) var listOfValuesWithDefault: List - by gsonPref("listOfValuesWithDefault", defaultValue = DEFAULT_LIST_VALUE) + by gsonPref("listOfValuesWithDefault").withDefault(DEFAULT_LIST_VALUE) var validatedValue: TestModel - by gsonPref(key = "validatedValue", defaultValue = DEFAULT_SIMPLE_VALUE) - .validate { newValue -> - newValue.x < newValue.y // arbitrary rule - } + by gsonPref("validatedValue").withDefault(DEFAULT_SIMPLE_VALUE) + .validate { newValue -> + newValue.x < newValue.y // arbitrary rule + } var validatedOptionalValue: List? by gsonPref>(key = "validatedOptionalValue") - .validate { newValue -> - newValue.isNullOrEmpty().not() - } + .validate { newValue -> + newValue.isNullOrEmpty().not() + } } diff --git a/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/Functions.kt b/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/Functions.kt index 4cd77e6..f81b1ed 100644 --- a/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/Functions.kt +++ b/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/Functions.kt @@ -18,15 +18,15 @@ import kotlin.reflect.typeOf */ @OptIn(ExperimentalStdlibApi::class) public inline fun Krate.kotlinxPref( - key: String, + key: String, ): KeyedKratePropertyProvider { return kotlinxPrefImpl(key, typeOf()) } @PublishedApi internal fun Krate.kotlinxPrefImpl( - key: String, - type: KType, + key: String, + type: KType, ): KeyedKratePropertyProvider { return KotlinxDelegateFactory(key, type) } diff --git a/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/optional/KotlinxDelegate.kt b/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/optional/KotlinxDelegate.kt index 7ce3838..6ac50f4 100644 --- a/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/optional/KotlinxDelegate.kt +++ b/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/optional/KotlinxDelegate.kt @@ -12,8 +12,8 @@ import kotlin.reflect.KType private class KotlinxDelegate( - override val key: String, - private val serializer: KSerializer, + override val key: String, + private val serializer: KSerializer, ) : KeyedKrateProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): T? { @@ -39,8 +39,8 @@ private class KotlinxDelegate( } internal class KotlinxDelegateFactory( - private val key: String, - private val type: KType, + private val key: String, + private val type: KType, ) : KeyedKratePropertyProvider { override fun provideDelegate(thisRef: Krate, property: KProperty<*>): KeyedKrateProperty { diff --git a/krate-kotlinx/src/test/java/hu/autsoft/krate/kotlinx/KotlinxTestKrate.kt b/krate-kotlinx/src/test/java/hu/autsoft/krate/kotlinx/KotlinxTestKrate.kt index 315797b..7d8bdea 100644 --- a/krate-kotlinx/src/test/java/hu/autsoft/krate/kotlinx/KotlinxTestKrate.kt +++ b/krate-kotlinx/src/test/java/hu/autsoft/krate/kotlinx/KotlinxTestKrate.kt @@ -21,21 +21,21 @@ internal class KotlinxTestKrate(context: Context) : SimpleKrate(context) { var listOfValues: List? by kotlinxPref("listOfValues") var simpleValueWithDefault: TestModel - by kotlinxPref("simpleValueWithDefault", DEFAULT_SIMPLE_VALUE) + by kotlinxPref("simpleValueWithDefault").withDefault(DEFAULT_SIMPLE_VALUE) var listOfValuesWithDefault: List - by kotlinxPref("listOfValuesWithDefault", defaultValue = DEFAULT_LIST_VALUE) + by kotlinxPref("listOfValuesWithDefault").withDefault(DEFAULT_LIST_VALUE) var validatedValue: TestModel - by kotlinxPref(key = "validatedValue", defaultValue = DEFAULT_SIMPLE_VALUE) - .validate { newValue -> - newValue.x < newValue.y // arbitrary rule - } + by kotlinxPref("validatedValue").withDefault(DEFAULT_SIMPLE_VALUE) + .validate { newValue -> + newValue.x < newValue.y // arbitrary rule + } var validatedOptionalValue: List? by kotlinxPref>(key = "validatedOptionalValue") - .validate { newValue -> - newValue.isNullOrEmpty().not() // arbitrary rule - } + .validate { newValue -> + newValue.isNullOrEmpty().not() // arbitrary rule + } } diff --git a/krate-moshi-codegen/src/test/java/hu/autsoft/krate/moshi/MoshiTestKrate.kt b/krate-moshi-codegen/src/test/java/hu/autsoft/krate/moshi/MoshiTestKrate.kt index 800ff45..ace2afe 100644 --- a/krate-moshi-codegen/src/test/java/hu/autsoft/krate/moshi/MoshiTestKrate.kt +++ b/krate-moshi-codegen/src/test/java/hu/autsoft/krate/moshi/MoshiTestKrate.kt @@ -21,21 +21,21 @@ internal class MoshiTestKrate(context: Context) : SimpleKrate(context) { var listOfValues: List? by moshiPref("listOfValues") var simpleValueWithDefault: TestModel - by moshiPref("simpleValueWithDefault", DEFAULT_SIMPLE_VALUE) + by moshiPref("simpleValueWithDefault").withDefault(DEFAULT_SIMPLE_VALUE) var listOfValuesWithDefault: List - by moshiPref("listOfValuesWithDefault", defaultValue = DEFAULT_LIST_VALUE) + by moshiPref("listOfValuesWithDefault").withDefault(DEFAULT_LIST_VALUE) var validatedValue: TestModel - by moshiPref(key = "validatedValue", defaultValue = DEFAULT_SIMPLE_VALUE) - .validate { newValue -> - newValue.x < newValue.y // arbitrary rule - } + by moshiPref("validatedValue").withDefault(DEFAULT_SIMPLE_VALUE) + .validate { newValue -> + newValue.x < newValue.y // arbitrary rule + } var validatedOptionalValue: List? - by moshiPref>(key = "validatedOptionalValue") - .validate { newValue -> - newValue.isNullOrEmpty().not() // arbitrary rule - } + by moshiPref>(key = "validatedOptionalValue") + .validate { newValue -> + newValue.isNullOrEmpty().not() // arbitrary rule + } } diff --git a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/MoshiInstances.kt b/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/MoshiInstances.kt index 10116da..11f32ed 100644 --- a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/MoshiInstances.kt +++ b/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/MoshiInstances.kt @@ -8,10 +8,10 @@ internal val moshiInstances: MutableMap = mutableMapOf( - override val key: String, - private val adapter: JsonAdapter, + override val key: String, + private val adapter: JsonAdapter, ) : KeyedKrateProperty { override fun getValue(thisRef: Krate, property: KProperty<*>): T? { @@ -37,8 +37,8 @@ private class MoshiDelegate( } internal class MoshiDelegateFactory( - private val key: String, - private val type: Type, + private val key: String, + private val type: Type, ) : KeyedKratePropertyProvider { override fun provideDelegate(thisRef: Krate, property: KProperty<*>): KeyedKrateProperty { diff --git a/krate-moshi-reflect/src/test/java/hu/autsoft/krate/moshi/MoshiTestKrate.kt b/krate-moshi-reflect/src/test/java/hu/autsoft/krate/moshi/MoshiTestKrate.kt index 800ff45..ace2afe 100644 --- a/krate-moshi-reflect/src/test/java/hu/autsoft/krate/moshi/MoshiTestKrate.kt +++ b/krate-moshi-reflect/src/test/java/hu/autsoft/krate/moshi/MoshiTestKrate.kt @@ -21,21 +21,21 @@ internal class MoshiTestKrate(context: Context) : SimpleKrate(context) { var listOfValues: List? by moshiPref("listOfValues") var simpleValueWithDefault: TestModel - by moshiPref("simpleValueWithDefault", DEFAULT_SIMPLE_VALUE) + by moshiPref("simpleValueWithDefault").withDefault(DEFAULT_SIMPLE_VALUE) var listOfValuesWithDefault: List - by moshiPref("listOfValuesWithDefault", defaultValue = DEFAULT_LIST_VALUE) + by moshiPref("listOfValuesWithDefault").withDefault(DEFAULT_LIST_VALUE) var validatedValue: TestModel - by moshiPref(key = "validatedValue", defaultValue = DEFAULT_SIMPLE_VALUE) - .validate { newValue -> - newValue.x < newValue.y // arbitrary rule - } + by moshiPref("validatedValue").withDefault(DEFAULT_SIMPLE_VALUE) + .validate { newValue -> + newValue.x < newValue.y // arbitrary rule + } var validatedOptionalValue: List? - by moshiPref>(key = "validatedOptionalValue") - .validate { newValue -> - newValue.isNullOrEmpty().not() // arbitrary rule - } + by moshiPref>(key = "validatedOptionalValue") + .validate { newValue -> + newValue.isNullOrEmpty().not() // arbitrary rule + } } diff --git a/krate/src/main/kotlin/hu/autsoft/krate/Functions.kt b/krate/src/main/kotlin/hu/autsoft/krate/Functions.kt index 94b727d..c3efbe5 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/Functions.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/Functions.kt @@ -4,7 +4,12 @@ package hu.autsoft.krate import hu.autsoft.krate.base.KeyedKrateProperty import hu.autsoft.krate.default.withDefault -import hu.autsoft.krate.optional.* +import hu.autsoft.krate.optional.BooleanDelegate +import hu.autsoft.krate.optional.FloatDelegate +import hu.autsoft.krate.optional.IntDelegate +import hu.autsoft.krate.optional.LongDelegate +import hu.autsoft.krate.optional.StringDelegate +import hu.autsoft.krate.optional.StringSetDelegate import kotlin.properties.ReadWriteProperty /** diff --git a/krate/src/main/kotlin/hu/autsoft/krate/default/DelegateWithDefault.kt b/krate/src/main/kotlin/hu/autsoft/krate/default/DelegateWithDefault.kt index 66c994d..3fdfa68 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/default/DelegateWithDefault.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/default/DelegateWithDefault.kt @@ -12,8 +12,8 @@ import kotlin.reflect.KProperty @InternalKrateApi public class DelegateWithDefault( - private val delegate: KeyedKrateProperty, - private val default: T, + private val delegate: KeyedKrateProperty, + private val default: T, ) : ReadWriteProperty { override fun setValue(thisRef: Krate, property: KProperty<*>, value: T) { @@ -27,8 +27,8 @@ public class DelegateWithDefault( @InternalKrateApi public class DelegateWithDefaultFactory( - private val propertyDelegateProvider: PropertyDelegateProvider>, - private val default: T, + private val propertyDelegateProvider: PropertyDelegateProvider>, + private val default: T, ) : PropertyDelegateProvider> { override fun provideDelegate(thisRef: Krate, property: KProperty<*>): ReadWriteProperty { @@ -51,7 +51,7 @@ public class DelegateWithDefaultFactory( * ``` */ public fun KeyedKrateProperty.withDefault( - default: T + default: T ): ReadWriteProperty { @OptIn(InternalKrateApi::class) return DelegateWithDefault(this, default) @@ -69,7 +69,7 @@ public fun KeyedKrateProperty.withDefault( * ``` */ public fun KeyedKratePropertyProvider.withDefault( - default: T + default: T ): PropertyDelegateProvider> { @OptIn(InternalKrateApi::class) return DelegateWithDefaultFactory(this, default) diff --git a/krate/src/main/kotlin/hu/autsoft/krate/internal/InternalKrateApi.kt b/krate/src/main/kotlin/hu/autsoft/krate/internal/InternalKrateApi.kt index 2601bbb..213fa5d 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/internal/InternalKrateApi.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/internal/InternalKrateApi.kt @@ -1,8 +1,8 @@ package hu.autsoft.krate.internal @RequiresOptIn( - message = "This API is meant to be used by Krate's own modules, and third-party extensions to Krate. " + - "If you're a client of the Krate library, you probably shouldn't be using it.", - level = RequiresOptIn.Level.ERROR + message = "This API is meant to be used by Krate's own modules, and third-party extensions to Krate. " + + "If you're a client of the Krate library, you probably shouldn't be using it.", + level = RequiresOptIn.Level.ERROR ) public annotation class InternalKrateApi From 5d5e589e24c8a6575e789dc23f1258c1a0d90aae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Braun?= Date: Wed, 17 Nov 2021 19:24:17 +0100 Subject: [PATCH 11/31] Raise deprecations to errors, remove unused impl functions --- .../kotlin/hu/autsoft/krate/gson/Functions.kt | 17 +++-------------- .../hu/autsoft/krate/kotlinx/Functions.kt | 17 +++-------------- .../kotlin/hu/autsoft/krate/moshi/Functions.kt | 17 +++-------------- .../main/kotlin/hu/autsoft/krate/Functions.kt | 10 +++++----- 4 files changed, 14 insertions(+), 47 deletions(-) diff --git a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/Functions.kt b/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/Functions.kt index 09c1aeb..7e30f4d 100644 --- a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/Functions.kt +++ b/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/Functions.kt @@ -5,9 +5,8 @@ package hu.autsoft.krate.gson import com.google.gson.reflect.TypeToken import hu.autsoft.krate.Krate import hu.autsoft.krate.base.KeyedKratePropertyProvider -import hu.autsoft.krate.default.DelegateWithDefaultFactory +import hu.autsoft.krate.default.withDefault import hu.autsoft.krate.gson.optional.GsonDelegateFactory -import hu.autsoft.krate.internal.InternalKrateApi import java.lang.reflect.Type import kotlin.properties.PropertyDelegateProvider import kotlin.properties.ReadWriteProperty @@ -36,7 +35,7 @@ internal fun Krate.gsonPrefImpl( */ @Deprecated( message = "Use .withDefault() on a gsonPref instead", - level = DeprecationLevel.WARNING, + level = DeprecationLevel.ERROR, replaceWith = ReplaceWith( "this.gsonPref(key).withDefault(defaultValue)", imports = arrayOf("hu.autsoft.krate.default.withDefault"), @@ -46,15 +45,5 @@ public inline fun Krate.gsonPref( key: String, defaultValue: T, ): PropertyDelegateProvider> { - return gsonPrefImpl(key, defaultValue, object : TypeToken() {}.type) -} - -@PublishedApi -internal fun Krate.gsonPrefImpl( - key: String, - defaultValue: T, - type: Type, -): PropertyDelegateProvider> { - @OptIn(InternalKrateApi::class) - return DelegateWithDefaultFactory(GsonDelegateFactory(key, type), defaultValue) + return gsonPref(key).withDefault(defaultValue) } diff --git a/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/Functions.kt b/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/Functions.kt index f81b1ed..92b51f3 100644 --- a/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/Functions.kt +++ b/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/Functions.kt @@ -4,8 +4,7 @@ package hu.autsoft.krate.kotlinx import hu.autsoft.krate.Krate import hu.autsoft.krate.base.KeyedKratePropertyProvider -import hu.autsoft.krate.default.DelegateWithDefaultFactory -import hu.autsoft.krate.internal.InternalKrateApi +import hu.autsoft.krate.default.withDefault import hu.autsoft.krate.kotlinx.optional.KotlinxDelegateFactory import kotlin.properties.PropertyDelegateProvider import kotlin.properties.ReadWriteProperty @@ -37,7 +36,7 @@ internal fun Krate.kotlinxPrefImpl( */ @Deprecated( message = "Use .withDefault() on a kotlinxPref instead", - level = DeprecationLevel.WARNING, + level = DeprecationLevel.ERROR, replaceWith = ReplaceWith( "this.kotlinxPref(key).withDefault(defaultValue)", imports = arrayOf("hu.autsoft.krate.default.withDefault"), @@ -48,15 +47,5 @@ public inline fun Krate.kotlinxPref( key: String, defaultValue: T, ): PropertyDelegateProvider> { - return kotlinxPrefImpl(key, defaultValue, typeOf()) -} - -@PublishedApi -internal fun Krate.kotlinxPrefImpl( - key: String, - defaultValue: T, - type: KType, -): PropertyDelegateProvider> { - @OptIn(InternalKrateApi::class) - return DelegateWithDefaultFactory(KotlinxDelegateFactory(key, type), defaultValue) + return kotlinxPref(key).withDefault(defaultValue) } diff --git a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/Functions.kt b/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/Functions.kt index b0bc3f6..4ac9350 100644 --- a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/Functions.kt +++ b/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/Functions.kt @@ -4,8 +4,7 @@ package hu.autsoft.krate.moshi import hu.autsoft.krate.Krate import hu.autsoft.krate.base.KeyedKratePropertyProvider -import hu.autsoft.krate.default.DelegateWithDefaultFactory -import hu.autsoft.krate.internal.InternalKrateApi +import hu.autsoft.krate.default.withDefault import hu.autsoft.krate.moshi.optional.MoshiDelegateFactory import java.lang.reflect.Type import kotlin.properties.PropertyDelegateProvider @@ -38,7 +37,7 @@ internal fun Krate.moshiPrefImpl( */ @Deprecated( message = "Use .withDefault() on a moshiPref instead", - level = DeprecationLevel.WARNING, + level = DeprecationLevel.ERROR, replaceWith = ReplaceWith( "this.moshiPref(key).withDefault(defaultValue)", imports = arrayOf("hu.autsoft.krate.default.withDefault"), @@ -49,15 +48,5 @@ public inline fun Krate.moshiPref( key: String, defaultValue: T, ): PropertyDelegateProvider> { - return moshiPrefImpl(key, defaultValue, typeOf().javaType) -} - -@PublishedApi -internal fun Krate.moshiPrefImpl( - key: String, - defaultValue: T, - type: Type, -): PropertyDelegateProvider> { - @OptIn(InternalKrateApi::class) - return DelegateWithDefaultFactory(MoshiDelegateFactory(key, type), defaultValue) + return moshiPref(key).withDefault(defaultValue) } diff --git a/krate/src/main/kotlin/hu/autsoft/krate/Functions.kt b/krate/src/main/kotlin/hu/autsoft/krate/Functions.kt index c3efbe5..9946742 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/Functions.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/Functions.kt @@ -60,7 +60,7 @@ public fun Krate.stringSetPref(key: String): KeyedKrateProperty?> { */ @Deprecated( message = "Use .withDefault() on a booleanPref instead", - level = DeprecationLevel.WARNING, + level = DeprecationLevel.ERROR, replaceWith = ReplaceWith( "this.booleanPref(key).withDefault(defaultValue)", imports = arrayOf("hu.autsoft.krate.default.withDefault"), @@ -75,7 +75,7 @@ public fun Krate.booleanPref(key: String, defaultValue: Boolean): ReadWritePrope */ @Deprecated( message = "Use .withDefault() on a floatPref instead", - level = DeprecationLevel.WARNING, + level = DeprecationLevel.ERROR, replaceWith = ReplaceWith( "this.floatPref(key).withDefault(defaultValue)", imports = arrayOf("hu.autsoft.krate.default.withDefault"), @@ -105,7 +105,7 @@ public fun Krate.intPref(key: String, defaultValue: Int): ReadWriteProperty Date: Wed, 17 Nov 2021 19:34:18 +0100 Subject: [PATCH 12/31] Fix type inference problems --- .../hu/autsoft/krate/gson/GsonTestKrate.kt | 30 ++++++++------- .../autsoft/krate/kotlinx/KotlinxTestKrate.kt | 37 ++++++++++--------- .../hu/autsoft/krate/moshi/MoshiTestKrate.kt | 18 +++++---- .../hu/autsoft/krate/moshi/MoshiTestKrate.kt | 18 +++++---- 4 files changed, 59 insertions(+), 44 deletions(-) diff --git a/krate-gson/src/test/java/hu/autsoft/krate/gson/GsonTestKrate.kt b/krate-gson/src/test/java/hu/autsoft/krate/gson/GsonTestKrate.kt index e7909fb..b144022 100644 --- a/krate-gson/src/test/java/hu/autsoft/krate/gson/GsonTestKrate.kt +++ b/krate-gson/src/test/java/hu/autsoft/krate/gson/GsonTestKrate.kt @@ -2,6 +2,7 @@ package hu.autsoft.krate.gson import android.content.Context import hu.autsoft.krate.SimpleKrate +import hu.autsoft.krate.default.withDefault import hu.autsoft.krate.validation.validate @@ -20,22 +21,25 @@ internal class GsonTestKrate(context: Context) : SimpleKrate(context) { var listOfValues: List? by gsonPref("listOfValues") - var simpleValueWithDefault: TestModel - by gsonPref("simpleValueWithDefault").withDefault(DEFAULT_SIMPLE_VALUE) + var simpleValueWithDefault + by gsonPref("simpleValueWithDefault") + .withDefault(DEFAULT_SIMPLE_VALUE) - var listOfValuesWithDefault: List - by gsonPref("listOfValuesWithDefault").withDefault(DEFAULT_LIST_VALUE) + var listOfValuesWithDefault + by gsonPref>("listOfValuesWithDefault") + .withDefault(DEFAULT_LIST_VALUE) - var validatedValue: TestModel - by gsonPref("validatedValue").withDefault(DEFAULT_SIMPLE_VALUE) - .validate { newValue -> - newValue.x < newValue.y // arbitrary rule - } + var validatedValue + by gsonPref("validatedValue") + .withDefault(DEFAULT_SIMPLE_VALUE) + .validate { newValue -> + newValue.x < newValue.y // arbitrary rule + } var validatedOptionalValue: List? - by gsonPref>(key = "validatedOptionalValue") - .validate { newValue -> - newValue.isNullOrEmpty().not() - } + by gsonPref>(key = "validatedOptionalValue") + .validate { newValue -> + newValue.isNullOrEmpty().not() + } } diff --git a/krate-kotlinx/src/test/java/hu/autsoft/krate/kotlinx/KotlinxTestKrate.kt b/krate-kotlinx/src/test/java/hu/autsoft/krate/kotlinx/KotlinxTestKrate.kt index 7d8bdea..1c65451 100644 --- a/krate-kotlinx/src/test/java/hu/autsoft/krate/kotlinx/KotlinxTestKrate.kt +++ b/krate-kotlinx/src/test/java/hu/autsoft/krate/kotlinx/KotlinxTestKrate.kt @@ -2,6 +2,7 @@ package hu.autsoft.krate.kotlinx import android.content.Context import hu.autsoft.krate.SimpleKrate +import hu.autsoft.krate.default.withDefault import hu.autsoft.krate.validation.validate @@ -20,22 +21,24 @@ internal class KotlinxTestKrate(context: Context) : SimpleKrate(context) { var listOfValues: List? by kotlinxPref("listOfValues") - var simpleValueWithDefault: TestModel - by kotlinxPref("simpleValueWithDefault").withDefault(DEFAULT_SIMPLE_VALUE) - - var listOfValuesWithDefault: List - by kotlinxPref("listOfValuesWithDefault").withDefault(DEFAULT_LIST_VALUE) - - var validatedValue: TestModel - by kotlinxPref("validatedValue").withDefault(DEFAULT_SIMPLE_VALUE) - .validate { newValue -> - newValue.x < newValue.y // arbitrary rule - } - - var validatedOptionalValue: List? - by kotlinxPref>(key = "validatedOptionalValue") - .validate { newValue -> - newValue.isNullOrEmpty().not() // arbitrary rule - } + var simpleValueWithDefault + by kotlinxPref("simpleValueWithDefault").withDefault(DEFAULT_SIMPLE_VALUE) + + var listOfValuesWithDefault + by kotlinxPref>("listOfValuesWithDefault") + .withDefault(DEFAULT_LIST_VALUE) + + var validatedValue + by kotlinxPref(key = "validatedValue") + .withDefault(DEFAULT_SIMPLE_VALUE) + .validate { newValue -> + newValue.x < newValue.y // arbitrary rule + } + + var validatedOptionalValue + by kotlinxPref>(key = "validatedOptionalValue") + .validate { newValue -> + newValue.isNullOrEmpty().not() // arbitrary rule + } } diff --git a/krate-moshi-codegen/src/test/java/hu/autsoft/krate/moshi/MoshiTestKrate.kt b/krate-moshi-codegen/src/test/java/hu/autsoft/krate/moshi/MoshiTestKrate.kt index ace2afe..58c4a28 100644 --- a/krate-moshi-codegen/src/test/java/hu/autsoft/krate/moshi/MoshiTestKrate.kt +++ b/krate-moshi-codegen/src/test/java/hu/autsoft/krate/moshi/MoshiTestKrate.kt @@ -2,6 +2,7 @@ package hu.autsoft.krate.moshi import android.content.Context import hu.autsoft.krate.SimpleKrate +import hu.autsoft.krate.default.withDefault import hu.autsoft.krate.validation.validate @@ -20,19 +21,22 @@ internal class MoshiTestKrate(context: Context) : SimpleKrate(context) { var listOfValues: List? by moshiPref("listOfValues") - var simpleValueWithDefault: TestModel - by moshiPref("simpleValueWithDefault").withDefault(DEFAULT_SIMPLE_VALUE) + var simpleValueWithDefault + by moshiPref("simpleValueWithDefault") + .withDefault(DEFAULT_SIMPLE_VALUE) - var listOfValuesWithDefault: List - by moshiPref("listOfValuesWithDefault").withDefault(DEFAULT_LIST_VALUE) + var listOfValuesWithDefault + by moshiPref>("listOfValuesWithDefault") + .withDefault(DEFAULT_LIST_VALUE) - var validatedValue: TestModel - by moshiPref("validatedValue").withDefault(DEFAULT_SIMPLE_VALUE) + var validatedValue + by moshiPref("validatedValue") + .withDefault(DEFAULT_SIMPLE_VALUE) .validate { newValue -> newValue.x < newValue.y // arbitrary rule } - var validatedOptionalValue: List? + var validatedOptionalValue by moshiPref>(key = "validatedOptionalValue") .validate { newValue -> newValue.isNullOrEmpty().not() // arbitrary rule diff --git a/krate-moshi-reflect/src/test/java/hu/autsoft/krate/moshi/MoshiTestKrate.kt b/krate-moshi-reflect/src/test/java/hu/autsoft/krate/moshi/MoshiTestKrate.kt index ace2afe..58c4a28 100644 --- a/krate-moshi-reflect/src/test/java/hu/autsoft/krate/moshi/MoshiTestKrate.kt +++ b/krate-moshi-reflect/src/test/java/hu/autsoft/krate/moshi/MoshiTestKrate.kt @@ -2,6 +2,7 @@ package hu.autsoft.krate.moshi import android.content.Context import hu.autsoft.krate.SimpleKrate +import hu.autsoft.krate.default.withDefault import hu.autsoft.krate.validation.validate @@ -20,19 +21,22 @@ internal class MoshiTestKrate(context: Context) : SimpleKrate(context) { var listOfValues: List? by moshiPref("listOfValues") - var simpleValueWithDefault: TestModel - by moshiPref("simpleValueWithDefault").withDefault(DEFAULT_SIMPLE_VALUE) + var simpleValueWithDefault + by moshiPref("simpleValueWithDefault") + .withDefault(DEFAULT_SIMPLE_VALUE) - var listOfValuesWithDefault: List - by moshiPref("listOfValuesWithDefault").withDefault(DEFAULT_LIST_VALUE) + var listOfValuesWithDefault + by moshiPref>("listOfValuesWithDefault") + .withDefault(DEFAULT_LIST_VALUE) - var validatedValue: TestModel - by moshiPref("validatedValue").withDefault(DEFAULT_SIMPLE_VALUE) + var validatedValue + by moshiPref("validatedValue") + .withDefault(DEFAULT_SIMPLE_VALUE) .validate { newValue -> newValue.x < newValue.y // arbitrary rule } - var validatedOptionalValue: List? + var validatedOptionalValue by moshiPref>(key = "validatedOptionalValue") .validate { newValue -> newValue.isNullOrEmpty().not() // arbitrary rule From 1929646572da064c24b3a264129d34271f219101 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Braun?= Date: Wed, 17 Nov 2021 19:42:25 +0100 Subject: [PATCH 13/31] Format primitive delegate constructors --- .../main/kotlin/hu/autsoft/krate/optional/BooleanDelegate.kt | 4 +++- .../main/kotlin/hu/autsoft/krate/optional/FloatDelegate.kt | 4 +++- .../src/main/kotlin/hu/autsoft/krate/optional/IntDelegate.kt | 4 +++- .../src/main/kotlin/hu/autsoft/krate/optional/LongDelegate.kt | 4 +++- .../main/kotlin/hu/autsoft/krate/optional/StringDelegate.kt | 4 +++- .../kotlin/hu/autsoft/krate/optional/StringSetDelegate.kt | 4 +++- 6 files changed, 18 insertions(+), 6 deletions(-) diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/BooleanDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/BooleanDelegate.kt index a378668..51b048e 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/BooleanDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/BooleanDelegate.kt @@ -5,7 +5,9 @@ import hu.autsoft.krate.base.KeyedKrateProperty import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty -internal class BooleanDelegate(override val key: String) : KeyedKrateProperty { +internal class BooleanDelegate( + override val key: String +) : KeyedKrateProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Boolean? { return if (!thisRef.sharedPreferences.contains(key)) { diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/FloatDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/FloatDelegate.kt index d81a8ab..3860643 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/FloatDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/FloatDelegate.kt @@ -5,7 +5,9 @@ import hu.autsoft.krate.base.KeyedKrateProperty import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty -internal class FloatDelegate(override val key: String) : KeyedKrateProperty { +internal class FloatDelegate( + override val key: String +) : KeyedKrateProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Float? { return if (!thisRef.sharedPreferences.contains(key)) { diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/IntDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/IntDelegate.kt index 876a30f..05f6f9a 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/IntDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/IntDelegate.kt @@ -5,7 +5,9 @@ import hu.autsoft.krate.base.KeyedKrateProperty import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty -internal class IntDelegate(override val key: String) : KeyedKrateProperty { +internal class IntDelegate( + override val key: String +) : KeyedKrateProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Int? { return if (!thisRef.sharedPreferences.contains(key)) { diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/LongDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/LongDelegate.kt index abad7b9..29d1b34 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/LongDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/LongDelegate.kt @@ -5,7 +5,9 @@ import hu.autsoft.krate.base.KeyedKrateProperty import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty -internal class LongDelegate(override val key: String) : KeyedKrateProperty { +internal class LongDelegate( + override val key: String +) : KeyedKrateProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Long? { return if (!thisRef.sharedPreferences.contains(key)) { diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/StringDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/StringDelegate.kt index e8b6ff6..e43c4b0 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/StringDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/StringDelegate.kt @@ -5,7 +5,9 @@ import hu.autsoft.krate.base.KeyedKrateProperty import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty -internal class StringDelegate(override val key: String) : KeyedKrateProperty { +internal class StringDelegate( + override val key: String +) : KeyedKrateProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): String? { return if (!thisRef.sharedPreferences.contains(key)) { diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/StringSetDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/StringSetDelegate.kt index 3dab962..c942453 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/StringSetDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/StringSetDelegate.kt @@ -5,7 +5,9 @@ import hu.autsoft.krate.base.KeyedKrateProperty import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty -internal class StringSetDelegate(override val key: String) : KeyedKrateProperty?> { +internal class StringSetDelegate( + override val key: String +) : KeyedKrateProperty?> { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Set? { return if (!thisRef.sharedPreferences.contains(key)) { From f54bf23960b4e47257048650f20572a2ccfb0c8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Braun?= Date: Wed, 17 Nov 2021 19:43:22 +0100 Subject: [PATCH 14/31] Format code again --- .../src/main/kotlin/hu/autsoft/krate/moshi/Functions.kt | 6 +++--- .../kotlin/hu/autsoft/krate/internal/InternalKrateApi.kt | 6 +++--- .../main/kotlin/hu/autsoft/krate/optional/LongDelegate.kt | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/Functions.kt b/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/Functions.kt index 4ac9350..d19a726 100644 --- a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/Functions.kt +++ b/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/Functions.kt @@ -18,15 +18,15 @@ import kotlin.reflect.typeOf */ @OptIn(ExperimentalStdlibApi::class) public inline fun Krate.moshiPref( - key: String, + key: String, ): KeyedKratePropertyProvider { return moshiPrefImpl(key, typeOf().javaType) } @PublishedApi internal fun Krate.moshiPrefImpl( - key: String, - type: Type, + key: String, + type: Type, ): KeyedKratePropertyProvider { return MoshiDelegateFactory(key, type) } diff --git a/krate/src/main/kotlin/hu/autsoft/krate/internal/InternalKrateApi.kt b/krate/src/main/kotlin/hu/autsoft/krate/internal/InternalKrateApi.kt index 213fa5d..152c0de 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/internal/InternalKrateApi.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/internal/InternalKrateApi.kt @@ -1,8 +1,8 @@ package hu.autsoft.krate.internal @RequiresOptIn( - message = "This API is meant to be used by Krate's own modules, and third-party extensions to Krate. " + - "If you're a client of the Krate library, you probably shouldn't be using it.", - level = RequiresOptIn.Level.ERROR + message = "This API is meant to be used by Krate's own modules, and third-party extensions to Krate. " + + "If you're a client of the Krate library, you probably shouldn't be using it.", + level = RequiresOptIn.Level.ERROR ) public annotation class InternalKrateApi diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/LongDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/LongDelegate.kt index 29d1b34..6c5f3ce 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/LongDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/LongDelegate.kt @@ -6,7 +6,7 @@ import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty internal class LongDelegate( - override val key: String + override val key: String ) : KeyedKrateProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Long? { From 1bd2e3d0a9b606f205b671f89c15b58433e7755d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Braun?= Date: Wed, 17 Nov 2021 19:51:06 +0100 Subject: [PATCH 15/31] Add KDocs --- .../main/kotlin/hu/autsoft/krate/base/KeyedKrateProperty.kt | 4 ++++ .../hu/autsoft/krate/base/KeyedKratePropertyProvider.kt | 3 +++ 2 files changed, 7 insertions(+) diff --git a/krate/src/main/kotlin/hu/autsoft/krate/base/KeyedKrateProperty.kt b/krate/src/main/kotlin/hu/autsoft/krate/base/KeyedKrateProperty.kt index c7d8640..1ce2523 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/base/KeyedKrateProperty.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/base/KeyedKrateProperty.kt @@ -3,6 +3,10 @@ package hu.autsoft.krate.base import hu.autsoft.krate.Krate import kotlin.properties.ReadWriteProperty +/** + * A Krate property delegate that holds the [key] to be used + * for storing the value. + */ public interface KeyedKrateProperty : ReadWriteProperty { public val key: String } diff --git a/krate/src/main/kotlin/hu/autsoft/krate/base/KeyedKratePropertyProvider.kt b/krate/src/main/kotlin/hu/autsoft/krate/base/KeyedKratePropertyProvider.kt index b2e79ae..f546e07 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/base/KeyedKratePropertyProvider.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/base/KeyedKratePropertyProvider.kt @@ -3,4 +3,7 @@ package hu.autsoft.krate.base import hu.autsoft.krate.Krate import kotlin.properties.PropertyDelegateProvider +/** + * A [PropertyDelegateProvider] that always provides [KeyedKrateProperty] instances. + */ public interface KeyedKratePropertyProvider : PropertyDelegateProvider> From 7b99aac2155442191de95d1545df9484bd50a1c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Braun?= Date: Wed, 17 Nov 2021 20:07:29 +0100 Subject: [PATCH 16/31] Use non-null keys in KeyedKrateProperty --- .../krate/gson/optional/GsonDelegate.kt | 10 ++-- .../krate/kotlinx/optional/KotlinxDelegate.kt | 10 ++-- .../krate/moshi/optional/MoshiDelegate.kt | 10 ++-- .../main/kotlin/hu/autsoft/krate/Functions.kt | 51 ++++++++++--------- .../autsoft/krate/base/KeyedKrateProperty.kt | 2 +- .../krate/default/DelegateWithDefault.kt | 6 ++- .../autsoft/krate/optional/BooleanDelegate.kt | 19 ++++--- .../autsoft/krate/optional/FloatDelegate.kt | 19 ++++--- .../hu/autsoft/krate/optional/IntDelegate.kt | 19 ++++--- .../hu/autsoft/krate/optional/LongDelegate.kt | 19 ++++--- .../autsoft/krate/optional/StringDelegate.kt | 19 ++++--- .../krate/optional/StringSetDelegate.kt | 19 ++++--- 12 files changed, 125 insertions(+), 78 deletions(-) diff --git a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/optional/GsonDelegate.kt b/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/optional/GsonDelegate.kt index de18e40..f4909ef 100644 --- a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/optional/GsonDelegate.kt +++ b/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/optional/GsonDelegate.kt @@ -11,15 +11,15 @@ import java.lang.reflect.Type import kotlin.reflect.KProperty private class GsonDelegate( - override val key: String?, + override val key: String, private val adapter: TypeAdapter, ) : KeyedKrateProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): T? { - return if (!thisRef.sharedPreferences.contains(key ?: property.name)) { + return if (!thisRef.sharedPreferences.contains(key)) { null } else { - val string = requireNotNull(thisRef.sharedPreferences.getString(key ?: property.name, null)) + val string = requireNotNull(thisRef.sharedPreferences.getString(key, null)) adapter.fromJson(string) } } @@ -27,11 +27,11 @@ private class GsonDelegate( override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: T?) { if (value == null) { thisRef.sharedPreferences.edit { - remove(key ?: property.name) + remove(key) } } else { thisRef.sharedPreferences.edit { - putString(key ?: property.name, adapter.toJson(value)) + putString(key, adapter.toJson(value)) } } } diff --git a/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/optional/KotlinxDelegate.kt b/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/optional/KotlinxDelegate.kt index a0007f2..54a61c4 100644 --- a/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/optional/KotlinxDelegate.kt +++ b/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/optional/KotlinxDelegate.kt @@ -12,15 +12,15 @@ import kotlin.reflect.KType private class KotlinxDelegate( - override val key: String?, + override val key: String, private val serializer: KSerializer, ) : KeyedKrateProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): T? { - return if (!thisRef.sharedPreferences.contains(key ?: property.name)) { + return if (!thisRef.sharedPreferences.contains(key)) { null } else { - val string = requireNotNull(thisRef.sharedPreferences.getString(key ?: property.name, null)) + val string = requireNotNull(thisRef.sharedPreferences.getString(key, null)) thisRef.internalJson.decodeFromString(serializer, string) } } @@ -28,11 +28,11 @@ private class KotlinxDelegate( override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: T?) { if (value == null) { thisRef.sharedPreferences.edit { - remove(key ?: property.name) + remove(key) } } else { thisRef.sharedPreferences.edit { - putString(key ?: property.name, thisRef.internalJson.encodeToString(serializer, value)) + putString(key, thisRef.internalJson.encodeToString(serializer, value)) } } } diff --git a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/optional/MoshiDelegate.kt b/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/optional/MoshiDelegate.kt index 3fc22ca..e8bb5b2 100644 --- a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/optional/MoshiDelegate.kt +++ b/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/optional/MoshiDelegate.kt @@ -10,15 +10,15 @@ import java.lang.reflect.Type import kotlin.reflect.KProperty private class MoshiDelegate( - override val key: String?, + override val key: String, private val adapter: JsonAdapter, ) : KeyedKrateProperty { override fun getValue(thisRef: Krate, property: KProperty<*>): T? { - return if (!thisRef.sharedPreferences.contains(key ?: property.name)) { + return if (!thisRef.sharedPreferences.contains(key)) { null } else { - val string = requireNotNull(thisRef.sharedPreferences.getString(key ?: property.name, null)) + val string = requireNotNull(thisRef.sharedPreferences.getString(key, null)) adapter.fromJson(string) } } @@ -26,11 +26,11 @@ private class MoshiDelegate( override fun setValue(thisRef: Krate, property: KProperty<*>, value: T?) { if (value == null) { thisRef.sharedPreferences.edit { - remove(key ?: property.name) + remove(key) } } else { thisRef.sharedPreferences.edit { - putString(key ?: property.name, adapter.toJson(value)) + putString(key, adapter.toJson(value)) } } } diff --git a/krate/src/main/kotlin/hu/autsoft/krate/Functions.kt b/krate/src/main/kotlin/hu/autsoft/krate/Functions.kt index f295355..6402214 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/Functions.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/Functions.kt @@ -2,62 +2,63 @@ package hu.autsoft.krate -import hu.autsoft.krate.base.KeyedKrateProperty +import hu.autsoft.krate.base.KeyedKratePropertyProvider import hu.autsoft.krate.default.withDefault -import hu.autsoft.krate.optional.BooleanDelegate -import hu.autsoft.krate.optional.FloatDelegate -import hu.autsoft.krate.optional.IntDelegate -import hu.autsoft.krate.optional.LongDelegate -import hu.autsoft.krate.optional.StringDelegate -import hu.autsoft.krate.optional.StringSetDelegate +import hu.autsoft.krate.optional.BooleanDelegateProvider +import hu.autsoft.krate.optional.FloatDelegateProvider +import hu.autsoft.krate.optional.IntDelegateProvider +import hu.autsoft.krate.optional.LongDelegateProvider +import hu.autsoft.krate.optional.StringDelegateProvider +import hu.autsoft.krate.optional.StringSetDelegateProvider +import kotlin.properties.PropertyDelegateProvider import kotlin.properties.ReadWriteProperty /** * Creates an optional preference of type [Boolean] with the given [key] in this [Krate] instance. * If [key] is `null` then the property name will be used as key. */ -public fun Krate.booleanPref(key: String? = null): KeyedKrateProperty { - return BooleanDelegate(key) +public fun Krate.booleanPref(key: String? = null): KeyedKratePropertyProvider { + return BooleanDelegateProvider(key) } /** * Creates an optional preference of type [Float] with the given [key] in this [Krate] instance. * If [key] is `null` then the property name will be used as key. */ -public fun Krate.floatPref(key: String? = null): KeyedKrateProperty { - return FloatDelegate(key) +public fun Krate.floatPref(key: String? = null): KeyedKratePropertyProvider { + return FloatDelegateProvider(key) } /** * Creates an optional preference of type [Int] with the given [key] in this [Krate] instance. * If [key] is `null` then the property name will be used as key. */ -public fun Krate.intPref(key: String? = null): KeyedKrateProperty { - return IntDelegate(key) +public fun Krate.intPref(key: String? = null): KeyedKratePropertyProvider { + return IntDelegateProvider(key) } /** * Creates an optional preference of type [Long] with the given [key] in this [Krate] instance. * If [key] is `null` then the property name will be used as key. */ -public fun Krate.longPref(key: String? = null): KeyedKrateProperty { - return LongDelegate(key) +public fun Krate.longPref(key: String? = null): KeyedKratePropertyProvider { + return LongDelegateProvider(key) } /** * Creates an optional preference of type [String] with the given [key] in this [Krate] instance. * If [key] is `null` then the property name will be used as key. */ -public fun Krate.stringPref(key: String? = null): KeyedKrateProperty { - return StringDelegate(key) +public fun Krate.stringPref(key: String? = null): KeyedKratePropertyProvider { + return StringDelegateProvider(key) } /** * Creates an optional preference of type Set with the given [key] in this [Krate] instance. * If [key] is `null` then the property name will be used as key. */ -public fun Krate.stringSetPref(key: String? = null): KeyedKrateProperty?> { - return StringSetDelegate(key) +public fun Krate.stringSetPref(key: String? = null): KeyedKratePropertyProvider?> { + return StringSetDelegateProvider(key) } @@ -73,7 +74,7 @@ public fun Krate.stringSetPref(key: String? = null): KeyedKrateProperty { +public fun Krate.booleanPref(key: String? = null, defaultValue: Boolean): PropertyDelegateProvider> { return booleanPref(key).withDefault(defaultValue) } @@ -89,7 +90,7 @@ public fun Krate.booleanPref(key: String? = null, defaultValue: Boolean): ReadWr imports = arrayOf("hu.autsoft.krate.default.withDefault"), ), ) -public fun Krate.floatPref(key: String? = null, defaultValue: Float): ReadWriteProperty { +public fun Krate.floatPref(key: String? = null, defaultValue: Float): PropertyDelegateProvider> { return floatPref(key).withDefault(defaultValue) } @@ -105,7 +106,7 @@ public fun Krate.floatPref(key: String? = null, defaultValue: Float): ReadWriteP imports = arrayOf("hu.autsoft.krate.default.withDefault"), ), ) -public fun Krate.intPref(key: String? = null, defaultValue: Int): ReadWriteProperty { +public fun Krate.intPref(key: String? = null, defaultValue: Int): PropertyDelegateProvider> { return intPref(key).withDefault(defaultValue) } @@ -121,7 +122,7 @@ public fun Krate.intPref(key: String? = null, defaultValue: Int): ReadWritePrope imports = arrayOf("hu.autsoft.krate.default.withDefault"), ), ) -public fun Krate.longPref(key: String? = null, defaultValue: Long): ReadWriteProperty { +public fun Krate.longPref(key: String? = null, defaultValue: Long): PropertyDelegateProvider> { return longPref(key).withDefault(defaultValue) } @@ -137,7 +138,7 @@ public fun Krate.longPref(key: String? = null, defaultValue: Long): ReadWritePro imports = arrayOf("hu.autsoft.krate.default.withDefault"), ), ) -public fun Krate.stringPref(key: String? = null, defaultValue: String): ReadWriteProperty { +public fun Krate.stringPref(key: String? = null, defaultValue: String): PropertyDelegateProvider> { return stringPref(key).withDefault(defaultValue) } @@ -153,6 +154,6 @@ public fun Krate.stringPref(key: String? = null, defaultValue: String): ReadWrit imports = arrayOf("hu.autsoft.krate.default.withDefault"), ), ) -public fun Krate.stringSetPref(key: String? = null, defaultValue: Set): ReadWriteProperty> { +public fun Krate.stringSetPref(key: String? = null, defaultValue: Set): PropertyDelegateProvider>> { return stringSetPref(key).withDefault(defaultValue) } diff --git a/krate/src/main/kotlin/hu/autsoft/krate/base/KeyedKrateProperty.kt b/krate/src/main/kotlin/hu/autsoft/krate/base/KeyedKrateProperty.kt index 8ba2f2d..1ce2523 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/base/KeyedKrateProperty.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/base/KeyedKrateProperty.kt @@ -8,5 +8,5 @@ import kotlin.properties.ReadWriteProperty * for storing the value. */ public interface KeyedKrateProperty : ReadWriteProperty { - public val key: String? + public val key: String } diff --git a/krate/src/main/kotlin/hu/autsoft/krate/default/DelegateWithDefault.kt b/krate/src/main/kotlin/hu/autsoft/krate/default/DelegateWithDefault.kt index bfaef84..ddba769 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/default/DelegateWithDefault.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/default/DelegateWithDefault.kt @@ -21,7 +21,11 @@ public class DelegateWithDefault( } override fun getValue(thisRef: Krate, property: KProperty<*>): T { - return if (!thisRef.sharedPreferences.contains(delegate.key ?: property.name)) default else delegate.getValue(thisRef, property)!! + return if (!thisRef.sharedPreferences.contains(delegate.key)) { + default + } else { + delegate.getValue(thisRef, property)!! + } } } diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/BooleanDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/BooleanDelegate.kt index 9c374f3..8867a36 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/BooleanDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/BooleanDelegate.kt @@ -2,27 +2,34 @@ package hu.autsoft.krate.optional import hu.autsoft.krate.Krate import hu.autsoft.krate.base.KeyedKrateProperty +import hu.autsoft.krate.base.KeyedKratePropertyProvider import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty internal class BooleanDelegate( - override val key: String? + override val key: String, ) : KeyedKrateProperty { - override operator fun getValue(thisRef: Krate, property: KProperty<*>): Boolean? { - return if (!thisRef.sharedPreferences.contains(key ?: property.name)) { + return if (!thisRef.sharedPreferences.contains(key)) { null } else { - thisRef.sharedPreferences.getBoolean(key ?: property.name, false) + thisRef.sharedPreferences.getBoolean(key, false) } } override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: Boolean?) { if (value == null) { - thisRef.sharedPreferences.edit { remove(key ?: property.name) } + thisRef.sharedPreferences.edit { remove(key) } } else { - thisRef.sharedPreferences.edit { putBoolean(key ?: property.name, value) } + thisRef.sharedPreferences.edit { putBoolean(key, value) } } } +} +internal class BooleanDelegateProvider( + val key: String?, +) : KeyedKratePropertyProvider { + override fun provideDelegate(thisRef: Krate, property: KProperty<*>): BooleanDelegate { + return BooleanDelegate(key ?: property.name) + } } diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/FloatDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/FloatDelegate.kt index a043db2..3a8ad3e 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/FloatDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/FloatDelegate.kt @@ -2,27 +2,34 @@ package hu.autsoft.krate.optional import hu.autsoft.krate.Krate import hu.autsoft.krate.base.KeyedKrateProperty +import hu.autsoft.krate.base.KeyedKratePropertyProvider import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty internal class FloatDelegate( - override val key: String? + override val key: String, ) : KeyedKrateProperty { - override operator fun getValue(thisRef: Krate, property: KProperty<*>): Float? { - return if (!thisRef.sharedPreferences.contains(key ?: property.name)) { + return if (!thisRef.sharedPreferences.contains(key)) { null } else { - thisRef.sharedPreferences.getFloat(key ?: property.name, 0f) + thisRef.sharedPreferences.getFloat(key, 0f) } } override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: Float?) { if (value == null) { - thisRef.sharedPreferences.edit { remove(key ?: property.name) } + thisRef.sharedPreferences.edit { remove(key) } } else { - thisRef.sharedPreferences.edit { putFloat(key ?: property.name, value) } + thisRef.sharedPreferences.edit { putFloat(key, value) } } } +} +internal class FloatDelegateProvider( + val key: String?, +) : KeyedKratePropertyProvider { + override fun provideDelegate(thisRef: Krate, property: KProperty<*>): FloatDelegate { + return FloatDelegate(key ?: property.name) + } } diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/IntDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/IntDelegate.kt index 5f1526f..d251171 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/IntDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/IntDelegate.kt @@ -2,27 +2,34 @@ package hu.autsoft.krate.optional import hu.autsoft.krate.Krate import hu.autsoft.krate.base.KeyedKrateProperty +import hu.autsoft.krate.base.KeyedKratePropertyProvider import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty internal class IntDelegate( - override val key: String? + override val key: String, ) : KeyedKrateProperty { - override operator fun getValue(thisRef: Krate, property: KProperty<*>): Int? { - return if (!thisRef.sharedPreferences.contains(key ?: property.name)) { + return if (!thisRef.sharedPreferences.contains(key)) { null } else { - thisRef.sharedPreferences.getInt(key ?: property.name, 0) + thisRef.sharedPreferences.getInt(key, 0) } } override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: Int?) { if (value == null) { - thisRef.sharedPreferences.edit { remove(key ?: property.name) } + thisRef.sharedPreferences.edit { remove(key) } } else { - thisRef.sharedPreferences.edit { putInt(key ?: property.name, value) } + thisRef.sharedPreferences.edit { putInt(key, value) } } } +} +internal class IntDelegateProvider( + val key: String?, +) : KeyedKratePropertyProvider { + override fun provideDelegate(thisRef: Krate, property: KProperty<*>): IntDelegate { + return IntDelegate(key ?: property.name) + } } diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/LongDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/LongDelegate.kt index d5f4ccf..00eb34a 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/LongDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/LongDelegate.kt @@ -2,27 +2,34 @@ package hu.autsoft.krate.optional import hu.autsoft.krate.Krate import hu.autsoft.krate.base.KeyedKrateProperty +import hu.autsoft.krate.base.KeyedKratePropertyProvider import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty internal class LongDelegate( - override val key: String?, + override val key: String, ) : KeyedKrateProperty { - override operator fun getValue(thisRef: Krate, property: KProperty<*>): Long? { - return if (!thisRef.sharedPreferences.contains(key ?: property.name)) { + return if (!thisRef.sharedPreferences.contains(key)) { null } else { - thisRef.sharedPreferences.getLong(key ?: property.name, 0L) + thisRef.sharedPreferences.getLong(key, 0L) } } override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: Long?) { if (value == null) { - thisRef.sharedPreferences.edit { remove(key ?: property.name) } + thisRef.sharedPreferences.edit { remove(key) } } else { - thisRef.sharedPreferences.edit { putLong(key ?: property.name, value) } + thisRef.sharedPreferences.edit { putLong(key, value) } } } +} +internal class LongDelegateProvider( + val key: String?, +) : KeyedKratePropertyProvider { + override fun provideDelegate(thisRef: Krate, property: KProperty<*>): LongDelegate { + return LongDelegate(key ?: property.name) + } } diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/StringDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/StringDelegate.kt index b74da01..b8ce783 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/StringDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/StringDelegate.kt @@ -2,27 +2,34 @@ package hu.autsoft.krate.optional import hu.autsoft.krate.Krate import hu.autsoft.krate.base.KeyedKrateProperty +import hu.autsoft.krate.base.KeyedKratePropertyProvider import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty internal class StringDelegate( - override val key: String?, + override val key: String, ) : KeyedKrateProperty { - override operator fun getValue(thisRef: Krate, property: KProperty<*>): String? { - return if (!thisRef.sharedPreferences.contains(key ?: property.name)) { + return if (!thisRef.sharedPreferences.contains(key)) { null } else { - thisRef.sharedPreferences.getString(key ?: property.name, null) + thisRef.sharedPreferences.getString(key, null) } } override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: String?) { if (value == null) { - thisRef.sharedPreferences.edit { remove(key ?: property.name) } + thisRef.sharedPreferences.edit { remove(key) } } else { - thisRef.sharedPreferences.edit { putString(key ?: property.name, value) } + thisRef.sharedPreferences.edit { putString(key, value) } } } +} +internal class StringDelegateProvider( + val key: String?, +) : KeyedKratePropertyProvider { + override fun provideDelegate(thisRef: Krate, property: KProperty<*>): StringDelegate { + return StringDelegate(key ?: property.name) + } } diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/StringSetDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/StringSetDelegate.kt index 6d736c1..da753c1 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/StringSetDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/StringSetDelegate.kt @@ -2,27 +2,34 @@ package hu.autsoft.krate.optional import hu.autsoft.krate.Krate import hu.autsoft.krate.base.KeyedKrateProperty +import hu.autsoft.krate.base.KeyedKratePropertyProvider import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty internal class StringSetDelegate( - override val key: String?, + override val key: String, ) : KeyedKrateProperty?> { - override operator fun getValue(thisRef: Krate, property: KProperty<*>): Set? { - return if (!thisRef.sharedPreferences.contains(key ?: property.name)) { + return if (!thisRef.sharedPreferences.contains(key)) { null } else { - thisRef.sharedPreferences.getStringSet(key ?: property.name, emptySet()) + thisRef.sharedPreferences.getStringSet(key, emptySet()) } } override operator fun setValue(thisRef: Krate, property: KProperty<*>, value: Set?) { if (value == null) { - thisRef.sharedPreferences.edit { remove(key ?: property.name) } + thisRef.sharedPreferences.edit { remove(key) } } else { - thisRef.sharedPreferences.edit { putStringSet(key ?: property.name, value) } + thisRef.sharedPreferences.edit { putStringSet(key, value) } } } +} +internal class StringSetDelegateProvider( + val key: String?, +) : KeyedKratePropertyProvider?> { + override fun provideDelegate(thisRef: Krate, property: KProperty<*>): StringSetDelegate { + return StringSetDelegate(key ?: property.name) + } } From 3b545939f631da60be1165a3533899508aa63008 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Braun?= Date: Wed, 17 Nov 2021 20:14:21 +0100 Subject: [PATCH 17/31] Rename default param to defaultValue --- .../krate/default/DelegateWithDefault.kt | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/krate/src/main/kotlin/hu/autsoft/krate/default/DelegateWithDefault.kt b/krate/src/main/kotlin/hu/autsoft/krate/default/DelegateWithDefault.kt index 3fdfa68..9740b56 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/default/DelegateWithDefault.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/default/DelegateWithDefault.kt @@ -13,7 +13,7 @@ import kotlin.reflect.KProperty @InternalKrateApi public class DelegateWithDefault( private val delegate: KeyedKrateProperty, - private val default: T, + private val defaultValue: T, ) : ReadWriteProperty { override fun setValue(thisRef: Krate, property: KProperty<*>, value: T) { @@ -21,20 +21,20 @@ public class DelegateWithDefault( } override fun getValue(thisRef: Krate, property: KProperty<*>): T { - return if (!thisRef.sharedPreferences.contains(delegate.key)) default else delegate.getValue(thisRef, property)!! + return if (!thisRef.sharedPreferences.contains(delegate.key)) defaultValue else delegate.getValue(thisRef, property)!! } } @InternalKrateApi public class DelegateWithDefaultFactory( private val propertyDelegateProvider: PropertyDelegateProvider>, - private val default: T, + private val defaultValue: T, ) : PropertyDelegateProvider> { override fun provideDelegate(thisRef: Krate, property: KProperty<*>): ReadWriteProperty { return DelegateWithDefault( delegate = propertyDelegateProvider.provideDelegate(thisRef, property), - default = default + defaultValue = defaultValue ) } } @@ -42,7 +42,7 @@ public class DelegateWithDefaultFactory( /** * Adds a default value to a Krate delegate. * - * If a delegate returns with null because it have not been set before, then this returns with [default] + * If a delegate returns with null because it have not been set before, then this returns with [defaultValue] * * Example property using this function: * @@ -51,16 +51,16 @@ public class DelegateWithDefaultFactory( * ``` */ public fun KeyedKrateProperty.withDefault( - default: T + defaultValue: T ): ReadWriteProperty { @OptIn(InternalKrateApi::class) - return DelegateWithDefault(this, default) + return DelegateWithDefault(this, defaultValue) } /** * Adds a default value to a Krate delegate factory. * - * If a delegate returns with null because it have not been set before, then this returns with [default] + * If a delegate returns with null because it have not been set before, then this returns with [defaultValue] * * Example property using this function: * @@ -69,8 +69,8 @@ public fun KeyedKrateProperty.withDefault( * ``` */ public fun KeyedKratePropertyProvider.withDefault( - default: T + defaultValue: T ): PropertyDelegateProvider> { @OptIn(InternalKrateApi::class) - return DelegateWithDefaultFactory(this, default) + return DelegateWithDefaultFactory(this, defaultValue) } From f9d62d9b793c2bced107f0e9908972377e3d7e3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Braun?= Date: Thu, 18 Nov 2021 16:13:41 +0100 Subject: [PATCH 18/31] Format and organize code --- .../hu/autsoft/krate/gson/optional/GsonDelegate.kt | 2 +- .../autsoft/krate/kotlinx/optional/KotlinxDelegate.kt | 2 +- .../kotlin/hu/autsoft/krate/moshi/MoshiInstances.kt | 8 ++++---- .../hu/autsoft/krate/moshi/optional/MoshiDelegate.kt | 2 +- .../hu/autsoft/krate/default/DelegateWithDefault.kt | 11 +++++++---- 5 files changed, 14 insertions(+), 11 deletions(-) diff --git a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/optional/GsonDelegate.kt b/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/optional/GsonDelegate.kt index f70e21c..b242282 100644 --- a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/optional/GsonDelegate.kt +++ b/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/optional/GsonDelegate.kt @@ -16,7 +16,7 @@ private class GsonDelegate( ) : KeyedKrateProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): T? { - return if (!thisRef.sharedPreferences.contains(key)) { + return if (key !in thisRef.sharedPreferences) { null } else { val string = requireNotNull(thisRef.sharedPreferences.getString(key, null)) diff --git a/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/optional/KotlinxDelegate.kt b/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/optional/KotlinxDelegate.kt index 6ac50f4..e44a8a9 100644 --- a/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/optional/KotlinxDelegate.kt +++ b/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/optional/KotlinxDelegate.kt @@ -17,7 +17,7 @@ private class KotlinxDelegate( ) : KeyedKrateProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): T? { - return if (!thisRef.sharedPreferences.contains(key)) { + return if (key !in thisRef.sharedPreferences) { null } else { val string = requireNotNull(thisRef.sharedPreferences.getString(key, null)) diff --git a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/MoshiInstances.kt b/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/MoshiInstances.kt index 11f32ed..d278d0c 100644 --- a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/MoshiInstances.kt +++ b/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/MoshiInstances.kt @@ -8,10 +8,10 @@ internal val moshiInstances: MutableMap = mutableMapOf( ) : KeyedKrateProperty { override fun getValue(thisRef: Krate, property: KProperty<*>): T? { - return if (!thisRef.sharedPreferences.contains(key)) { + return if (key !in thisRef.sharedPreferences) { null } else { val string = requireNotNull(thisRef.sharedPreferences.getString(key, null)) diff --git a/krate/src/main/kotlin/hu/autsoft/krate/default/DelegateWithDefault.kt b/krate/src/main/kotlin/hu/autsoft/krate/default/DelegateWithDefault.kt index 9740b56..3e0c225 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/default/DelegateWithDefault.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/default/DelegateWithDefault.kt @@ -15,14 +15,17 @@ public class DelegateWithDefault( private val delegate: KeyedKrateProperty, private val defaultValue: T, ) : ReadWriteProperty { + override fun getValue(thisRef: Krate, property: KProperty<*>): T { + return if (delegate.key !in thisRef.sharedPreferences) { + defaultValue + } else { + delegate.getValue(thisRef, property)!! + } + } override fun setValue(thisRef: Krate, property: KProperty<*>, value: T) { delegate.setValue(thisRef, property, value) } - - override fun getValue(thisRef: Krate, property: KProperty<*>): T { - return if (!thisRef.sharedPreferences.contains(delegate.key)) defaultValue else delegate.getValue(thisRef, property)!! - } } @InternalKrateApi From d1c8d4195aab547f6ab360c302306d7e20dc250b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Braun?= Date: Thu, 18 Nov 2021 16:18:50 +0100 Subject: [PATCH 19/31] Update replacements --- krate-gson/src/main/kotlin/hu/autsoft/krate/gson/Functions.kt | 2 +- .../src/main/kotlin/hu/autsoft/krate/kotlinx/Functions.kt | 2 +- .../src/main/kotlin/hu/autsoft/krate/moshi/Functions.kt | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/Functions.kt b/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/Functions.kt index 7e30f4d..957b3d4 100644 --- a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/Functions.kt +++ b/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/Functions.kt @@ -37,7 +37,7 @@ internal fun Krate.gsonPrefImpl( message = "Use .withDefault() on a gsonPref instead", level = DeprecationLevel.ERROR, replaceWith = ReplaceWith( - "this.gsonPref(key).withDefault(defaultValue)", + "this.gsonPref(key).withDefault(defaultValue)", imports = arrayOf("hu.autsoft.krate.default.withDefault"), ), ) diff --git a/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/Functions.kt b/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/Functions.kt index 92b51f3..9fb49f3 100644 --- a/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/Functions.kt +++ b/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/Functions.kt @@ -38,7 +38,7 @@ internal fun Krate.kotlinxPrefImpl( message = "Use .withDefault() on a kotlinxPref instead", level = DeprecationLevel.ERROR, replaceWith = ReplaceWith( - "this.kotlinxPref(key).withDefault(defaultValue)", + "this.kotlinxPref(key).withDefault(defaultValue)", imports = arrayOf("hu.autsoft.krate.default.withDefault"), ), ) diff --git a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/Functions.kt b/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/Functions.kt index d19a726..7d23901 100644 --- a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/Functions.kt +++ b/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/Functions.kt @@ -39,7 +39,7 @@ internal fun Krate.moshiPrefImpl( message = "Use .withDefault() on a moshiPref instead", level = DeprecationLevel.ERROR, replaceWith = ReplaceWith( - "this.moshiPref(key).withDefault(defaultValue)", + "this.moshiPref(key).withDefault(defaultValue)", imports = arrayOf("hu.autsoft.krate.default.withDefault"), ), ) From c72dcfb5fb6846a9a240acbdc88d6e849ffd8e2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Braun?= Date: Thu, 18 Nov 2021 16:25:51 +0100 Subject: [PATCH 20/31] Improve kdocs --- .../krate/default/DelegateWithDefault.kt | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/krate/src/main/kotlin/hu/autsoft/krate/default/DelegateWithDefault.kt b/krate/src/main/kotlin/hu/autsoft/krate/default/DelegateWithDefault.kt index 3e0c225..bdc26f8 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/default/DelegateWithDefault.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/default/DelegateWithDefault.kt @@ -10,6 +10,11 @@ import kotlin.properties.PropertyDelegateProvider import kotlin.properties.ReadWriteProperty import kotlin.reflect.KProperty +/** + * Wraps [delegate] and returns [defaultValue] when the underlying delegate's + * value is accessed, but the SharedPreferences instead does not contain the + * associated key. + */ @InternalKrateApi public class DelegateWithDefault( private val delegate: KeyedKrateProperty, @@ -28,6 +33,9 @@ public class DelegateWithDefault( } } +/** + * Wraps the delegate provided by [propertyDelegateProvider] into a [DelegateWithDefault]. + */ @InternalKrateApi public class DelegateWithDefaultFactory( private val propertyDelegateProvider: PropertyDelegateProvider>, @@ -43,14 +51,13 @@ public class DelegateWithDefaultFactory( } /** - * Adds a default value to a Krate delegate. - * - * If a delegate returns with null because it have not been set before, then this returns with [defaultValue] + * Adds a default value to a Krate delegate: if a Krate property's value + * not been set before, [defaultValue] is returned instead of `null`. * * Example property using this function: * * ```kotlin - * var defaultString by stringPref("validatedString").withDefault("default") + * var username by stringPref("username").withDefault("Default User") * ``` */ public fun KeyedKrateProperty.withDefault( @@ -61,14 +68,13 @@ public fun KeyedKrateProperty.withDefault( } /** - * Adds a default value to a Krate delegate factory. - * - * If a delegate returns with null because it have not been set before, then this returns with [defaultValue] + * Adds a default value to a Krate delegate: if a Krate property's value + * not been set before, [defaultValue] is returned instead of `null`. * * Example property using this function: * * ```kotlin - * var defaultModel by kotlinxPref("defaultModel").withDefault(DefaultModel()) + * var user by moshiPref("user").withDefault(User("Guest")) * ``` */ public fun KeyedKratePropertyProvider.withDefault( From f807806a374f88e4d1e9a0d58e63440739ddc7d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Braun?= Date: Thu, 18 Nov 2021 16:36:09 +0100 Subject: [PATCH 21/31] Clean app sample app changes --- app/build.gradle | 2 +- .../hu/autsoft/krateexample/ExampleActivity.kt | 6 +++--- .../krateexample/krates/ExampleCustomKrate.kt | 14 ++++++++++---- .../krateexample/krates/ExampleSimpleKrate.kt | 14 ++++++++++---- .../java/hu/autsoft/krateexample/models/User.kt | 2 +- app/src/main/res/layout/activity_example.xml | 16 ++++++++-------- 6 files changed, 33 insertions(+), 21 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index d611400..021c5e9 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -39,7 +39,7 @@ dependencies { implementation 'androidx.constraintlayout:constraintlayout:2.1.0' // KotlinX - implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.0-RC' + implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.1' // Moshi def moshiVersion = "1.12.0" diff --git a/app/src/main/java/hu/autsoft/krateexample/ExampleActivity.kt b/app/src/main/java/hu/autsoft/krateexample/ExampleActivity.kt index 4a774c5..cde0b88 100644 --- a/app/src/main/java/hu/autsoft/krateexample/ExampleActivity.kt +++ b/app/src/main/java/hu/autsoft/krateexample/ExampleActivity.kt @@ -68,15 +68,15 @@ class ExampleActivity : AppCompatActivity() { binding.stringSetPreferenceInput.text.toString().split(",").map(String::trim).toSet() exampleSettings.exampleUserGson = User( binding.gsonPreferenceFirstInput.text.toString(), - binding.gsonPreferenceLastInput.text.toString() + binding.gsonPreferenceLastInput.text.toString(), ) exampleSettings.exampleUserKotlinX = User( binding.kotlinxPreferenceFirstInput.text.toString(), - binding.kotlinxPreferenceLastInput.text.toString() + binding.kotlinxPreferenceLastInput.text.toString(), ) exampleSettings.exampleUserMoshi = User( binding.moshiPreferenceFirstInput.text.toString(), - binding.moshiPreferenceLastInput.text.toString() + binding.moshiPreferenceLastInput.text.toString(), ) } diff --git a/app/src/main/java/hu/autsoft/krateexample/krates/ExampleCustomKrate.kt b/app/src/main/java/hu/autsoft/krateexample/krates/ExampleCustomKrate.kt index cb5700a..a5b4510 100644 --- a/app/src/main/java/hu/autsoft/krateexample/krates/ExampleCustomKrate.kt +++ b/app/src/main/java/hu/autsoft/krateexample/krates/ExampleCustomKrate.kt @@ -4,11 +4,17 @@ package hu.autsoft.krateexample.krates import android.content.Context import android.content.SharedPreferences -import hu.autsoft.krate.* +import hu.autsoft.krate.Krate +import hu.autsoft.krate.booleanPref import hu.autsoft.krate.default.withDefault +import hu.autsoft.krate.floatPref import hu.autsoft.krate.gson.gsonPref +import hu.autsoft.krate.intPref import hu.autsoft.krate.kotlinx.kotlinxPref +import hu.autsoft.krate.longPref import hu.autsoft.krate.moshi.moshiPref +import hu.autsoft.krate.stringPref +import hu.autsoft.krate.stringSetPref import hu.autsoft.krateexample.models.User class ExampleCustomKrate(context: Context) : Krate, ExampleSettings { @@ -25,8 +31,8 @@ class ExampleCustomKrate(context: Context) : Krate, ExampleSettings { override var exampleLong by longPref("exampleLong").withDefault(0L) override var exampleString by stringPref("exampleString").withDefault("") override var exampleStringSet by stringSetPref("exampleStringSet").withDefault(setOf()) - override var exampleUserGson by gsonPref("user_gson").withDefault(User("Gson", "Smith")) - override var exampleUserKotlinX by kotlinxPref("user_kotlinx").withDefault(User("KotlinX", "Smith")) - override var exampleUserMoshi by moshiPref("user_moshi").withDefault(User("Moshi", "Smith")) + override var exampleUserGson by gsonPref("user_gson").withDefault(User("Gson", "Green")) + override var exampleUserKotlinX by kotlinxPref("user_kotlinx").withDefault(User("KotlinX", "Klark")) + override var exampleUserMoshi by moshiPref("user_moshi").withDefault(User("Moshi", "Miller")) } diff --git a/app/src/main/java/hu/autsoft/krateexample/krates/ExampleSimpleKrate.kt b/app/src/main/java/hu/autsoft/krateexample/krates/ExampleSimpleKrate.kt index cee1fcb..0323c7d 100644 --- a/app/src/main/java/hu/autsoft/krateexample/krates/ExampleSimpleKrate.kt +++ b/app/src/main/java/hu/autsoft/krateexample/krates/ExampleSimpleKrate.kt @@ -1,11 +1,17 @@ package hu.autsoft.krateexample.krates import android.content.Context -import hu.autsoft.krate.* +import hu.autsoft.krate.SimpleKrate +import hu.autsoft.krate.booleanPref import hu.autsoft.krate.default.withDefault +import hu.autsoft.krate.floatPref import hu.autsoft.krate.gson.gsonPref +import hu.autsoft.krate.intPref import hu.autsoft.krate.kotlinx.kotlinxPref +import hu.autsoft.krate.longPref import hu.autsoft.krate.moshi.moshiPref +import hu.autsoft.krate.stringPref +import hu.autsoft.krate.stringSetPref import hu.autsoft.krateexample.models.User class ExampleSimpleKrate(context: Context) : SimpleKrate(context, NAME), ExampleSettings { @@ -20,7 +26,7 @@ class ExampleSimpleKrate(context: Context) : SimpleKrate(context, NAME), Example override var exampleLong by longPref("exampleLong").withDefault(0L) override var exampleString by stringPref("exampleString").withDefault("") override var exampleStringSet by stringSetPref("exampleStringSet").withDefault(setOf()) - override var exampleUserGson by gsonPref("user_gson").withDefault(User("Gson", "Smith")) - override var exampleUserKotlinX by kotlinxPref("user_kotlinx").withDefault(User("KotlinX", "Smith")) - override var exampleUserMoshi by moshiPref("user_moshi").withDefault(User("Moshi", "Smith")) + override var exampleUserGson by gsonPref("user_gson").withDefault(User("Gson", "Green")) + override var exampleUserKotlinX by kotlinxPref("user_kotlinx").withDefault(User("KotlinX", "Klark")) + override var exampleUserMoshi by moshiPref("user_moshi").withDefault(User("Moshi", "Miller")) } diff --git a/app/src/main/java/hu/autsoft/krateexample/models/User.kt b/app/src/main/java/hu/autsoft/krateexample/models/User.kt index ede4265..e2497b7 100644 --- a/app/src/main/java/hu/autsoft/krateexample/models/User.kt +++ b/app/src/main/java/hu/autsoft/krateexample/models/User.kt @@ -7,5 +7,5 @@ import kotlinx.serialization.Serializable @Serializable data class User( val firstName: String, - val lastName: String + val lastName: String, ) diff --git a/app/src/main/res/layout/activity_example.xml b/app/src/main/res/layout/activity_example.xml index 57b6bbb..ad79c41 100644 --- a/app/src/main/res/layout/activity_example.xml +++ b/app/src/main/res/layout/activity_example.xml @@ -129,7 +129,7 @@ android:id="@+id/gsonPreferenceFirstInput" android:layout_width="match_parent" android:layout_height="wrap_content" - android:hint="First Gson" + android:hint="First Name" android:inputType="text" /> @@ -148,7 +148,7 @@ android:id="@+id/gsonPreferenceLastInput" android:layout_width="match_parent" android:layout_height="wrap_content" - android:hint="Last Gson" + android:hint="Last Name" android:inputType="text" /> @@ -168,7 +168,7 @@ android:id="@+id/kotlinxPreferenceFirstInput" android:layout_width="match_parent" android:layout_height="wrap_content" - android:hint="First KotlinX" + android:hint="First Name" android:inputType="text" /> @@ -187,7 +187,7 @@ android:id="@+id/kotlinxPreferenceLastInput" android:layout_width="match_parent" android:layout_height="wrap_content" - android:hint="Last KotlinX" + android:hint="Last Name" android:inputType="text" /> @@ -197,7 +197,7 @@ android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="8dp" - app:layout_constraintEnd_toStartOf="@+id/gsonPreferenceLast" + app:layout_constraintEnd_toStartOf="@+id/moshiPreferenceLast" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintHorizontal_chainStyle="spread" app:layout_constraintStart_toStartOf="parent" @@ -207,7 +207,7 @@ android:id="@+id/moshiPreferenceFirstInput" android:layout_width="match_parent" android:layout_height="wrap_content" - android:hint="First Moshi" + android:hint="First Name" android:inputType="text" /> @@ -219,14 +219,14 @@ android:layout_margin="8dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" - app:layout_constraintStart_toEndOf="@+id/gsonPreferenceFirst" + app:layout_constraintStart_toEndOf="@+id/moshiPreferenceFirst" app:layout_constraintTop_toBottomOf="@+id/kotlinxPreferenceLast"> From c6fdf1376c2c3c41cf372f1dd51724003d17ef4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Braun?= Date: Thu, 18 Nov 2021 16:53:53 +0100 Subject: [PATCH 22/31] Rewrite README sections around basic usage --- README.md | 50 ++++++++++++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index d045a83..f947aba 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ ![Krate banner](./docs/krate.png) -_Krate_ is a `SharedPreferences` wrapper library that uses delegated properties for convenient access to `SharedPreferences` values. +**Krate** is a `SharedPreferences` wrapper library that uses delegated properties for convenient access to `SharedPreferences` values. Here's what its basic usage looks like, extending the provided `SimpleKrate` class: @@ -13,7 +13,7 @@ class UserSettings(context: Context) : SimpleKrate(context) { var notificationsEnabled by booleanPref("notifications_enabled").withDefault(false) var loginCount by intPref("login_count").withDefault(0) - var nickname by stringPref("nickname").withDefault("") + var nickname by stringPref("nickname") } @@ -24,34 +24,44 @@ Log.d("LOGIN_COUNT", "Count: ${settings.loginCount}") # Dependency -You can include _Krate_ in your project from the `mavenCentral()` repository, like so: +Krate is available from `mavenCentral()`. You can add it to your dependencies with the following: ```groovy implementation 'hu.autsoft:krate:1.2.0' ``` -# Optionals vs defaults +# Basics -Each stored property can be declared with or without a default value. Here's how the two options differ: - -### Optional values: - -A property declared with the one-argument delegate function will have a nullable type. It will have a `null` value if no value has been set for this property yet, and its current value can be erased from `SharedPreferences` completely by setting it to `null`. +A Krate property is nullable by default. It will have a `null` value if no value has been set for the property yet, and its current value can be erased from `SharedPreferences` completely by setting it to `null`. ```kotlin var username: String? by stringPref("username") ``` -### Default values: +### Default values -An extension function declared takes the default value as argument, and it will have a non-nullable type. Reading from this property will -return either the value it was last set to or the default value. Setting this property will update the value stored in `SharedPreferences`. -Note that there's no way to remove these values from `SharedPreferences` (although you could set it explicitly to the default value). +You can provide a default value for the property by chaining a `withDefault` call on the delegate function. This will give the property a non-nullable type. ```kotlin var username: String by stringPref("username").withDefault("admin") ``` +Reading from this property will return either the value it was last set to, or the default value if it's never been set. + +> Note that there's no way to remove these values from `SharedPreferences` (although you could set it explicitly to the default value). + +### Validation + +You can add validation rules to your Krate properties by calling `validate` on any of Krate's delegate functions: + +```kotlin +var percentage: Int by intPref("percentage") + .withDefault(0) + .validate { it in 0..100 } +``` + +If this validation fails, an `IllegalArgumentException` will be thrown. + # Custom Krate implementations You can usually get away with extending `SimpleKrate`, as it does allow you to pass in a custom name for the `SharedPreferences` to be used to store your values in its constructor as an optional parameter. (If you pass in no `name` parameter to its constructor, it will default to using the instance returned by `PreferenceManager.getDefaultSharedPreferences(context)`.) @@ -105,19 +115,7 @@ class EncryptedKrate(applicationContext: Context) : Krate { } ``` -# Validation - -You can add validation rules to your Krate properties by calling `validate` on any of Krate's delegate functions: - -```kotlin -var percentage: Int by intPref("percentage") - .withDefault(0) - .validate { it in 0..100 } -``` - -If this validation fails, an `IllegalArgumentException` will be thrown. - -# Addons +# Serialization addons Krate, by default, supports the types that `SharedPreferences` supports. These are `Boolean`, `Float`, `Int`, `Long`, `String` and `Set`. You may of course want to store additional types in Krate. From b9746600d00c668fe47fdd5f2716ca6f3f4f0968 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Braun?= Date: Thu, 18 Nov 2021 17:08:46 +0100 Subject: [PATCH 23/31] Update Kdocs --- .../kotlin/hu/autsoft/krate/gson/Functions.kt | 6 +++-- .../hu/autsoft/krate/kotlinx/Functions.kt | 8 ++++--- .../hu/autsoft/krate/moshi/Functions.kt | 8 ++++--- .../main/kotlin/hu/autsoft/krate/Functions.kt | 24 +++++++++---------- 4 files changed, 26 insertions(+), 20 deletions(-) diff --git a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/Functions.kt b/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/Functions.kt index a740367..c06536d 100644 --- a/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/Functions.kt +++ b/krate-gson/src/main/kotlin/hu/autsoft/krate/gson/Functions.kt @@ -13,7 +13,8 @@ import kotlin.properties.ReadWriteProperty /** * Creates an optional preference of type T with the given [key] in this [Krate] instance. - * If [key] is `null` then the property name will be used as key. + * If no [key] is provided, the property's name will be used as the key. + * * This instance will be serialized using Gson. */ public inline fun Krate.gsonPref( @@ -32,7 +33,8 @@ internal fun Krate.gsonPrefImpl( /** * Creates a non-optional preference of type T with the given [key] and [defaultValue] in this [Krate] instance. - * If [key] is `null` then the property name will be used as key. + * If no [key] is provided, the property's name will be used as the key. + * * This instance will be serialized using Gson. */ @Deprecated( diff --git a/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/Functions.kt b/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/Functions.kt index c66f4a8..a106f87 100644 --- a/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/Functions.kt +++ b/krate-kotlinx/src/main/kotlin/hu/autsoft/krate/kotlinx/Functions.kt @@ -13,7 +13,8 @@ import kotlin.reflect.typeOf /** * Creates an optional preference of type T with the given [key] in this [Krate] instance. - * If [key] is `null` then the property name will be used as key. + * If no [key] is provided, the property's name will be used as the key. + * * This instance will be serialized using kotlinx.serialization. */ @OptIn(ExperimentalStdlibApi::class) @@ -25,7 +26,7 @@ public inline fun Krate.kotlinxPref( @PublishedApi internal fun Krate.kotlinxPrefImpl( - key: String? = null, + key: String?, type: KType, ): KeyedKratePropertyProvider { return KotlinxDelegateFactory(key, type) @@ -33,7 +34,8 @@ internal fun Krate.kotlinxPrefImpl( /** * Creates a non-optional preference of type T with the given [key] and [defaultValue] in this [Krate] instance. - * If [key] is `null` then the property name will be used as key. + * If no [key] is provided, the property's name will be used as the key. + * * This instance will be serialized using kotlinx.serialization. */ @Deprecated( diff --git a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/Functions.kt b/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/Functions.kt index 7255c30..2757d3f 100644 --- a/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/Functions.kt +++ b/krate-moshi-core/src/main/kotlin/hu/autsoft/krate/moshi/Functions.kt @@ -14,7 +14,8 @@ import kotlin.reflect.typeOf /** * Creates an optional preference of type T with the given [key] in this [Krate] instance. - * If [key] is `null` then the property name will be used as key. + * If no [key] is provided, the property's name will be used as the key. + * * This instance will be serialized using Moshi. */ @OptIn(ExperimentalStdlibApi::class) @@ -26,7 +27,7 @@ public inline fun Krate.moshiPref( @PublishedApi internal fun Krate.moshiPrefImpl( - key: String? = null, + key: String?, type: Type, ): KeyedKratePropertyProvider { return MoshiDelegateFactory(key, type) @@ -34,7 +35,8 @@ internal fun Krate.moshiPrefImpl( /** * Creates a non-optional preference of type T with the given [key] and [defaultValue] in this [Krate] instance. - * If [key] is `null` then the property name will be used as key. + * If no [key] is provided, the property's name will be used as the key. + * * This instance will be serialized using Moshi. */ @Deprecated( diff --git a/krate/src/main/kotlin/hu/autsoft/krate/Functions.kt b/krate/src/main/kotlin/hu/autsoft/krate/Functions.kt index 6402214..18449c1 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/Functions.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/Functions.kt @@ -15,7 +15,7 @@ import kotlin.properties.ReadWriteProperty /** * Creates an optional preference of type [Boolean] with the given [key] in this [Krate] instance. - * If [key] is `null` then the property name will be used as key. + * If no [key] is provided, the property's name will be used as the key. */ public fun Krate.booleanPref(key: String? = null): KeyedKratePropertyProvider { return BooleanDelegateProvider(key) @@ -23,7 +23,7 @@ public fun Krate.booleanPref(key: String? = null): KeyedKratePropertyProvider { return FloatDelegateProvider(key) @@ -31,7 +31,7 @@ public fun Krate.floatPref(key: String? = null): KeyedKratePropertyProvider { return IntDelegateProvider(key) @@ -39,7 +39,7 @@ public fun Krate.intPref(key: String? = null): KeyedKratePropertyProvider /** * Creates an optional preference of type [Long] with the given [key] in this [Krate] instance. - * If [key] is `null` then the property name will be used as key. + * If no [key] is provided, the property's name will be used as the key. */ public fun Krate.longPref(key: String? = null): KeyedKratePropertyProvider { return LongDelegateProvider(key) @@ -47,7 +47,7 @@ public fun Krate.longPref(key: String? = null): KeyedKratePropertyProvider { return StringDelegateProvider(key) @@ -55,7 +55,7 @@ public fun Krate.stringPref(key: String? = null): KeyedKratePropertyProvider with the given [key] in this [Krate] instance. - * If [key] is `null` then the property name will be used as key. + * If no [key] is provided, the property's name will be used as the key. */ public fun Krate.stringSetPref(key: String? = null): KeyedKratePropertyProvider?> { return StringSetDelegateProvider(key) @@ -64,7 +64,7 @@ public fun Krate.stringSetPref(key: String? = null): KeyedKratePropertyProvider< /** * Creates a non-optional preference of type [Boolean] with the given [key] and [defaultValue] in this [Krate] instance. - * If [key] is `null` then the property name will be used as key. + * If no [key] is provided, the property's name will be used as the key. */ @Deprecated( message = "Use .withDefault() on a booleanPref instead", @@ -80,7 +80,7 @@ public fun Krate.booleanPref(key: String? = null, defaultValue: Boolean): Proper /** * Creates a non-optional preference of type [Float] with the given [key] and [defaultValue] in this [Krate] instance. - * If [key] is `null` then the property name will be used as key. + * If no [key] is provided, the property's name will be used as the key. */ @Deprecated( message = "Use .withDefault() on a floatPref instead", @@ -96,7 +96,7 @@ public fun Krate.floatPref(key: String? = null, defaultValue: Float): PropertyDe /** * Creates a non-optional preference of type [Int] with the given [key] and [defaultValue] in this [Krate] instance. - * If [key] is `null` then the property name will be used as key. + * If no [key] is provided, the property's name will be used as the key. */ @Deprecated( message = "Use .withDefault() on an intPref instead", @@ -112,7 +112,7 @@ public fun Krate.intPref(key: String? = null, defaultValue: Int): PropertyDelega /** * Creates a non-optional preference of type [Long] with the given [key] and [defaultValue] in this [Krate] instance. - * If [key] is `null` then the property name will be used as key. + * If no [key] is provided, the property's name will be used as the key. */ @Deprecated( message = "Use .withDefault() on a longPref instead", @@ -128,7 +128,7 @@ public fun Krate.longPref(key: String? = null, defaultValue: Long): PropertyDele /** * Creates a non-optional preference of type [String] with the given [key] and [defaultValue] in this [Krate] instance. - * If [key] is `null` then the property name will be used as key. + * If no [key] is provided, the property's name will be used as the key. */ @Deprecated( message = "Use .withDefault() on a stringPref instead", @@ -144,7 +144,7 @@ public fun Krate.stringPref(key: String? = null, defaultValue: String): Property /** * Creates a non-optional preference of type Set with the given [key] and [defaultValue] in this [Krate] instance. - * If [key] is `null` then the property name will be used as key. + * If no [key] is provided, the property's name will be used as the key. */ @Deprecated( message = "Use .withDefault() on a stringSetPref instead", From 558cb5ba1fc54f303ab7b8cbd8a21955678f8c61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Braun?= Date: Thu, 18 Nov 2021 17:12:07 +0100 Subject: [PATCH 24/31] Update README with Custom keys section --- README.md | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 011ea26..913caca 100644 --- a/README.md +++ b/README.md @@ -46,20 +46,20 @@ You can provide a default value for the property by chaining a `withDefault` cal var username: String by stringPref().withDefault("admin") ``` -# Custom keys +Reading from this property will return either the value it was last set to, or the default value if it's never been set. + +> Note that there's no way to remove these values from `SharedPreferences` (although you could set it explicitly to the default value). + +### Custom keys -By default the the property will be stored under the key of the property's name in the `SharedPreferences`. +By default, the the property will be stored under the key of the property's name in the underlying `SharedPreferences` instance. -You can change this behaviour by explicitly give the key in the argument of the delegate function. +You can change this behaviour by explicitly providing the key as an argument: ```kotlin var username: String by stringPref("username").withDefault("admin") ``` -Reading from this property will return either the value it was last set to, or the default value if it's never been set. - -> Note that there's no way to remove these values from `SharedPreferences` (although you could set it explicitly to the default value). - ### Validation You can add validation rules to your Krate properties by calling `validate` on any of Krate's delegate functions: @@ -74,13 +74,9 @@ If this validation fails, an `IllegalArgumentException` will be thrown. # Custom Krate implementations -You can usually get away with extending `SimpleKrate`, as it does allow you to pass in a custom name for the `SharedPreferences` to be used -to store your values in its constructor as an optional parameter. (If you pass in no `name` parameter to its constructor, it will default to -using the instance returned by `PreferenceManager.getDefaultSharedPreferences(context)`.) +You can usually get away with extending `SimpleKrate`, as it does allow you to pass in a custom name for the `SharedPreferences` to be used to store your values in its constructor as an optional parameter. (If you pass in no `name` parameter to its constructor, it will default to using the instance returned by `PreferenceManager.getDefaultSharedPreferences(context)`.) -However, you can also implement the `Krate` interface directly if you want to manage the `SharedPreferences` instance yourself for whatever -reason - all this interface requires is a property that holds a `SharedPreferences` instance. With that, you can use the delegate functions -the same way as shown above: +However, you can also implement the `Krate` interface directly if you want to manage the `SharedPreferences` instance yourself for whatever reason - all this interface requires is a property that holds a `SharedPreferences` instance. With that, you can use the delegate functions the same way as shown above: ```kotlin class ExampleCustomKrate(context: Context) : Krate { From 0c638074797d5ddaf1c247045244206ce3d21466 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Braun?= Date: Thu, 18 Nov 2021 17:13:26 +0100 Subject: [PATCH 25/31] Update custom key code sample --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 913caca..ace74c1 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ By default, the the property will be stored under the key of the property's name You can change this behaviour by explicitly providing the key as an argument: ```kotlin -var username: String by stringPref("username").withDefault("admin") +var username: String? by stringPref(key = "USER_NAME") ``` ### Validation From 3587036cd95b263dd9e7ff8b5e366937cbed339b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Braun?= Date: Sat, 20 Nov 2021 18:46:56 +0100 Subject: [PATCH 26/31] Update README.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: István Juhos --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f947aba..e0df04f 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ Log.d("LOGIN_COUNT", "Count: ${settings.loginCount}") # Dependency -Krate is available from `mavenCentral()`. You can add it to your dependencies with the following: +Krate is available from `mavenCentral()`. You can add it to your dependencies with the following line: ```groovy implementation 'hu.autsoft:krate:1.2.0' From 35239cc588c96d65244dbdb0a8e2b1e55689ed38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Braun?= Date: Sat, 20 Nov 2021 18:50:40 +0100 Subject: [PATCH 27/31] =?UTF-8?q?Add=20trailing=20commas=20=E2=9C=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/kotlin/hu/autsoft/krate/optional/BooleanDelegate.kt | 2 +- .../src/main/kotlin/hu/autsoft/krate/optional/FloatDelegate.kt | 2 +- krate/src/main/kotlin/hu/autsoft/krate/optional/IntDelegate.kt | 2 +- krate/src/main/kotlin/hu/autsoft/krate/optional/LongDelegate.kt | 2 +- .../src/main/kotlin/hu/autsoft/krate/optional/StringDelegate.kt | 2 +- .../main/kotlin/hu/autsoft/krate/optional/StringSetDelegate.kt | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/BooleanDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/BooleanDelegate.kt index 51b048e..9281132 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/BooleanDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/BooleanDelegate.kt @@ -6,7 +6,7 @@ import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty internal class BooleanDelegate( - override val key: String + override val key: String, ) : KeyedKrateProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Boolean? { diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/FloatDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/FloatDelegate.kt index 3860643..df257fd 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/FloatDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/FloatDelegate.kt @@ -6,7 +6,7 @@ import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty internal class FloatDelegate( - override val key: String + override val key: String, ) : KeyedKrateProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Float? { diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/IntDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/IntDelegate.kt index 05f6f9a..d7c512c 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/IntDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/IntDelegate.kt @@ -6,7 +6,7 @@ import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty internal class IntDelegate( - override val key: String + override val key: String, ) : KeyedKrateProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Int? { diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/LongDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/LongDelegate.kt index 6c5f3ce..4b2cdbe 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/LongDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/LongDelegate.kt @@ -6,7 +6,7 @@ import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty internal class LongDelegate( - override val key: String + override val key: String, ) : KeyedKrateProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Long? { diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/StringDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/StringDelegate.kt index e43c4b0..33ff495 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/StringDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/StringDelegate.kt @@ -6,7 +6,7 @@ import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty internal class StringDelegate( - override val key: String + override val key: String, ) : KeyedKrateProperty { override operator fun getValue(thisRef: Krate, property: KProperty<*>): String? { diff --git a/krate/src/main/kotlin/hu/autsoft/krate/optional/StringSetDelegate.kt b/krate/src/main/kotlin/hu/autsoft/krate/optional/StringSetDelegate.kt index c942453..0ce8bf2 100644 --- a/krate/src/main/kotlin/hu/autsoft/krate/optional/StringSetDelegate.kt +++ b/krate/src/main/kotlin/hu/autsoft/krate/optional/StringSetDelegate.kt @@ -6,7 +6,7 @@ import hu.autsoft.krate.util.edit import kotlin.reflect.KProperty internal class StringSetDelegate( - override val key: String + override val key: String, ) : KeyedKrateProperty?> { override operator fun getValue(thisRef: Krate, property: KProperty<*>): Set? { From 33494cb2fccb1dc494d1773de43b445f240a274f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Braun?= Date: Tue, 23 Nov 2021 21:30:37 +0100 Subject: [PATCH 28/31] Bump version number to 2.0.0 --- README.md | 10 +++++----- build.gradle | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 1585be9..a9ecfbf 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ Log.d("LOGIN_COUNT", "Count: ${settings.loginCount}") Krate is available from `mavenCentral()`. You can add it to your dependencies with the following line: ```groovy -implementation 'hu.autsoft:krate:1.2.0' +implementation 'hu.autsoft:krate:2.0.0' ``` # Basics @@ -161,7 +161,7 @@ class CustomMoshiKrate(context: Context) : SimpleKrate(context) { If you only want to use Moshi adapters that you generate via Moshi's [codegen facilities](https://github.com/square/moshi#codegen), you can use the following Krate artifact in your project to make use of these adapters: ```groovy -implementation 'hu.autsoft:krate-moshi-codegen:1.2.0' +implementation 'hu.autsoft:krate-moshi-codegen:2.0.0' ``` This will give you a default `Moshi` instance created by a call to `Moshi.Builder().build()`. This instance will find and use any of the adapters generated by Moshi's codegen automatically. @@ -171,7 +171,7 @@ This will give you a default `Moshi` instance created by a call to `Moshi.Builde If you rely on [reflection](https://github.com/square/moshi#reflection) for your Moshi serialization, and therefore need a `KotlinJsonAdapterFactory` included in your `Moshi` instance, use the following Krate Moshi dependency: ```groovy -implementation 'hu.autsoft:krate-moshi-reflect:1.2.0' +implementation 'hu.autsoft:krate-moshi-reflect:2.0.0' ``` The default `Moshi` instance from this dependency will include the aforementioned factory, and be able to serialize any Kotlin class. Note that this approach relies on the `kotlin-reflect` library, which is a large dependency. @@ -183,7 +183,7 @@ You may choose to use Moshi's codegen for some classes in your project, and seri The `krate-kotlinx` artifact provides a `kotlinxPref` delegate which can store any arbitrary type, as long as Kotlinx.serializazion can serialize and deserialize it. This addon, like the base library, is available from `mavenCentral()`: ```groovy -implementation 'hu.autsoft:krate-kotlinx:1.2.0' +implementation 'hu.autsoft:krate-kotlinx:2.0.0' ``` Its usage is the same as with any of the base library's delegates: @@ -215,7 +215,7 @@ class CustomKotlinxKrate(context: Context) : SimpleKrate(context) { The `krate-gson` artifact provides a `gsonPref` delegate which can store any arbitrary type, as long as Gson can serialize and deserialize it. This addon, like the base library, is available from `mavenCentral()`: ```groovy -implementation 'hu.autsoft:krate-gson:1.2.0' +implementation 'hu.autsoft:krate-gson:2.0.0' ``` Its basic usage is the same as with any of the base library's delegates: diff --git a/build.gradle b/build.gradle index b8dcff9..45071e9 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,6 @@ buildscript { ext.kotlin_version = '1.5.31' - ext.krate_version = '1.2.0' + ext.krate_version = '2.0.0' ext.moshi_version = '1.12.0' ext.junit_version = '4.13.2' From 23d01aeca32a2dc2b5e294082b19118ed3c7fe66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Braun?= Date: Tue, 23 Nov 2021 21:31:37 +0100 Subject: [PATCH 29/31] Update dependency versions --- app/build.gradle | 4 ++-- build.gradle | 2 +- krate-kotlinx/build.gradle | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 021c5e9..485d658 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -35,8 +35,8 @@ dependencies { // Android implementation 'com.google.android.material:material:1.4.0' - implementation 'androidx.appcompat:appcompat:1.3.1' - implementation 'androidx.constraintlayout:constraintlayout:2.1.0' + implementation 'androidx.appcompat:appcompat:1.4.0' + implementation 'androidx.constraintlayout:constraintlayout:2.1.2' // KotlinX implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.1' diff --git a/build.gradle b/build.gradle index 45071e9..7884631 100644 --- a/build.gradle +++ b/build.gradle @@ -17,7 +17,7 @@ buildscript { maven { url "https://plugins.gradle.org/m2/" } } dependencies { - classpath 'com.android.tools.build:gradle:7.0.2' + classpath 'com.android.tools.build:gradle:7.0.3' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version" classpath "io.github.gradle-nexus:publish-plugin:1.1.0" diff --git a/krate-kotlinx/build.gradle b/krate-kotlinx/build.gradle index 51b2146..4b73b09 100644 --- a/krate-kotlinx/build.gradle +++ b/krate-kotlinx/build.gradle @@ -30,7 +30,7 @@ tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all { dependencies { implementation project(':krate') - implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.2.2" + implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.1" testImplementation "junit:junit:$junit_version" testImplementation "org.robolectric:robolectric:$robolectric_version" From 9399bbbb2bf5df3b7cdd89e66776bdf61fe1c053 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Braun?= Date: Tue, 23 Nov 2021 21:38:50 +0100 Subject: [PATCH 30/31] Add GitHub Actions status badge to README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a9ecfbf..0e0b3c0 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Krate -[![Build Status](https://app.bitrise.io/app/40d6bd22db4cfda8/status.svg?token=0neqv73n3TXp9F0nNxj_rA&branch=main)](https://app.bitrise.io/app/40d6bd22db4cfda8) +![Build Status](https://github.com/AutSoft/Krate/workflows/Build%20and%20test/badge.svg?branch=main) ![Krate banner](./docs/krate.png) From 7c8b1a67d32fdb8bf34ccc11f57bf3229648ac74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Braun?= Date: Tue, 23 Nov 2021 21:42:11 +0100 Subject: [PATCH 31/31] Add note about possible breaking change when renaming a property --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 0e0b3c0..5695144 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,8 @@ You can change this behaviour by explicitly providing the key as an argument: var username: String? by stringPref(key = "USER_NAME") ``` +> Note that if you rely on property names as keys, renaming a Krate property will become a breaking change, and the previously stored value will be lost. This can be avoided by adding an explicit key with the name of the original property. + ### Validation You can add validation rules to your Krate properties by calling `validate` on any of Krate's delegate functions: