pull/37/head
kunfei 5 years ago
parent 9517a66c4f
commit 6f2f506bee
  1. 44
      app/src/main/java/io/legado/app/help/ReadBookConfig.kt

@ -20,36 +20,40 @@ import java.io.IOException
*/ */
object ReadBookConfig { object ReadBookConfig {
const val readConfigFileName = "readConfig.json" const val readConfigFileName = "readConfig.json"
val configList: ArrayList<Config> = arrayListOf<Config>() val configList: ArrayList<Config> = arrayListOf()
.apply {
upConfig(this)
}
var styleSelect var styleSelect
get() = App.INSTANCE.getPrefInt("readStyleSelect") get() = App.INSTANCE.getPrefInt("readStyleSelect")
set(value) = App.INSTANCE.putPrefInt("readStyleSelect", value) set(value) = App.INSTANCE.putPrefInt("readStyleSelect", value)
var bg: Drawable? = null var bg: Drawable? = null
init {
upConfig()
}
@Synchronized
fun getConfig(index: Int = styleSelect): Config { fun getConfig(index: Int = styleSelect): Config {
if (configList.size < 5) {
reset()
}
return configList[index] return configList[index]
} }
fun upConfig(list: ArrayList<Config> = configList) { fun upConfig() {
val configFile = val configFile =
File(App.INSTANCE.filesDir.absolutePath + File.separator + readConfigFileName) File(App.INSTANCE.filesDir.absolutePath + File.separator + readConfigFileName)
val json = if (configFile.exists()) { val json = if (configFile.exists()) {
String(configFile.readBytes()) configFile.readText()
} else { } else {
String(App.INSTANCE.assets.open(readConfigFileName).readBytes()) String(App.INSTANCE.assets.open(readConfigFileName).readBytes())
} }
try { try {
GSON.fromJsonArray<Config>(json)?.let { GSON.fromJsonArray<Config>(json)?.let {
list.clear() configList.clear()
list.addAll(it) configList.addAll(it)
} } ?: reset()
} catch (e: Exception) { } catch (e: Exception) {
list.clear() reset()
list.addAll(getOnError())
} }
} }
@ -78,27 +82,13 @@ object ReadBookConfig {
} }
} }
fun reset() { private fun reset() {
try {
val json = String(App.INSTANCE.assets.open(readConfigFileName).readBytes()) val json = String(App.INSTANCE.assets.open(readConfigFileName).readBytes())
GSON.fromJsonArray<Config>(json)?.let { GSON.fromJsonArray<Config>(json)?.let {
configList.clear() configList.clear()
configList.addAll(it) configList.addAll(it)
} }
} catch (e: Exception) { save()
configList.clear()
configList.addAll(getOnError())
}
}
private fun getOnError(): ArrayList<Config> {
val list = arrayListOf<Config>()
list.add(Config())
list.add(Config())
list.add(Config())
list.add(Config())
list.add(Config())
return list
} }
data class Config( data class Config(

Loading…
Cancel
Save