Skip to content

Commit 6c1a74b

Browse files
author
lizixian
committed
demo
1 parent e0b09fa commit 6c1a74b

File tree

8 files changed

+107
-11
lines changed

8 files changed

+107
-11
lines changed

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,5 @@ dependencies {
3939
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
4040
implementation project(':pref')
4141
implementation 'com.google.code.gson:gson:2.8.7'
42+
implementation 'com.tencent:mmkv-static:1.2.9'
4243
}

app/src/main/java/com/lzx/kvpref/MainActivity.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ import android.os.Bundle
44
import android.util.Log
55
import android.widget.Button
66
import androidx.appcompat.app.AppCompatActivity
7-
import com.google.gson.Gson
8-
import com.lzx.pref.KvPrefModel
9-
import com.lzx.pref.Serializer
10-
import java.lang.reflect.Type
117

128
class MainActivity : AppCompatActivity() {
139
override fun onCreate(savedInstanceState: Bundle?) {
@@ -17,7 +13,10 @@ class MainActivity : AppCompatActivity() {
1713

1814
findViewById<Button>(R.id.write).setOnClickListener {
1915
SpFileDemo.name = "大妈蛋"
20-
Log.i("XIAN", "read = " + SpFileDemo.name)
16+
Log.i("MainActivity", "read = " + SpFileDemo.name)
2117
}
18+
19+
20+
2221
}
2322
}

app/src/main/java/com/lzx/kvpref/MainActivityJava.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
1616
findViewById(R.id.write).setOnClickListener(new View.OnClickListener() {
1717
@Override
1818
public void onClick(View view) {
19-
// SpFileDemoJava.INSTANCE.getPeople("哈哈哈");
20-
Log.i("XIAN", "read = " + SpFileDemoJava.INSTANCE.getPeople());
19+
Log.i("MainActivity", "read = " + SpFileDemoJava.INSTANCE.getPeople());
2120
}
2221
});
2322
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.lzx.kvpref
2+
3+
import android.content.Context
4+
import android.content.SharedPreferences
5+
import com.lzx.pref.KvPrefProvider
6+
import com.tencent.mmkv.MMKV
7+
8+
class MmKvPref : KvPrefProvider {
9+
override fun get(context: Context, name: String, mode: Int): SharedPreferences {
10+
if (MMKV.getRootDir().isNullOrEmpty()) {
11+
MMKV.initialize(context)
12+
}
13+
return MMKV.mmkvWithID(name, MMKV.SINGLE_PROCESS_MODE) as SharedPreferences
14+
}
15+
}

app/src/main/java/com/lzx/kvpref/SpTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ object SpFileDemo : KvPrefModel("spFileName") {
1010
var age: Int by intPref()
1111
var height: Long by longPref()
1212
var weight: Float by floatPref()
13-
var isGay: Boolean by booleanPref(false, key = "是否是变态")
13+
var isGay: Boolean? by booleanPrefNullable(false, key = "是否是变态")
1414
}
1515

1616
/**

pref/src/main/java/com/lzx/pref/KvPrefModel.kt

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,27 +63,55 @@ open class KvPrefModel constructor(
6363
synchronous: Boolean = isCommitProperties
6464
) = IntPref(default, synchronous, key, keyUpperCase)
6565

66+
fun intPrefNullable(
67+
default: Int = 0,
68+
key: String? = null,
69+
keyUpperCase: Boolean = isKeyUpperCase,
70+
synchronous: Boolean = isCommitProperties
71+
) = IntNullablePref(default, synchronous, key, keyUpperCase)
72+
6673
fun longPref(
6774
default: Long = 0,
6875
key: String? = null,
6976
keyUpperCase: Boolean = isKeyUpperCase,
7077
synchronous: Boolean = isCommitProperties
7178
) = LongPref(default, synchronous, key, keyUpperCase)
7279

80+
fun longPrefNullable(
81+
default: Long = 0,
82+
key: String? = null,
83+
keyUpperCase: Boolean = isKeyUpperCase,
84+
synchronous: Boolean = isCommitProperties
85+
) = LongNullablePref(default, synchronous, key, keyUpperCase)
86+
7387
fun floatPref(
7488
default: Float = 0f,
7589
key: String? = null,
7690
keyUpperCase: Boolean = isKeyUpperCase,
7791
synchronous: Boolean = isCommitProperties
7892
) = FloatPref(default, synchronous, key, keyUpperCase)
7993

94+
fun floatPrefNullable(
95+
default: Float = 0f,
96+
key: String? = null,
97+
keyUpperCase: Boolean = isKeyUpperCase,
98+
synchronous: Boolean = isCommitProperties
99+
) = FloatNullablePref(default, synchronous, key, keyUpperCase)
100+
80101
fun booleanPref(
81102
default: Boolean = false,
82103
key: String? = null,
83104
keyUpperCase: Boolean = isKeyUpperCase,
84105
synchronous: Boolean = isCommitProperties
85106
) = BooleanPref(default, synchronous, key, keyUpperCase)
86107

108+
fun booleanPrefNullable(
109+
default: Boolean = false,
110+
key: String? = null,
111+
keyUpperCase: Boolean = isKeyUpperCase,
112+
synchronous: Boolean = isCommitProperties
113+
) = BooleanNullablePref(default, synchronous, key, keyUpperCase)
114+
87115
inline fun <reified T : Any> objPref(
88116
default: T? = null,
89117
key: String? = null,
@@ -153,12 +181,19 @@ open class KvPrefModel constructor(
153181
return kvProperties[property.name]?.preferenceKey()
154182
}
155183

184+
/**
185+
* 获取key变量名
186+
*/
187+
fun getPrefName(property: KProperty<*>): String? {
188+
return kvProperties[property.name]?.propertyName
189+
}
190+
156191
/**
157192
* remove
158193
*/
159194
@SuppressLint("CommitPrefEdits")
160-
fun remove(property: KProperty<*>) {
161-
preference.edit().remove(getPrefKey(property)).execute(isCommitProperties)
195+
fun remove(property: KProperty<*>, synchronous: Boolean = isCommitProperties) {
196+
preference.edit().remove(getPrefKey(property)).execute(synchronous)
162197
}
163198

164199
/**

pref/src/main/java/com/lzx/pref/KvPrefProperty.kt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ class StringNullablePref(
3535
class IntPref(default: Int, synchronous: Boolean, key: String?, keyUpperCase: Boolean) :
3636
KvPrefProperty<Int>(Int::class, Int::class.java, synchronous, key, keyUpperCase, default)
3737

38+
class IntNullablePref(default: Int, synchronous: Boolean, key: String?, keyUpperCase: Boolean) :
39+
KvPrefNullableProperty<Int>(
40+
Int::class,
41+
Int::class.java,
42+
synchronous,
43+
key,
44+
keyUpperCase,
45+
default
46+
)
47+
3848
class BooleanPref(default: Boolean, synchronous: Boolean, key: String?, keyUpperCase: Boolean) :
3949
KvPrefProperty<Boolean>(
4050
Boolean::class,
@@ -45,12 +55,48 @@ class BooleanPref(default: Boolean, synchronous: Boolean, key: String?, keyUpper
4555
default
4656
)
4757

58+
class BooleanNullablePref(
59+
default: Boolean,
60+
synchronous: Boolean,
61+
key: String?,
62+
keyUpperCase: Boolean
63+
) :
64+
KvPrefNullableProperty<Boolean>(
65+
Boolean::class,
66+
Boolean::class.java,
67+
synchronous,
68+
key,
69+
keyUpperCase,
70+
default
71+
)
72+
4873
class FloatPref(default: Float, synchronous: Boolean, key: String?, keyUpperCase: Boolean) :
4974
KvPrefProperty<Float>(Float::class, Float::class.java, synchronous, key, keyUpperCase, default)
5075

76+
class FloatNullablePref(default: Float, synchronous: Boolean, key: String?, keyUpperCase: Boolean) :
77+
KvPrefNullableProperty<Float>(
78+
Float::class,
79+
Float::class.java,
80+
synchronous,
81+
key,
82+
keyUpperCase,
83+
default
84+
)
85+
5186
class LongPref(default: Long, synchronous: Boolean, key: String?, keyUpperCase: Boolean) :
5287
KvPrefProperty<Long>(Long::class, Long::class.java, synchronous, key, keyUpperCase, default)
5388

89+
class LongNullablePref(default: Long, synchronous: Boolean, key: String?, keyUpperCase: Boolean) :
90+
KvPrefNullableProperty<Long>(
91+
Long::class,
92+
Long::class.java,
93+
synchronous,
94+
key,
95+
keyUpperCase,
96+
default
97+
)
98+
99+
54100
open class KvPrefProperty<T : Any>(
55101
private val clazz: KClass<T>,
56102
private val type: Type,

pref/src/main/java/com/lzx/pref/KvPrefProvider.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ class DefKvPref : KvPrefProvider {
1717
override fun get(context: Context, name: String, mode: Int): SharedPreferences {
1818
return context.getSharedPreferences(name, mode)
1919
}
20-
}
20+
}
21+

0 commit comments

Comments
 (0)