feat: 优化代码

pull/97/head
kunfei 5 years ago
parent 2a95f558af
commit 404660526f
  1. 48
      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,26 +31,33 @@ 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())
(getConfigs() ?: getDefaultConfigs()).let {
configList.clear()
configList.addAll(it)
}
try {
GSON.fromJsonArray<Config>(json)?.let {
configList.clear()
configList.addAll(it)
} ?: reset()
} catch (e: Exception) {
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) {
}
}
return null
}
private fun getDefaultConfigs(): List<Config> {
val json = String(App.INSTANCE.assets.open(readConfigFileName).readBytes())
return GSON.fromJsonArray(json)!!
}
fun upBg() {
@ -68,13 +75,20 @@ 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()
}
save()
}
data class Config(

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

Loading…
Cancel
Save