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

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

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

@ -92,14 +92,14 @@ class ImportRssSourceDialog() : BaseDialogFragment(R.layout.dialog_recycler_view
adapter.notifyDataSetChanged()
upSelectText()
}
viewModel.errorLiveData.observe(this, {
viewModel.errorLiveData.observe(this) {
binding.rotateLoading.hide()
binding.tvMsg.apply {
text = it
visible()
}
})
viewModel.successLiveData.observe(this, {
}
viewModel.successLiveData.observe(this) {
binding.rotateLoading.hide()
if (it > 0) {
adapter.setItems(viewModel.allSources)
@ -110,7 +110,7 @@ class ImportRssSourceDialog() : BaseDialogFragment(R.layout.dialog_recycler_view
visible()
}
}
})
}
val source = arguments?.getString("source")
if (source.isNullOrEmpty()) {
dismiss()
@ -188,7 +188,7 @@ class ImportRssSourceDialog() : BaseDialogFragment(R.layout.dialog_recycler_view
override fun onCodeSave(code: String, requestId: String?) {
requestId?.toInt()?.let {
RssSource.fromJson(code)?.let { source ->
RssSource.fromJson(code).getOrNull()?.let { source ->
viewModel.allSources[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.http.newCallResponseBody
import io.legado.app.help.http.okHttpClient
import io.legado.app.help.http.text
import io.legado.app.utils.*
class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) {
@ -95,7 +94,7 @@ class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) {
importSourceUrl(it)
}
} else {
RssSource.fromJsonArray(mText).let {
RssSource.fromJsonArray(mText).getOrThrow().let {
allSources.addAll(it)
}
}
@ -104,7 +103,7 @@ class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) {
val items: List<Map<String, Any>> = jsonPath.parse(mText).read("$")
for (item in items) {
val jsonItem = jsonPath.parse(item)
RssSource.fromJsonDoc(jsonItem)?.let {
RssSource.fromJsonDoc(jsonItem).getOrThrow().let {
allSources.add(it)
}
}
@ -124,11 +123,11 @@ class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) {
private suspend fun importSourceUrl(url: String) {
okHttpClient.newCallResponseBody {
url(url)
}.text("utf-8").let { body ->
}.byteStream().let { body ->
val items: List<Map<String, Any>> = jsonPath.parse(body).read("$")
for (item in items) {
val jsonItem = jsonPath.parse(item)
RssSource.fromJson(jsonItem.jsonString())?.let { source ->
RssSource.fromJson(jsonItem.jsonString()).getOrThrow().let { source ->
allSources.add(source)
}
}

@ -52,7 +52,7 @@ class RssSourceEditViewModel(application: Application) : BaseViewModel(applicati
execute(context = Dispatchers.Main) {
var source: RssSource? = null
context.getClipText()?.let { json ->
source = RssSource.fromJson(json)
source = RssSource.fromJson(json).getOrThrow()
}
source
}.onError {
@ -69,7 +69,7 @@ class RssSourceEditViewModel(application: Application) : BaseViewModel(applicati
fun importSource(text: String, finally: (source: RssSource) -> Unit) {
execute {
val text1 = text.trim()
RssSource.fromJson(text1)?.let {
RssSource.fromJson(text1).getOrThrow().let {
finally.invoke(it)
}
}.onError {

Loading…
Cancel
Save