-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
可以增强一下吗?DataSaverMutableListState 有时候在使用的时候并不需要创建默认数据,现在不支持 #7
Comments
您好,感谢使用,对于这种情况,如果您的 Entity 已经注册,可以手动注解类型+将默认值设置为 emptyList。代码为: var entityList: List<Entity> by rememberDataSaverListState(key = "main.entity.list", initialValue = emptyList()) 在 kotlin 标准库实现中, public fun <T> emptyList(): List<T> = EmptyList
internal object EmptyList : List<Nothing>, Serializable, RandomAccess {
// ...
} 不过代码确实写的有问题,比如 |
感谢及时回复,我尝试过你说的这种方式,但是如果在已经保存的有数据的情况下加载数据是报错的 |
你是否标注了具体的泛型,比如 |
下面是我的代码,我在尝试使用自定义类型的时候会报错(添加一点数据,然后再次启动就可以重现),基础类型String不会报错
|
感谢反馈,我试了下确实有问题,逻辑有些错误,将会在下一版本进行改正 |
非常感谢 ♪(・ω・)ノ,期待新版本 |
连写两天,新的 var list: List<Entity> = mutableDataSaverState(key = "main.entity.list", initialValue = emptyList()) 同时注册 更多迁移指南和代码变更见:https://github.com/FunnySaltyFish/ComposeDataSaver/releases/tag/v1.2.1 |
你好,我没有找到 import AppConfig.dataSaver
import androidx.compose.desktop.ui.tooling.preview.Preview
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application
import cn.hutool.json.JSONUtil
import com.funny.data_saver.core.DataSaverConverter
import com.funny.data_saver.core.DataSaverInterface
import com.funny.data_saver.core.DataSaverProperties
import com.funny.data_saver.core.LocalDataSaver
import com.funny.data_saver.core.mutableDataSaverStateOf
// 实体
data class Entity(var name: String = "", var remark: String = "")
// 配置
object AppConfig {
private const val FILE_NAME = "config.properties"
private val userHome = System.getProperty("user.home")
private const val PROJECT_NAME = "test-demo"
val dataSaver: DataSaverInterface = DataSaverProperties("$userHome/$PROJECT_NAME/$FILE_NAME")
}
@Composable
@Preview
fun App() {
var list: List<Entity> by mutableDataSaverStateOf<List<Entity>>(
dataSaverInterface = dataSaver,
key = "main.entity.list",
initialValue = emptyList<Entity>()
)
MaterialTheme {
Column {
Button(onClick = {
list += Entity("new entity ${list.size + 1}", "remark")
}) {
Text("add new entity")
}
LazyColumn {
items(list) {
Text(it.name)
}
}
}
}
}
fun main() = application {
// 删除第一个
DataSaverConverter.typeConverters.removeAt(0)
// 添加自定义转换
DataSaverConverter.registerTypeConverters<Entity>(
save = { bean -> JSONUtil.toJsonStr(bean) },
restore = { str ->
JSONUtil.toBean(str, Entity::class.java)
}
)
DataSaverConverter.registerTypeConverters<List<Entity>>(
save = { bean -> JSONUtil.toJsonStr(bean) },
restore = { str ->
JSONUtil.toList<Entity>(str, Entity::class.java)
}
)
Window(onCloseRequest = ::exitApplication) {
CompositionLocalProvider(LocalDataSaver provides dataSaver) {
App()
}
}
}
|
抱歉让您折腾这么久,确实没有 |
代码写的确实有问题,由于数据在读取之前不知道转换后具体是什么,这个 此外,顺带一提,由于您的 fun App() {
CompositionLocalProvider(LocalDataSaver provides AppConfig.dataSaver) {
MaterialTheme {
var list: List<Entity> by rememberDataSaverState<List<Entity>>(
key = "main.entity.list",
initialValue = emptyList()
)
Column {
Button(onClick = {
list += Entity("new entity ${list.size + 1}", "remark")
}) {
Text("add new entity")
}
LazyColumn {
items(list) {
Text(it.name)
}
}
}
}
}
} |
😄我看到了,但是对于 inline fun <reified T> mutableDataSaverStateOf(
dataSaverInterface: DataSaverInterface,
key: String,
initialValue: T,
savePolicy: SavePolicy = SavePolicy.IMMEDIATELY,
async: Boolean = true,
coroutineScope: CoroutineScope? = null
): DataSaverMutableState<T> {
val data = try {
if (!dataSaverInterface.contains(key)) initialValue
else dataSaverInterface.readData(key, initialValue)
} catch (e: Exception) {
val restore = DataSaverConverter.findRestorer<T>(initialValue)
restore ?: throw e
runCatching {
restore(dataSaverInterface.readData(key, "")) as T
}.onFailure {
DataSaverLogger.e("error while restoring data(key=$key), set to default. StackTrace:\n${it.stackTraceToString()}")
}.getOrDefault(initialValue)
}
return DataSaverMutableState(dataSaverInterface, key, data, savePolicy, async, coroutineScope)
} |
嗯嗯,这个方法是必传的,因为没有 @composable 修饰(可以用在 ViewModel 之中或其他全局单例),没法从 CompositionLocal 中获取到当前 DataSaverInterface 实例。我刚刚的回复是在 Composable 中,可以用 |
懂你的意思,非常感谢。(^▽^) |
哈喽,我又来了 var list: MutableList<Entity> by rememberDataSaverState<MutableList<Entity>>(
key = "main.entity.list",
initialValue = mutableListOf<Entity>(),
senseExternalDataChange = true
) |
Sorry这两天有点别的事,回的晚了些。 |
好的,有没有考虑过对mutableListOf进行支持。目前看添加后页面没有刷新(迷),数据没有保存 var list: MutableList<Entity> by rememberDataSaverState<MutableList<Entity>>(
key = "main.entity.list",
initialValue = mutableListOf<Entity>()
)
list.add(Entity("new entity ${list.size + 1}", "remark")) |
很难支持,因为我没有办法监听到 var list: MutableList<Entity> by rememberDataSaverState<MutableList<Entity>>(
key = "main.entity.list",
initialValue = mutableListOf<Entity>()
)
list = mutableListOf<Entity>().apply {
addAll(list)
add(Entity("new entity ${list.size + 1}", "remark"))
} 或者直接用 public operator fun <T> Collection<T>.plus(element: T): List<T> {
val result = ArrayList<T>(size + 1)
result.addAll(this)
result.add(element)
return result
}
var list: List<Entity> by rememberDataSaverState(
key = "main.entity.list",
initialValue = emptyList<Entity>()
)
list += Entity("new entity ${list.size + 1}", "remark") |
好的,大概什么时候发布新版本呐? |
抱歉才回复,这两天事情有点多。二是我发现了个小问题,如果注册了多个 我会尽量找时间维护的 |
希望可以使用类创建
比如
var entityList: List by rememberDataSaverListState(key = "main.entity.list", clazz = Entity::class.java)
The text was updated successfully, but these errors were encountered: