mirror of
https://github.com/gedoor/legado.git
synced 2025-08-10 00:52:30 +00:00
优化
This commit is contained in:
@@ -8,6 +8,7 @@ import io.legado.app.data.entities.BookSource
|
||||
import io.legado.app.help.config.SourceConfig
|
||||
import io.legado.app.utils.GSON
|
||||
import io.legado.app.utils.fromJsonArray
|
||||
import io.legado.app.utils.fromJsonObject
|
||||
|
||||
object BookSourceController {
|
||||
|
||||
@@ -23,7 +24,7 @@ object BookSourceController {
|
||||
fun saveSource(postData: String?): ReturnData {
|
||||
val returnData = ReturnData()
|
||||
postData ?: return returnData.setErrorMsg("数据不能为空")
|
||||
val bookSource = BookSource.fromJson(postData).getOrNull()
|
||||
val bookSource = GSON.fromJsonObject<BookSource>(postData).getOrNull()
|
||||
if (bookSource != null) {
|
||||
if (TextUtils.isEmpty(bookSource.bookSourceName) || TextUtils.isEmpty(bookSource.bookSourceUrl)) {
|
||||
returnData.setErrorMsg("源名称和URL不能为空")
|
||||
@@ -40,7 +41,7 @@ object BookSourceController {
|
||||
fun saveSources(postData: String?): ReturnData {
|
||||
postData ?: return ReturnData().setErrorMsg("数据为空")
|
||||
val okSources = arrayListOf<BookSource>()
|
||||
val bookSources = BookSource.fromJsonArray(postData).getOrNull()
|
||||
val bookSources = GSON.fromJsonArray<BookSource>(postData).getOrNull()
|
||||
if (bookSources.isNullOrEmpty()) {
|
||||
return ReturnData().setErrorMsg("转换源失败")
|
||||
}
|
||||
|
||||
@@ -5,6 +5,9 @@ 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
|
||||
|
||||
object RssSourceController {
|
||||
|
||||
@@ -20,7 +23,7 @@ object RssSourceController {
|
||||
fun saveSource(postData: String?): ReturnData {
|
||||
val returnData = ReturnData()
|
||||
postData ?: return returnData.setErrorMsg("数据不能为空")
|
||||
RssSource.fromJson(postData).onFailure {
|
||||
GSON.fromJsonObject<RssSource>(postData).onFailure {
|
||||
returnData.setErrorMsg("转换源失败${it.localizedMessage}")
|
||||
}.onSuccess { source ->
|
||||
if (TextUtils.isEmpty(source.sourceName) || TextUtils.isEmpty(source.sourceUrl)) {
|
||||
@@ -36,7 +39,7 @@ object RssSourceController {
|
||||
fun saveSources(postData: String?): ReturnData {
|
||||
postData ?: return ReturnData().setErrorMsg("数据不能为空")
|
||||
val okSources = arrayListOf<RssSource>()
|
||||
val source = RssSource.fromJsonArray(postData).getOrNull()
|
||||
val source = GSON.fromJsonArray<RssSource>(postData).getOrNull()
|
||||
if (source.isNullOrEmpty()) {
|
||||
return ReturnData().setErrorMsg("转换源失败")
|
||||
}
|
||||
@@ -63,7 +66,7 @@ object RssSourceController {
|
||||
|
||||
fun deleteSources(postData: String?): ReturnData {
|
||||
postData ?: return ReturnData().setErrorMsg("没有传递数据")
|
||||
RssSource.fromJsonArray(postData).onFailure {
|
||||
GSON.fromJsonArray<RssSource>(postData).onFailure {
|
||||
return ReturnData().setErrorMsg("格式不对")
|
||||
}.onSuccess {
|
||||
it.forEach { source ->
|
||||
|
||||
@@ -7,11 +7,9 @@ import io.legado.app.constant.AppPattern
|
||||
import io.legado.app.constant.BookSourceType
|
||||
import io.legado.app.data.entities.rule.*
|
||||
import io.legado.app.utils.GSON
|
||||
import io.legado.app.utils.fromJsonArray
|
||||
import io.legado.app.utils.fromJsonObject
|
||||
import io.legado.app.utils.splitNotBlank
|
||||
import kotlinx.parcelize.Parcelize
|
||||
import java.io.InputStream
|
||||
|
||||
@Suppress("unused")
|
||||
@Parcelize
|
||||
@@ -220,31 +218,6 @@ data class BookSource(
|
||||
|
||||
private fun equal(a: String?, b: String?) = a == b || (a.isNullOrEmpty() && b.isNullOrEmpty())
|
||||
|
||||
companion object {
|
||||
|
||||
private val gson = GSON.newBuilder()
|
||||
.registerTypeAdapter(ExploreRule::class.java, ExploreRule.jsonDeserializer)
|
||||
.registerTypeAdapter(SearchRule::class.java, SearchRule.jsonDeserializer)
|
||||
.registerTypeAdapter(BookInfoRule::class.java, BookInfoRule.jsonDeserializer)
|
||||
.registerTypeAdapter(TocRule::class.java, TocRule.jsonDeserializer)
|
||||
.registerTypeAdapter(ContentRule::class.java, ContentRule.jsonDeserializer)
|
||||
.registerTypeAdapter(ReviewRule::class.java, ReviewRule.jsonDeserializer)
|
||||
.create()
|
||||
|
||||
fun fromJson(json: String): Result<BookSource> {
|
||||
return gson.fromJsonObject(json)
|
||||
}
|
||||
|
||||
fun fromJsonArray(json: String): Result<List<BookSource>> {
|
||||
return gson.fromJsonArray(json)
|
||||
}
|
||||
|
||||
fun fromJsonArray(inputStream: InputStream): Result<List<BookSource>> {
|
||||
return gson.fromJsonArray(inputStream)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Converters {
|
||||
|
||||
@TypeConverter
|
||||
|
||||
@@ -2,9 +2,12 @@ package io.legado.app.data.entities
|
||||
|
||||
import android.os.Parcelable
|
||||
import android.text.TextUtils
|
||||
import androidx.room.*
|
||||
import androidx.room.ColumnInfo
|
||||
import androidx.room.Entity
|
||||
import androidx.room.Index
|
||||
import androidx.room.PrimaryKey
|
||||
import io.legado.app.constant.AppPattern
|
||||
import io.legado.app.utils.*
|
||||
import io.legado.app.utils.splitNotBlank
|
||||
import kotlinx.parcelize.Parcelize
|
||||
|
||||
@Parcelize
|
||||
@@ -168,17 +171,4 @@ data class RssSource(
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("MemberVisibilityCanBePrivate")
|
||||
companion object {
|
||||
|
||||
fun fromJson(json: String): Result<RssSource> {
|
||||
return GSON.fromJsonObject(json)
|
||||
}
|
||||
|
||||
fun fromJsonArray(jsonArray: String): Result<List<RssSource>> {
|
||||
return GSON.fromJsonArray(jsonArray)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package io.legado.app.data.entities.rule
|
||||
|
||||
import android.os.Parcelable
|
||||
import com.google.gson.JsonDeserializer
|
||||
import io.legado.app.utils.GSON
|
||||
import io.legado.app.utils.INITIAL_GSON
|
||||
import kotlinx.parcelize.Parcelize
|
||||
|
||||
/**
|
||||
@@ -28,8 +28,11 @@ data class BookInfoRule(
|
||||
|
||||
val jsonDeserializer = JsonDeserializer<BookInfoRule?> { json, _, _ ->
|
||||
when {
|
||||
json.isJsonObject -> GSON.fromJson(json, BookInfoRule::class.java)
|
||||
json.isJsonPrimitive -> GSON.fromJson(json.asString, BookInfoRule::class.java)
|
||||
json.isJsonObject -> INITIAL_GSON.fromJson(json, BookInfoRule::class.java)
|
||||
json.isJsonPrimitive -> INITIAL_GSON.fromJson(
|
||||
json.asString,
|
||||
BookInfoRule::class.java
|
||||
)
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package io.legado.app.data.entities.rule
|
||||
|
||||
import android.os.Parcelable
|
||||
import com.google.gson.JsonDeserializer
|
||||
import io.legado.app.utils.GSON
|
||||
import io.legado.app.utils.INITIAL_GSON
|
||||
import kotlinx.parcelize.Parcelize
|
||||
|
||||
/**
|
||||
@@ -25,8 +25,11 @@ data class ContentRule(
|
||||
|
||||
val jsonDeserializer = JsonDeserializer<ContentRule?> { json, _, _ ->
|
||||
when {
|
||||
json.isJsonObject -> GSON.fromJson(json, ContentRule::class.java)
|
||||
json.isJsonPrimitive -> GSON.fromJson(json.asString, ContentRule::class.java)
|
||||
json.isJsonObject -> INITIAL_GSON.fromJson(json, ContentRule::class.java)
|
||||
json.isJsonPrimitive -> INITIAL_GSON.fromJson(
|
||||
json.asString,
|
||||
ContentRule::class.java
|
||||
)
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package io.legado.app.data.entities.rule
|
||||
|
||||
import android.os.Parcelable
|
||||
import com.google.gson.JsonDeserializer
|
||||
import io.legado.app.utils.GSON
|
||||
import io.legado.app.utils.INITIAL_GSON
|
||||
import kotlinx.parcelize.Parcelize
|
||||
|
||||
/**
|
||||
@@ -26,8 +26,11 @@ data class ExploreRule(
|
||||
|
||||
val jsonDeserializer = JsonDeserializer<ExploreRule?> { json, _, _ ->
|
||||
when {
|
||||
json.isJsonObject -> GSON.fromJson(json, ExploreRule::class.java)
|
||||
json.isJsonPrimitive -> GSON.fromJson(json.asString, ExploreRule::class.java)
|
||||
json.isJsonObject -> INITIAL_GSON.fromJson(json, ExploreRule::class.java)
|
||||
json.isJsonPrimitive -> INITIAL_GSON.fromJson(
|
||||
json.asString,
|
||||
ExploreRule::class.java
|
||||
)
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package io.legado.app.data.entities.rule
|
||||
|
||||
import android.os.Parcelable
|
||||
import com.google.gson.JsonDeserializer
|
||||
import io.legado.app.utils.GSON
|
||||
import io.legado.app.utils.INITIAL_GSON
|
||||
import kotlinx.parcelize.Parcelize
|
||||
|
||||
@Parcelize
|
||||
@@ -25,8 +25,8 @@ data class ReviewRule(
|
||||
|
||||
val jsonDeserializer = JsonDeserializer<ReviewRule?> { json, _, _ ->
|
||||
when {
|
||||
json.isJsonObject -> GSON.fromJson(json, ReviewRule::class.java)
|
||||
json.isJsonPrimitive -> GSON.fromJson(json.asString, ReviewRule::class.java)
|
||||
json.isJsonObject -> INITIAL_GSON.fromJson(json, ReviewRule::class.java)
|
||||
json.isJsonPrimitive -> INITIAL_GSON.fromJson(json.asString, ReviewRule::class.java)
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package io.legado.app.data.entities.rule
|
||||
|
||||
import android.os.Parcelable
|
||||
import com.google.gson.JsonDeserializer
|
||||
import io.legado.app.utils.GSON
|
||||
import io.legado.app.utils.INITIAL_GSON
|
||||
import kotlinx.parcelize.Parcelize
|
||||
|
||||
/**
|
||||
@@ -28,8 +28,8 @@ data class SearchRule(
|
||||
|
||||
val jsonDeserializer = JsonDeserializer<SearchRule?> { json, _, _ ->
|
||||
when {
|
||||
json.isJsonObject -> GSON.fromJson(json, SearchRule::class.java)
|
||||
json.isJsonPrimitive -> GSON.fromJson(json.asString, SearchRule::class.java)
|
||||
json.isJsonObject -> INITIAL_GSON.fromJson(json, SearchRule::class.java)
|
||||
json.isJsonPrimitive -> INITIAL_GSON.fromJson(json.asString, SearchRule::class.java)
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package io.legado.app.data.entities.rule
|
||||
|
||||
import android.os.Parcelable
|
||||
import com.google.gson.JsonDeserializer
|
||||
import io.legado.app.utils.GSON
|
||||
import io.legado.app.utils.INITIAL_GSON
|
||||
import kotlinx.parcelize.Parcelize
|
||||
|
||||
@Parcelize
|
||||
@@ -22,8 +22,8 @@ data class TocRule(
|
||||
|
||||
val jsonDeserializer = JsonDeserializer<TocRule?> { json, _, _ ->
|
||||
when {
|
||||
json.isJsonObject -> GSON.fromJson(json, TocRule::class.java)
|
||||
json.isJsonPrimitive -> GSON.fromJson(json.asString, TocRule::class.java)
|
||||
json.isJsonObject -> INITIAL_GSON.fromJson(json, TocRule::class.java)
|
||||
json.isJsonPrimitive -> INITIAL_GSON.fromJson(json.asString, TocRule::class.java)
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,7 +79,7 @@ object DefaultData {
|
||||
appCtx.assets.open("defaultData${File.separator}rssSources.json")
|
||||
.readBytes()
|
||||
)
|
||||
RssSource.fromJsonArray(json).getOrDefault(emptyList())
|
||||
GSON.fromJsonArray<RssSource>(json).getOrDefault(emptyList())
|
||||
}
|
||||
|
||||
val coverRule: BookCover.CoverRule by lazy {
|
||||
|
||||
@@ -191,7 +191,7 @@ class ImportBookSourceDialog() : BaseDialogFragment(R.layout.dialog_recycler_vie
|
||||
|
||||
override fun onCodeSave(code: String, requestId: String?) {
|
||||
requestId?.toInt()?.let {
|
||||
BookSource.fromJson(code).getOrNull()?.let { source ->
|
||||
GSON.fromJsonObject<BookSource>(code).getOrNull()?.let { source ->
|
||||
viewModel.allSources[it] = source
|
||||
adapter.setItem(it, source)
|
||||
}
|
||||
|
||||
@@ -98,26 +98,27 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) {
|
||||
kotlin.runCatching {
|
||||
val json = JsonPath.parse(mText)
|
||||
json.read<List<String>>("$.sourceUrls")
|
||||
}.onSuccess {
|
||||
it.forEach {
|
||||
}.onSuccess { listUrl ->
|
||||
listUrl.forEach {
|
||||
importSourceUrl(it)
|
||||
}
|
||||
}.onFailure {
|
||||
BookSource.fromJson(mText).getOrThrow().let {
|
||||
GSON.fromJsonObject<BookSource>(mText).getOrThrow().let {
|
||||
allSources.add(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
mText.isJsonArray() -> BookSource.fromJsonArray(mText).getOrThrow().let { items ->
|
||||
allSources.addAll(items)
|
||||
}
|
||||
mText.isJsonArray() -> GSON.fromJsonArray<BookSource>(mText).getOrThrow()
|
||||
.let { items ->
|
||||
allSources.addAll(items)
|
||||
}
|
||||
mText.isAbsUrl() -> {
|
||||
importSourceUrl(mText)
|
||||
}
|
||||
mText.isUri() -> {
|
||||
val uri = Uri.parse(mText)
|
||||
uri.inputStream(context).getOrThrow().let {
|
||||
allSources.addAll(BookSource.fromJsonArray(it).getOrThrow())
|
||||
allSources.addAll(GSON.fromJsonArray<BookSource>(it).getOrThrow())
|
||||
}
|
||||
}
|
||||
else -> throw NoStackTraceException(context.getString(R.string.wrong_format))
|
||||
@@ -139,7 +140,7 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) {
|
||||
url(url)
|
||||
}
|
||||
}.byteStream().let {
|
||||
allSources.addAll(BookSource.fromJsonArray(it).getOrThrow())
|
||||
allSources.addAll(GSON.fromJsonArray<BookSource>(it).getOrThrow())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -190,7 +190,7 @@ class ImportRssSourceDialog() : BaseDialogFragment(R.layout.dialog_recycler_view
|
||||
|
||||
override fun onCodeSave(code: String, requestId: String?) {
|
||||
requestId?.toInt()?.let {
|
||||
RssSource.fromJson(code).getOrNull()?.let { source ->
|
||||
GSON.fromJsonObject<RssSource>(code).getOrNull()?.let { source ->
|
||||
viewModel.allSources[it] = source
|
||||
adapter.setItem(it, source)
|
||||
}
|
||||
|
||||
@@ -98,13 +98,13 @@ class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) {
|
||||
importSourceUrl(it)
|
||||
}
|
||||
} else {
|
||||
RssSource.fromJsonArray(mText).getOrThrow().let {
|
||||
GSON.fromJsonArray<RssSource>(mText).getOrThrow().let {
|
||||
allSources.addAll(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
mText.isJsonArray() -> {
|
||||
RssSource.fromJsonArray(mText).getOrThrow().let {
|
||||
GSON.fromJsonArray<RssSource>(mText).getOrThrow().let {
|
||||
allSources.addAll(it)
|
||||
}
|
||||
}
|
||||
@@ -132,7 +132,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)
|
||||
RssSource.fromJson(jsonItem.jsonString()).getOrThrow().let { source ->
|
||||
GSON.fromJsonObject<RssSource>(jsonItem.jsonString()).getOrThrow().let { source ->
|
||||
allSources.add(source)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,7 +96,7 @@ class BookSourceEditViewModel(application: Application) : BaseViewModel(applicat
|
||||
val jsonItem = jsonPath.parse(items[0])
|
||||
ImportOldData.fromOldBookSource(jsonItem)
|
||||
} else {
|
||||
BookSource.fromJsonArray(text).getOrThrow()[0]
|
||||
GSON.fromJsonArray<BookSource>(text).getOrThrow()[0]
|
||||
}
|
||||
}
|
||||
text.isJsonObject() -> {
|
||||
@@ -104,7 +104,7 @@ class BookSourceEditViewModel(application: Application) : BaseViewModel(applicat
|
||||
val jsonItem = jsonPath.parse(text)
|
||||
ImportOldData.fromOldBookSource(jsonItem)
|
||||
} else {
|
||||
BookSource.fromJson(text).getOrThrow()
|
||||
GSON.fromJsonObject<BookSource>(text).getOrThrow()
|
||||
}
|
||||
}
|
||||
else -> throw NoStackTraceException("格式不对")
|
||||
|
||||
@@ -9,11 +9,8 @@ import io.legado.app.data.entities.RssSource
|
||||
import io.legado.app.exception.NoStackTraceException
|
||||
import io.legado.app.help.RuleComplete
|
||||
import io.legado.app.help.http.CookieStore
|
||||
import io.legado.app.utils.getClipText
|
||||
import io.legado.app.utils.printOnDebug
|
||||
import io.legado.app.utils.stackTraceStr
|
||||
import io.legado.app.utils.*
|
||||
|
||||
import io.legado.app.utils.toastOnUi
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
|
||||
|
||||
@@ -57,7 +54,7 @@ class RssSourceEditViewModel(application: Application) : BaseViewModel(applicati
|
||||
execute(context = Dispatchers.Main) {
|
||||
var source: RssSource? = null
|
||||
context.getClipText()?.let { json ->
|
||||
source = RssSource.fromJson(json).getOrThrow()
|
||||
source = GSON.fromJsonObject<RssSource>(json).getOrThrow()
|
||||
}
|
||||
source
|
||||
}.onError {
|
||||
@@ -74,7 +71,7 @@ class RssSourceEditViewModel(application: Application) : BaseViewModel(applicati
|
||||
fun importSource(text: String, finally: (source: RssSource) -> Unit) {
|
||||
execute {
|
||||
val text1 = text.trim()
|
||||
RssSource.fromJson(text1).getOrThrow().let {
|
||||
GSON.fromJsonObject<RssSource>(text1).getOrThrow().let {
|
||||
finally.invoke(it)
|
||||
}
|
||||
}.onError {
|
||||
|
||||
@@ -13,8 +13,7 @@ import java.lang.reflect.ParameterizedType
|
||||
import java.lang.reflect.Type
|
||||
import kotlin.math.ceil
|
||||
|
||||
|
||||
val GSON: Gson by lazy {
|
||||
val INITIAL_GSON by lazy {
|
||||
GsonBuilder()
|
||||
.registerTypeAdapter(
|
||||
object : TypeToken<Map<String?, Any?>?>() {}.type,
|
||||
@@ -27,6 +26,17 @@ val GSON: Gson by lazy {
|
||||
.create()
|
||||
}
|
||||
|
||||
val GSON: Gson by lazy {
|
||||
INITIAL_GSON.newBuilder()
|
||||
.registerTypeAdapter(ExploreRule::class.java, ExploreRule.jsonDeserializer)
|
||||
.registerTypeAdapter(SearchRule::class.java, SearchRule.jsonDeserializer)
|
||||
.registerTypeAdapter(BookInfoRule::class.java, BookInfoRule.jsonDeserializer)
|
||||
.registerTypeAdapter(TocRule::class.java, TocRule.jsonDeserializer)
|
||||
.registerTypeAdapter(ContentRule::class.java, ContentRule.jsonDeserializer)
|
||||
.registerTypeAdapter(ReviewRule::class.java, ReviewRule.jsonDeserializer)
|
||||
.create()
|
||||
}
|
||||
|
||||
inline fun <reified T> genericType(): Type = object : TypeToken<T>() {}.type
|
||||
|
||||
inline fun <reified T> Gson.fromJsonObject(json: String?): Result<T> {
|
||||
|
||||
Reference in New Issue
Block a user