pull/1751/head
kunfei 3 years ago
parent b477f2eecb
commit 4e4a75cb3b
  1. 27
      app/src/main/java/io/legado/app/api/controller/RssSourceController.kt
  2. 14
      app/src/main/java/io/legado/app/data/entities/RssSource.kt
  3. 4
      app/src/main/java/io/legado/app/help/DefaultData.kt
  4. 10
      app/src/main/java/io/legado/app/ui/association/ImportRssSourceDialog.kt
  5. 9
      app/src/main/java/io/legado/app/ui/association/ImportRssSourceViewModel.kt
  6. 4
      app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditViewModel.kt

@ -5,7 +5,6 @@ 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.msg
object RssSourceController { object RssSourceController {
@ -21,20 +20,15 @@ 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("数据不能为空")
kotlin.runCatching { RssSource.fromJson(postData).onFailure {
val source = RssSource.fromJson(postData) returnData.setErrorMsg("转换源失败${it.localizedMessage}")
if (source != null) { }.onSuccess { source ->
if (TextUtils.isEmpty(source.sourceName) || TextUtils.isEmpty(source.sourceUrl)) { if (TextUtils.isEmpty(source.sourceName) || TextUtils.isEmpty(source.sourceUrl)) {
returnData.setErrorMsg("源名称和URL不能为空") returnData.setErrorMsg("源名称和URL不能为空")
} else { } else {
appDb.rssSourceDao.insert(source) appDb.rssSourceDao.insert(source)
returnData.setData("") returnData.setData("")
} }
} else {
returnData.setErrorMsg("转换源失败")
}
}.onFailure {
returnData.setErrorMsg(it.msg)
} }
return returnData return returnData
} }
@ -42,8 +36,10 @@ 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) val source = RssSource.fromJsonArray(postData).getOrNull()
if (source.isNotEmpty()) { if (source.isNullOrEmpty()) {
return ReturnData().setErrorMsg("转换源失败")
}
for (rssSource in source) { for (rssSource in source) {
if (rssSource.sourceName.isBlank() || rssSource.sourceUrl.isBlank()) { if (rssSource.sourceName.isBlank() || rssSource.sourceUrl.isBlank()) {
continue continue
@ -51,9 +47,6 @@ object RssSourceController {
appDb.rssSourceDao.insert(rssSource) appDb.rssSourceDao.insert(rssSource)
okSources.add(rssSource) okSources.add(rssSource)
} }
} else {
return ReturnData().setErrorMsg("转换源失败")
}
return ReturnData().setData(okSources) return ReturnData().setData(okSources)
} }
@ -70,13 +63,13 @@ object RssSourceController {
fun deleteSources(postData: String?): ReturnData { fun deleteSources(postData: String?): ReturnData {
postData ?: return ReturnData().setErrorMsg("没有传递数据") postData ?: return ReturnData().setErrorMsg("没有传递数据")
kotlin.runCatching { RssSource.fromJsonArray(postData).onFailure {
RssSource.fromJsonArray(postData).let { return ReturnData().setErrorMsg("格式不对")
}.onSuccess {
it.forEach { source -> it.forEach { source ->
appDb.rssSourceDao.delete(source) appDb.rssSourceDao.delete(source)
} }
} }
}
return ReturnData().setData("已执行"/*okSources*/) return ReturnData().setData("已执行"/*okSources*/)
} }
} }

@ -121,7 +121,7 @@ data class RssSource(
@Suppress("MemberVisibilityCanBePrivate") @Suppress("MemberVisibilityCanBePrivate")
companion object { companion object {
fun fromJsonDoc(doc: DocumentContext): RssSource? { fun fromJsonDoc(doc: DocumentContext): Result<RssSource> {
return kotlin.runCatching { return kotlin.runCatching {
val loginUi = doc.read<Any>("$.loginUi") val loginUi = doc.read<Any>("$.loginUi")
RssSource( RssSource(
@ -152,23 +152,25 @@ data class RssSource(
loadWithBaseUrl = doc.readBool("$.loadWithBaseUrl") ?: true, loadWithBaseUrl = doc.readBool("$.loadWithBaseUrl") ?: true,
customOrder = doc.readInt("$.customOrder") ?: 0 customOrder = doc.readInt("$.customOrder") ?: 0
) )
}.getOrNull() }
} }
fun fromJson(json: String): RssSource? { fun fromJson(json: String): Result<RssSource> {
return fromJsonDoc(jsonPath.parse(json)) return fromJsonDoc(jsonPath.parse(json))
} }
fun fromJsonArray(jsonArray: String): ArrayList<RssSource> { fun fromJsonArray(jsonArray: String): Result<ArrayList<RssSource>> {
return kotlin.runCatching {
val sources = arrayListOf<RssSource>() val sources = arrayListOf<RssSource>()
val doc = jsonPath.parse(jsonArray).read<List<*>>("$") val doc = jsonPath.parse(jsonArray).read<List<*>>("$")
doc.forEach { doc.forEach {
val jsonItem = jsonPath.parse(it) val jsonItem = jsonPath.parse(it)
fromJsonDoc(jsonItem)?.let { source -> fromJsonDoc(jsonItem).getOrThrow().let { source ->
sources.add(source) sources.add(source)
} }
} }
return sources sources
}
} }
} }

@ -54,13 +54,11 @@ object DefaultData {
} }
val rssSources: List<RssSource> by lazy { val rssSources: List<RssSource> by lazy {
kotlin.runCatching {
val json = String( val json = String(
appCtx.assets.open("defaultData${File.separator}rssSources.json") appCtx.assets.open("defaultData${File.separator}rssSources.json")
.readBytes() .readBytes()
) )
RssSource.fromJsonArray(json) RssSource.fromJsonArray(json).getOrDefault(emptyList())
}.getOrDefault(emptyList())
} }
val coverRuleConfig: BookCover.CoverRuleConfig by lazy { val coverRuleConfig: BookCover.CoverRuleConfig by lazy {

@ -92,14 +92,14 @@ class ImportRssSourceDialog() : BaseDialogFragment(R.layout.dialog_recycler_view
adapter.notifyDataSetChanged() adapter.notifyDataSetChanged()
upSelectText() upSelectText()
} }
viewModel.errorLiveData.observe(this, { viewModel.errorLiveData.observe(this) {
binding.rotateLoading.hide() binding.rotateLoading.hide()
binding.tvMsg.apply { binding.tvMsg.apply {
text = it text = it
visible() visible()
} }
}) }
viewModel.successLiveData.observe(this, { viewModel.successLiveData.observe(this) {
binding.rotateLoading.hide() binding.rotateLoading.hide()
if (it > 0) { if (it > 0) {
adapter.setItems(viewModel.allSources) adapter.setItems(viewModel.allSources)
@ -110,7 +110,7 @@ class ImportRssSourceDialog() : BaseDialogFragment(R.layout.dialog_recycler_view
visible() visible()
} }
} }
}) }
val source = arguments?.getString("source") val source = arguments?.getString("source")
if (source.isNullOrEmpty()) { if (source.isNullOrEmpty()) {
dismiss() dismiss()
@ -188,7 +188,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)?.let { source -> RssSource.fromJson(code).getOrNull()?.let { source ->
viewModel.allSources[it] = source viewModel.allSources[it] = source
adapter.setItem(it, source) adapter.setItem(it, source)
} }

@ -13,7 +13,6 @@ import io.legado.app.help.SourceHelp
import io.legado.app.help.config.AppConfig import io.legado.app.help.config.AppConfig
import io.legado.app.help.http.newCallResponseBody import io.legado.app.help.http.newCallResponseBody
import io.legado.app.help.http.okHttpClient import io.legado.app.help.http.okHttpClient
import io.legado.app.help.http.text
import io.legado.app.utils.* import io.legado.app.utils.*
class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) { class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) {
@ -95,7 +94,7 @@ class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) {
importSourceUrl(it) importSourceUrl(it)
} }
} else { } else {
RssSource.fromJsonArray(mText).let { RssSource.fromJsonArray(mText).getOrThrow().let {
allSources.addAll(it) allSources.addAll(it)
} }
} }
@ -104,7 +103,7 @@ class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) {
val items: List<Map<String, Any>> = jsonPath.parse(mText).read("$") val items: List<Map<String, Any>> = jsonPath.parse(mText).read("$")
for (item in items) { for (item in items) {
val jsonItem = jsonPath.parse(item) val jsonItem = jsonPath.parse(item)
RssSource.fromJsonDoc(jsonItem)?.let { RssSource.fromJsonDoc(jsonItem).getOrThrow().let {
allSources.add(it) allSources.add(it)
} }
} }
@ -124,11 +123,11 @@ class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) {
private suspend fun importSourceUrl(url: String) { private suspend fun importSourceUrl(url: String) {
okHttpClient.newCallResponseBody { okHttpClient.newCallResponseBody {
url(url) url(url)
}.text("utf-8").let { body -> }.byteStream().let { body ->
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())?.let { source -> RssSource.fromJson(jsonItem.jsonString()).getOrThrow().let { source ->
allSources.add(source) allSources.add(source)
} }
} }

@ -52,7 +52,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) source = RssSource.fromJson(json).getOrThrow()
} }
source source
}.onError { }.onError {
@ -69,7 +69,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)?.let { RssSource.fromJson(text1).getOrThrow().let {
finally.invoke(it) finally.invoke(it)
} }
}.onError { }.onError {

Loading…
Cancel
Save