Skip to content

Commit f6eceee

Browse files
committed
Add Set<String>/Converter support
1 parent f4b225c commit f6eceee

File tree

21 files changed

+524
-215
lines changed

21 files changed

+524
-215
lines changed

.idea/compiler.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 85 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,43 @@ This is a wrapper library for `SharedPreferences` for Android, based on Lombok (
88

99
2. Add dependencies in `build.gradle`.
1010

11-
```groovy
12-
dependencies {
13-
implementation "io.github.sgpublic:exsp-runtime:$latest"
14-
annotationProcessor "io.github.sgpublic:exsp-compiler:$latest"
15-
16-
def lombok_ver = "1.18.24"
17-
compileOnly "org.projectlombok:lombok:$lombok_ver"
18-
annotationProcessor "org.projectlombok:lombok:$lombok_ver"
19-
}
20-
```
11+
+ For Java project
2112

22-
3. If your project is using `Kotlin`, you also need to add to your `build.gradle`:
13+
```groovy
14+
dependencies {
15+
implementation "io.github.sgpublic:exsp-runtime:$latest"
16+
annotationProcessor "io.github.sgpublic:exsp-compiler:$latest"
2317
24-
```groovy
25-
plugins {
26-
id 'org.jetbrains.kotlin.plugin.lombok' version '1.7.10'
27-
id 'io.freefair.lombok' version '5.3.0'
28-
}
29-
30-
kapt {
31-
keepJavacAnnotationProcessors = true
32-
}
33-
```
18+
def lombok_ver = "1.18.24"
19+
compileOnly "org.projectlombok:lombok:$lombok_ver"
20+
annotationProcessor "org.projectlombok:lombok:$lombok_ver"
21+
}
22+
```
23+
24+
+ 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+
}
3432
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+
}
3636
37-
4. Create a new class for managing `SharedPreferences`, and add `@ExSharedPreference` and `@Data` annotations.
37+
dependencies {
38+
implementation "io.github.sgpublic:exsp-runtime:$latest"
39+
kapt "io.github.sgpublic:exsp-compiler:$latest"
40+
41+
def lombok_ver = "1.18.24"
42+
compileOnly "org.projectlombok:lombok:$lombok_ver"
43+
annotationProcessor "org.projectlombok:lombok:$lombok_ver"
44+
}
45+
```
46+
47+
3. Create a new class for managing `SharedPreferences`, and add `@ExSharedPreference` and `@Data` annotations.
3848
3949
**PS: Whether your project uses `Java` or `Kotlin`, this class must be `Java`!**
4050
@@ -45,42 +55,42 @@ This is a wrapper library for `SharedPreferences` for Android, based on Lombok (
4555
}
4656
```
4757

48-
5. Add member variables to this class and add the `@ExValue` annotation to set the default value.
58+
4. Add member variables to this class and add the `@ExValue` annotation to set the default value.
4959

5060
```java
5161
@Data
5262
@ExSharedPreference(name = "name_of_shared_preference")
5363
public class TestPreference {
5464
@ExValue(defVal = "test")
5565
private String testString;
56-
66+
5767
@ExValue(defVal = "0")
5868
private float testFloat;
59-
69+
6070
@ExValue(defVal = "0")
6171
private int testInt;
62-
72+
6373
@ExValue(defVal = "0")
6474
private long testLong;
65-
75+
6676
@ExValue(defVal = "false")
6777
private boolean testBool;
6878
}
6979
```
7080

71-
6. Use `ExPreference.init(Context context)` method in `Application` to initialize `Context`.
81+
5. Use `ExPreference.init(Context context)` method in `Application` to initialize `Context`.
7282

7383
```kotlin
7484
class App: Application() {
7585
override fun onCreate() {
7686
super.onCreate()
77-
87+
7888
ExPreference.init(this)
7989
}
8090
}
8191
```
8292

83-
7. Now you can manage `SharedPreferences` directly using `getters`/`setters` provided by `Lombok`, enjoy it!
93+
6. Now you can manage `SharedPreferences` directly using `getters`/`setters` provided by `Lombok`, enjoy it!
8494

8595
+ Kotlin
8696

@@ -98,11 +108,47 @@ This is a wrapper library for `SharedPreferences` for Android, based on Lombok (
98108
Log.d("TestPreference#testString", test.getTestString());
99109
```
100110

111+
## Custom Type
112+
113+
`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 class directly.
116+
117+
**PS: The `defVal` needs to fill in the string of the original type value, not your custom type!**
118+
119+
```java
120+
@Data
121+
@ExSharedPreference(name = "name_of_shared_preference")
122+
public class TestPreference {
123+
...
124+
@ExValue(defVal = "-1")
125+
private Date testDate;
126+
...
127+
}
128+
```
129+
130+
2. Create a class that implements the Converter interface and add `@ExConverter` annotation.
131+
132+
```kotlin
133+
@ExConverter
134+
class DateConverter: Converter<Date, Long> {
135+
override fun toPreference(origin: Date): Long {
136+
return origin.time
137+
}
138+
139+
override fun fromPreference(target: Long): Date {
140+
return Date(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+
101147
## Demo
102148

103149
We have a demo using `Kotlin` to demonstrate how `ExSharedPreference` used: [demo-kotlin](/demo/src/main/java/io/github/sgpublic/exsp/demo).
104150

105-
## Customize
151+
## API
106152

107153
### @ExSharedPreference
108154

@@ -154,10 +200,15 @@ sharedPreference.editor()
154200

155201
#### Parameter explanation
156202

157-
+ `key`: `String`
203+
+ `name`: `String`
158204

159205
**(Optional)** The name of the preference to retrieve, default is the variable name with a capital letter.
160206

161207
+ `defVal`: `String`
162208

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.
214+

compiler/build.gradle.kts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ plugins {
99
}
1010

1111
java {
12-
sourceCompatibility = JavaVersion.VERSION_11
13-
targetCompatibility = JavaVersion.VERSION_11
12+
sourceCompatibility = JavaVersion.VERSION_1_8
13+
targetCompatibility = JavaVersion.VERSION_1_8
1414

1515
withJavadocJar()
1616
withSourcesJar()
@@ -29,5 +29,5 @@ tasks.test {
2929
}
3030

3131
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
32-
kotlinOptions.jvmTarget = "11"
32+
kotlinOptions.jvmTarget = "1.8"
3333
}

0 commit comments

Comments
 (0)