This commit is contained in:
Horis
2025-06-26 18:43:04 +08:00
parent c364fac58a
commit b3dcd37d66
4 changed files with 105 additions and 80 deletions
@@ -5,6 +5,7 @@ import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.ColorDrawable import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import androidx.annotation.Keep import androidx.annotation.Keep
import androidx.core.graphics.toColorInt
import io.legado.app.R import io.legado.app.R
import io.legado.app.constant.AppLog import io.legado.app.constant.AppLog
import io.legado.app.constant.PageAnim import io.legado.app.constant.PageAnim
@@ -30,8 +31,6 @@ import io.legado.app.utils.printOnDebug
import io.legado.app.utils.putPrefBoolean import io.legado.app.utils.putPrefBoolean
import io.legado.app.utils.putPrefInt import io.legado.app.utils.putPrefInt
import io.legado.app.utils.resizeAndRecycle import io.legado.app.utils.resizeAndRecycle
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.withContext
import splitties.init.appCtx import splitties.init.appCtx
import java.io.File import java.io.File
@@ -439,9 +438,7 @@ object ReadBookConfig {
return exportConfig return exportConfig
} }
suspend fun import(byteArray: ByteArray): Result<Config> { fun import(byteArray: ByteArray): Config {
return kotlin.runCatching {
withContext(IO) {
val configZipPath = FileUtils.getPath(appCtx.externalCache, "readConfig.zip") val configZipPath = FileUtils.getPath(appCtx.externalCache, "readConfig.zip")
FileUtils.delete(configZipPath) FileUtils.delete(configZipPath)
val zipFile = FileUtils.createFileIfNotExist(configZipPath) val zipFile = FileUtils.createFileIfNotExist(configZipPath)
@@ -471,6 +468,8 @@ object ReadBookConfig {
} }
} }
config.bgStr = bgPath config.bgStr = bgPath
} else if (config.bgType == 0) {
config.bgStr.toColorInt()
} }
if (config.bgTypeNight == 2) { if (config.bgTypeNight == 2) {
val bgName = FileUtils.getName(config.bgStrNight) val bgName = FileUtils.getName(config.bgStrNight)
@@ -483,6 +482,8 @@ object ReadBookConfig {
} }
} }
config.bgStrNight = bgPath config.bgStrNight = bgPath
} else if (config.bgTypeNight == 0) {
config.bgStrNight.toColorInt()
} }
if (config.bgTypeEInk == 2) { if (config.bgTypeEInk == 2) {
val bgName = FileUtils.getName(config.bgStrEInk) val bgName = FileUtils.getName(config.bgStrEInk)
@@ -495,10 +496,11 @@ object ReadBookConfig {
} }
} }
config.bgStrEInk = bgPath config.bgStrEInk = bgPath
} else {
config.bgStrEInk.toColorInt()
} }
return@withContext config config.curTextColor()
} return config
}
} }
@Keep @Keep
@@ -6,7 +6,9 @@ import android.graphics.Color
import android.util.DisplayMetrics import android.util.DisplayMetrics
import androidx.annotation.Keep import androidx.annotation.Keep
import androidx.appcompat.app.AppCompatDelegate import androidx.appcompat.app.AppCompatDelegate
import androidx.core.graphics.toColorInt
import io.legado.app.R import io.legado.app.R
import io.legado.app.constant.AppLog
import io.legado.app.constant.EventBus import io.legado.app.constant.EventBus
import io.legado.app.constant.PreferKey import io.legado.app.constant.PreferKey
import io.legado.app.constant.Theme import io.legado.app.constant.Theme
@@ -115,13 +117,18 @@ object ThemeConfig {
fun addConfig(json: String): Boolean { fun addConfig(json: String): Boolean {
GSON.fromJsonObject<Config>(json.trim { it < ' ' }).getOrNull() GSON.fromJsonObject<Config>(json.trim { it < ' ' }).getOrNull()
?.let { ?.let {
if (validateConfig(it)) {
addConfig(it) addConfig(it)
return true return true
} }
}
return false return false
} }
fun addConfig(newConfig: Config) { fun addConfig(newConfig: Config) {
if (!validateConfig(newConfig)) {
return
}
configList.forEachIndexed { index, config -> configList.forEachIndexed { index, config ->
if (newConfig.themeName == config.themeName) { if (newConfig.themeName == config.themeName) {
configList[index] = newConfig configList[index] = newConfig
@@ -132,6 +139,18 @@ object ThemeConfig {
save() save()
} }
private fun validateConfig(config: Config): Boolean {
try {
config.primaryColor.toColorInt()
config.accentColor.toColorInt()
config.backgroundColor.toColorInt()
config.bottomBackground.toColorInt()
return true
} catch (_: Exception) {
return false
}
}
private fun getConfigs(): List<Config>? { private fun getConfigs(): List<Config>? {
val configFile = File(configFilePath) val configFile = File(configFilePath)
if (configFile.exists()) { if (configFile.exists()) {
@@ -146,6 +165,7 @@ object ThemeConfig {
} }
fun applyConfig(context: Context, config: Config) { fun applyConfig(context: Context, config: Config) {
try {
val primary = Color.parseColor(config.primaryColor) val primary = Color.parseColor(config.primaryColor)
val accent = Color.parseColor(config.accentColor) val accent = Color.parseColor(config.accentColor)
val background = Color.parseColor(config.backgroundColor) val background = Color.parseColor(config.backgroundColor)
@@ -163,6 +183,9 @@ object ThemeConfig {
} }
AppConfig.isNightTheme = config.isNightTheme AppConfig.isNightTheme = config.isNightTheme
applyDayNight(context) applyDayNight(context)
} catch (e: Exception) {
AppLog.put("设置主题出错\n$e", e, true)
}
} }
fun saveDayTheme(context: Context, name: String) { fun saveDayTheme(context: Context, name: String) {
@@ -56,7 +56,7 @@ class OnLineImportViewModel(app: Application) : BaseAssociationViewModel(app) {
fun importReadConfig(bytes: ByteArray, finally: (title: String, msg: String) -> Unit) { fun importReadConfig(bytes: ByteArray, finally: (title: String, msg: String) -> Unit) {
execute { execute {
val config = ReadBookConfig.import(bytes).getOrThrow() val config = ReadBookConfig.import(bytes)
ReadBookConfig.configList.forEachIndexed { index, c -> ReadBookConfig.configList.forEachIndexed { index, c ->
if (c.name == config.name) { if (c.name == config.name) {
ReadBookConfig.configList[index] = config ReadBookConfig.configList[index] = config
@@ -383,7 +383,7 @@ class BgTextConfigDialog : BaseDialogFragment(R.layout.dialog_read_bg_text) {
private fun importConfig(byteArray: ByteArray) { private fun importConfig(byteArray: ByteArray) {
execute { execute {
ReadBookConfig.import(byteArray).getOrThrow() ReadBookConfig.import(byteArray)
}.onSuccess { }.onSuccess {
ReadBookConfig.durConfig = it ReadBookConfig.durConfig = it
postEvent(EventBus.UP_CONFIG, arrayListOf(1, 2, 5)) postEvent(EventBus.UP_CONFIG, arrayListOf(1, 2, 5))