From 513e5537211482430eef69caa3b28299c1abb07a Mon Sep 17 00:00:00 2001 From: gedoor Date: Mon, 20 Jul 2020 22:14:09 +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/constant/PreferKey.kt | 1 + .../java/io/legado/app/help/storage/Restore.kt | 15 +++++++++++++++ .../io/legado/app/lib/dialogs/AlertBuilder.kt | 11 ++++++++++- .../app/lib/dialogs/AndroidAlertBuilder.kt | 15 ++++++++++++++- .../legado/app/ui/config/BackupConfigFragment.kt | 16 ++++++++++++++++ 5 files changed, 56 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/io/legado/app/constant/PreferKey.kt b/app/src/main/java/io/legado/app/constant/PreferKey.kt index 7e18a675b..729225d5f 100644 --- a/app/src/main/java/io/legado/app/constant/PreferKey.kt +++ b/app/src/main/java/io/legado/app/constant/PreferKey.kt @@ -25,6 +25,7 @@ object PreferKey { const val readBookFont = "readBookFont" const val fontFolder = "fontFolder" const val backupPath = "backupUri" + const val restoreIgnore = "restoreIgnore" const val threadCount = "threadCount" const val webPort = "webPort" const val keepLight = "keep_light" 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 770e2e337..4cd8b1712 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 @@ -24,6 +24,16 @@ import org.jetbrains.anko.defaultSharedPreferences import java.io.File object Restore { + private val ignoreConfigPath = + App.INSTANCE.filesDir.absolutePath + File.separator + "restoreIgnore.json" + val ignoreConfig: HashMap by lazy { + val file = FileUtils.createFileIfNotExist(ignoreConfigPath) + val json = file.readText() + GSON.fromJsonObject>(json) ?: hashMapOf() + } + val ignoreKeys = arrayOf("readConfig") + val ignoreTitle = arrayOf("阅读界面设置") + val jsonPath: ParseContext by lazy { JsonPath.using( Configuration.builder() @@ -143,6 +153,11 @@ object Restore { } } + fun saveIgnoreConfig() { + val json = GSON.toJson(ignoreConfig) + FileUtils.createFileIfNotExist(ignoreConfigPath).writeText(json) + } + private inline fun fileToListT(path: String, fileName: String): List? { try { val file = FileUtils.createFileIfNotExist(path + File.separator + fileName) diff --git a/app/src/main/java/io/legado/app/lib/dialogs/AlertBuilder.kt b/app/src/main/java/io/legado/app/lib/dialogs/AlertBuilder.kt index ae477acd6..56aa3a2be 100644 --- a/app/src/main/java/io/legado/app/lib/dialogs/AlertBuilder.kt +++ b/app/src/main/java/io/legado/app/lib/dialogs/AlertBuilder.kt @@ -75,7 +75,16 @@ interface AlertBuilder { fun onKeyPressed(handler: (dialog: DialogInterface, keyCode: Int, e: KeyEvent) -> Boolean) fun items(items: List, onItemSelected: (dialog: DialogInterface, index: Int) -> Unit) - fun items(items: List, onItemSelected: (dialog: DialogInterface, item: T, index: Int) -> Unit) + fun items( + items: List, + onItemSelected: (dialog: DialogInterface, item: T, index: Int) -> Unit + ) + + fun multiChoiceItems( + items: Array, + checkedItems: BooleanArray, + onClick: (dialog: DialogInterface, which: Int, isChecked: Boolean) -> Unit + ) fun build(): D fun show(): D diff --git a/app/src/main/java/io/legado/app/lib/dialogs/AndroidAlertBuilder.kt b/app/src/main/java/io/legado/app/lib/dialogs/AndroidAlertBuilder.kt index 52711a17a..49d059b84 100644 --- a/app/src/main/java/io/legado/app/lib/dialogs/AndroidAlertBuilder.kt +++ b/app/src/main/java/io/legado/app/lib/dialogs/AndroidAlertBuilder.kt @@ -105,12 +105,25 @@ internal class AndroidAlertBuilder(override val ctx: Context) : AlertBuilder items(items: List, onItemSelected: (dialog: DialogInterface, item: T, index: Int) -> Unit) { + override fun items( + items: List, + onItemSelected: (dialog: DialogInterface, item: T, index: Int) -> Unit + ) { builder.setItems(Array(items.size) { i -> items[i].toString() }) { dialog, which -> onItemSelected(dialog, items[which], which) } } + override fun multiChoiceItems( + items: Array, + checkedItems: BooleanArray, + onClick: (dialog: DialogInterface, which: Int, isChecked: Boolean) -> Unit + ) { + builder.setMultiChoiceItems(items, checkedItems) { dialog, which, isChecked -> + onClick(dialog, which, isChecked) + } + } + override fun build(): AlertDialog = builder.create() override fun show(): AlertDialog = builder.show() diff --git a/app/src/main/java/io/legado/app/ui/config/BackupConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/BackupConfigFragment.kt index 9df96e3f2..e684ccfff 100644 --- a/app/src/main/java/io/legado/app/ui/config/BackupConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/BackupConfigFragment.kt @@ -11,6 +11,8 @@ import androidx.preference.Preference import androidx.preference.PreferenceFragmentCompat import io.legado.app.R import io.legado.app.constant.PreferKey +import io.legado.app.help.storage.Restore +import io.legado.app.lib.dialogs.alert import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.accentColor import io.legado.app.ui.filechooser.FileChooserDialog @@ -104,6 +106,7 @@ class BackupConfigFragment : PreferenceFragmentCompat(), override fun onPreferenceTreeClick(preference: Preference?): Boolean { when (preference?.key) { PreferKey.backupPath -> BackupRestoreUi.selectBackupFolder(this) + PreferKey.restoreIgnore -> restoreIgnore() "web_dav_backup" -> BackupRestoreUi.backup(this) "web_dav_restore" -> BackupRestoreUi.restore(this) "import_old" -> BackupRestoreUi.importOldData(this) @@ -111,6 +114,19 @@ class BackupConfigFragment : PreferenceFragmentCompat(), return super.onPreferenceTreeClick(preference) } + private fun restoreIgnore() { + val checkedItems = BooleanArray(Restore.ignoreKeys.size) { + Restore.ignoreConfig[Restore.ignoreKeys[it]] ?: false + } + alert(R.string.restore_ignore) { + multiChoiceItems(Restore.ignoreTitle, checkedItems) { _, which, isChecked -> + Restore.ignoreConfig[Restore.ignoreKeys[which]] = isChecked + } + }.show().setOnDismissListener { + Restore.saveIgnoreConfig() + } + } + override fun onFilePicked(requestCode: Int, currentPath: String) { BackupRestoreUi.onFilePicked(requestCode, currentPath) }