From 7305f80b77ef564d804cbf5f1e5135e76900a8f4 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 7 Jan 2020 21:49:05 +0800 Subject: [PATCH] up --- .../io/legado/app/help/storage/OldRule.kt | 25 ++++++++++++++++++- .../io/legado/app/help/storage/Restore.kt | 15 +++-------- 2 files changed, 27 insertions(+), 13 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 14b4a9c53..c8036e559 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 @@ -3,6 +3,7 @@ package io.legado.app.help.storage import io.legado.app.constant.AppConst import io.legado.app.constant.BookType import io.legado.app.data.entities.BookSource +import io.legado.app.data.entities.ReplaceRule import io.legado.app.data.entities.rule.* import io.legado.app.help.storage.Restore.jsonPath import io.legado.app.utils.* @@ -205,5 +206,27 @@ object OldRule { return GSON.toJson(map) } - + fun jsonToReplaceRule(json: String): ReplaceRule? { + var replaceRule: ReplaceRule? = null + runCatching { + replaceRule = GSON.fromJsonObject(json.trim()) + } + runCatching { + if (replaceRule == null || replaceRule?.pattern.isNullOrBlank()) { + val jsonItem = jsonPath.parse(json.trim()) + val rule = ReplaceRule() + rule.id = jsonItem.readLong("$.id") ?: System.currentTimeMillis() + rule.pattern = jsonItem.readString("$.regex") ?: "" + if (rule.pattern.isEmpty()) return null + 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") ?: 0 + return rule + } + } + return replaceRule + } } 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 a95626228..d7de379d4 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 @@ -226,20 +226,11 @@ object Restore { 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) + OldRule.jsonToReplaceRule(jsonItem.jsonString())?.let { + replaceRules.add(it) + } } App.db.replaceRuleDao().insert(*replaceRules.toTypedArray()) return replaceRules.size