fix: dataStore逻辑

This commit is contained in:
HapeLee committed 2026-06-08 13:51:18 +08:00
1 parent 99b6635f04
commit 51d5efbc2b
2 files changed
+8 -12

No files matched your search

@@ -59,7 +59,6 @@ fun <T> prefDelegate(
// 从 DataStore 读取初始值(DS 为唯一读取源) // 从 DataStore 读取初始值(DS 为唯一读取源)
scope.launch { scope.launch {
val dsValue = readFromDs() val dsValue = readFromDs()
android.util.Log.d("PrefDelegate", "readFromDs key=$key defaultValue=$defaultValue dsValue=$dsValue")
if (dsValue != null) { if (dsValue != null) {
_value.value = dsValue _value.value = dsValue
} }
@@ -128,7 +127,7 @@ fun <T> prefDelegate(
} }
// 同步写入 DataStore,确保持久化后再返回 // 同步写入 DataStore,确保持久化后再返回
// 异常不阻断流程:SP 已写入,readFromDs() 会回退到 SP 读取 // 异常不阻断流程:SP 已写入,readFromDs() 会回退到 SP 读取
val dsResult = runCatching { runCatching {
runBlocking(Dispatchers.IO) { runBlocking(Dispatchers.IO) {
when (value) { when (value) {
is String? -> DsSync.putString(key, value) is String? -> DsSync.putString(key, value)
@@ -139,7 +138,6 @@ fun <T> prefDelegate(
} }
} }
} }
android.util.Log.d("PrefDelegate", "setValue key=$key value=$value dsResult=${dsResult.isSuccess}")
onValueChange?.invoke(value) onValueChange?.invoke(value)
} }
} }
@@ -177,12 +175,8 @@ fun <T> prefDelegate(
null null
} }
// DS 有值,直接返回 // DS 有值,直接返回
if (dsValue != null) { if (dsValue != null) return dsValue
android.util.Log.d("PrefDelegate", "readFromDs key=$key DS hit=$dsValue")
return dsValue
}
// DS 无值,回退到 SP(迁移遗漏时的补偿) // DS 无值,回退到 SP(迁移遗漏时的补偿)
android.util.Log.d("PrefDelegate", "readFromDs key=$key DS miss, falling back to SP")
val spValue: T? = when { val spValue: T? = when {
defaultValue is String || defaultValue == null -> defaultValue is String || defaultValue == null ->
appCtx.getPrefString(key, defaultValue as String?) as T? appCtx.getPrefString(key, defaultValue as String?) as T?
@@ -208,7 +202,6 @@ fun <T> prefDelegate(
} }
} }
} }
android.util.Log.d("PrefDelegate", "readFromDs key=$key SP fallback=$spValue (defaultValue=$defaultValue)")
return spValue return spValue
} }
} }
@@ -846,9 +846,12 @@ fun BookshelfScreen(
val group = uiState.groups.getOrNull(pageIndex) val group = uiState.groups.getOrNull(pageIndex)
if (group != null) { if (group != null) {
val isSelectedGroup = group.groupId == uiState.selectedGroupId val isSelectedGroup = group.groupId == uiState.selectedGroupId
val books = if (isSelectedGroup) uiState.items val books = if (isSelectedGroup && uiState.isSearch) {
else uiState.allGroupBooks[group.groupId] uiState.items
?: persistentListOf() } else {
uiState.allGroupBooks[group.groupId]
?: persistentListOf()
}
val canReorderBooks = isEditMode && val canReorderBooks = isEditMode &&
!uiState.isSearch && !uiState.isSearch &&
(group.bookSort.takeIf { it >= 0 } (group.bookSort.takeIf { it >= 0 }