优化
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.help.config.SourceConfig
|
||||||
import io.legado.app.utils.GSON
|
import io.legado.app.utils.GSON
|
||||||
import io.legado.app.utils.fromJsonArray
|
import io.legado.app.utils.fromJsonArray
|
||||||
|
import io.legado.app.utils.fromJsonObject
|
||||||
|
|
||||||
object BookSourceController {
|
object BookSourceController {
|
||||||
|
|
||||||
@@ -23,7 +24,7 @@ object BookSourceController {
|
|||||||
fun saveSource(postData: String?): ReturnData {
|
fun saveSource(postData: String?): ReturnData {
|
||||||
val returnData = ReturnData()
|
val returnData = ReturnData()
|
||||||
postData ?: return returnData.setErrorMsg("数据不能为空")
|
postData ?: return returnData.setErrorMsg("数据不能为空")
|
||||||
val bookSource = BookSource.fromJson(postData).getOrNull()
|
val bookSource = GSON.fromJsonObject<BookSource>(postData).getOrNull()
|
||||||
if (bookSource != null) {
|
if (bookSource != null) {
|
||||||
if (TextUtils.isEmpty(bookSource.bookSourceName) || TextUtils.isEmpty(bookSource.bookSourceUrl)) {
|
if (TextUtils.isEmpty(bookSource.bookSourceName) || TextUtils.isEmpty(bookSource.bookSourceUrl)) {
|
||||||
returnData.setErrorMsg("源名称和URL不能为空")
|
returnData.setErrorMsg("源名称和URL不能为空")
|
||||||
@@ -40,7 +41,7 @@ object BookSourceController {
|
|||||||
fun saveSources(postData: String?): ReturnData {
|
fun saveSources(postData: String?): ReturnData {
|
||||||
postData ?: return ReturnData().setErrorMsg("数据为空")
|
postData ?: return ReturnData().setErrorMsg("数据为空")
|
||||||
val okSources = arrayListOf<BookSource>()
|
val okSources = arrayListOf<BookSource>()
|
||||||
val bookSources = BookSource.fromJsonArray(postData).getOrNull()
|
val bookSources = GSON.fromJsonArray<BookSource>(postData).getOrNull()
|
||||||
if (bookSources.isNullOrEmpty()) {
|
if (bookSources.isNullOrEmpty()) {
|
||||||
return ReturnData().setErrorMsg("转换源失败")
|
return ReturnData().setErrorMsg("转换源失败")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,9 @@ import android.text.TextUtils
|
|||||||
import io.legado.app.api.ReturnData
|
import io.legado.app.api.ReturnData
|
||||||
import io.legado.app.data.appDb
|
import io.legado.app.data.appDb
|
||||||
import io.legado.app.data.entities.RssSource
|
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 {
|
object RssSourceController {
|
||||||
|
|
||||||
@@ -20,7 +23,7 @@ object RssSourceController {
|
|||||||
fun saveSource(postData: String?): ReturnData {
|
fun saveSource(postData: String?): ReturnData {
|
||||||
val returnData = ReturnData()
|
val returnData = ReturnData()
|
||||||
postData ?: return returnData.setErrorMsg("数据不能为空")
|
postData ?: return returnData.setErrorMsg("数据不能为空")
|
||||||
RssSource.fromJson(postData).onFailure {
|
GSON.fromJsonObject<RssSource>(postData).onFailure {
|
||||||
returnData.setErrorMsg("转换源失败${it.localizedMessage}")
|
returnData.setErrorMsg("转换源失败${it.localizedMessage}")
|
||||||
}.onSuccess { source ->
|
}.onSuccess { source ->
|
||||||
if (TextUtils.isEmpty(source.sourceName) || TextUtils.isEmpty(source.sourceUrl)) {
|
if (TextUtils.isEmpty(source.sourceName) || TextUtils.isEmpty(source.sourceUrl)) {
|
||||||
@@ -36,7 +39,7 @@ object RssSourceController {
|
|||||||
fun saveSources(postData: String?): ReturnData {
|
fun saveSources(postData: String?): ReturnData {
|
||||||
postData ?: return ReturnData().setErrorMsg("数据不能为空")
|
postData ?: return ReturnData().setErrorMsg("数据不能为空")
|
||||||
val okSources = arrayListOf<RssSource>()
|
val okSources = arrayListOf<RssSource>()
|
||||||
val source = RssSource.fromJsonArray(postData).getOrNull()
|
val source = GSON.fromJsonArray<RssSource>(postData).getOrNull()
|
||||||
if (source.isNullOrEmpty()) {
|
if (source.isNullOrEmpty()) {
|
||||||
return ReturnData().setErrorMsg("转换源失败")
|
return ReturnData().setErrorMsg("转换源失败")
|
||||||
}
|
}
|
||||||
@@ -63,7 +66,7 @@ object RssSourceController {
|
|||||||
|
|
||||||
fun deleteSources(postData: String?): ReturnData {
|
fun deleteSources(postData: String?): ReturnData {
|
||||||
postData ?: return ReturnData().setErrorMsg("没有传递数据")
|
postData ?: return ReturnData().setErrorMsg("没有传递数据")
|
||||||
RssSource.fromJsonArray(postData).onFailure {
|
GSON.fromJsonArray<RssSource>(postData).onFailure {
|
||||||
return ReturnData().setErrorMsg("格式不对")
|
return ReturnData().setErrorMsg("格式不对")
|
||||||
}.onSuccess {
|
}.onSuccess {
|
||||||
it.forEach { source ->
|
it.forEach { source ->
|
||||||
|
|||||||
@@ -7,11 +7,9 @@ import io.legado.app.constant.AppPattern
|
|||||||
import io.legado.app.constant.BookSourceType
|
import io.legado.app.constant.BookSourceType
|
||||||
import io.legado.app.data.entities.rule.*
|
import io.legado.app.data.entities.rule.*
|
||||||
import io.legado.app.utils.GSON
|
import io.legado.app.utils.GSON
|
||||||
import io.legado.app.utils.fromJsonArray
|
|
||||||
import io.legado.app.utils.fromJsonObject
|
import io.legado.app.utils.fromJsonObject
|
||||||
import io.legado.app.utils.splitNotBlank
|
import io.legado.app.utils.splitNotBlank
|
||||||
import kotlinx.parcelize.Parcelize
|
import kotlinx.parcelize.Parcelize
|
||||||
import java.io.InputStream
|
|
||||||
|
|
||||||
@Suppress("unused")
|
@Suppress("unused")
|
||||||
@Parcelize
|
@Parcelize
|
||||||
@@ -220,22 +218,6 @@ data class BookSource(
|
|||||||
|
|
||||||
private fun equal(a: String?, b: String?) = a == b || (a.isNullOrEmpty() && b.isNullOrEmpty())
|
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 {
|
class Converters {
|
||||||
|
|
||||||
@TypeConverter
|
@TypeConverter
|
||||||
|
|||||||
@@ -2,9 +2,12 @@ package io.legado.app.data.entities
|
|||||||
|
|
||||||
import android.os.Parcelable
|
import android.os.Parcelable
|
||||||
import android.text.TextUtils
|
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.constant.AppPattern
|
||||||
import io.legado.app.utils.*
|
import io.legado.app.utils.splitNotBlank
|
||||||
import kotlinx.parcelize.Parcelize
|
import kotlinx.parcelize.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)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ object DefaultData {
|
|||||||
appCtx.assets.open("defaultData${File.separator}rssSources.json")
|
appCtx.assets.open("defaultData${File.separator}rssSources.json")
|
||||||
.readBytes()
|
.readBytes()
|
||||||
)
|
)
|
||||||
RssSource.fromJsonArray(json).getOrDefault(emptyList())
|
GSON.fromJsonArray<RssSource>(json).getOrDefault(emptyList())
|
||||||
}
|
}
|
||||||
|
|
||||||
val coverRule: BookCover.CoverRule by lazy {
|
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?) {
|
override fun onCodeSave(code: String, requestId: String?) {
|
||||||
requestId?.toInt()?.let {
|
requestId?.toInt()?.let {
|
||||||
BookSource.fromJson(code).getOrNull()?.let { source ->
|
GSON.fromJsonObject<BookSource>(code).getOrNull()?.let { source ->
|
||||||
viewModel.allSources[it] = source
|
viewModel.allSources[it] = source
|
||||||
adapter.setItem(it, source)
|
adapter.setItem(it, source)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -99,16 +99,16 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) {
|
|||||||
val json = JsonPath.parse(mText)
|
val json = JsonPath.parse(mText)
|
||||||
json.read<List<String>>("$.sourceUrls")
|
json.read<List<String>>("$.sourceUrls")
|
||||||
}.onSuccess {
|
}.onSuccess {
|
||||||
it.forEach {
|
it.forEach { url ->
|
||||||
importSourceUrl(it)
|
importSourceUrl(url)
|
||||||
}
|
}
|
||||||
}.onFailure {
|
}.onFailure {
|
||||||
BookSource.fromJson(mText).getOrThrow().let {
|
GSON.fromJsonObject<BookSource>(mText).getOrThrow().let {
|
||||||
allSources.add(it)
|
allSources.add(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mText.isJsonArray() -> BookSource.fromJsonArray(mText).getOrThrow().let { items ->
|
mText.isJsonArray() -> GSON.fromJsonArray<BookSource>(mText).getOrThrow().let { items ->
|
||||||
allSources.addAll(items)
|
allSources.addAll(items)
|
||||||
}
|
}
|
||||||
mText.isAbsUrl() -> {
|
mText.isAbsUrl() -> {
|
||||||
@@ -117,7 +117,7 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) {
|
|||||||
mText.isUri() -> {
|
mText.isUri() -> {
|
||||||
val uri = Uri.parse(mText)
|
val uri = Uri.parse(mText)
|
||||||
uri.inputStream(context).getOrThrow().let {
|
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))
|
else -> throw NoStackTraceException(context.getString(R.string.wrong_format))
|
||||||
@@ -139,7 +139,7 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) {
|
|||||||
url(url)
|
url(url)
|
||||||
}
|
}
|
||||||
}.byteStream().let {
|
}.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?) {
|
override fun onCodeSave(code: String, requestId: String?) {
|
||||||
requestId?.toInt()?.let {
|
requestId?.toInt()?.let {
|
||||||
RssSource.fromJson(code).getOrNull()?.let { source ->
|
GSON.fromJsonObject<RssSource>(code).getOrNull()?.let { source ->
|
||||||
viewModel.allSources[it] = source
|
viewModel.allSources[it] = source
|
||||||
adapter.setItem(it, source)
|
adapter.setItem(it, source)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -98,13 +98,13 @@ class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) {
|
|||||||
importSourceUrl(it)
|
importSourceUrl(it)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
RssSource.fromJsonArray(mText).getOrThrow().let {
|
GSON.fromJsonArray<RssSource>(mText).getOrThrow().let {
|
||||||
allSources.addAll(it)
|
allSources.addAll(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mText.isJsonArray() -> {
|
mText.isJsonArray() -> {
|
||||||
RssSource.fromJsonArray(mText).getOrThrow().let {
|
GSON.fromJsonArray<RssSource>(mText).getOrThrow().let {
|
||||||
allSources.addAll(it)
|
allSources.addAll(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -132,7 +132,7 @@ class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) {
|
|||||||
val items: List<Map<String, Any>> = jsonPath.parse(body).read("$")
|
val items: List<Map<String, Any>> = jsonPath.parse(body).read("$")
|
||||||
for (item in items) {
|
for (item in items) {
|
||||||
val jsonItem = jsonPath.parse(item)
|
val jsonItem = jsonPath.parse(item)
|
||||||
RssSource.fromJson(jsonItem.jsonString()).getOrThrow().let { source ->
|
GSON.fromJsonObject<RssSource>(jsonItem.jsonString()).getOrThrow().let { source ->
|
||||||
allSources.add(source)
|
allSources.add(source)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -93,12 +93,12 @@ class BookSourceEditViewModel(application: Application) : BaseViewModel(applicat
|
|||||||
text.isJsonArray() -> {
|
text.isJsonArray() -> {
|
||||||
val items: List<Map<String, Any>> = jsonPath.parse(text).read("$")
|
val items: List<Map<String, Any>> = jsonPath.parse(text).read("$")
|
||||||
val jsonItem = jsonPath.parse(items[0])
|
val jsonItem = jsonPath.parse(items[0])
|
||||||
BookSource.fromJson(jsonItem.jsonString()).getOrElse {
|
GSON.fromJsonObject<BookSource>(jsonItem.jsonString()).getOrElse {
|
||||||
ImportOldData.fromOldBookSource(jsonItem)
|
ImportOldData.fromOldBookSource(jsonItem)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
text.isJsonObject() -> {
|
text.isJsonObject() -> {
|
||||||
BookSource.fromJson(text).getOrElse {
|
GSON.fromJsonObject<BookSource>(text).getOrElse {
|
||||||
val jsonItem = jsonPath.parse(text)
|
val jsonItem = jsonPath.parse(text)
|
||||||
ImportOldData.fromOldBookSource(jsonItem)
|
ImportOldData.fromOldBookSource(jsonItem)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,11 +9,8 @@ import io.legado.app.data.entities.RssSource
|
|||||||
import io.legado.app.exception.NoStackTraceException
|
import io.legado.app.exception.NoStackTraceException
|
||||||
import io.legado.app.help.RuleComplete
|
import io.legado.app.help.RuleComplete
|
||||||
import io.legado.app.help.http.CookieStore
|
import io.legado.app.help.http.CookieStore
|
||||||
import io.legado.app.utils.getClipText
|
import io.legado.app.utils.*
|
||||||
import io.legado.app.utils.printOnDebug
|
|
||||||
import io.legado.app.utils.stackTraceStr
|
|
||||||
|
|
||||||
import io.legado.app.utils.toastOnUi
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
|
||||||
|
|
||||||
@@ -57,7 +54,7 @@ class RssSourceEditViewModel(application: Application) : BaseViewModel(applicati
|
|||||||
execute(context = Dispatchers.Main) {
|
execute(context = Dispatchers.Main) {
|
||||||
var source: RssSource? = null
|
var source: RssSource? = null
|
||||||
context.getClipText()?.let { json ->
|
context.getClipText()?.let { json ->
|
||||||
source = RssSource.fromJson(json).getOrThrow()
|
source = GSON.fromJsonObject<RssSource>(json).getOrThrow()
|
||||||
}
|
}
|
||||||
source
|
source
|
||||||
}.onError {
|
}.onError {
|
||||||
@@ -74,7 +71,7 @@ class RssSourceEditViewModel(application: Application) : BaseViewModel(applicati
|
|||||||
fun importSource(text: String, finally: (source: RssSource) -> Unit) {
|
fun importSource(text: String, finally: (source: RssSource) -> Unit) {
|
||||||
execute {
|
execute {
|
||||||
val text1 = text.trim()
|
val text1 = text.trim()
|
||||||
RssSource.fromJson(text1).getOrThrow().let {
|
GSON.fromJsonObject<RssSource>(text1).getOrThrow().let {
|
||||||
finally.invoke(it)
|
finally.invoke(it)
|
||||||
}
|
}
|
||||||
}.onError {
|
}.onError {
|
||||||
|
|||||||
Reference in New Issue
Block a user