This commit is contained in:
gedoor 2021-10-15 14:07:02 +08:00
parent 3fd3781f05
commit d7e2df9c9b
6 changed files with 32 additions and 25 deletions

View File

@ -43,9 +43,10 @@ object BookSourceController {
}
fun saveSources(postData: String?): ReturnData {
postData ?: return ReturnData().setErrorMsg("数据为空")
val okSources = arrayListOf<BookSource>()
val bookSources = GSON.fromJsonArray<BookSource>(postData)
if (bookSources != null) {
val bookSources = BookSourceAnalyzer.jsonToBookSources(postData)
if (bookSources.isNotEmpty()) {
bookSources.forEach { bookSource ->
if (bookSource.bookSourceName.isNotBlank()
&& bookSource.bookSourceUrl.isNotBlank()

View File

@ -14,6 +14,18 @@ object BookSourceAnalyzer {
private val headerPattern = Pattern.compile("@Header:\\{.+?\\}", Pattern.CASE_INSENSITIVE)
private val jsPattern = Pattern.compile("\\{\\{.+?\\}\\}", Pattern.CASE_INSENSITIVE)
fun jsonToBookSources(json: String): List<BookSource> {
val bookSources = mutableListOf<BookSource>()
val items: List<Map<String, Any>> = jsonPath.parse(json).read("$")
for (item in items) {
val jsonItem = jsonPath.parse(item)
jsonToBookSource(jsonItem.jsonString())?.let {
bookSources.add(it)
}
}
return bookSources
}
fun jsonToBookSource(json: String): BookSource? {
val source = BookSource()
val sourceAny = try {

View File

@ -4,9 +4,11 @@ import android.content.Context
import android.net.Uri
import androidx.documentfile.provider.DocumentFile
import io.legado.app.data.appDb
import io.legado.app.data.entities.BookSource
import io.legado.app.help.BookSourceAnalyzer
import io.legado.app.utils.*
import io.legado.app.utils.DocumentUtils
import io.legado.app.utils.FileUtils
import io.legado.app.utils.isContentScheme
import io.legado.app.utils.toastOnUi
import java.io.File
object ImportOldData {
@ -90,14 +92,7 @@ object ImportOldData {
}
fun importOldSource(json: String): Int {
val bookSources = mutableListOf<BookSource>()
val items: List<Map<String, Any>> = jsonPath.parse(json).read("$")
for (item in items) {
val jsonItem = jsonPath.parse(item)
BookSourceAnalyzer.jsonToBookSource(jsonItem.jsonString())?.let {
bookSources.add(it)
}
}
val bookSources = BookSourceAnalyzer.jsonToBookSources(json)
appDb.bookSourceDao.insert(*bookSources.toTypedArray())
return bookSources.size
}

View File

@ -17,13 +17,13 @@ class WebTileService : TileService() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
try {
when (intent?.action) {
IntentAction.start -> {
qsTile.state = Tile.STATE_ACTIVE
qsTile.updateTile()
IntentAction.start -> qsTile?.run {
state = Tile.STATE_ACTIVE
updateTile()
}
IntentAction.stop -> {
qsTile.state = Tile.STATE_INACTIVE
qsTile.updateTile()
IntentAction.stop -> qsTile?.run {
state = Tile.STATE_INACTIVE
updateTile()
}
}
} catch (e: Exception) {

View File

@ -104,13 +104,8 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) {
}
}
mText.isJsonArray() -> {
val items: List<Map<String, Any>> = jsonPath.parse(mText).read("$")
for (item in items) {
val jsonItem = jsonPath.parse(item)
BookSourceAnalyzer.jsonToBookSource(jsonItem.jsonString())?.let {
allSources.add(it)
}
}
val items = BookSourceAnalyzer.jsonToBookSources(mText)
allSources.addAll(items)
}
mText.isAbsUrl() -> {
importSourceUrl(mText)

View File

@ -25,12 +25,16 @@ inline fun <reified T> genericType(): Type = object : TypeToken<T>() {}.type
inline fun <reified T> Gson.fromJsonObject(json: String?): T? {//可转成任意类型
return kotlin.runCatching {
fromJson(json, genericType<T>()) as? T
}.onFailure {
it.printOnDebug()
}.getOrNull()
}
inline fun <reified T> Gson.fromJsonArray(json: String?): List<T>? {
return kotlin.runCatching {
fromJson(json, ParameterizedTypeImpl(T::class.java)) as? List<T>
}.onFailure {
it.printOnDebug()
}.getOrNull()
}