diff --git a/app/src/main/java/io/legado/app/api/controller/BookSourceController.kt b/app/src/main/java/io/legado/app/api/controller/BookSourceController.kt index 344b81c9e..c705dd2a8 100644 --- a/app/src/main/java/io/legado/app/api/controller/BookSourceController.kt +++ b/app/src/main/java/io/legado/app/api/controller/BookSourceController.kt @@ -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() - val bookSources = BookSourceAnalyzer.jsonToBookSources(postData) + val bookSources = SourceAnalyzer.jsonToBookSources(postData) if (bookSources.isNotEmpty()) { bookSources.forEach { bookSource -> if (bookSource.bookSourceName.isNotBlank() diff --git a/app/src/main/java/io/legado/app/api/controller/RssSourceController.kt b/app/src/main/java/io/legado/app/api/controller/RssSourceController.kt index d0366acb8..95d80abe0 100644 --- a/app/src/main/java/io/legado/app/api/controller/RssSourceController.kt +++ b/app/src/main/java/io/legado/app/api/controller/RssSourceController.kt @@ -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(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() - val source = GSON.fromJsonArray(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(postData)?.let { + RssSource.fromJsonArray(postData).let { it.forEach { source -> appDb.rssSourceDao.delete(source) } diff --git a/app/src/main/java/io/legado/app/data/entities/HttpTTS.kt b/app/src/main/java/io/legado/app/data/entities/HttpTTS.kt index bed7eb730..23bf125b1 100644 --- a/app/src/main/java/io/legado/app/data/entities/HttpTTS.kt +++ b/app/src/main/java/io/legado/app/data/entities/HttpTTS.kt @@ -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("$.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 { + val sources = arrayListOf() + val doc = jsonPath.parse(jsonArray).read>("$") + doc.forEach { + val jsonItem = jsonPath.parse(it) + fromJsonDoc(jsonItem)?.let { source -> + sources.add(source) + } + } + return sources + } + + } + } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/data/entities/RssSource.kt b/app/src/main/java/io/legado/app/data/entities/RssSource.kt index 3584dea0a..78830285a 100644 --- a/app/src/main/java/io/legado/app/data/entities/RssSource.kt +++ b/app/src/main/java/io/legado/app/data/entities/RssSource.kt @@ -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("$.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 { + val sources = arrayListOf() + val doc = jsonPath.parse(jsonArray).read>("$") + doc.forEach { + val jsonItem = jsonPath.parse(it) + fromJsonDoc(jsonItem)?.let { source -> + sources.add(source) + } + } + return sources + } + } + } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/DefaultData.kt b/app/src/main/java/io/legado/app/help/DefaultData.kt index 94231a2fe..3dcc0282d 100644 --- a/app/src/main/java/io/legado/app/help/DefaultData.kt +++ b/app/src/main/java/io/legado/app/help/DefaultData.kt @@ -14,45 +14,45 @@ object DefaultData { const val httpTtsFileName = "httpTTS.json" const val txtTocRuleFileName = "txtTocRule.json" - val httpTTS by lazy { + val httpTTS: List by lazy { val json = String( appCtx.assets.open("defaultData${File.separator}$httpTtsFileName") .readBytes() ) - GSON.fromJsonArray(json)!! + GSON.fromJsonArray(json)!! } - val readConfigs by lazy { + val readConfigs: List by lazy { val json = String( appCtx.assets.open("defaultData${File.separator}${ReadBookConfig.configFileName}") .readBytes() ) - GSON.fromJsonArray(json)!! + GSON.fromJsonArray(json)!! } - val txtTocRules by lazy { + val txtTocRules: List by lazy { val json = String( appCtx.assets.open("defaultData${File.separator}$txtTocRuleFileName") .readBytes() ) - GSON.fromJsonArray(json)!! + GSON.fromJsonArray(json)!! } - val themeConfigs by lazy { + val themeConfigs: List by lazy { val json = String( appCtx.assets.open("defaultData${File.separator}${ThemeConfig.configFileName}") .readBytes() ) - GSON.fromJsonArray(json)!! + GSON.fromJsonArray(json)!! } - val rssSources by lazy { + val rssSources: List by lazy { val json = String( appCtx.assets.open("defaultData${File.separator}rssSources.json") .readBytes() ) - GSON.fromJsonArray(json)!! + RssSource.fromJsonArray(json) } fun importDefaultHttpTTS() { diff --git a/app/src/main/java/io/legado/app/help/BookSourceAnalyzer.kt b/app/src/main/java/io/legado/app/help/SourceAnalyzer.kt similarity index 99% rename from app/src/main/java/io/legado/app/help/BookSourceAnalyzer.kt rename to app/src/main/java/io/legado/app/help/SourceAnalyzer.kt index c6622191d..81f55b549 100644 --- a/app/src/main/java/io/legado/app/help/BookSourceAnalyzer.kt +++ b/app/src/main/java/io/legado/app/help/SourceAnalyzer.kt @@ -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) diff --git a/app/src/main/java/io/legado/app/help/storage/ImportOldData.kt b/app/src/main/java/io/legado/app/help/storage/ImportOldData.kt index bcbd4c928..1cf361213 100644 --- a/app/src/main/java/io/legado/app/help/storage/ImportOldData.kt +++ b/app/src/main/java/io/legado/app/help/storage/ImportOldData.kt @@ -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 } diff --git a/app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt index 2fbcf2c81..520886401 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt @@ -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> = 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) } } diff --git a/app/src/main/java/io/legado/app/ui/association/ImportRssSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/association/ImportRssSourceViewModel.kt index 840ed67ce..ac83bc3db 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportRssSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportRssSourceViewModel.kt @@ -95,7 +95,7 @@ class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) { importSourceUrl(it) } } else { - GSON.fromJsonArray(mText)?.let { + RssSource.fromJsonArray(mText).let { allSources.addAll(it) } } @@ -104,7 +104,7 @@ class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) { val items: List> = jsonPath.parse(mText).read("$") for (item in items) { val jsonItem = jsonPath.parse(item) - GSON.fromJsonObject(jsonItem.jsonString())?.let { + RssSource.fromJsonDoc(jsonItem)?.let { allSources.add(it) } } @@ -128,7 +128,7 @@ class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) { val items: List> = jsonPath.parse(body).read("$") for (item in items) { val jsonItem = jsonPath.parse(item) - GSON.fromJsonObject(jsonItem.jsonString())?.let { source -> + RssSource.fromJson(jsonItem.jsonString())?.let { source -> allSources.add(source) } } diff --git a/app/src/main/java/io/legado/app/ui/association/OnLineImportViewModel.kt b/app/src/main/java/io/legado/app/ui/association/OnLineImportViewModel.kt index 8c257a28f..ddc5518ec 100644 --- a/app/src/main/java/io/legado/app/ui/association/OnLineImportViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/association/OnLineImportViewModel.kt @@ -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(json)?.let { + HttpTTS.fromJsonArray(json).let { appDb.httpTTSDao.insert(*it.toTypedArray()) return@execute it.size - } ?: throw NoStackTraceException("格式不对") + } } else { - GSON.fromJsonObject(json)?.let { + HttpTTS.fromJson(json)?.let { appDb.httpTTSDao.insert(it) return@execute 1 } ?: throw NoStackTraceException("格式不对") diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/HttpTtsEditViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/config/HttpTtsEditViewModel.kt index 1c8e48580..2978e8f3f 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/HttpTtsEditViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/HttpTtsEditViewModel.kt @@ -53,10 +53,10 @@ class HttpTtsEditViewModel(app: Application) : BaseViewModel(app) { execute { when { text1.isJsonObject() -> { - GSON.fromJsonObject(text1) + HttpTTS.fromJson(text1) } text1.isJsonArray() -> { - GSON.fromJsonArray(text1)?.firstOrNull() + HttpTTS.fromJsonArray(text1).firstOrNull() } else -> { throw NoStackTraceException("格式不对") diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineViewModel.kt index dec3b7bd0..f8033f57f 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineViewModel.kt @@ -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(text)?.let { + HttpTTS.fromJsonArray(text).let { appDb.httpTTSDao.insert(*it.toTypedArray()) } } text.isJsonObject() -> { - GSON.fromJsonObject(text)?.let { + HttpTTS.fromJson(text)?.let { appDb.httpTTSDao.insert(it) } } diff --git a/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditViewModel.kt b/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditViewModel.kt index 592238a6b..71c5673d0 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditViewModel.kt @@ -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> = 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 diff --git a/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditViewModel.kt b/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditViewModel.kt index e6c65e5fa..768a4b3b7 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditViewModel.kt @@ -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(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(text1)?.let { + RssSource.fromJson(text1)?.let { finally.invoke(it) } }.onError {