From d9b46e3e48bd6cb060c0f789913f9a9f4490797c Mon Sep 17 00:00:00 2001 From: kunfei Date: Wed, 4 Mar 2020 14:05:17 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/data/AppDatabase.kt | 2 +- .../main/java/io/legado/app/help/BookHelp.kt | 16 ++++--- .../io/legado/app/help/storage/Restore.kt | 43 +++++++++++-------- .../io/legado/app/help/storage/WebDavHelp.kt | 3 +- .../legado/app/ui/config/BackupRestoreUi.kt | 3 +- 5 files changed, 38 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/AppDatabase.kt b/app/src/main/java/io/legado/app/data/AppDatabase.kt index e548f4584..f32d6a199 100644 --- a/app/src/main/java/io/legado/app/data/AppDatabase.kt +++ b/app/src/main/java/io/legado/app/data/AppDatabase.kt @@ -32,7 +32,7 @@ abstract class AppDatabase : RoomDatabase() { .fallbackToDestructiveMigration() .addCallback(object : Callback() { override fun onDestructiveMigration(db: SupportSQLiteDatabase) { - GlobalScope.launch { Restore.restore(Backup.backupPath) } + GlobalScope.launch { Restore.restoreDatabase(Backup.backupPath) } } }) .build() diff --git a/app/src/main/java/io/legado/app/help/BookHelp.kt b/app/src/main/java/io/legado/app/help/BookHelp.kt index 7df2540c5..35d20ee45 100644 --- a/app/src/main/java/io/legado/app/help/BookHelp.kt +++ b/app/src/main/java/io/legado/app/help/BookHelp.kt @@ -272,13 +272,15 @@ object BookHelp { var c = content if (enableReplace) { upReplaceRules(name, origin) - for (item in replaceRules) { - item.pattern.let { - if (it.isNotEmpty()) { - c = if (item.isRegex) { - c.replace(it.toRegex(), item.replacement) - } else { - c.replace(it, item.replacement) + kotlin.runCatching { + for (item in replaceRules) { + item.pattern.let { + if (it.isNotEmpty()) { + c = if (item.isRegex) { + c.replace(it.toRegex(), item.replacement) + } else { + c.replace(it, item.replacement) + } } } } 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 8fb24a11e..8cdce0ae5 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 @@ -62,10 +62,11 @@ object Restore { } } } - restore(Backup.backupPath) + restoreDatabase() + restoreConfig() } - suspend fun restore(path: String) { + suspend fun restoreDatabase(path: String = Backup.backupPath) { withContext(IO) { fileToListT(path, "bookshelf.json")?.let { App.db.bookDao().insert(*it.toTypedArray()) @@ -85,11 +86,16 @@ object Restore { fileToListT(path, "replaceRule.json")?.let { App.db.replaceRuleDao().insert(*it.toTypedArray()) } + } + } + + suspend fun restoreConfig(path: String = Backup.backupPath) { + withContext(IO) { try { val file = FileUtils.createFileIfNotExist(path + File.separator + ReadBookConfig.readConfigFileName) val configFile = - File(App.INSTANCE.filesDir.absolutePath + File.separator + ReadBookConfig.readConfigFileName) + FileUtils.getFile(App.INSTANCE.filesDir, ReadBookConfig.readConfigFileName) if (file.exists()) { file.copyTo(configFile, true) ReadBookConfig.upConfig() @@ -109,24 +115,23 @@ object Restore { } edit.putInt(PreferKey.versionCode, App.INSTANCE.versionCode) edit.apply() - ReadBookConfig.apply { - styleSelect = App.INSTANCE.getPrefInt(PreferKey.readStyleSelect) - shareLayout = App.INSTANCE.getPrefBoolean(PreferKey.shareLayout) - pageAnim = App.INSTANCE.getPrefInt(PreferKey.pageAnim) - hideStatusBar = App.INSTANCE.getPrefBoolean(PreferKey.hideStatusBar) - hideNavigationBar = App.INSTANCE.getPrefBoolean(PreferKey.hideNavigationBar) - bodyIndentCount = App.INSTANCE.getPrefInt(PreferKey.bodyIndent, 2) - } - ChapterProvider.upStyle() - ReadBook.loadContent() - withContext(Main) { - App.INSTANCE.applyDayNight() - } - if (!BuildConfig.DEBUG) - LauncherIconHelp.changeIcon(App.INSTANCE.getPrefString(PreferKey.launcherIcon)) } + ReadBookConfig.apply { + styleSelect = App.INSTANCE.getPrefInt(PreferKey.readStyleSelect) + shareLayout = App.INSTANCE.getPrefBoolean(PreferKey.shareLayout) + pageAnim = App.INSTANCE.getPrefInt(PreferKey.pageAnim) + hideStatusBar = App.INSTANCE.getPrefBoolean(PreferKey.hideStatusBar) + hideNavigationBar = App.INSTANCE.getPrefBoolean(PreferKey.hideNavigationBar) + bodyIndentCount = App.INSTANCE.getPrefInt(PreferKey.bodyIndent, 2) + } + ChapterProvider.upStyle() + ReadBook.loadContent() + } + withContext(Main) { + App.INSTANCE.applyDayNight() + if (!BuildConfig.DEBUG) + LauncherIconHelp.changeIcon(App.INSTANCE.getPrefString(PreferKey.launcherIcon)) } - } private inline fun fileToListT(path: String, fileName: String): List? { diff --git a/app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt b/app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt index 50857893d..ad4c93a2a 100644 --- a/app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt +++ b/app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt @@ -95,7 +95,8 @@ object WebDavHelp { webDav.downloadTo(zipFilePath, true) @Suppress("BlockingMethodInNonBlockingContext") ZipUtils.unzipFile(zipFilePath, Backup.backupPath) - Restore.restore(Backup.backupPath) + Restore.restoreDatabase() + Restore.restoreConfig() } } }.onSuccess { diff --git a/app/src/main/java/io/legado/app/ui/config/BackupRestoreUi.kt b/app/src/main/java/io/legado/app/ui/config/BackupRestoreUi.kt index 1bec0a184..913f78971 100644 --- a/app/src/main/java/io/legado/app/ui/config/BackupRestoreUi.kt +++ b/app/src/main/java/io/legado/app/ui/config/BackupRestoreUi.kt @@ -114,7 +114,8 @@ object BackupRestoreUi { .onGranted { Coroutine.async { AppConfig.backupPath = path - Restore.restore(path) + Restore.restoreDatabase(path) + Restore.restoreConfig(path) }.onSuccess { fragment.toast(R.string.restore_success) }