From 9b5c5f2a0d44450a2f5c38424ec845873c1e2d97 Mon Sep 17 00:00:00 2001 From: gedoor Date: Mon, 20 Jul 2020 22:36:51 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/help/ReadBookConfig.kt | 3 +- .../io/legado/app/help/storage/Restore.kt | 39 +++++++++++++------ .../java/io/legado/app/utils/FileUtils.kt | 11 ++++-- 3 files changed, 35 insertions(+), 18 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 fca1ef1fa..73e344393 100644 --- a/app/src/main/java/io/legado/app/help/ReadBookConfig.kt +++ b/app/src/main/java/io/legado/app/help/ReadBookConfig.kt @@ -19,8 +19,7 @@ import java.io.File @Keep object ReadBookConfig { const val readConfigFileName = "readConfig.json" - private val configFilePath = - App.INSTANCE.filesDir.absolutePath + File.separator + readConfigFileName + private val configFilePath = FileUtils.getPath(App.INSTANCE.filesDir, readConfigFileName) val configList: ArrayList = arrayListOf() private val defaultConfigs by lazy { val json = String(App.INSTANCE.assets.open(readConfigFileName).readBytes()) diff --git a/app/src/main/java/io/legado/app/help/storage/Restore.kt b/app/src/main/java/io/legado/app/help/storage/Restore.kt index 4cd8b1712..930132990 100644 --- a/app/src/main/java/io/legado/app/help/storage/Restore.kt +++ b/app/src/main/java/io/legado/app/help/storage/Restore.kt @@ -33,6 +33,16 @@ object Restore { } val ignoreKeys = arrayOf("readConfig") val ignoreTitle = arrayOf("阅读界面设置") + private val ignorePrefKeys = arrayOf(PreferKey.versionCode, PreferKey.defaultCover) + private val readPrefKeys = arrayOf( + PreferKey.readStyleSelect, + PreferKey.shareLayout, + PreferKey.pageAnim, + PreferKey.hideStatusBar, + PreferKey.hideNavigationBar, + PreferKey.bodyIndent, + PreferKey.autoReadSpeed + ) val jsonPath: ParseContext by lazy { JsonPath.using( @@ -102,24 +112,27 @@ object Restore { suspend fun restoreConfig(path: String = Backup.backupPath) { withContext(IO) { - try { - val file = - FileUtils.createFileIfNotExist(path + File.separator + ReadBookConfig.readConfigFileName) - val configFile = - FileUtils.getFile(App.INSTANCE.filesDir, ReadBookConfig.readConfigFileName) - if (file.exists()) { - file.copyTo(configFile, true) - ReadBookConfig.upConfig() + if (!ignoreReadConfig) { + try { + val file = + FileUtils.createFileIfNotExist(path + File.separator + ReadBookConfig.readConfigFileName) + val configFile = + FileUtils.getFile(App.INSTANCE.filesDir, ReadBookConfig.readConfigFileName) + if (file.exists()) { + file.copyTo(configFile, true) + ReadBookConfig.upConfig() + } + } catch (e: Exception) { + e.printStackTrace() } - } catch (e: Exception) { - e.printStackTrace() } Preferences.getSharedPreferences(App.INSTANCE, path, "config")?.all ?.let { map -> - val ignoreKeys = arrayOf(PreferKey.versionCode, PreferKey.defaultCover) val edit = App.INSTANCE.defaultSharedPreferences.edit() map.forEach { - if (!ignoreKeys.contains(it.key)) { + if (!ignorePrefKeys.contains(it.key) + && !(readPrefKeys.contains(it.key) && ignoreReadConfig) + ) { when (val value = it.value) { is Int -> edit.putInt(it.key, value) is Boolean -> edit.putBoolean(it.key, value) @@ -153,6 +166,8 @@ object Restore { } } + val ignoreReadConfig: Boolean get() = ignoreConfig["readConfig"] == true + fun saveIgnoreConfig() { val json = GSON.toJson(ignoreConfig) FileUtils.createFileIfNotExist(ignoreConfigPath).writeText(json) diff --git a/app/src/main/java/io/legado/app/utils/FileUtils.kt b/app/src/main/java/io/legado/app/utils/FileUtils.kt index a9408dd10..24882c752 100644 --- a/app/src/main/java/io/legado/app/utils/FileUtils.kt +++ b/app/src/main/java/io/legado/app/utils/FileUtils.kt @@ -60,11 +60,14 @@ object FileUtils { } fun getPath(root: File, fileName: String? = null, vararg subDirs: String): String { - return if (fileName.isNullOrEmpty()) { - root.absolutePath + File.separator + subDirs.joinToString(File.separator) - } else { - root.absolutePath + File.separator + subDirs.joinToString(File.separator) + File.separator + fileName + val path = StringBuilder(root.absolutePath).append(File.separator) + subDirs.forEach { + path.append(it).append(File.separator) } + if (!fileName.isNullOrEmpty()) { + path.append(fileName) + } + return path.toString() } //递归删除文件夹下的数据