From b477f2eecb02ed073ae0e08a891b89b515e08e40 Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 31 Mar 2022 17:09:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/controller/BookSourceController.kt | 42 ++++++++--------- .../io/legado/app/data/entities/BookSource.kt | 4 +- .../java/io/legado/app/help/SourceAnalyzer.kt | 46 ++++++++++++------- .../legado/app/help/storage/ImportOldData.kt | 7 +-- .../ui/association/ImportBookSourceDialog.kt | 2 +- .../association/ImportBookSourceViewModel.kt | 5 +- .../source/edit/BookSourceEditViewModel.kt | 8 ++-- 7 files changed, 60 insertions(+), 54 deletions(-) diff --git a/app/src/main/java/io/legado/app/api/controller/BookSourceController.kt b/app/src/main/java/io/legado/app/api/controller/BookSourceController.kt index f5c6d007e..d4c1eb14f 100644 --- a/app/src/main/java/io/legado/app/api/controller/BookSourceController.kt +++ b/app/src/main/java/io/legado/app/api/controller/BookSourceController.kt @@ -7,7 +7,6 @@ import io.legado.app.data.appDb import io.legado.app.data.entities.BookSource import io.legado.app.utils.GSON import io.legado.app.utils.fromJsonArray -import io.legado.app.utils.msg object BookSourceController { @@ -23,20 +22,16 @@ object BookSourceController { fun saveSource(postData: String?): ReturnData { val returnData = ReturnData() postData ?: return returnData.setErrorMsg("数据不能为空") - kotlin.runCatching { - val bookSource = BookSource.fromJson(postData) - if (bookSource != null) { - if (TextUtils.isEmpty(bookSource.bookSourceName) || TextUtils.isEmpty(bookSource.bookSourceUrl)) { - returnData.setErrorMsg("源名称和URL不能为空") - } else { - appDb.bookSourceDao.insert(bookSource) - returnData.setData("") - } + val bookSource = BookSource.fromJson(postData).getOrNull() + if (bookSource != null) { + if (TextUtils.isEmpty(bookSource.bookSourceName) || TextUtils.isEmpty(bookSource.bookSourceUrl)) { + returnData.setErrorMsg("源名称和URL不能为空") } else { - returnData.setErrorMsg("转换源失败") + appDb.bookSourceDao.insert(bookSource) + returnData.setData("") } - }.onFailure { - returnData.setErrorMsg(it.msg) + } else { + returnData.setErrorMsg("转换源失败") } return returnData } @@ -44,19 +39,18 @@ object BookSourceController { fun saveSources(postData: String?): ReturnData { postData ?: return ReturnData().setErrorMsg("数据为空") val okSources = arrayListOf() - val bookSources = BookSource.fromJsonArray(postData) - if (bookSources.isNotEmpty()) { - bookSources.forEach { bookSource -> - if (bookSource.bookSourceName.isNotBlank() - && bookSource.bookSourceUrl.isNotBlank() - ) { - appDb.bookSourceDao.insert(bookSource) - okSources.add(bookSource) - } - } - } else { + val bookSources = BookSource.fromJsonArray(postData).getOrNull() + if (bookSources.isNullOrEmpty()) { return ReturnData().setErrorMsg("转换源失败") } + bookSources.forEach { bookSource -> + if (bookSource.bookSourceName.isNotBlank() + && bookSource.bookSourceUrl.isNotBlank() + ) { + appDb.bookSourceDao.insert(bookSource) + okSources.add(bookSource) + } + } return ReturnData().setData(okSources) } diff --git a/app/src/main/java/io/legado/app/data/entities/BookSource.kt b/app/src/main/java/io/legado/app/data/entities/BookSource.kt index d29f868ee..fed30b16c 100644 --- a/app/src/main/java/io/legado/app/data/entities/BookSource.kt +++ b/app/src/main/java/io/legado/app/data/entities/BookSource.kt @@ -204,11 +204,11 @@ data class BookSource( companion object { - fun fromJson(json: String): BookSource? { + fun fromJson(json: String): Result { return SourceAnalyzer.jsonToBookSource(json) } - fun fromJsonArray(json: String): List { + fun fromJsonArray(json: String): Result> { return SourceAnalyzer.jsonToBookSources(json) } diff --git a/app/src/main/java/io/legado/app/help/SourceAnalyzer.kt b/app/src/main/java/io/legado/app/help/SourceAnalyzer.kt index da9682b9f..b5c461f25 100644 --- a/app/src/main/java/io/legado/app/help/SourceAnalyzer.kt +++ b/app/src/main/java/io/legado/app/help/SourceAnalyzer.kt @@ -7,6 +7,7 @@ import io.legado.app.constant.AppLog import io.legado.app.constant.BookType import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.rule.* +import io.legado.app.exception.NoStackTraceException import io.legado.app.utils.* import java.io.InputStream @@ -17,16 +18,30 @@ object SourceAnalyzer { private val headerPattern = Pattern.compile("@Header:\\{.+?\\}", Pattern.CASE_INSENSITIVE) private val jsPattern = Pattern.compile("\\{\\{.+?\\}\\}", Pattern.CASE_INSENSITIVE) - fun jsonToBookSources(json: String): List { - val bookSources = mutableListOf() - val items: List> = jsonPath.parse(json).read("$") - for (item in items) { - val jsonItem = jsonPath.parse(item) - jsonToBookSource(jsonItem.jsonString())?.let { - bookSources.add(it) + fun jsonToBookSources(json: String): Result> { + return kotlin.runCatching { + val bookSources = mutableListOf() + when { + json.isJsonArray() -> { + val items: List> = jsonPath.parse(json).read("$") + for (item in items) { + val jsonItem = jsonPath.parse(item) + jsonToBookSource(jsonItem.jsonString()).getOrThrow().let { + bookSources.add(it) + } + } + } + json.isJsonObject() -> { + jsonToBookSource(json).getOrThrow().let { + bookSources.add(it) + } + } + else -> { + throw NoStackTraceException("格式不对") + } } + bookSources } - return bookSources } fun jsonToBookSources(inputStream: InputStream): Result> { @@ -36,14 +51,14 @@ object SourceAnalyzer { val items: List> = jsonPath.parse(inputStream).read("$") for (item in items) { val jsonItem = jsonPath.parse(item) - jsonToBookSource(jsonItem.jsonString())?.let { + jsonToBookSource(jsonItem.jsonString()).getOrThrow().let { bookSources.add(it) } } }.onFailure { val item: Map = jsonPath.parse(inputStream).read("$") val jsonItem = jsonPath.parse(item) - jsonToBookSource(jsonItem.jsonString())?.let { + jsonToBookSource(jsonItem.jsonString()).getOrThrow().let { bookSources.add(it) } } @@ -51,17 +66,18 @@ object SourceAnalyzer { } } - fun jsonToBookSource(json: String): BookSource? { + fun jsonToBookSource(json: String): Result { val source = BookSource() val sourceAny = GSON.fromJsonObject(json.trim()) .onFailure { AppLog.put("转化书源出错", it) }.getOrNull() - try { + return kotlin.runCatching { if (sourceAny?.ruleToc == null) { source.apply { val jsonItem = jsonPath.parse(json.trim()) - bookSourceUrl = jsonItem.readString("bookSourceUrl") ?: return null + bookSourceUrl = jsonItem.readString("bookSourceUrl") + ?: throw NoStackTraceException("格式不对") bookSourceName = jsonItem.readString("bookSourceName") ?: "" bookSourceGroup = jsonItem.readString("bookSourceGroup") loginUrl = jsonItem.readString("loginUrl") @@ -189,10 +205,8 @@ object SourceAnalyzer { .getOrNull() } } - } catch (e: Exception) { - e.printOnDebug() + source } - return source } @Keep diff --git a/app/src/main/java/io/legado/app/help/storage/ImportOldData.kt b/app/src/main/java/io/legado/app/help/storage/ImportOldData.kt index 9e157dc1c..1a10c0280 100644 --- a/app/src/main/java/io/legado/app/help/storage/ImportOldData.kt +++ b/app/src/main/java/io/legado/app/help/storage/ImportOldData.kt @@ -91,9 +91,10 @@ object ImportOldData { } fun importOldSource(json: String): Int { - val bookSources = BookSource.fromJsonArray(json) - appDb.bookSourceDao.insert(*bookSources.toTypedArray()) - return bookSources.size + val count = BookSource.fromJsonArray(json).onSuccess { + appDb.bookSourceDao.insert(*it.toTypedArray()) + }.getOrNull()?.size + return count ?: 0 } private fun importOldReplaceRule(json: String): Int { diff --git a/app/src/main/java/io/legado/app/ui/association/ImportBookSourceDialog.kt b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceDialog.kt index 9892ba503..a6aacbfd3 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportBookSourceDialog.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceDialog.kt @@ -189,7 +189,7 @@ class ImportBookSourceDialog() : BaseDialogFragment(R.layout.dialog_recycler_vie override fun onCodeSave(code: String, requestId: String?) { requestId?.toInt()?.let { - BookSource.fromJson(code)?.let { source -> + BookSource.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/ImportBookSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt index 5ccb368b2..93cc32bc0 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt @@ -97,13 +97,12 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) { importSourceUrl(it) } } else { - BookSource.fromJson(mText)?.let { + BookSource.fromJson(mText).getOrThrow().let { allSources.add(it) } } } - mText.isJsonArray() -> { - val items = BookSource.fromJsonArray(mText) + mText.isJsonArray() -> BookSource.fromJsonArray(mText).getOrThrow().let { items -> allSources.addAll(items) } mText.isAbsUrl() -> { diff --git a/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditViewModel.kt b/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditViewModel.kt index 059e91a90..3bf1e3562 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditViewModel.kt @@ -86,14 +86,12 @@ class BookSourceEditViewModel(application: Application) : BaseViewModel(applicat text.isJsonArray() -> { val items: List> = jsonPath.parse(text).read("$") val jsonItem = jsonPath.parse(items[0]) - BookSource.fromJson(jsonItem.jsonString()) + BookSource.fromJson(jsonItem.jsonString()).getOrThrow() } text.isJsonObject() -> { - BookSource.fromJson(text) - } - else -> { - null + BookSource.fromJson(text).getOrThrow() } + else -> throw NoStackTraceException("格式不对") } }