From a64d5b8cfb38654bde3c5f3f665cc6838521dbea Mon Sep 17 00:00:00 2001 From: kunfei Date: Wed, 1 Jan 2020 21:52:13 +0800 Subject: [PATCH] up --- .../io/legado/app/help/storage/OldRule.kt | 1 + .../io/legado/app/help/storage/Restore.kt | 157 ++++++++++-------- .../app/ui/config/WebDavConfigFragment.kt | 25 ++- 3 files changed, 108 insertions(+), 75 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/storage/OldRule.kt b/app/src/main/java/io/legado/app/help/storage/OldRule.kt index 0c9baa659..14b4a9c53 100644 --- a/app/src/main/java/io/legado/app/help/storage/OldRule.kt +++ b/app/src/main/java/io/legado/app/help/storage/OldRule.kt @@ -205,4 +205,5 @@ object OldRule { return GSON.toJson(map) } + } 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 b2e34783e..920d61c0d 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 @@ -111,47 +111,12 @@ object Restore { fun importYueDuData(context: Context) { GlobalScope.launch(IO) { try {// 导入书架 - val shelfFile = FileHelp.getFile(Backup.defaultPath + File.separator + "myBookShelf.json") - val books = mutableListOf() - val items: List> = jsonPath.parse(shelfFile.readText()).read("$") - val existingBooks = App.db.bookDao().allBookUrls.toSet() - for (item in items) { - val jsonItem = jsonPath.parse(item) - val book = Book() - book.bookUrl = jsonItem.readString("$.noteUrl") ?: "" - if (book.bookUrl.isBlank()) continue - book.name = jsonItem.readString("$.bookInfoBean.name") ?: "" - if (book.bookUrl in existingBooks) { - Log.d(AppConst.APP_TAG, "Found existing book: ${book.name}") - continue - } - book.origin = jsonItem.readString("$.tag") ?: "" - book.originName = jsonItem.readString("$.bookInfoBean.origin") ?: "" - book.author = jsonItem.readString("$.bookInfoBean.author") ?: "" - book.type = - if (jsonItem.readString("$.bookInfoBean.bookSourceType") == "AUDIO") 1 else 0 - book.tocUrl = jsonItem.readString("$.bookInfoBean.chapterUrl") ?: book.bookUrl - book.coverUrl = jsonItem.readString("$.bookInfoBean.coverUrl") - book.customCoverUrl = jsonItem.readString("$.customCoverPath") - book.lastCheckTime = jsonItem.readLong("$.bookInfoBean.finalRefreshData") ?: 0 - book.canUpdate = jsonItem.readBool("$.allowUpdate") == true - book.totalChapterNum = jsonItem.readInt("$.chapterListSize") ?: 0 - book.durChapterIndex = jsonItem.readInt("$.durChapter") ?: 0 - book.durChapterTitle = jsonItem.readString("$.durChapterName") - book.durChapterPos = jsonItem.readInt("$.durChapterPage") ?: 0 - book.durChapterTime = jsonItem.readLong("$.finalDate") ?: 0 - book.group = jsonItem.readInt("$.group") ?: 0 - book.intro = jsonItem.readString("$.bookInfoBean.introduce") - book.latestChapterTitle = jsonItem.readString("$.lastChapterName") - book.lastCheckCount = jsonItem.readInt("$.newChapters") ?: 0 - book.order = jsonItem.readInt("$.serialNumber") ?: 0 - book.useReplaceRule = jsonItem.readBool("$.useReplaceRule") == true - book.variable = jsonItem.readString("$.variable") - books.add(book) - } - App.db.bookDao().insert(*books.toTypedArray()) + val shelfFile = + FileHelp.getFile(Backup.defaultPath + File.separator + "myBookShelf.json") + val json = shelfFile.readText() + val importCount = importOldBookshelf(json) withContext(Main) { - context.toast("成功导入书籍${books.size}") + context.toast("成功导入书籍${importCount}") } } catch (e: Exception) { withContext(Main) { @@ -162,17 +127,10 @@ object Restore { try {// Book source val sourceFile = FileHelp.getFile(Backup.defaultPath + File.separator + "myBookSource.json") - val bookSources = mutableListOf() - val items: List> = jsonPath.parse(sourceFile.readText()).read("$") - for (item in items) { - val jsonItem = jsonPath.parse(item) - OldRule.jsonToBookSource(jsonItem.jsonString())?.let { - bookSources.add(it) - } - } - App.db.bookSourceDao().insert(*bookSources.toTypedArray()) + val json = sourceFile.readText() + val importCount = importOldSource(json) withContext(Main) { - context.toast("成功导入书源${bookSources.size}") + context.toast("成功导入书源${importCount}") } } catch (e: Exception) { withContext(Main) { @@ -183,26 +141,10 @@ object Restore { try {// Replace rules val ruleFile = FileHelp.getFile(Backup.defaultPath + File.separator + "myBookReplaceRule.json") - val replaceRules = mutableListOf() - val items: List> = jsonPath.parse(ruleFile.readText()).read("$") - val existingRules = App.db.replaceRuleDao().all.map { it.pattern }.toSet() - for ((index: Int, item: Map) in items.withIndex()) { - val jsonItem = jsonPath.parse(item) - val rule = ReplaceRule() - rule.id = jsonItem.readLong("$.id") ?: System.currentTimeMillis().plus(index) - rule.pattern = jsonItem.readString("$.regex") ?: "" - if (rule.pattern.isEmpty() || rule.pattern in existingRules) continue - rule.name = jsonItem.readString("$.replaceSummary") ?: "" - rule.replacement = jsonItem.readString("$.replacement") ?: "" - rule.isRegex = jsonItem.readBool("$.isRegex") == true - rule.scope = jsonItem.readString("$.useTo") - rule.isEnabled = jsonItem.readBool("$.enable") == true - rule.order = jsonItem.readInt("$.serialNumber") ?: index - replaceRules.add(rule) - } - App.db.replaceRuleDao().insert(*replaceRules.toTypedArray()) + val json = ruleFile.readText() + val importCount = importOldReplaceRule(json) withContext(Main) { - context.toast("成功导入替换规则${replaceRules.size}") + context.toast("成功导入替换规则${importCount}") } } catch (e: Exception) { withContext(Main) { @@ -211,4 +153,81 @@ object Restore { } } } + + fun importOldBookshelf(json: String): Int { + val books = mutableListOf() + val items: List> = jsonPath.parse(json).read("$") + val existingBooks = App.db.bookDao().allBookUrls.toSet() + for (item in items) { + val jsonItem = jsonPath.parse(item) + val book = Book() + book.bookUrl = jsonItem.readString("$.noteUrl") ?: "" + if (book.bookUrl.isBlank()) continue + book.name = jsonItem.readString("$.bookInfoBean.name") ?: "" + if (book.bookUrl in existingBooks) { + Log.d(AppConst.APP_TAG, "Found existing book: ${book.name}") + continue + } + book.origin = jsonItem.readString("$.tag") ?: "" + book.originName = jsonItem.readString("$.bookInfoBean.origin") ?: "" + book.author = jsonItem.readString("$.bookInfoBean.author") ?: "" + book.type = + if (jsonItem.readString("$.bookInfoBean.bookSourceType") == "AUDIO") 1 else 0 + book.tocUrl = jsonItem.readString("$.bookInfoBean.chapterUrl") ?: book.bookUrl + book.coverUrl = jsonItem.readString("$.bookInfoBean.coverUrl") + book.customCoverUrl = jsonItem.readString("$.customCoverPath") + book.lastCheckTime = jsonItem.readLong("$.bookInfoBean.finalRefreshData") ?: 0 + book.canUpdate = jsonItem.readBool("$.allowUpdate") == true + book.totalChapterNum = jsonItem.readInt("$.chapterListSize") ?: 0 + book.durChapterIndex = jsonItem.readInt("$.durChapter") ?: 0 + book.durChapterTitle = jsonItem.readString("$.durChapterName") + book.durChapterPos = jsonItem.readInt("$.durChapterPage") ?: 0 + book.durChapterTime = jsonItem.readLong("$.finalDate") ?: 0 + book.group = jsonItem.readInt("$.group") ?: 0 + book.intro = jsonItem.readString("$.bookInfoBean.introduce") + book.latestChapterTitle = jsonItem.readString("$.lastChapterName") + book.lastCheckCount = jsonItem.readInt("$.newChapters") ?: 0 + book.order = jsonItem.readInt("$.serialNumber") ?: 0 + book.useReplaceRule = jsonItem.readBool("$.useReplaceRule") == true + book.variable = jsonItem.readString("$.variable") + books.add(book) + } + App.db.bookDao().insert(*books.toTypedArray()) + return books.size + } + + fun importOldSource(json: String): Int { + val bookSources = mutableListOf() + val items: List> = jsonPath.parse(json).read("$") + for (item in items) { + val jsonItem = jsonPath.parse(item) + OldRule.jsonToBookSource(jsonItem.jsonString())?.let { + bookSources.add(it) + } + } + App.db.bookSourceDao().insert(*bookSources.toTypedArray()) + return bookSources.size + } + + fun importOldReplaceRule(json: String): Int { + val replaceRules = mutableListOf() + val items: List> = jsonPath.parse(json).read("$") + val existingRules = App.db.replaceRuleDao().all.map { it.pattern }.toSet() + for ((index: Int, item: Map) in items.withIndex()) { + val jsonItem = jsonPath.parse(item) + val rule = ReplaceRule() + rule.id = jsonItem.readLong("$.id") ?: System.currentTimeMillis().plus(index) + rule.pattern = jsonItem.readString("$.regex") ?: "" + if (rule.pattern.isEmpty() || rule.pattern in existingRules) continue + rule.name = jsonItem.readString("$.replaceSummary") ?: "" + rule.replacement = jsonItem.readString("$.replacement") ?: "" + rule.isRegex = jsonItem.readBool("$.isRegex") == true + rule.scope = jsonItem.readString("$.useTo") + rule.isEnabled = jsonItem.readBool("$.enable") == true + rule.order = jsonItem.readInt("$.serialNumber") ?: index + replaceRules.add(rule) + } + App.db.replaceRuleDao().insert(*replaceRules.toTypedArray()) + return replaceRules.size + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/config/WebDavConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/WebDavConfigFragment.kt index 3cfe0af2e..0ccf98f7c 100644 --- a/app/src/main/java/io/legado/app/ui/config/WebDavConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/WebDavConfigFragment.kt @@ -23,6 +23,7 @@ import io.legado.app.lib.dialogs.noButton import io.legado.app.lib.dialogs.yesButton import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.accentColor +import io.legado.app.utils.DocumentUtils import io.legado.app.utils.LogUtils import io.legado.app.utils.applyTint import io.legado.app.utils.getPrefString @@ -142,7 +143,7 @@ class WebDavConfigFragment : PreferenceFragmentCompat(), Preference.OnPreference private fun needInstallApps(callback: () -> Unit) { - fun canRequestPackageInstalls() :Boolean { + fun canRequestPackageInstalls(): Boolean { if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) { return requireContext().packageManager.canRequestPackageInstalls() } @@ -158,7 +159,7 @@ class WebDavConfigFragment : PreferenceFragmentCompat(), Preference.OnPreference } }.show().applyTint() } else { - LogUtils.d("xxx","import old") + LogUtils.d("xxx", "import old") callback() } } @@ -166,13 +167,25 @@ class WebDavConfigFragment : PreferenceFragmentCompat(), Preference.OnPreference override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) when (requestCode) { - oldDataRequestCode -> if (resultCode == RESULT_OK) { - data?.data?.let { - DocumentFile.fromTreeUri(requireContext(), it)?.listFiles()?.forEach { - + oldDataRequestCode -> if (resultCode == RESULT_OK) data?.data?.let { uri -> + DocumentFile.fromTreeUri(requireContext(), uri)?.listFiles()?.forEach { + when (it.name) { + "myBookShelf.json" -> + DocumentUtils.readText(requireContext(), it.uri)?.let { json -> + Restore.importOldBookshelf(json) + } + "myBookSource.json" -> + DocumentUtils.readText(requireContext(), it.uri)?.let { json -> + Restore.importOldSource(json) + } + "myBookReplaceRule.json" -> + DocumentUtils.readText(requireContext(), it.uri)?.let { json -> + Restore.importOldReplaceRule(json) + } } } } + } } } \ No newline at end of file