fix: 修复颜色字符串非法时 Color.parseColor 崩溃的问题

ensureColorInts 和 ensureAccentColorInts 中对 toColorInt() 加 try-catch 保护,
解析失败时回退到字段默认值,避免数据库中存储损坏颜色字符串导致崩溃
This commit is contained in:
HapeLee
2026-06-17 23:27:56 +08:00
parent 047dbc6cf6
commit 502f7fbe12
@@ -1141,21 +1141,25 @@ object ReadBookConfig {
} }
} }
private fun String.toColorIntSafe(fallback: Int): Int {
return runCatching { toColorInt() }.getOrDefault(fallback)
}
private fun ensureColorInts() { private fun ensureColorInts() {
if (initColorInt) { if (initColorInt) {
return return
} }
textColorIntEInk = textColorEInk.toColorInt() textColorIntEInk = textColorEInk.toColorIntSafe(0xFF000000.toInt())
textColorIntNight = textColorNight.toColorInt() textColorIntNight = textColorNight.toColorIntSafe(0xFFADADAD.toInt())
textColorInt = textColor.toColorInt() textColorInt = textColor.toColorIntSafe(0xFF3E3D3B.toInt())
shadowColorNightInt = shadowColorN.toColorInt() shadowColorNightInt = shadowColorN.toColorIntSafe(0xFF3E3D3B.toInt())
shadowColorInt = shadowColor.toColorInt() shadowColorInt = shadowColor.toColorIntSafe(0xFF3E3D3B.toInt())
menuBgColorInt = menuBgColor.toColorInt() menuBgColorInt = menuBgColor.toColorIntSafe(-1)
menuBgColorNightInt = menuBgColorNight.toColorInt() menuBgColorNightInt = menuBgColorNight.toColorIntSafe(-1)
menuAcColorInt = menuAcColor.toColorInt() menuAcColorInt = menuAcColor.toColorIntSafe(-1)
menuAcColorNightInt = menuAcColorNight.toColorInt() menuAcColorNightInt = menuAcColorNight.toColorIntSafe(-1)
underlineColorInt = underlineColor.toColorInt() underlineColorInt = underlineColor.toColorIntSafe(0xFF3E3D3B.toInt())
underlineColorNightInt = underlineColorNight.toColorInt() underlineColorNightInt = underlineColorNight.toColorIntSafe(0xFFADADAD.toInt())
initColorInt = true initColorInt = true
} }
@@ -1163,9 +1167,9 @@ object ReadBookConfig {
if (initAccentColorInt) { if (initAccentColorInt) {
return return
} }
textAccentColorIntEInk = textAccentColorEInk.toColorInt() textAccentColorIntEInk = textAccentColorEInk.toColorIntSafe(0xFF000000.toInt())
textAccentColorIntNight = textAccentColorNight.toColorInt() textAccentColorIntNight = textAccentColorNight.toColorIntSafe(0xFFFE4D55.toInt())
textAccentColorInt = textAccentColor.toColorInt() textAccentColorInt = textAccentColor.toColorIntSafe(0xFF834E00.toInt())
initAccentColorInt = true initAccentColorInt = true
} }