Skip to content

Commit ec4d0fb

Browse files
author
Roman Ivanov
authored
core-ktx: Make SharedPreferences delegate property owner nullable (#39)
1 parent 4de73df commit ec4d0fb

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

core-ktx/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## Unreleased
22

3+
### Fixed
4+
5+
- **SharedPreferences delegates**: added ability to use delegate in local and top-level properties
6+
37
## [1.6.0-1] (2021-10-03)
48

59
### Added

core-ktx/src/main/kotlin/content/SharedPreferencesDelegates.kt

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import kotlin.reflect.KProperty
1313
public fun SharedPreferences.boolean(
1414
key: String? = null,
1515
default: () -> Boolean = { false },
16-
): ReadWriteProperty<Any, Boolean> {
16+
): ReadWriteProperty<Any?, Boolean> {
1717
return delegate(
1818
key = key,
1919
fakeDefault = false,
@@ -29,7 +29,10 @@ public fun SharedPreferences.boolean(
2929
* If the key is `null`, uses name of the property as the key.
3030
* Returns result of [default] function if there is no argument for the given key. Default value is `0.0`.
3131
*/
32-
public fun SharedPreferences.float(key: String? = null, default: () -> Float = { 0f }): ReadWriteProperty<Any, Float> {
32+
public fun SharedPreferences.float(
33+
key: String? = null,
34+
default: () -> Float = { 0f }
35+
): ReadWriteProperty<Any?, Float> {
3336
return delegate(
3437
key = key,
3538
fakeDefault = 0f,
@@ -45,7 +48,10 @@ public fun SharedPreferences.float(key: String? = null, default: () -> Float = {
4548
* If the key is `null`, uses name of the property as the key.
4649
* Returns result of [default] function if there is no argument for the given key. Default value is `0`.
4750
*/
48-
public fun SharedPreferences.int(key: String? = null, default: () -> Int = { 0 }): ReadWriteProperty<Any, Int> {
51+
public fun SharedPreferences.int(
52+
key: String? = null,
53+
default: () -> Int = { 0 }
54+
): ReadWriteProperty<Any?, Int> {
4955
return delegate(
5056
key = key,
5157
fakeDefault = 0,
@@ -61,7 +67,10 @@ public fun SharedPreferences.int(key: String? = null, default: () -> Int = { 0 }
6167
* If the key is `null`, uses name of the property as the key.
6268
* Returns result of [default] function if there is no argument for the given key. Default value is `0`.
6369
*/
64-
public fun SharedPreferences.long(key: String? = null, default: () -> Long = { 0 }): ReadWriteProperty<Any, Long> {
70+
public fun SharedPreferences.long(
71+
key: String? = null,
72+
default: () -> Long = { 0 }
73+
): ReadWriteProperty<Any?, Long> {
6574
return delegate(
6675
key = key,
6776
fakeDefault = 0,
@@ -80,7 +89,7 @@ public fun SharedPreferences.long(key: String? = null, default: () -> Long = { 0
8089
public fun SharedPreferences.string(
8190
key: String? = null,
8291
default: () -> String = { "" },
83-
): ReadWriteProperty<Any, String> {
92+
): ReadWriteProperty<Any?, String> {
8493
return delegate(
8594
key = key,
8695
fakeDefault = "",
@@ -96,7 +105,7 @@ public fun SharedPreferences.string(
96105
* If the key is `null`, uses name of the property as the key.
97106
* Returns result of [default] function if there is no argument for the given key. Default value is `null`.
98107
*/
99-
public fun SharedPreferences.stringNullable(key: String? = null): ReadWriteProperty<Any, String?> {
108+
public fun SharedPreferences.stringNullable(key: String? = null): ReadWriteProperty<Any?, String?> {
100109
return delegate(
101110
key = key,
102111
fakeDefault = null,
@@ -115,7 +124,7 @@ public fun SharedPreferences.stringNullable(key: String? = null): ReadWritePrope
115124
public fun SharedPreferences.stringSet(
116125
key: String? = null,
117126
default: () -> Set<String> = { emptySet() },
118-
): ReadWriteProperty<Any, Set<String>> {
127+
): ReadWriteProperty<Any?, Set<String>> {
119128
return delegate(
120129
key = key,
121130
fakeDefault = emptySet(),
@@ -131,7 +140,7 @@ public fun SharedPreferences.stringSet(
131140
* If the key is `null`, uses name of the property as the key.
132141
* Returns result of [default] function if there is no argument for the given key. Default value is `null`.
133142
*/
134-
public fun SharedPreferences.stringSetNullable(key: String? = null): ReadWriteProperty<Any, Set<String>?> {
143+
public fun SharedPreferences.stringSetNullable(key: String? = null): ReadWriteProperty<Any?, Set<String>?> {
135144
return delegate(
136145
key = key,
137146
fakeDefault = null,
@@ -147,9 +156,9 @@ private inline fun <T> SharedPreferences.delegate(
147156
crossinline lazyDefault: () -> T,
148157
crossinline getValue: SharedPreferences.(key: String, defaultValue: T) -> T?,
149158
crossinline setValue: SharedPreferences.Editor.(key: String, value: T) -> SharedPreferences.Editor,
150-
): ReadWriteProperty<Any, T> {
151-
return object : ReadWriteProperty<Any, T> {
152-
override fun getValue(thisRef: Any, property: KProperty<*>): T {
159+
): ReadWriteProperty<Any?, T> {
160+
return object : ReadWriteProperty<Any?, T> {
161+
override fun getValue(thisRef: Any?, property: KProperty<*>): T {
153162
val finalKey = key ?: property.name
154163
return if (contains(finalKey)) {
155164
// 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 <T> SharedPreferences.delegate(
160169
}
161170
}
162171

163-
override fun setValue(thisRef: Any, property: KProperty<*>, value: T) {
172+
override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
164173
edit().setValue(key ?: property.name, value).apply()
165174
}
166175
}

0 commit comments

Comments
 (0)