From 6f2f506bee1b9ff4576c0d81c9cbccc38c5d940f Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 15 Oct 2019 16:00:34 +0800 Subject: [PATCH] up --- .../java/io/legado/app/help/ReadBookConfig.kt | 50 ++++++++----------- 1 file changed, 20 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/ReadBookConfig.kt b/app/src/main/java/io/legado/app/help/ReadBookConfig.kt index 8c0266c63..8c7d4ae18 100644 --- a/app/src/main/java/io/legado/app/help/ReadBookConfig.kt +++ b/app/src/main/java/io/legado/app/help/ReadBookConfig.kt @@ -20,36 +20,40 @@ import java.io.IOException */ object ReadBookConfig { const val readConfigFileName = "readConfig.json" - val configList: ArrayList = arrayListOf() - .apply { - upConfig(this) - } + val configList: ArrayList = arrayListOf() var styleSelect get() = App.INSTANCE.getPrefInt("readStyleSelect") set(value) = App.INSTANCE.putPrefInt("readStyleSelect", value) var bg: Drawable? = null + init { + upConfig() + } + + @Synchronized fun getConfig(index: Int = styleSelect): Config { + if (configList.size < 5) { + reset() + } return configList[index] } - fun upConfig(list: ArrayList = configList) { + fun upConfig() { val configFile = File(App.INSTANCE.filesDir.absolutePath + File.separator + readConfigFileName) val json = if (configFile.exists()) { - String(configFile.readBytes()) + configFile.readText() } else { String(App.INSTANCE.assets.open(readConfigFileName).readBytes()) } try { GSON.fromJsonArray(json)?.let { - list.clear() - list.addAll(it) - } + configList.clear() + configList.addAll(it) + } ?: reset() } catch (e: Exception) { - list.clear() - list.addAll(getOnError()) + reset() } } @@ -78,27 +82,13 @@ object ReadBookConfig { } } - fun reset() { - try { - val json = String(App.INSTANCE.assets.open(readConfigFileName).readBytes()) - GSON.fromJsonArray(json)?.let { - configList.clear() - configList.addAll(it) - } - } catch (e: Exception) { + private fun reset() { + val json = String(App.INSTANCE.assets.open(readConfigFileName).readBytes()) + GSON.fromJsonArray(json)?.let { configList.clear() - configList.addAll(getOnError()) + configList.addAll(it) } - } - - private fun getOnError(): ArrayList { - val list = arrayListOf() - list.add(Config()) - list.add(Config()) - list.add(Config()) - list.add(Config()) - list.add(Config()) - return list + save() } data class Config(