From ec4d0fb864c2563a799ee21b2d3785aedc7f672d Mon Sep 17 00:00:00 2001 From: Roman Ivanov Date: Tue, 2 Nov 2021 11:56:43 +0300 Subject: [PATCH] core-ktx: Make SharedPreferences delegate property owner nullable (#39) --- core-ktx/CHANGELOG.md | 4 +++ .../content/SharedPreferencesDelegates.kt | 33 ++++++++++++------- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/core-ktx/CHANGELOG.md b/core-ktx/CHANGELOG.md index 2aa6e3f..cb2bada 100644 --- a/core-ktx/CHANGELOG.md +++ b/core-ktx/CHANGELOG.md @@ -1,5 +1,9 @@ ## Unreleased +### Fixed + +- **SharedPreferences delegates**: added ability to use delegate in local and top-level properties + ## [1.6.0-1] (2021-10-03) ### Added diff --git a/core-ktx/src/main/kotlin/content/SharedPreferencesDelegates.kt b/core-ktx/src/main/kotlin/content/SharedPreferencesDelegates.kt index f6ba2e3..3136b6b 100644 --- a/core-ktx/src/main/kotlin/content/SharedPreferencesDelegates.kt +++ b/core-ktx/src/main/kotlin/content/SharedPreferencesDelegates.kt @@ -13,7 +13,7 @@ import kotlin.reflect.KProperty public fun SharedPreferences.boolean( key: String? = null, default: () -> Boolean = { false }, -): ReadWriteProperty { +): ReadWriteProperty { return delegate( key = key, fakeDefault = false, @@ -29,7 +29,10 @@ public fun SharedPreferences.boolean( * If the key is `null`, uses name of the property as the key. * Returns result of [default] function if there is no argument for the given key. Default value is `0.0`. */ -public fun SharedPreferences.float(key: String? = null, default: () -> Float = { 0f }): ReadWriteProperty { +public fun SharedPreferences.float( + key: String? = null, + default: () -> Float = { 0f } +): ReadWriteProperty { return delegate( key = key, fakeDefault = 0f, @@ -45,7 +48,10 @@ public fun SharedPreferences.float(key: String? = null, default: () -> Float = { * If the key is `null`, uses name of the property as the key. * Returns result of [default] function if there is no argument for the given key. Default value is `0`. */ -public fun SharedPreferences.int(key: String? = null, default: () -> Int = { 0 }): ReadWriteProperty { +public fun SharedPreferences.int( + key: String? = null, + default: () -> Int = { 0 } +): ReadWriteProperty { return delegate( key = key, fakeDefault = 0, @@ -61,7 +67,10 @@ public fun SharedPreferences.int(key: String? = null, default: () -> Int = { 0 } * If the key is `null`, uses name of the property as the key. * Returns result of [default] function if there is no argument for the given key. Default value is `0`. */ -public fun SharedPreferences.long(key: String? = null, default: () -> Long = { 0 }): ReadWriteProperty { +public fun SharedPreferences.long( + key: String? = null, + default: () -> Long = { 0 } +): ReadWriteProperty { return delegate( key = key, fakeDefault = 0, @@ -80,7 +89,7 @@ public fun SharedPreferences.long(key: String? = null, default: () -> Long = { 0 public fun SharedPreferences.string( key: String? = null, default: () -> String = { "" }, -): ReadWriteProperty { +): ReadWriteProperty { return delegate( key = key, fakeDefault = "", @@ -96,7 +105,7 @@ public fun SharedPreferences.string( * If the key is `null`, uses name of the property as the key. * Returns result of [default] function if there is no argument for the given key. Default value is `null`. */ -public fun SharedPreferences.stringNullable(key: String? = null): ReadWriteProperty { +public fun SharedPreferences.stringNullable(key: String? = null): ReadWriteProperty { return delegate( key = key, fakeDefault = null, @@ -115,7 +124,7 @@ public fun SharedPreferences.stringNullable(key: String? = null): ReadWritePrope public fun SharedPreferences.stringSet( key: String? = null, default: () -> Set = { emptySet() }, -): ReadWriteProperty> { +): ReadWriteProperty> { return delegate( key = key, fakeDefault = emptySet(), @@ -131,7 +140,7 @@ public fun SharedPreferences.stringSet( * If the key is `null`, uses name of the property as the key. * Returns result of [default] function if there is no argument for the given key. Default value is `null`. */ -public fun SharedPreferences.stringSetNullable(key: String? = null): ReadWriteProperty?> { +public fun SharedPreferences.stringSetNullable(key: String? = null): ReadWriteProperty?> { return delegate( key = key, fakeDefault = null, @@ -147,9 +156,9 @@ private inline fun SharedPreferences.delegate( crossinline lazyDefault: () -> T, crossinline getValue: SharedPreferences.(key: String, defaultValue: T) -> T?, crossinline setValue: SharedPreferences.Editor.(key: String, value: T) -> SharedPreferences.Editor, -): ReadWriteProperty { - return object : ReadWriteProperty { - override fun getValue(thisRef: Any, property: KProperty<*>): T { +): ReadWriteProperty { + return object : ReadWriteProperty { + override fun getValue(thisRef: Any?, property: KProperty<*>): T { val finalKey = key ?: property.name return if (contains(finalKey)) { // fakeDefault will never be used and value should never be null, so we can cast value to T safely @@ -160,7 +169,7 @@ private inline fun SharedPreferences.delegate( } } - override fun setValue(thisRef: Any, property: KProperty<*>, value: T) { + override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) { edit().setValue(key ?: property.name, value).apply() } }