diff --git a/app/src/main/java/io/legado/app/api/controller/RssSourceController.kt b/app/src/main/java/io/legado/app/api/controller/RssSourceController.kt index 95d80abe0..6df9c5b28 100644 --- a/app/src/main/java/io/legado/app/api/controller/RssSourceController.kt +++ b/app/src/main/java/io/legado/app/api/controller/RssSourceController.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) { - if (TextUtils.isEmpty(source.sourceName) || TextUtils.isEmpty(source.sourceUrl)) { - returnData.setErrorMsg("源名称和URL不能为空") - } else { - appDb.rssSourceDao.insert(source) - returnData.setData("") - } + RssSource.fromJson(postData).onFailure { + returnData.setErrorMsg("转换源失败${it.localizedMessage}") + }.onSuccess { source -> + if (TextUtils.isEmpty(source.sourceName) || TextUtils.isEmpty(source.sourceUrl)) { + returnData.setErrorMsg("源名称和URL不能为空") } else { - returnData.setErrorMsg("转换源失败") + appDb.rssSourceDao.insert(source) + returnData.setData("") } - }.onFailure { - returnData.setErrorMsg(it.msg) } return returnData } @@ -42,18 +36,17 @@ object RssSourceController { fun saveSources(postData: String?): ReturnData { postData ?: return ReturnData().setErrorMsg("数据不能为空") val okSources = arrayListOf() - val source = RssSource.fromJsonArray(postData) - if (source.isNotEmpty()) { - for (rssSource in source) { - if (rssSource.sourceName.isBlank() || rssSource.sourceUrl.isBlank()) { - continue - } - appDb.rssSourceDao.insert(rssSource) - okSources.add(rssSource) - } - } else { + val source = RssSource.fromJsonArray(postData).getOrNull() + if (source.isNullOrEmpty()) { return ReturnData().setErrorMsg("转换源失败") } + for (rssSource in source) { + if (rssSource.sourceName.isBlank() || rssSource.sourceUrl.isBlank()) { + continue + } + appDb.rssSourceDao.insert(rssSource) + okSources.add(rssSource) + } return ReturnData().setData(okSources) } @@ -70,11 +63,11 @@ object RssSourceController { fun deleteSources(postData: String?): ReturnData { postData ?: return ReturnData().setErrorMsg("没有传递数据") - kotlin.runCatching { - RssSource.fromJsonArray(postData).let { - it.forEach { source -> - appDb.rssSourceDao.delete(source) - } + RssSource.fromJsonArray(postData).onFailure { + return ReturnData().setErrorMsg("格式不对") + }.onSuccess { + it.forEach { source -> + appDb.rssSourceDao.delete(source) } } return ReturnData().setData("已执行"/*okSources*/) diff --git a/app/src/main/java/io/legado/app/data/entities/RssSource.kt b/app/src/main/java/io/legado/app/data/entities/RssSource.kt index b4fa7dfe6..32c5a2743 100644 --- a/app/src/main/java/io/legado/app/data/entities/RssSource.kt +++ b/app/src/main/java/io/legado/app/data/entities/RssSource.kt @@ -121,7 +121,7 @@ data class RssSource( @Suppress("MemberVisibilityCanBePrivate") companion object { - fun fromJsonDoc(doc: DocumentContext): RssSource? { + fun fromJsonDoc(doc: DocumentContext): Result { return kotlin.runCatching { val loginUi = doc.read("$.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 { return fromJsonDoc(jsonPath.parse(json)) } - fun fromJsonArray(jsonArray: String): ArrayList { - val sources = arrayListOf() - val doc = jsonPath.parse(jsonArray).read>("$") - doc.forEach { - val jsonItem = jsonPath.parse(it) - fromJsonDoc(jsonItem)?.let { source -> - sources.add(source) + fun fromJsonArray(jsonArray: String): Result> { + return kotlin.runCatching { + val sources = arrayListOf() + val doc = jsonPath.parse(jsonArray).read>("$") + doc.forEach { + val jsonItem = jsonPath.parse(it) + fromJsonDoc(jsonItem).getOrThrow().let { source -> + sources.add(source) + } } + sources } - return sources } } diff --git a/app/src/main/java/io/legado/app/help/DefaultData.kt b/app/src/main/java/io/legado/app/help/DefaultData.kt index 774b0ea4c..33ebfb05e 100644 --- a/app/src/main/java/io/legado/app/help/DefaultData.kt +++ b/app/src/main/java/io/legado/app/help/DefaultData.kt @@ -54,13 +54,11 @@ object DefaultData { } val rssSources: List by lazy { - kotlin.runCatching { - val json = String( - appCtx.assets.open("defaultData${File.separator}rssSources.json") - .readBytes() - ) - RssSource.fromJsonArray(json) - }.getOrDefault(emptyList()) + val json = String( + appCtx.assets.open("defaultData${File.separator}rssSources.json") + .readBytes() + ) + RssSource.fromJsonArray(json).getOrDefault(emptyList()) } val coverRuleConfig: BookCover.CoverRuleConfig by lazy { diff --git a/app/src/main/java/io/legado/app/ui/association/ImportRssSourceDialog.kt b/app/src/main/java/io/legado/app/ui/association/ImportRssSourceDialog.kt index 8ac19ba45..830f0941d 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportRssSourceDialog.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportRssSourceDialog.kt @@ -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) } diff --git a/app/src/main/java/io/legado/app/ui/association/ImportRssSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/association/ImportRssSourceViewModel.kt index cbfaf03e7..d44414c9a 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportRssSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportRssSourceViewModel.kt @@ -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> = 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> = 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) } } diff --git a/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditViewModel.kt b/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditViewModel.kt index 75634c2a1..25002eb3a 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditViewModel.kt @@ -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 {