You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
+ For Kotlin project, see [Lombok compiler plugin | Kotlin (kotlinlang.org)](https://kotlinlang.org/docs/lombok.html#using-with-kapt) for more details.
25
+
26
+
```groovy
27
+
plugins {
28
+
id 'org.jetbrains.kotlin.plugin.lombok' version '1.7.10'
29
+
id 'io.freefair.lombok' version '5.3.0'
30
+
id 'kotlin-kapt'
31
+
}
34
32
35
-
See [Lombok compiler plugin | Kotlin (kotlinlang.org)](https://kotlinlang.org/docs/lombok.html#using-with-kapt) for more details.
33
+
kapt {
34
+
keepJavacAnnotationProcessors = true
35
+
}
36
36
37
-
4. Create a new class for managing `SharedPreferences`, and add `@ExSharedPreference` and `@Data` annotations.
`ExSharedPreference` allows you to save custom types into SharedPreferences, but since SharedPreferences only supports a limited number of types, we use the conversion mechanism to complete this function.
114
+
115
+
1. Add the required custom types to the classdirectly.
116
+
117
+
**PS:The `defVal` needs to fill in the string of the original type value, not your custom type!**
2. Create a class that implements the Converter interface and add `@ExConverter` annotation.
131
+
132
+
```kotlin
133
+
@ExConverter
134
+
classDateConverter: Converter<Date, Long> {
135
+
overridefuntoPreference(origin:Date): Long {
136
+
return origin.time
137
+
}
138
+
139
+
overridefunfromPreference(target:Long): Date {
140
+
returnDate(target)
141
+
}
142
+
}
143
+
```
144
+
145
+
The first parameter of the `Converter` generic parameter is your custom type, and the second parameter is the type actually stored in `SharedPreferences`.
146
+
101
147
## Demo
102
148
103
149
We have a demo using `Kotlin` to demonstrate how `ExSharedPreference` used: [demo-kotlin](/demo/src/main/java/io/github/sgpublic/exsp/demo).
104
150
105
-
## Customize
151
+
## API
106
152
107
153
### @ExSharedPreference
108
154
@@ -154,10 +200,15 @@ sharedPreference.editor()
154
200
155
201
#### Parameter explanation
156
202
157
-
+`key`: `String`
203
+
+`name`: `String`
158
204
159
205
**(Optional)** The name of the preference to retrieve, default is the variable name with a capital letter.
160
206
161
207
+`defVal`: `String`
162
208
163
-
**(Required)** Value to return if this preference does not exist.
209
+
**(Required)** Value to return if this preference does not exist.
210
+
211
+
### @ExConverter
212
+
213
+
This annotation is used to mark a custom type converter for `ExSharedPreference` processing.
0 commit comments