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 @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()) { configList.clear()
configFile.readText() configList.addAll(it)
} else {
String(App.INSTANCE.assets.open(readConfigFileName).readBytes())
} }
try { }
GSON.fromJsonArray<Config>(json)?.let {
configList.clear() private fun getConfigs(): List<Config>? {
configList.addAll(it) val configFile = File(configFilePath)
} ?: reset() if (configFile.exists()) {
} catch (e: Exception) { try {
reset() 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() { fun upBg() {
@ -68,13 +75,20 @@ 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(

@ -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