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.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,9 +438,7 @@ object ReadBookConfig {
return exportConfig
}
suspend fun import(byteArray: ByteArray): Result<Config> {
return kotlin.runCatching {
withContext(IO) {
fun import(byteArray: ByteArray): Config {
val configZipPath = FileUtils.getPath(appCtx.externalCache, "readConfig.zip")
FileUtils.delete(configZipPath)
val zipFile = FileUtils.createFileIfNotExist(configZipPath)
@@ -471,6 +468,8 @@ object ReadBookConfig {
}
}
config.bgStr = bgPath
} else if (config.bgType == 0) {
config.bgStr.toColorInt()
}
if (config.bgTypeNight == 2) {
val bgName = FileUtils.getName(config.bgStrNight)
@@ -483,6 +482,8 @@ object ReadBookConfig {
}
}
config.bgStrNight = bgPath
} else if (config.bgTypeNight == 0) {
config.bgStrNight.toColorInt()
}
if (config.bgTypeEInk == 2) {
val bgName = FileUtils.getName(config.bgStrEInk)
@@ -495,10 +496,11 @@ object ReadBookConfig {
}
}
config.bgStrEInk = bgPath
} else {
config.bgStrEInk.toColorInt()
}
return@withContext config
}
}
config.curTextColor()
return config
}
@Keep
@@ -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<Config>(json.trim { it < ' ' }).getOrNull()
?.let {
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<Config>? {
val configFile = File(configFilePath)
if (configFile.exists()) {
@@ -146,6 +165,7 @@ object ThemeConfig {
}
fun applyConfig(context: Context, config: Config) {
try {
val primary = Color.parseColor(config.primaryColor)
val accent = Color.parseColor(config.accentColor)
val background = Color.parseColor(config.backgroundColor)
@@ -163,6 +183,9 @@ object ThemeConfig {
}
AppConfig.isNightTheme = config.isNightTheme
applyDayNight(context)
} catch (e: Exception) {
AppLog.put("设置主题出错\n$e", e, true)
}
}
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) {
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
@@ -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))