Skip to content

Commit

Permalink
Revert "优化"
Browse files Browse the repository at this point in the history
This reverts commit 872a971
  • Loading branch information
gedoor committed Mar 21, 2023
1 parent 5e6fdc9 commit 40c300e
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ 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 {

Expand All @@ -24,7 +23,7 @@ object BookSourceController {
fun saveSource(postData: String?): ReturnData {
val returnData = ReturnData()
postData ?: return returnData.setErrorMsg("数据不能为空")
val bookSource = GSON.fromJsonObject<BookSource>(postData).getOrNull()
val bookSource = BookSource.fromJson(postData).getOrNull()
if (bookSource != null) {
if (TextUtils.isEmpty(bookSource.bookSourceName) || TextUtils.isEmpty(bookSource.bookSourceUrl)) {
returnData.setErrorMsg("源名称和URL不能为空")
Expand All @@ -41,7 +40,7 @@ object BookSourceController {
fun saveSources(postData: String?): ReturnData {
postData ?: return ReturnData().setErrorMsg("数据为空")
val okSources = arrayListOf<BookSource>()
val bookSources = GSON.fromJsonArray<BookSource>(postData).getOrNull()
val bookSources = BookSource.fromJsonArray(postData).getOrNull()
if (bookSources.isNullOrEmpty()) {
return ReturnData().setErrorMsg("转换源失败")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

object RssSourceController {

Expand All @@ -23,7 +20,7 @@ object RssSourceController {
fun saveSource(postData: String?): ReturnData {
val returnData = ReturnData()
postData ?: return returnData.setErrorMsg("数据不能为空")
GSON.fromJsonObject<RssSource>(postData).onFailure {
RssSource.fromJson(postData).onFailure {
returnData.setErrorMsg("转换源失败${it.localizedMessage}")
}.onSuccess { source ->
if (TextUtils.isEmpty(source.sourceName) || TextUtils.isEmpty(source.sourceUrl)) {
Expand All @@ -39,7 +36,7 @@ object RssSourceController {
fun saveSources(postData: String?): ReturnData {
postData ?: return ReturnData().setErrorMsg("数据不能为空")
val okSources = arrayListOf<RssSource>()
val source = GSON.fromJsonArray<RssSource>(postData).getOrNull()
val source = RssSource.fromJsonArray(postData).getOrNull()
if (source.isNullOrEmpty()) {
return ReturnData().setErrorMsg("转换源失败")
}
Expand All @@ -66,7 +63,7 @@ object RssSourceController {

fun deleteSources(postData: String?): ReturnData {
postData ?: return ReturnData().setErrorMsg("没有传递数据")
GSON.fromJsonArray<RssSource>(postData).onFailure {
RssSource.fromJsonArray(postData).onFailure {
return ReturnData().setErrorMsg("格式不对")
}.onSuccess {
it.forEach { source ->
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/java/io/legado/app/data/entities/BookSource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ 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
Expand Down Expand Up @@ -218,6 +220,22 @@ data class BookSource(

private fun equal(a: String?, b: String?) = a == b || (a.isNullOrEmpty() && b.isNullOrEmpty())

companion object {

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
Expand Down
20 changes: 15 additions & 5 deletions app/src/main/java/io/legado/app/data/entities/RssSource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ package io.legado.app.data.entities

import android.os.Parcelable
import android.text.TextUtils
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.Index
import androidx.room.PrimaryKey
import androidx.room.*
import io.legado.app.constant.AppPattern
import io.legado.app.utils.splitNotBlank
import io.legado.app.utils.*
import kotlinx.parcelize.Parcelize

@Parcelize
Expand Down Expand Up @@ -171,4 +168,17 @@ 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 changes: 1 addition & 1 deletion app/src/main/java/io/legado/app/help/DefaultData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ object DefaultData {
appCtx.assets.open("defaultData${File.separator}rssSources.json")
.readBytes()
)
GSON.fromJsonArray<RssSource>(json).getOrDefault(emptyList())
RssSource.fromJsonArray(json).getOrDefault(emptyList())
}

val coverRule: BookCover.CoverRule by lazy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ class ImportBookSourceDialog() : BaseDialogFragment(R.layout.dialog_recycler_vie

override fun onCodeSave(code: String, requestId: String?) {
requestId?.toInt()?.let {
GSON.fromJsonObject<BookSource>(code).getOrNull()?.let { source ->
BookSource.fromJson(code).getOrNull()?.let { source ->
viewModel.allSources[it] = source
adapter.setItem(it, source)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.jayway.jsonpath.JsonPath
import io.legado.app.R
import io.legado.app.base.BaseViewModel
import io.legado.app.constant.AppConst
import io.legado.app.constant.AppLog
import io.legado.app.constant.AppPattern
import io.legado.app.data.appDb
import io.legado.app.data.entities.BookSource
Expand Down Expand Up @@ -100,35 +99,31 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) {
val json = JsonPath.parse(mText)
json.read<List<String>>("$.sourceUrls")
}.onSuccess {
it.forEach { url ->
importSourceUrl(url)
it.forEach {
importSourceUrl(it)
}
}.onFailure {
GSON.fromJsonObject<BookSource>(mText).getOrThrow().let {
BookSource.fromJson(mText).getOrThrow().let {
allSources.add(it)
}
}
}
mText.isJsonArray() -> {
GSON.fromJsonArray<BookSource>(mText).getOrThrow().let { items ->
allSources.addAll(items)
}
mText.isJsonArray() -> BookSource.fromJsonArray(mText).getOrThrow().let { items ->
allSources.addAll(items)
}
mText.isAbsUrl() -> {
importSourceUrl(mText)
}
mText.isUri() -> {
val uri = Uri.parse(mText)
uri.inputStream(context).getOrThrow().use {
GSON.fromJsonArray<BookSource>(it).getOrThrow().let {bookSources ->
allSources.addAll(bookSources)
}
uri.inputStream(context).getOrThrow().let {
allSources.addAll(BookSource.fromJsonArray(it).getOrThrow())
}
}
else -> throw NoStackTraceException(context.getString(R.string.wrong_format))
}
}.onError {
AppLog.put("读取书源出错", it)
it.printOnDebug()
errorLiveData.postValue(it.localizedMessage ?: "")
}.onSuccess {
comparisonSource()
Expand All @@ -144,7 +139,7 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) {
url(url)
}
}.byteStream().let {
allSources.addAll(GSON.fromJsonArray<BookSource>(it).getOrThrow())
allSources.addAll(BookSource.fromJsonArray(it).getOrThrow())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ class ImportRssSourceDialog() : BaseDialogFragment(R.layout.dialog_recycler_view

override fun onCodeSave(code: String, requestId: String?) {
requestId?.toInt()?.let {
GSON.fromJsonObject<RssSource>(code).getOrNull()?.let { source ->
RssSource.fromJson(code).getOrNull()?.let { source ->
viewModel.allSources[it] = source
adapter.setItem(it, source)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) {
importSourceUrl(it)
}
} else {
GSON.fromJsonArray<RssSource>(mText).getOrThrow().let {
RssSource.fromJsonArray(mText).getOrThrow().let {
allSources.addAll(it)
}
}
}
mText.isJsonArray() -> {
GSON.fromJsonArray<RssSource>(mText).getOrThrow().let {
RssSource.fromJsonArray(mText).getOrThrow().let {
allSources.addAll(it)
}
}
Expand Down Expand Up @@ -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)
GSON.fromJsonObject<RssSource>(jsonItem.jsonString()).getOrThrow().let { source ->
RssSource.fromJson(jsonItem.jsonString()).getOrThrow().let { source ->
allSources.add(source)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ class BookSourceEditViewModel(application: Application) : BaseViewModel(applicat
text.isJsonArray() -> {
val items: List<Map<String, Any>> = jsonPath.parse(text).read("$")
val jsonItem = jsonPath.parse(items[0])
GSON.fromJsonObject<BookSource>(jsonItem.jsonString()).getOrElse {
BookSource.fromJson(jsonItem.jsonString()).getOrElse {
ImportOldData.fromOldBookSource(jsonItem)
}
}
text.isJsonObject() -> {
GSON.fromJsonObject<BookSource>(text).getOrElse {
BookSource.fromJson(text).getOrElse {
val jsonItem = jsonPath.parse(text)
ImportOldData.fromOldBookSource(jsonItem)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ 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.*
import io.legado.app.utils.getClipText
import io.legado.app.utils.printOnDebug
import io.legado.app.utils.stackTraceStr

import io.legado.app.utils.toastOnUi
import kotlinx.coroutines.Dispatchers


Expand Down Expand Up @@ -54,7 +57,7 @@ class RssSourceEditViewModel(application: Application) : BaseViewModel(applicati
execute(context = Dispatchers.Main) {
var source: RssSource? = null
context.getClipText()?.let { json ->
source = GSON.fromJsonObject<RssSource>(json).getOrThrow()
source = RssSource.fromJson(json).getOrThrow()
}
source
}.onError {
Expand All @@ -71,7 +74,7 @@ class RssSourceEditViewModel(application: Application) : BaseViewModel(applicati
fun importSource(text: String, finally: (source: RssSource) -> Unit) {
execute {
val text1 = text.trim()
GSON.fromJsonObject<RssSource>(text1).getOrThrow().let {
RssSource.fromJson(text1).getOrThrow().let {
finally.invoke(it)
}
}.onError {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/io/legado/app/utils/GsonExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ val GSON: Gson by lazy {
)
.registerTypeAdapter(Int::class.java, IntJsonDeserializer())
.registerTypeAdapter(String::class.java, StringJsonDeserializer())
// .registerTypeAdapter(ExploreRule::class.java, ExploreRule.jsonDeserializer)
.registerTypeAdapter(ExploreRule::class.java, ExploreRule.jsonDeserializer)
// .registerTypeAdapter(SearchRule::class.java, SearchRule.jsonDeserializer)
// .registerTypeAdapter(BookInfoRule::class.java, BookInfoRule.jsonDeserializer)
// .registerTypeAdapter(TocRule::class.java, TocRule.jsonDeserializer)
Expand Down

0 comments on commit 40c300e

Please sign in to comment.