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,66 +438,69 @@ object ReadBookConfig {
return exportConfig return exportConfig
} }
suspend fun import(byteArray: ByteArray): Result<Config> { fun import(byteArray: ByteArray): Config {
return kotlin.runCatching { val configZipPath = FileUtils.getPath(appCtx.externalCache, "readConfig.zip")
withContext(IO) { FileUtils.delete(configZipPath)
val configZipPath = FileUtils.getPath(appCtx.externalCache, "readConfig.zip") val zipFile = FileUtils.createFileIfNotExist(configZipPath)
FileUtils.delete(configZipPath) zipFile.writeBytes(byteArray)
val zipFile = FileUtils.createFileIfNotExist(configZipPath) val configDir = appCtx.externalCache.getFile("readConfig")
zipFile.writeBytes(byteArray) configDir.createFolderReplace()
val configDir = appCtx.externalCache.getFile("readConfig") ZipUtils.unZipToPath(zipFile, configDir)
configDir.createFolderReplace() val configFile = configDir.getFile(configFileName)
ZipUtils.unZipToPath(zipFile, configDir) val config: Config = GSON.fromJsonObject<Config>(configFile.readText()).getOrThrow()
val configFile = configDir.getFile(configFileName) if (config.textFont.isNotEmpty()) {
val config: Config = GSON.fromJsonObject<Config>(configFile.readText()).getOrThrow() val fontName = FileUtils.getName(config.textFont)
if (config.textFont.isNotEmpty()) { val fontPath =
val fontName = FileUtils.getName(config.textFont) FileUtils.getPath(appCtx.externalFiles, "font", fontName)
val fontPath = if (!FileUtils.exist(fontPath)) {
FileUtils.getPath(appCtx.externalFiles, "font", fontName) configDir.getFile(fontName).copyTo(File(fontPath))
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
} }
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 @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 {
addConfig(it) if (validateConfig(it)) {
return true addConfig(it)
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,23 +165,27 @@ object ThemeConfig {
} }
fun applyConfig(context: Context, config: Config) { fun applyConfig(context: Context, config: Config) {
val primary = Color.parseColor(config.primaryColor) try {
val accent = Color.parseColor(config.accentColor) val primary = Color.parseColor(config.primaryColor)
val background = Color.parseColor(config.backgroundColor) val accent = Color.parseColor(config.accentColor)
val bBackground = Color.parseColor(config.bottomBackground) val background = Color.parseColor(config.backgroundColor)
if (config.isNightTheme) { val bBackground = Color.parseColor(config.bottomBackground)
context.putPrefInt(PreferKey.cNPrimary, primary) if (config.isNightTheme) {
context.putPrefInt(PreferKey.cNAccent, accent) context.putPrefInt(PreferKey.cNPrimary, primary)
context.putPrefInt(PreferKey.cNBackground, background) context.putPrefInt(PreferKey.cNAccent, accent)
context.putPrefInt(PreferKey.cNBBackground, bBackground) context.putPrefInt(PreferKey.cNBackground, background)
} else { context.putPrefInt(PreferKey.cNBBackground, bBackground)
context.putPrefInt(PreferKey.cPrimary, primary) } else {
context.putPrefInt(PreferKey.cAccent, accent) context.putPrefInt(PreferKey.cPrimary, primary)
context.putPrefInt(PreferKey.cBackground, background) context.putPrefInt(PreferKey.cAccent, accent)
context.putPrefInt(PreferKey.cBBackground, bBackground) 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) { 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))