@@ -13,7 +13,7 @@ import kotlin.reflect.KProperty
13
13
public fun SharedPreferences.boolean (
14
14
key : String? = null,
15
15
default : () -> Boolean = { false },
16
- ): ReadWriteProperty <Any , Boolean > {
16
+ ): ReadWriteProperty <Any ? , Boolean > {
17
17
return delegate(
18
18
key = key,
19
19
fakeDefault = false ,
@@ -29,7 +29,10 @@ public fun SharedPreferences.boolean(
29
29
* If the key is `null`, uses name of the property as the key.
30
30
* Returns result of [default] function if there is no argument for the given key. Default value is `0.0`.
31
31
*/
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 > {
33
36
return delegate(
34
37
key = key,
35
38
fakeDefault = 0f ,
@@ -45,7 +48,10 @@ public fun SharedPreferences.float(key: String? = null, default: () -> Float = {
45
48
* If the key is `null`, uses name of the property as the key.
46
49
* Returns result of [default] function if there is no argument for the given key. Default value is `0`.
47
50
*/
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 > {
49
55
return delegate(
50
56
key = key,
51
57
fakeDefault = 0 ,
@@ -61,7 +67,10 @@ public fun SharedPreferences.int(key: String? = null, default: () -> Int = { 0 }
61
67
* If the key is `null`, uses name of the property as the key.
62
68
* Returns result of [default] function if there is no argument for the given key. Default value is `0`.
63
69
*/
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 > {
65
74
return delegate(
66
75
key = key,
67
76
fakeDefault = 0 ,
@@ -80,7 +89,7 @@ public fun SharedPreferences.long(key: String? = null, default: () -> Long = { 0
80
89
public fun SharedPreferences.string (
81
90
key : String? = null,
82
91
default : () -> String = { "" },
83
- ): ReadWriteProperty <Any , String > {
92
+ ): ReadWriteProperty <Any ? , String > {
84
93
return delegate(
85
94
key = key,
86
95
fakeDefault = " " ,
@@ -96,7 +105,7 @@ public fun SharedPreferences.string(
96
105
* If the key is `null`, uses name of the property as the key.
97
106
* Returns result of [default] function if there is no argument for the given key. Default value is `null`.
98
107
*/
99
- public fun SharedPreferences.stringNullable (key : String? = null): ReadWriteProperty <Any , String ?> {
108
+ public fun SharedPreferences.stringNullable (key : String? = null): ReadWriteProperty <Any ? , String ?> {
100
109
return delegate(
101
110
key = key,
102
111
fakeDefault = null ,
@@ -115,7 +124,7 @@ public fun SharedPreferences.stringNullable(key: String? = null): ReadWritePrope
115
124
public fun SharedPreferences.stringSet (
116
125
key : String? = null,
117
126
default : () -> Set <String > = { emptySet() },
118
- ): ReadWriteProperty <Any , Set <String >> {
127
+ ): ReadWriteProperty <Any ? , Set <String >> {
119
128
return delegate(
120
129
key = key,
121
130
fakeDefault = emptySet(),
@@ -131,7 +140,7 @@ public fun SharedPreferences.stringSet(
131
140
* If the key is `null`, uses name of the property as the key.
132
141
* Returns result of [default] function if there is no argument for the given key. Default value is `null`.
133
142
*/
134
- public fun SharedPreferences.stringSetNullable (key : String? = null): ReadWriteProperty <Any , Set <String >? > {
143
+ public fun SharedPreferences.stringSetNullable (key : String? = null): ReadWriteProperty <Any ? , Set <String >? > {
135
144
return delegate(
136
145
key = key,
137
146
fakeDefault = null ,
@@ -147,9 +156,9 @@ private inline fun <T> SharedPreferences.delegate(
147
156
crossinline lazyDefault : () -> T ,
148
157
crossinline getValue : SharedPreferences .(key: String , defaultValue: T ) -> T ? ,
149
158
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 {
153
162
val finalKey = key ? : property.name
154
163
return if (contains(finalKey)) {
155
164
// 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(
160
169
}
161
170
}
162
171
163
- override fun setValue (thisRef : Any , property : KProperty <* >, value : T ) {
172
+ override fun setValue (thisRef : Any? , property : KProperty <* >, value : T ) {
164
173
edit().setValue(key ? : property.name, value).apply ()
165
174
}
166
175
}
0 commit comments