Skip to content

Commit

Permalink
feat: SerializeDelegate 新增name回调参数动态设置存储键名
Browse files Browse the repository at this point in the history
  • Loading branch information
liangjingkanji committed Mar 10, 2023
1 parent a4a7e70 commit 4cc4478
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ dependencyResolutionManagement {
然后在 module 的 build.gradle 添加依赖框架

```groovy
implementation 'com.github.liangjingkanji:Serialize:3.0.0'
implementation 'com.github.liangjingkanji:Serialize:3.0.1'
```

<br>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/drake/serialize/sample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class MainActivity : EngineActivity<ActivityMainBinding>(R.layout.activity_main)
private var data: KotlinSerializableModel? by serialLazy()
private var amount: String by serial("默认值", "自定义键名")
private val liveData: MutableLiveData<String> by serialLiveData("默认值")
private var userId: String = "0123"
private var balance: String by serial("0.0", { "balance-$userId" })

override fun initView() {
binding.v = this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ inline fun <reified V> serial(
default: V? = null,
name: String? = null,
kv: MMKV? = null
): ReadWriteProperty<Any, V> = SerialDelegate(default, V::class.java, { name }, kv)


/**
* 其修饰的属性字段的读写都会自动映射到本地磁盘
* 线程安全
* 本函数属于阻塞函数, 同步读写磁盘
* @param default 默认值
* @param name 指定字段名, 最终存储key为`配置名称.字段名`, 配置名称默认为当前全路径类名([SerializeConfig]注解可以指定配置名称), 例如 `com.example.Class.field`
* 请注意如果完全不指定配置名称或字段名情况下重命名包/类/字段名称会导致无法读取旧值
* @throws NullPointerException 字段如果属于不可空, 但是读取本地失败会导致抛出异常
*/
inline fun <reified V> serial(
default: V? = null,
noinline name: () -> String?,
kv: MMKV? = null
): ReadWriteProperty<Any, V> = SerialDelegate(default, V::class.java, name, kv)

/**
Expand All @@ -57,6 +73,25 @@ inline fun <reified V> serialLiveData(
default: V? = null,
name: String? = null,
kv: MMKV? = null
): ReadOnlyProperty<Any, MutableLiveData<V>> = SerializeLiveDataDelegate(default, V::class.java, { name }, kv)

/**
* 可观察的数据来源[MutableLiveData]
* 其修饰的属性字段的读写都会自动映射到本地磁盘
* 和[serial]不同的是通过内存/磁盘双通道读写来优化读写性能
* 其修饰的属性字段第一次会读取磁盘数据, 然后拷贝到内存中, 后续都是直接读取内存中的拷贝
* 写入会优先写入到内存中的拷贝份, 然后通过子线程异步写入到磁盘
* 线程安全
*
* @param default 默认值, 默认值会在订阅时触发一次
* @param name 指定字段名, 最终存储key为`配置名称.字段名`, 配置名称默认为当前全路径类名([SerializeConfig]注解可以指定配置名称), 例如 `com.example.Class.field`
* 请注意如果完全不指定配置名称或字段名情况下重命名包/类/字段名称会导致无法读取旧值
* @throws NullPointerException 字段如果属于不可空, 但是读取本地失败会导致抛出异常
*/
inline fun <reified V> serialLiveData(
default: V? = null,
noinline name: () -> String?,
kv: MMKV? = null
): ReadOnlyProperty<Any, MutableLiveData<V>> = SerializeLiveDataDelegate(default, V::class.java, name, kv)

/**
Expand All @@ -75,5 +110,23 @@ inline fun <reified V> serialLazy(
default: V? = null,
name: String? = null,
kv: MMKV? = null
): ReadWriteProperty<Any, V> = SerialLazyDelegate(default, V::class.java, { name }, kv)

/**
* 其修饰的属性字段的读写都会自动映射到本地磁盘
* 和[serial]不同的是通过内存/磁盘双通道读写来优化读写性能
* 其修饰的属性字段第一次会读取磁盘数据, 然后拷贝到内存中, 后续都是直接读取内存中的拷贝
* 写入会优先写入到内存中的拷贝份, 然后通过子线程异步写入到磁盘
* 线程安全
*
* @param default 默认值
* @param name 指定字段名, 最终存储key为`配置名称.字段名`, 配置名称默认为当前全路径类名([SerializeConfig]注解可以指定配置名称), 例如 `com.example.Class.field`
* 请注意如果完全不指定配置名称或字段名情况下重命名包/类/字段名称会导致无法读取旧值
* @throws NullPointerException 字段如果属于不可空, 但是读取本地失败会导致抛出异常
*/
inline fun <reified V> serialLazy(
default: V? = null,
noinline name: () -> String?,
kv: MMKV? = null
): ReadWriteProperty<Any, V> = SerialLazyDelegate(default, V::class.java, name, kv)

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import kotlin.reflect.KProperty
internal class SerialDelegate<V>(
private val default: V?,
private val type: Class<V>,
private val name: String?,
private val name: () -> String?,
private var kv: MMKV?
) : ReadWriteProperty<Any, V> {

Expand All @@ -42,13 +42,13 @@ internal class SerialDelegate<V>(

override fun getValue(thisRef: Any, property: KProperty<*>): V {
val mmkv = mmkvWithConfig(thisRef)
val name = name ?: property.name
val name = name() ?: property.name
return mmkv.deserialize(type, name, default)
}

override fun setValue(thisRef: Any, property: KProperty<*>, value: V) {
val mmkv = mmkvWithConfig(thisRef)
val name = name ?: property.name
val name = name() ?: property.name
mmkv.serialize(name to value)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ import kotlin.reflect.KProperty
internal class SerialLazyDelegate<V>(
private val default: V?,
private val type: Class<V>,
private val name: String?,
private val name: () -> String?,
private var kv: MMKV?
) : ReadWriteProperty<Any, V> {
@Volatile
private var value: V? = null
private var hasAnnotation: Boolean = true
private var config: SerializeConfig? = null
private var lastName: String? = null

private fun mmkvWithConfig(thisRef: Any): MMKV {
return kv ?: kotlin.run {
Expand All @@ -45,10 +46,11 @@ internal class SerialLazyDelegate<V>(
}

override fun getValue(thisRef: Any, property: KProperty<*>): V = synchronized(this) {
if (value == null) {
val name = name() ?: property.name
if (value == null || name != lastName) {
val mmkv = mmkvWithConfig(thisRef)
val name = name ?: property.name
value = mmkv.deserialize(type, name, default)
lastName = name
}
value as V
}
Expand All @@ -58,7 +60,7 @@ internal class SerialLazyDelegate<V>(
//写入本地在子线程处理,单一线程保证了写入顺序
taskExecutor.execute {
val mmkv = mmkvWithConfig(thisRef)
val name = name ?: property.name
val name = name() ?: property.name
mmkv.serialize(name to value)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import kotlin.reflect.KProperty
internal class SerializeLiveDataDelegate<V>(
private val default: V?,
private val type: Class<V>,
private val name: String?,
private val name: () -> String?,
private var kv: MMKV?,
) : ReadOnlyProperty<Any, MutableLiveData<V>>, MutableLiveData<V>() {

Expand Down Expand Up @@ -50,7 +50,7 @@ internal class SerializeLiveDataDelegate<V>(
var value = super.getValue()
if (value == null) {
val mmkv = mmkvWithConfig(thisRef)
val name = name ?: property.name
val name = name() ?: property.name
value = mmkv.deserialize(type, name, default)
}
value
Expand Down Expand Up @@ -83,7 +83,7 @@ internal class SerializeLiveDataDelegate<V>(
private fun asyncSerialize(value: V) {
taskExecutor.execute {
val mmkv = mmkvWithConfig(thisRef)
val name = name ?: property.name
val name = name() ?: property.name
mmkv.serialize(name to value)
}
}
Expand Down

0 comments on commit 4cc4478

Please sign in to comment.