优化
This commit is contained in:
@@ -3,6 +3,7 @@ package io.legado.app.help.storage
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import androidx.documentfile.provider.DocumentFile
|
||||
import com.jayway.jsonpath.DocumentContext
|
||||
import io.legado.app.R
|
||||
import io.legado.app.constant.AppConst
|
||||
import io.legado.app.constant.BookSourceType
|
||||
@@ -20,7 +21,9 @@ import java.util.regex.Pattern
|
||||
|
||||
object ImportOldData {
|
||||
|
||||
@Suppress("RegExpRedundantEscape")
|
||||
private val headerPattern = Pattern.compile("@Header:\\{.+?\\}", Pattern.CASE_INSENSITIVE)
|
||||
@Suppress("RegExpRedundantEscape")
|
||||
private val jsPattern = Pattern.compile("\\{\\{.+?\\}\\}", Pattern.CASE_INSENSITIVE)
|
||||
|
||||
fun importUri(context: Context, uri: Uri) {
|
||||
@@ -162,8 +165,15 @@ object ImportOldData {
|
||||
val items: List<Map<String, Any>> = jsonPath.parse(json).read("$")
|
||||
for (item in items) {
|
||||
val jsonItem = jsonPath.parse(item)
|
||||
val source = fromOldBookSource(jsonItem)
|
||||
sources.add(source)
|
||||
}
|
||||
return sources
|
||||
}
|
||||
|
||||
fun fromOldBookSource(jsonItem: DocumentContext): BookSource {
|
||||
val source = BookSource()
|
||||
source.apply {
|
||||
return source.apply {
|
||||
bookSourceUrl = jsonItem.readString("bookSourceUrl")
|
||||
?: throw NoStackTraceException(appCtx.getString(R.string.wrong_format))
|
||||
bookSourceName = jsonItem.readString("bookSourceName") ?: ""
|
||||
@@ -230,9 +240,6 @@ object ImportOldData {
|
||||
nextContentUrl = toNewRule(jsonItem.readString("ruleContentUrlNext"))
|
||||
)
|
||||
}
|
||||
sources.add(source)
|
||||
}
|
||||
return sources
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ import io.legado.app.help.config.SourceConfig
|
||||
import io.legado.app.help.http.CookieStore
|
||||
import io.legado.app.help.http.newCallStrResponse
|
||||
import io.legado.app.help.http.okHttpClient
|
||||
import io.legado.app.help.storage.ImportOldData
|
||||
import io.legado.app.utils.*
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
|
||||
@@ -92,10 +93,15 @@ class BookSourceEditViewModel(application: Application) : BaseViewModel(applicat
|
||||
text.isJsonArray() -> {
|
||||
val items: List<Map<String, Any>> = jsonPath.parse(text).read("$")
|
||||
val jsonItem = jsonPath.parse(items[0])
|
||||
BookSource.fromJson(jsonItem.jsonString()).getOrThrow()
|
||||
BookSource.fromJson(jsonItem.jsonString()).getOrElse {
|
||||
ImportOldData.fromOldBookSource(jsonItem)
|
||||
}
|
||||
}
|
||||
text.isJsonObject() -> {
|
||||
BookSource.fromJson(text).getOrThrow()
|
||||
BookSource.fromJson(text).getOrElse {
|
||||
val jsonItem = jsonPath.parse(text)
|
||||
ImportOldData.fromOldBookSource(jsonItem)
|
||||
}
|
||||
}
|
||||
else -> throw NoStackTraceException("格式不对")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user