feat: 优化代码

pull/97/head
kunfei 5 years ago
parent 2a95f558af
commit 404660526f
  1. 44
      app/src/main/java/io/legado/app/help/ReadBookConfig.kt
  2. 3
      app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt

@ -31,27 +31,34 @@ object ReadBookConfig {
@Synchronized
fun getConfig(index: Int = styleSelect): Config {
if (configList.size < 5) {
reset()
resetAll()
}
return configList[index]
}
fun upConfig() {
val configFile = File(configFilePath)
val json = if (configFile.exists()) {
configFile.readText()
} else {
String(App.INSTANCE.assets.open(readConfigFileName).readBytes())
}
try {
GSON.fromJsonArray<Config>(json)?.let {
(getConfigs() ?: getDefaultConfigs()).let {
configList.clear()
configList.addAll(it)
} ?: reset()
}
}
private fun getConfigs(): List<Config>? {
val configFile = File(configFilePath)
if (configFile.exists()) {
try {
val json = configFile.readText()
return GSON.fromJsonArray(json)
} catch (e: Exception) {
reset()
}
}
return null
}
private fun getDefaultConfigs(): List<Config> {
val json = String(App.INSTANCE.assets.open(readConfigFileName).readBytes())
return GSON.fromJsonArray(json)!!
}
fun upBg() {
val resources = App.INSTANCE.resources
@ -68,14 +75,21 @@ object ReadBookConfig {
}
}
private fun reset() {
val json = String(App.INSTANCE.assets.open(readConfigFileName).readBytes())
GSON.fromJsonArray<Config>(json)?.let {
fun resetDur() {
getDefaultConfigs()[styleSelect].let {
getConfig().setBg(it.bgType(), it.bgStr())
getConfig().setTextColor(it.textColor())
save()
}
}
private fun resetAll() {
getDefaultConfigs().let {
configList.clear()
configList.addAll(it)
}
save()
}
}
data class Config(
var bgStr: String = "#EEEEEE",//白天背景

@ -128,7 +128,8 @@ class BgTextConfigDialog : DialogFragment() {
.show(requireActivity())
}
tv_default.onClick {
ReadBookConfig.resetDur()
postEvent(EventBus.UP_CONFIG, false)
}
}

Loading…
Cancel
Save