From b3dcd37d668e5d87a674ed45599369005420148e Mon Sep 17 00:00:00 2001 From: Horis <8674809+821938089@users.noreply.github.com> Date: Thu, 26 Jun 2025 18:43:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../legado/app/help/config/ReadBookConfig.kt | 122 +++++++++--------- .../io/legado/app/help/config/ThemeConfig.kt | 59 ++++++--- .../ui/association/OnLineImportViewModel.kt | 2 +- .../ui/book/read/config/BgTextConfigDialog.kt | 2 +- 4 files changed, 105 insertions(+), 80 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/config/ReadBookConfig.kt b/app/src/main/java/io/legado/app/help/config/ReadBookConfig.kt index e7e198140..7121196de 100644 --- a/app/src/main/java/io/legado/app/help/config/ReadBookConfig.kt +++ b/app/src/main/java/io/legado/app/help/config/ReadBookConfig.kt @@ -5,6 +5,7 @@ import android.graphics.drawable.BitmapDrawable import android.graphics.drawable.ColorDrawable import android.graphics.drawable.Drawable import androidx.annotation.Keep +import androidx.core.graphics.toColorInt import io.legado.app.R import io.legado.app.constant.AppLog 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.putPrefInt import io.legado.app.utils.resizeAndRecycle -import kotlinx.coroutines.Dispatchers.IO -import kotlinx.coroutines.withContext import splitties.init.appCtx import java.io.File @@ -439,66 +438,69 @@ object ReadBookConfig { return exportConfig } - suspend fun import(byteArray: ByteArray): Result { - return kotlin.runCatching { - withContext(IO) { - val configZipPath = FileUtils.getPath(appCtx.externalCache, "readConfig.zip") - FileUtils.delete(configZipPath) - val zipFile = FileUtils.createFileIfNotExist(configZipPath) - zipFile.writeBytes(byteArray) - val configDir = appCtx.externalCache.getFile("readConfig") - configDir.createFolderReplace() - ZipUtils.unZipToPath(zipFile, configDir) - val configFile = configDir.getFile(configFileName) - val config: Config = GSON.fromJsonObject(configFile.readText()).getOrThrow() - if (config.textFont.isNotEmpty()) { - val fontName = FileUtils.getName(config.textFont) - val fontPath = - FileUtils.getPath(appCtx.externalFiles, "font", fontName) - if (!FileUtils.exist(fontPath)) { - configDir.getFile(fontName).copyTo(File(fontPath)) - } - config.textFont = fontPath - } - if (config.bgType == 2) { - val bgName = FileUtils.getName(config.bgStr) - config.bgStr = bgName - val bgPath = FileUtils.getPath(appCtx.externalFiles, "bg", bgName) - if (!FileUtils.exist(bgPath)) { - val bgFile = configDir.getFile(bgName) - if (bgFile.exists()) { - bgFile.copyTo(File(bgPath)) - } - } - config.bgStr = bgPath - } - if (config.bgTypeNight == 2) { - val bgName = FileUtils.getName(config.bgStrNight) - config.bgStrNight = bgName - val bgPath = FileUtils.getPath(appCtx.externalFiles, "bg", bgName) - if (!FileUtils.exist(bgPath)) { - val bgFile = configDir.getFile(bgName) - if (bgFile.exists()) { - bgFile.copyTo(File(bgPath)) - } - } - config.bgStrNight = bgPath - } - if (config.bgTypeEInk == 2) { - val bgName = FileUtils.getName(config.bgStrEInk) - config.bgStrEInk = bgName - val bgPath = FileUtils.getPath(appCtx.externalFiles, "bg", bgName) - if (!FileUtils.exist(bgPath)) { - val bgFile = configDir.getFile(bgName) - if (bgFile.exists()) { - bgFile.copyTo(File(bgPath)) - } - } - config.bgStrEInk = bgPath - } - return@withContext config + fun import(byteArray: ByteArray): Config { + val configZipPath = FileUtils.getPath(appCtx.externalCache, "readConfig.zip") + FileUtils.delete(configZipPath) + val zipFile = FileUtils.createFileIfNotExist(configZipPath) + zipFile.writeBytes(byteArray) + val configDir = appCtx.externalCache.getFile("readConfig") + configDir.createFolderReplace() + ZipUtils.unZipToPath(zipFile, configDir) + val configFile = configDir.getFile(configFileName) + val config: Config = GSON.fromJsonObject(configFile.readText()).getOrThrow() + if (config.textFont.isNotEmpty()) { + val fontName = FileUtils.getName(config.textFont) + val fontPath = + FileUtils.getPath(appCtx.externalFiles, "font", fontName) + if (!FileUtils.exist(fontPath)) { + configDir.getFile(fontName).copyTo(File(fontPath)) } + config.textFont = fontPath } + if (config.bgType == 2) { + val bgName = FileUtils.getName(config.bgStr) + config.bgStr = bgName + val bgPath = FileUtils.getPath(appCtx.externalFiles, "bg", bgName) + if (!FileUtils.exist(bgPath)) { + val bgFile = configDir.getFile(bgName) + if (bgFile.exists()) { + bgFile.copyTo(File(bgPath)) + } + } + config.bgStr = bgPath + } else if (config.bgType == 0) { + config.bgStr.toColorInt() + } + if (config.bgTypeNight == 2) { + val bgName = FileUtils.getName(config.bgStrNight) + config.bgStrNight = bgName + val bgPath = FileUtils.getPath(appCtx.externalFiles, "bg", bgName) + if (!FileUtils.exist(bgPath)) { + val bgFile = configDir.getFile(bgName) + if (bgFile.exists()) { + bgFile.copyTo(File(bgPath)) + } + } + config.bgStrNight = bgPath + } else if (config.bgTypeNight == 0) { + config.bgStrNight.toColorInt() + } + if (config.bgTypeEInk == 2) { + val bgName = FileUtils.getName(config.bgStrEInk) + config.bgStrEInk = bgName + val bgPath = FileUtils.getPath(appCtx.externalFiles, "bg", bgName) + if (!FileUtils.exist(bgPath)) { + val bgFile = configDir.getFile(bgName) + if (bgFile.exists()) { + bgFile.copyTo(File(bgPath)) + } + } + config.bgStrEInk = bgPath + } else { + config.bgStrEInk.toColorInt() + } + config.curTextColor() + return config } @Keep diff --git a/app/src/main/java/io/legado/app/help/config/ThemeConfig.kt b/app/src/main/java/io/legado/app/help/config/ThemeConfig.kt index ddc4d0029..5e86913d9 100644 --- a/app/src/main/java/io/legado/app/help/config/ThemeConfig.kt +++ b/app/src/main/java/io/legado/app/help/config/ThemeConfig.kt @@ -6,7 +6,9 @@ import android.graphics.Color import android.util.DisplayMetrics import androidx.annotation.Keep import androidx.appcompat.app.AppCompatDelegate +import androidx.core.graphics.toColorInt import io.legado.app.R +import io.legado.app.constant.AppLog import io.legado.app.constant.EventBus import io.legado.app.constant.PreferKey import io.legado.app.constant.Theme @@ -115,13 +117,18 @@ object ThemeConfig { fun addConfig(json: String): Boolean { GSON.fromJsonObject(json.trim { it < ' ' }).getOrNull() ?.let { - addConfig(it) - return true + if (validateConfig(it)) { + addConfig(it) + return true + } } return false } fun addConfig(newConfig: Config) { + if (!validateConfig(newConfig)) { + return + } configList.forEachIndexed { index, config -> if (newConfig.themeName == config.themeName) { configList[index] = newConfig @@ -132,6 +139,18 @@ object ThemeConfig { 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? { val configFile = File(configFilePath) if (configFile.exists()) { @@ -146,23 +165,27 @@ object ThemeConfig { } fun applyConfig(context: Context, config: Config) { - val primary = Color.parseColor(config.primaryColor) - val accent = Color.parseColor(config.accentColor) - val background = Color.parseColor(config.backgroundColor) - val bBackground = Color.parseColor(config.bottomBackground) - if (config.isNightTheme) { - context.putPrefInt(PreferKey.cNPrimary, primary) - context.putPrefInt(PreferKey.cNAccent, accent) - context.putPrefInt(PreferKey.cNBackground, background) - context.putPrefInt(PreferKey.cNBBackground, bBackground) - } else { - context.putPrefInt(PreferKey.cPrimary, primary) - context.putPrefInt(PreferKey.cAccent, accent) - context.putPrefInt(PreferKey.cBackground, background) - context.putPrefInt(PreferKey.cBBackground, bBackground) + try { + val primary = Color.parseColor(config.primaryColor) + val accent = Color.parseColor(config.accentColor) + val background = Color.parseColor(config.backgroundColor) + val bBackground = Color.parseColor(config.bottomBackground) + if (config.isNightTheme) { + context.putPrefInt(PreferKey.cNPrimary, primary) + context.putPrefInt(PreferKey.cNAccent, accent) + context.putPrefInt(PreferKey.cNBackground, background) + context.putPrefInt(PreferKey.cNBBackground, bBackground) + } else { + context.putPrefInt(PreferKey.cPrimary, primary) + context.putPrefInt(PreferKey.cAccent, accent) + context.putPrefInt(PreferKey.cBackground, background) + context.putPrefInt(PreferKey.cBBackground, bBackground) + } + AppConfig.isNightTheme = config.isNightTheme + applyDayNight(context) + } catch (e: Exception) { + AppLog.put("设置主题出错\n$e", e, true) } - AppConfig.isNightTheme = config.isNightTheme - applyDayNight(context) } fun saveDayTheme(context: Context, name: String) { diff --git a/app/src/main/java/io/legado/app/ui/association/OnLineImportViewModel.kt b/app/src/main/java/io/legado/app/ui/association/OnLineImportViewModel.kt index d6f949280..d95aac5ec 100644 --- a/app/src/main/java/io/legado/app/ui/association/OnLineImportViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/association/OnLineImportViewModel.kt @@ -56,7 +56,7 @@ class OnLineImportViewModel(app: Application) : BaseAssociationViewModel(app) { fun importReadConfig(bytes: ByteArray, finally: (title: String, msg: String) -> Unit) { execute { - val config = ReadBookConfig.import(bytes).getOrThrow() + val config = ReadBookConfig.import(bytes) ReadBookConfig.configList.forEachIndexed { index, c -> if (c.name == config.name) { ReadBookConfig.configList[index] = config diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt index 28b6d058f..d8a6182e3 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt @@ -383,7 +383,7 @@ class BgTextConfigDialog : BaseDialogFragment(R.layout.dialog_read_bg_text) { private fun importConfig(byteArray: ByteArray) { execute { - ReadBookConfig.import(byteArray).getOrThrow() + ReadBookConfig.import(byteArray) }.onSuccess { ReadBookConfig.durConfig = it postEvent(EventBus.UP_CONFIG, arrayListOf(1, 2, 5))