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

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

Loading…
Cancel
Save