mirror of
https://github.com/gedoor/legado.git
synced 2024-09-01 09:34:25 +08:00
优化
This commit is contained in:
parent
9acb2c2bde
commit
0804e0018b
@ -5,7 +5,7 @@ import android.text.TextUtils
|
||||
import io.legado.app.api.ReturnData
|
||||
import io.legado.app.data.appDb
|
||||
import io.legado.app.data.entities.BookSource
|
||||
import io.legado.app.help.BookSourceAnalyzer
|
||||
import io.legado.app.help.SourceAnalyzer
|
||||
import io.legado.app.utils.GSON
|
||||
import io.legado.app.utils.fromJsonArray
|
||||
import io.legado.app.utils.msg
|
||||
@ -25,7 +25,7 @@ object BookSourceController {
|
||||
val returnData = ReturnData()
|
||||
postData ?: return returnData.setErrorMsg("数据不能为空")
|
||||
kotlin.runCatching {
|
||||
val bookSource = BookSourceAnalyzer.jsonToBookSource(postData)
|
||||
val bookSource = SourceAnalyzer.jsonToBookSource(postData)
|
||||
if (bookSource != null) {
|
||||
if (TextUtils.isEmpty(bookSource.bookSourceName) || TextUtils.isEmpty(bookSource.bookSourceUrl)) {
|
||||
returnData.setErrorMsg("源名称和URL不能为空")
|
||||
@ -45,7 +45,7 @@ object BookSourceController {
|
||||
fun saveSources(postData: String?): ReturnData {
|
||||
postData ?: return ReturnData().setErrorMsg("数据为空")
|
||||
val okSources = arrayListOf<BookSource>()
|
||||
val bookSources = BookSourceAnalyzer.jsonToBookSources(postData)
|
||||
val bookSources = SourceAnalyzer.jsonToBookSources(postData)
|
||||
if (bookSources.isNotEmpty()) {
|
||||
bookSources.forEach { bookSource ->
|
||||
if (bookSource.bookSourceName.isNotBlank()
|
||||
|
@ -5,9 +5,6 @@ import android.text.TextUtils
|
||||
import io.legado.app.api.ReturnData
|
||||
import io.legado.app.data.appDb
|
||||
import io.legado.app.data.entities.RssSource
|
||||
import io.legado.app.utils.GSON
|
||||
import io.legado.app.utils.fromJsonArray
|
||||
import io.legado.app.utils.fromJsonObject
|
||||
import io.legado.app.utils.msg
|
||||
|
||||
object RssSourceController {
|
||||
@ -25,7 +22,7 @@ object RssSourceController {
|
||||
val returnData = ReturnData()
|
||||
postData ?: return returnData.setErrorMsg("数据不能为空")
|
||||
kotlin.runCatching {
|
||||
val source = GSON.fromJsonObject<RssSource>(postData)
|
||||
val source = RssSource.fromJson(postData)
|
||||
if (source != null) {
|
||||
if (TextUtils.isEmpty(source.sourceName) || TextUtils.isEmpty(source.sourceUrl)) {
|
||||
returnData.setErrorMsg("源名称和URL不能为空")
|
||||
@ -43,9 +40,10 @@ object RssSourceController {
|
||||
}
|
||||
|
||||
fun saveSources(postData: String?): ReturnData {
|
||||
postData ?: return ReturnData().setErrorMsg("数据不能为空")
|
||||
val okSources = arrayListOf<RssSource>()
|
||||
val source = GSON.fromJsonArray<RssSource>(postData)
|
||||
if (source != null) {
|
||||
val source = RssSource.fromJsonArray(postData)
|
||||
if (source.isNotEmpty()) {
|
||||
for (rssSource in source) {
|
||||
if (rssSource.sourceName.isBlank() || rssSource.sourceUrl.isBlank()) {
|
||||
continue
|
||||
@ -71,8 +69,9 @@ object RssSourceController {
|
||||
}
|
||||
|
||||
fun deleteSources(postData: String?): ReturnData {
|
||||
postData ?: return ReturnData().setErrorMsg("没有传递数据")
|
||||
kotlin.runCatching {
|
||||
GSON.fromJsonArray<RssSource>(postData)?.let {
|
||||
RssSource.fromJsonArray(postData).let {
|
||||
it.forEach { source ->
|
||||
appDb.rssSourceDao.delete(source)
|
||||
}
|
||||
|
@ -2,6 +2,11 @@ package io.legado.app.data.entities
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
import com.jayway.jsonpath.DocumentContext
|
||||
import io.legado.app.utils.GSON
|
||||
import io.legado.app.utils.jsonPath
|
||||
import io.legado.app.utils.readLong
|
||||
import io.legado.app.utils.readString
|
||||
|
||||
/**
|
||||
* 在线朗读引擎
|
||||
@ -31,4 +36,41 @@ data class HttpTTS(
|
||||
return this
|
||||
}
|
||||
|
||||
@Suppress("MemberVisibilityCanBePrivate")
|
||||
companion object {
|
||||
|
||||
fun fromJsonDoc(doc: DocumentContext): HttpTTS? {
|
||||
return kotlin.runCatching {
|
||||
val loginUi = doc.read<Any>("$.loginUi")
|
||||
HttpTTS(
|
||||
id = doc.readLong("$.id") ?: System.currentTimeMillis(),
|
||||
name = doc.readString("$.name")!!,
|
||||
url = doc.readString("$.url")!!,
|
||||
concurrentRate = doc.readString("$.concurrentRate"),
|
||||
loginUrl = doc.readString("$.loginUrl"),
|
||||
loginUi = if (loginUi is List<*>) GSON.toJson(loginUi) else loginUi?.toString(),
|
||||
header = doc.readString("$.header"),
|
||||
loginCheckJs = doc.readString("$.loginCheckJs")
|
||||
)
|
||||
}.getOrNull()
|
||||
}
|
||||
|
||||
fun fromJson(json: String): HttpTTS? {
|
||||
return fromJsonDoc(jsonPath.parse(json))
|
||||
}
|
||||
|
||||
fun fromJsonArray(jsonArray: String): ArrayList<HttpTTS> {
|
||||
val sources = arrayListOf<HttpTTS>()
|
||||
val doc = jsonPath.parse(jsonArray).read<List<*>>("$")
|
||||
doc.forEach {
|
||||
val jsonItem = jsonPath.parse(it)
|
||||
fromJsonDoc(jsonItem)?.let { source ->
|
||||
sources.add(source)
|
||||
}
|
||||
}
|
||||
return sources
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -4,7 +4,8 @@ import android.os.Parcelable
|
||||
import androidx.room.Entity
|
||||
import androidx.room.Index
|
||||
import androidx.room.PrimaryKey
|
||||
import io.legado.app.utils.ACache
|
||||
import com.jayway.jsonpath.DocumentContext
|
||||
import io.legado.app.utils.*
|
||||
import kotlinx.parcelize.Parcelize
|
||||
import splitties.init.appCtx
|
||||
|
||||
@ -112,4 +113,58 @@ data class RssSource(
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("MemberVisibilityCanBePrivate")
|
||||
companion object {
|
||||
|
||||
fun fromJsonDoc(doc: DocumentContext): RssSource? {
|
||||
return kotlin.runCatching {
|
||||
val loginUi = doc.read<Any>("$.loginUi")
|
||||
RssSource(
|
||||
sourceUrl = doc.readString("$.sourceUrl")!!,
|
||||
sourceName = doc.readString("$.sourceName")!!,
|
||||
sourceIcon = doc.readString("$.sourceIcon") ?: "",
|
||||
sourceGroup = doc.readString("$.sourceGroup"),
|
||||
sourceComment = doc.readString("$.sourceComment"),
|
||||
enabled = doc.readBool("$.enabled") ?: true,
|
||||
concurrentRate = doc.readString("$.concurrentRate"),
|
||||
header = doc.readString("$.header"),
|
||||
loginUrl = doc.readString("$.loginUrl"),
|
||||
loginUi = if (loginUi is List<*>) GSON.toJson(loginUi) else loginUi?.toString(),
|
||||
loginCheckJs = doc.readString("$.loginCheckJs"),
|
||||
sortUrl = doc.readString("$.sortUrl"),
|
||||
singleUrl = doc.readBool("$.singleUrl") ?: false,
|
||||
articleStyle = doc.readInt("$.articleStyle") ?: 0,
|
||||
ruleArticles = doc.readString("$.ruleArticles"),
|
||||
ruleNextPage = doc.readString("$.ruleNextPage"),
|
||||
ruleTitle = doc.readString("$.ruleTitle"),
|
||||
rulePubDate = doc.readString("$.rulePubDate"),
|
||||
ruleDescription = doc.readString("$.ruleDescription"),
|
||||
ruleImage = doc.readString("$.ruleImage"),
|
||||
ruleLink = doc.readString("$.ruleLink"),
|
||||
ruleContent = doc.readString("$.ruleContent"),
|
||||
style = doc.readString("$.style"),
|
||||
enableJs = doc.readBool("$.enableJs") ?: true,
|
||||
loadWithBaseUrl = doc.readBool("$.loadWithBaseUrl") ?: true,
|
||||
customOrder = doc.readInt("$.customOrder") ?: 0
|
||||
)
|
||||
}.getOrNull()
|
||||
}
|
||||
|
||||
fun fromJson(json: String): RssSource? {
|
||||
return fromJsonDoc(jsonPath.parse(json))
|
||||
}
|
||||
|
||||
fun fromJsonArray(jsonArray: String): ArrayList<RssSource> {
|
||||
val sources = arrayListOf<RssSource>()
|
||||
val doc = jsonPath.parse(jsonArray).read<List<*>>("$")
|
||||
doc.forEach {
|
||||
val jsonItem = jsonPath.parse(it)
|
||||
fromJsonDoc(jsonItem)?.let { source ->
|
||||
sources.add(source)
|
||||
}
|
||||
}
|
||||
return sources
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -14,45 +14,45 @@ object DefaultData {
|
||||
const val httpTtsFileName = "httpTTS.json"
|
||||
const val txtTocRuleFileName = "txtTocRule.json"
|
||||
|
||||
val httpTTS by lazy {
|
||||
val httpTTS: List<HttpTTS> by lazy {
|
||||
val json =
|
||||
String(
|
||||
appCtx.assets.open("defaultData${File.separator}$httpTtsFileName")
|
||||
.readBytes()
|
||||
)
|
||||
GSON.fromJsonArray<HttpTTS>(json)!!
|
||||
GSON.fromJsonArray(json)!!
|
||||
}
|
||||
|
||||
val readConfigs by lazy {
|
||||
val readConfigs: List<ReadBookConfig.Config> by lazy {
|
||||
val json = String(
|
||||
appCtx.assets.open("defaultData${File.separator}${ReadBookConfig.configFileName}")
|
||||
.readBytes()
|
||||
)
|
||||
GSON.fromJsonArray<ReadBookConfig.Config>(json)!!
|
||||
GSON.fromJsonArray(json)!!
|
||||
}
|
||||
|
||||
val txtTocRules by lazy {
|
||||
val txtTocRules: List<TxtTocRule> by lazy {
|
||||
val json = String(
|
||||
appCtx.assets.open("defaultData${File.separator}$txtTocRuleFileName")
|
||||
.readBytes()
|
||||
)
|
||||
GSON.fromJsonArray<TxtTocRule>(json)!!
|
||||
GSON.fromJsonArray(json)!!
|
||||
}
|
||||
|
||||
val themeConfigs by lazy {
|
||||
val themeConfigs: List<ThemeConfig.Config> by lazy {
|
||||
val json = String(
|
||||
appCtx.assets.open("defaultData${File.separator}${ThemeConfig.configFileName}")
|
||||
.readBytes()
|
||||
)
|
||||
GSON.fromJsonArray<ThemeConfig.Config>(json)!!
|
||||
GSON.fromJsonArray(json)!!
|
||||
}
|
||||
|
||||
val rssSources by lazy {
|
||||
val rssSources: List<RssSource> by lazy {
|
||||
val json = String(
|
||||
appCtx.assets.open("defaultData${File.separator}rssSources.json")
|
||||
.readBytes()
|
||||
)
|
||||
GSON.fromJsonArray<RssSource>(json)!!
|
||||
RssSource.fromJsonArray(json)
|
||||
}
|
||||
|
||||
fun importDefaultHttpTTS() {
|
||||
|
@ -10,7 +10,7 @@ import io.legado.app.utils.*
|
||||
import java.util.regex.Pattern
|
||||
|
||||
@Suppress("RegExpRedundantEscape")
|
||||
object BookSourceAnalyzer {
|
||||
object SourceAnalyzer {
|
||||
private val headerPattern = Pattern.compile("@Header:\\{.+?\\}", Pattern.CASE_INSENSITIVE)
|
||||
private val jsPattern = Pattern.compile("\\{\\{.+?\\}\\}", Pattern.CASE_INSENSITIVE)
|
||||
|
@ -4,7 +4,7 @@ import android.content.Context
|
||||
import android.net.Uri
|
||||
import androidx.documentfile.provider.DocumentFile
|
||||
import io.legado.app.data.appDb
|
||||
import io.legado.app.help.BookSourceAnalyzer
|
||||
import io.legado.app.help.SourceAnalyzer
|
||||
import io.legado.app.utils.*
|
||||
import java.io.File
|
||||
|
||||
@ -89,7 +89,7 @@ object ImportOldData {
|
||||
}
|
||||
|
||||
fun importOldSource(json: String): Int {
|
||||
val bookSources = BookSourceAnalyzer.jsonToBookSources(json)
|
||||
val bookSources = SourceAnalyzer.jsonToBookSources(json)
|
||||
appDb.bookSourceDao.insert(*bookSources.toTypedArray())
|
||||
return bookSources.size
|
||||
}
|
||||
|
@ -9,8 +9,8 @@ import io.legado.app.constant.AppPattern
|
||||
import io.legado.app.data.appDb
|
||||
import io.legado.app.data.entities.BookSource
|
||||
import io.legado.app.help.AppConfig
|
||||
import io.legado.app.help.BookSourceAnalyzer
|
||||
import io.legado.app.help.ContentProcessor
|
||||
import io.legado.app.help.SourceAnalyzer
|
||||
import io.legado.app.help.SourceHelp
|
||||
import io.legado.app.help.http.newCallResponseBody
|
||||
import io.legado.app.help.http.okHttpClient
|
||||
@ -98,13 +98,13 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) {
|
||||
importSourceUrl(it)
|
||||
}
|
||||
} else {
|
||||
BookSourceAnalyzer.jsonToBookSource(mText)?.let {
|
||||
SourceAnalyzer.jsonToBookSource(mText)?.let {
|
||||
allSources.add(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
mText.isJsonArray() -> {
|
||||
val items = BookSourceAnalyzer.jsonToBookSources(mText)
|
||||
val items = SourceAnalyzer.jsonToBookSources(mText)
|
||||
allSources.addAll(items)
|
||||
}
|
||||
mText.isAbsUrl() -> {
|
||||
@ -129,13 +129,13 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) {
|
||||
val items: List<Map<String, Any>> = jsonPath.parse(body).read("$")
|
||||
for (item in items) {
|
||||
val jsonItem = jsonPath.parse(item)
|
||||
BookSourceAnalyzer.jsonToBookSource(jsonItem.jsonString())?.let { source ->
|
||||
SourceAnalyzer.jsonToBookSource(jsonItem.jsonString())?.let { source ->
|
||||
allSources.add(source)
|
||||
}
|
||||
}
|
||||
}
|
||||
body.isJsonObject() -> {
|
||||
BookSourceAnalyzer.jsonToBookSource(body)?.let {
|
||||
SourceAnalyzer.jsonToBookSource(body)?.let {
|
||||
allSources.add(it)
|
||||
}
|
||||
}
|
||||
|
@ -95,7 +95,7 @@ class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) {
|
||||
importSourceUrl(it)
|
||||
}
|
||||
} else {
|
||||
GSON.fromJsonArray<RssSource>(mText)?.let {
|
||||
RssSource.fromJsonArray(mText).let {
|
||||
allSources.addAll(it)
|
||||
}
|
||||
}
|
||||
@ -104,7 +104,7 @@ class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) {
|
||||
val items: List<Map<String, Any>> = jsonPath.parse(mText).read("$")
|
||||
for (item in items) {
|
||||
val jsonItem = jsonPath.parse(item)
|
||||
GSON.fromJsonObject<RssSource>(jsonItem.jsonString())?.let {
|
||||
RssSource.fromJsonDoc(jsonItem)?.let {
|
||||
allSources.add(it)
|
||||
}
|
||||
}
|
||||
@ -128,7 +128,7 @@ class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) {
|
||||
val items: List<Map<String, Any>> = jsonPath.parse(body).read("$")
|
||||
for (item in items) {
|
||||
val jsonItem = jsonPath.parse(item)
|
||||
GSON.fromJsonObject<RssSource>(jsonItem.jsonString())?.let { source ->
|
||||
RssSource.fromJson(jsonItem.jsonString())?.let { source ->
|
||||
allSources.add(source)
|
||||
}
|
||||
}
|
||||
|
@ -76,12 +76,12 @@ class OnLineImportViewModel(app: Application) : BaseViewModel(app) {
|
||||
fun importHttpTTS(json: String, finally: (title: String, msg: String) -> Unit) {
|
||||
execute {
|
||||
if (json.isJsonArray()) {
|
||||
GSON.fromJsonArray<HttpTTS>(json)?.let {
|
||||
HttpTTS.fromJsonArray(json).let {
|
||||
appDb.httpTTSDao.insert(*it.toTypedArray())
|
||||
return@execute it.size
|
||||
} ?: throw NoStackTraceException("格式不对")
|
||||
}
|
||||
} else {
|
||||
GSON.fromJsonObject<HttpTTS>(json)?.let {
|
||||
HttpTTS.fromJson(json)?.let {
|
||||
appDb.httpTTSDao.insert(it)
|
||||
return@execute 1
|
||||
} ?: throw NoStackTraceException("格式不对")
|
||||
|
@ -53,10 +53,10 @@ class HttpTtsEditViewModel(app: Application) : BaseViewModel(app) {
|
||||
execute {
|
||||
when {
|
||||
text1.isJsonObject() -> {
|
||||
GSON.fromJsonObject<HttpTTS>(text1)
|
||||
HttpTTS.fromJson(text1)
|
||||
}
|
||||
text1.isJsonArray() -> {
|
||||
GSON.fromJsonArray<HttpTTS>(text1)?.firstOrNull()
|
||||
HttpTTS.fromJsonArray(text1).firstOrNull()
|
||||
}
|
||||
else -> {
|
||||
throw NoStackTraceException("格式不对")
|
||||
|
@ -11,7 +11,10 @@ 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.model.NoStackTraceException
|
||||
import io.legado.app.utils.*
|
||||
import io.legado.app.utils.isJsonArray
|
||||
import io.legado.app.utils.isJsonObject
|
||||
import io.legado.app.utils.readText
|
||||
import io.legado.app.utils.toastOnUi
|
||||
|
||||
class SpeakEngineViewModel(application: Application) : BaseViewModel(application) {
|
||||
|
||||
@ -52,12 +55,12 @@ class SpeakEngineViewModel(application: Application) : BaseViewModel(application
|
||||
fun import(text: String) {
|
||||
when {
|
||||
text.isJsonArray() -> {
|
||||
GSON.fromJsonArray<HttpTTS>(text)?.let {
|
||||
HttpTTS.fromJsonArray(text).let {
|
||||
appDb.httpTTSDao.insert(*it.toTypedArray())
|
||||
}
|
||||
}
|
||||
text.isJsonObject() -> {
|
||||
GSON.fromJsonObject<HttpTTS>(text)?.let {
|
||||
HttpTTS.fromJson(text)?.let {
|
||||
appDb.httpTTSDao.insert(it)
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ import android.content.Intent
|
||||
import io.legado.app.base.BaseViewModel
|
||||
import io.legado.app.data.appDb
|
||||
import io.legado.app.data.entities.BookSource
|
||||
import io.legado.app.help.BookSourceAnalyzer
|
||||
import io.legado.app.help.SourceAnalyzer
|
||||
import io.legado.app.help.http.newCallStrResponse
|
||||
import io.legado.app.help.http.okHttpClient
|
||||
import io.legado.app.model.NoStackTraceException
|
||||
@ -85,10 +85,10 @@ class BookSourceEditViewModel(application: Application) : BaseViewModel(applicat
|
||||
text.isJsonArray() -> {
|
||||
val items: List<Map<String, Any>> = jsonPath.parse(text).read("$")
|
||||
val jsonItem = jsonPath.parse(items[0])
|
||||
BookSourceAnalyzer.jsonToBookSource(jsonItem.jsonString())
|
||||
SourceAnalyzer.jsonToBookSource(jsonItem.jsonString())
|
||||
}
|
||||
text.isJsonObject() -> {
|
||||
BookSourceAnalyzer.jsonToBookSource(text)
|
||||
SourceAnalyzer.jsonToBookSource(text)
|
||||
}
|
||||
else -> {
|
||||
null
|
||||
|
@ -5,7 +5,10 @@ import android.content.Intent
|
||||
import io.legado.app.base.BaseViewModel
|
||||
import io.legado.app.data.appDb
|
||||
import io.legado.app.data.entities.RssSource
|
||||
import io.legado.app.utils.*
|
||||
import io.legado.app.utils.getClipText
|
||||
import io.legado.app.utils.msg
|
||||
import io.legado.app.utils.printOnDebug
|
||||
import io.legado.app.utils.toastOnUi
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
|
||||
class RssSourceEditViewModel(application: Application) : BaseViewModel(application) {
|
||||
@ -46,7 +49,7 @@ class RssSourceEditViewModel(application: Application) : BaseViewModel(applicati
|
||||
execute(context = Dispatchers.Main) {
|
||||
var source: RssSource? = null
|
||||
context.getClipText()?.let { json ->
|
||||
source = GSON.fromJsonObject<RssSource>(json)
|
||||
source = RssSource.fromJson(json)
|
||||
}
|
||||
source
|
||||
}.onError {
|
||||
@ -63,7 +66,7 @@ class RssSourceEditViewModel(application: Application) : BaseViewModel(applicati
|
||||
fun importSource(text: String, finally: (source: RssSource) -> Unit) {
|
||||
execute {
|
||||
val text1 = text.trim()
|
||||
GSON.fromJsonObject<RssSource>(text1)?.let {
|
||||
RssSource.fromJson(text1)?.let {
|
||||
finally.invoke(it)
|
||||
}
|
||||
}.onError {
|
||||
|
Loading…
Reference in New Issue
Block a user