kunfei 4 лет назад
Родитель
Сommit
05bd6c53fc

+ 19 - 15
app/src/main/java/io/legado/app/help/ReplaceAnalyzer.kt

@@ -1,43 +1,47 @@
 package io.legado.app.help
 
 import io.legado.app.data.entities.ReplaceRule
+import io.legado.app.exception.NoStackTraceException
 import io.legado.app.utils.*
 
 object ReplaceAnalyzer {
 
-    fun jsonToReplaceRules(json: String): List<ReplaceRule> {
-        val replaceRules = mutableListOf<ReplaceRule>()
-        val items: List<Map<String, Any>> = jsonPath.parse(json).read("$")
-        for (item in items) {
-            val jsonItem = jsonPath.parse(item)
-            jsonToReplaceRule(jsonItem.jsonString())?.let {
-                if (it.isValid()) {
-                    replaceRules.add(it)
+    fun jsonToReplaceRules(json: String): Result<MutableList<ReplaceRule>> {
+        return kotlin.runCatching {
+            val replaceRules = mutableListOf<ReplaceRule>()
+            val items: List<Map<String, Any>> = jsonPath.parse(json).read("$")
+            for (item in items) {
+                val jsonItem = jsonPath.parse(item)
+                jsonToReplaceRule(jsonItem.jsonString()).getOrThrow().let {
+                    if (it.isValid()) {
+                        replaceRules.add(it)
+                    }
                 }
             }
+            replaceRules
         }
-        return replaceRules
     }
 
-    private fun jsonToReplaceRule(json: String): ReplaceRule? {
-        val replaceRule: ReplaceRule? = GSON.fromJsonObject<ReplaceRule>(json.trim()).getOrNull()
-        runCatching {
+    fun jsonToReplaceRule(json: String): Result<ReplaceRule> {
+        return runCatching {
+            val replaceRule: ReplaceRule? =
+                GSON.fromJsonObject<ReplaceRule>(json.trim()).getOrNull()
             if (replaceRule == null || replaceRule.pattern.isBlank()) {
                 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
+                if (rule.pattern.isEmpty()) throw NoStackTraceException("格式不对")
                 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@runCatching rule
             }
+            return@runCatching replaceRule
         }
-        return replaceRule
     }
 
 }

+ 6 - 3
app/src/main/java/io/legado/app/help/storage/ImportOldData.kt

@@ -98,9 +98,12 @@ object ImportOldData {
     }
 
     private fun importOldReplaceRule(json: String): Int {
-        val rules = ReplaceAnalyzer.jsonToReplaceRules(json)
-        appDb.replaceRuleDao.insert(*rules.toTypedArray())
-        return rules.size
+        val rules = ReplaceAnalyzer.jsonToReplaceRules(json).getOrNull()
+        rules?.let {
+            appDb.replaceRuleDao.insert(*rules.toTypedArray())
+            return rules.size
+        }
+        return 0
     }
 
     private fun fromOldBooks(json: String): List<Book> {

+ 19 - 16
app/src/main/java/io/legado/app/ui/association/ImportHttpTtsViewModel.kt

@@ -61,21 +61,7 @@ class ImportHttpTtsViewModel(app: Application) : BaseViewModel(app) {
 
     fun importSource(text: String) {
         execute {
-            val mText = text.trim()
-            when {
-                mText.isJsonObject() -> {
-                    HttpTTS.fromJson(mText).getOrThrow().let {
-                        allSources.add(it)
-                    }
-                }
-                mText.isJsonArray() -> HttpTTS.fromJsonArray(mText).getOrThrow().let { items ->
-                    allSources.addAll(items)
-                }
-                mText.isAbsUrl() -> {
-                    importSourceUrl(mText)
-                }
-                else -> throw NoStackTraceException(context.getString(R.string.wrong_format))
-            }
+            importSourceAwait(text.trim())
         }.onError {
             it.printOnDebug()
             errorLiveData.postValue(it.localizedMessage ?: "")
@@ -84,11 +70,28 @@ class ImportHttpTtsViewModel(app: Application) : BaseViewModel(app) {
         }
     }
 
+    private suspend fun importSourceAwait(text: String) {
+        when {
+            text.isJsonObject() -> {
+                HttpTTS.fromJson(text).getOrThrow().let {
+                    allSources.add(it)
+                }
+            }
+            text.isJsonArray() -> HttpTTS.fromJsonArray(text).getOrThrow().let { items ->
+                allSources.addAll(items)
+            }
+            text.isAbsUrl() -> {
+                importSourceUrl(text)
+            }
+            else -> throw NoStackTraceException(context.getString(R.string.wrong_format))
+        }
+    }
+
     private suspend fun importSourceUrl(url: String) {
         okHttpClient.newCallResponseBody {
             url(url)
         }.text().let {
-            allSources.addAll(HttpTTS.fromJsonArray(it).getOrThrow())
+            importSourceAwait(it)
         }
     }
 

+ 27 - 11
app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleViewModel.kt

@@ -6,12 +6,15 @@ import io.legado.app.base.BaseViewModel
 import io.legado.app.constant.AppPattern
 import io.legado.app.data.appDb
 import io.legado.app.data.entities.ReplaceRule
+import io.legado.app.exception.NoStackTraceException
 import io.legado.app.help.ReplaceAnalyzer
 import io.legado.app.help.config.AppConfig
 import io.legado.app.help.http.newCallResponseBody
 import io.legado.app.help.http.okHttpClient
 import io.legado.app.help.http.text
 import io.legado.app.utils.isAbsUrl
+import io.legado.app.utils.isJsonArray
+import io.legado.app.utils.isJsonObject
 import io.legado.app.utils.splitNotBlank
 
 class ImportReplaceRuleViewModel(app: Application) : BaseViewModel(app) {
@@ -83,17 +86,7 @@ class ImportReplaceRuleViewModel(app: Application) : BaseViewModel(app) {
 
     fun import(text: String) {
         execute {
-            if (text.isAbsUrl()) {
-                okHttpClient.newCallResponseBody {
-                    url(text)
-                }.text("utf-8").let {
-                    val rules = ReplaceAnalyzer.jsonToReplaceRules(it)
-                    allRules.addAll(rules)
-                }
-            } else {
-                val rules = ReplaceAnalyzer.jsonToReplaceRules(text)
-                allRules.addAll(rules)
-            }
+            importAwait(text.trim())
         }.onError {
             errorLiveData.postValue(it.localizedMessage ?: "ERROR")
         }.onSuccess {
@@ -101,6 +94,29 @@ class ImportReplaceRuleViewModel(app: Application) : BaseViewModel(app) {
         }
     }
 
+    private suspend fun importAwait(text: String) {
+        when {
+            text.isAbsUrl() -> importUrl(text)
+            text.isJsonArray() -> {
+                val rules = ReplaceAnalyzer.jsonToReplaceRules(text).getOrThrow()
+                allRules.addAll(rules)
+            }
+            text.isJsonObject() -> {
+                val rule = ReplaceAnalyzer.jsonToReplaceRule(text).getOrThrow()
+                allRules.add(rule)
+            }
+            else -> throw NoStackTraceException("格式不对")
+        }
+    }
+
+    private suspend fun importUrl(url: String) {
+        okHttpClient.newCallResponseBody {
+            url(url)
+        }.text("utf-8").let {
+            importAwait(it)
+        }
+    }
+
     private fun comparisonSource() {
         execute {
             allRules.forEach {