pull/1876/head^2
kunfei 3 years ago
parent 5c89ca941f
commit 9821b260a3
  1. 34
      app/src/main/java/io/legado/app/help/ReplaceAnalyzer.kt
  2. 9
      app/src/main/java/io/legado/app/help/storage/ImportOldData.kt
  3. 35
      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>> {
val replaceRules = mutableListOf<ReplaceRule>() return kotlin.runCatching {
val items: List<Map<String, Any>> = jsonPath.parse(json).read("$") val replaceRules = mutableListOf<ReplaceRule>()
for (item in items) { val items: List<Map<String, Any>> = jsonPath.parse(json).read("$")
val jsonItem = jsonPath.parse(item) for (item in items) {
jsonToReplaceRule(jsonItem.jsonString())?.let { val jsonItem = jsonPath.parse(item)
if (it.isValid()) { jsonToReplaceRule(jsonItem.jsonString()).getOrThrow().let {
replaceRules.add(it) if (it.isValid()) {
replaceRules.add(it)
}
} }
} }
replaceRules
} }
return 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,9 +98,12 @@ 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()
appDb.replaceRuleDao.insert(*rules.toTypedArray()) rules?.let {
return rules.size appDb.replaceRuleDao.insert(*rules.toTypedArray())
return rules.size
}
return 0
} }
private fun fromOldBooks(json: String): List<Book> { private fun fromOldBooks(json: String): List<Book> {

@ -61,21 +61,7 @@ class ImportHttpTtsViewModel(app: Application) : BaseViewModel(app) {
fun importSource(text: String) { fun importSource(text: String) {
execute { execute {
val mText = text.trim() importSourceAwait(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))
}
}.onError { }.onError {
it.printOnDebug() it.printOnDebug()
errorLiveData.postValue(it.localizedMessage ?: "") 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) { 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