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
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> {
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())?.let {
jsonToReplaceRule(jsonItem.jsonString()).getOrThrow().let {
if (it.isValid()) {
replaceRules.add(it)
}
}
}
return replaceRules
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
}
}

@ -98,10 +98,13 @@ object ImportOldData {
}
private fun importOldReplaceRule(json: String): Int {
val rules = ReplaceAnalyzer.jsonToReplaceRules(json)
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> {
val books = mutableListOf<Book>()

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

Loading…
Cancel
Save