pull/1876/head^2
kunfei 3 years ago
parent 5c89ca941f
commit 9821b260a3
  1. 22
      app/src/main/java/io/legado/app/help/ReplaceAnalyzer.kt
  2. 5
      app/src/main/java/io/legado/app/help/storage/ImportOldData.kt
  3. 29
      app/src/main/java/io/legado/app/ui/association/ImportHttpTtsViewModel.kt
  4. 38
      app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleViewModel.kt

@ -1,43 +1,47 @@
package io.legado.app.help package io.legado.app.help
import io.legado.app.data.entities.ReplaceRule import io.legado.app.data.entities.ReplaceRule
import io.legado.app.exception.NoStackTraceException
import io.legado.app.utils.* import io.legado.app.utils.*
object ReplaceAnalyzer { object ReplaceAnalyzer {
fun jsonToReplaceRules(json: String): List<ReplaceRule> { fun jsonToReplaceRules(json: String): Result<MutableList<ReplaceRule>> {
return kotlin.runCatching {
val replaceRules = mutableListOf<ReplaceRule>() val replaceRules = mutableListOf<ReplaceRule>()
val items: List<Map<String, Any>> = jsonPath.parse(json).read("$") val items: List<Map<String, Any>> = jsonPath.parse(json).read("$")
for (item in items) { for (item in items) {
val jsonItem = jsonPath.parse(item) val jsonItem = jsonPath.parse(item)
jsonToReplaceRule(jsonItem.jsonString())?.let { jsonToReplaceRule(jsonItem.jsonString()).getOrThrow().let {
if (it.isValid()) { if (it.isValid()) {
replaceRules.add(it) replaceRules.add(it)
} }
} }
} }
return replaceRules replaceRules
}
} }
private fun jsonToReplaceRule(json: String): ReplaceRule? { fun jsonToReplaceRule(json: String): Result<ReplaceRule> {
val replaceRule: ReplaceRule? = GSON.fromJsonObject<ReplaceRule>(json.trim()).getOrNull() return runCatching {
runCatching { val replaceRule: ReplaceRule? =
GSON.fromJsonObject<ReplaceRule>(json.trim()).getOrNull()
if (replaceRule == null || replaceRule.pattern.isBlank()) { if (replaceRule == null || replaceRule.pattern.isBlank()) {
val jsonItem = jsonPath.parse(json.trim()) val jsonItem = jsonPath.parse(json.trim())
val rule = ReplaceRule() val rule = ReplaceRule()
rule.id = jsonItem.readLong("$.id") ?: System.currentTimeMillis() rule.id = jsonItem.readLong("$.id") ?: System.currentTimeMillis()
rule.pattern = jsonItem.readString("$.regex") ?: "" rule.pattern = jsonItem.readString("$.regex") ?: ""
if (rule.pattern.isEmpty()) return null if (rule.pattern.isEmpty()) throw NoStackTraceException("格式不对")
rule.name = jsonItem.readString("$.replaceSummary") ?: "" rule.name = jsonItem.readString("$.replaceSummary") ?: ""
rule.replacement = jsonItem.readString("$.replacement") ?: "" rule.replacement = jsonItem.readString("$.replacement") ?: ""
rule.isRegex = jsonItem.readBool("$.isRegex") == true rule.isRegex = jsonItem.readBool("$.isRegex") == true
rule.scope = jsonItem.readString("$.useTo") rule.scope = jsonItem.readString("$.useTo")
rule.isEnabled = jsonItem.readBool("$.enable") == true rule.isEnabled = jsonItem.readBool("$.enable") == true
rule.order = jsonItem.readInt("$.serialNumber") ?: 0 rule.order = jsonItem.readInt("$.serialNumber") ?: 0
return rule return@runCatching rule
} }
return@runCatching replaceRule
} }
return replaceRule
} }
} }

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

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

@ -6,12 +6,15 @@ import io.legado.app.base.BaseViewModel
import io.legado.app.constant.AppPattern import io.legado.app.constant.AppPattern
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.data.entities.ReplaceRule import io.legado.app.data.entities.ReplaceRule
import io.legado.app.exception.NoStackTraceException
import io.legado.app.help.ReplaceAnalyzer import io.legado.app.help.ReplaceAnalyzer
import io.legado.app.help.config.AppConfig import io.legado.app.help.config.AppConfig
import io.legado.app.help.http.newCallResponseBody import io.legado.app.help.http.newCallResponseBody
import io.legado.app.help.http.okHttpClient import io.legado.app.help.http.okHttpClient
import io.legado.app.help.http.text import io.legado.app.help.http.text
import io.legado.app.utils.isAbsUrl import io.legado.app.utils.isAbsUrl
import io.legado.app.utils.isJsonArray
import io.legado.app.utils.isJsonObject
import io.legado.app.utils.splitNotBlank import io.legado.app.utils.splitNotBlank
class ImportReplaceRuleViewModel(app: Application) : BaseViewModel(app) { class ImportReplaceRuleViewModel(app: Application) : BaseViewModel(app) {
@ -83,17 +86,7 @@ class ImportReplaceRuleViewModel(app: Application) : BaseViewModel(app) {
fun import(text: String) { fun import(text: String) {
execute { execute {
if (text.isAbsUrl()) { importAwait(text.trim())
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)
}
}.onError { }.onError {
errorLiveData.postValue(it.localizedMessage ?: "ERROR") errorLiveData.postValue(it.localizedMessage ?: "ERROR")
}.onSuccess { }.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() { private fun comparisonSource() {
execute { execute {
allRules.forEach { allRules.forEach {

Loading…
Cancel
Save