添加校验设置;fix:直链规则错误提示不显示

This commit is contained in:
Xwite
2022-01-27 08:19:20 +08:00
parent 812d564edc
commit 5b9eeef696
10 changed files with 263 additions and 42 deletions
@@ -91,6 +91,8 @@ object PreferKey {
const val doublePageHorizontal = "doublePageHorizontal"
const val readUrlOpenInBrowser = "readUrlInBrowser"
const val defaultBookTreeUri = "defaultBookTreeUri"
const val checkSource = "checkSource"
const val uploadRule = "uploadRule"
const val cPrimary = "colorPrimary"
const val cAccent = "colorAccent"
@@ -7,9 +7,17 @@ import io.legado.app.data.entities.BookSource
import io.legado.app.service.CheckSourceService
import io.legado.app.utils.startService
import io.legado.app.utils.toastOnUi
import io.legado.app.help.CacheManager
object CheckSource {
var keyword = "我的"
//校验设置
var timeout = CacheManager.getLong("checkSourceTimeout") ?: 180000L
var checkSearch = CacheManager.get("checkSearch")?.toBoolean() ?: true
var checkDiscovery = CacheManager.get("checkDiscovery")?.toBoolean() ?: true
var checkInfo = CacheManager.get("checkInfo")?.toBoolean() ?: true
var checkCategory = CacheManager.get("checkCategory")?.toBoolean() ?: true
var checkContent = CacheManager.get("checkContent")?.toBoolean() ?: true
fun start(context: Context, sources: List<BookSource>) {
if (sources.isEmpty()) {
@@ -31,4 +39,13 @@ object CheckSource {
action = IntentAction.stop
}
}
fun putConfig() {
CacheManager.put("checkSourceTimeout", timeout * 1000)
CacheManager.put("checkSearch", checkSearch)
CacheManager.put("checkDiscovery", checkDiscovery)
CacheManager.put("checkInfo", checkInfo)
CacheManager.put("checkCategory", checkCategory)
CacheManager.put("checkContent", checkContent)
}
}
@@ -9,6 +9,7 @@ import io.legado.app.constant.EventBus
import io.legado.app.constant.IntentAction
import io.legado.app.data.appDb
import io.legado.app.data.entities.BookSource
import io.legado.app.data.entities.SearchBook
import io.legado.app.help.AppConfig
import io.legado.app.help.coroutine.CompositeCoroutine
import io.legado.app.model.CheckSource
@@ -33,6 +34,8 @@ class CheckSourceService : BaseService() {
private val checkedIds = ArrayList<String>()
private var processIndex = 0
private var notificationMsg = ""
private var books = ArrayList<SearchBook>()
private val notificationBuilder by lazy {
NotificationCompat.Builder(this, AppConst.channelIdReadAloud)
.setSmallIcon(R.drawable.ic_network_check)
@@ -118,45 +121,58 @@ class CheckSourceService : BaseService() {
searchWord = it
}
}
var books = WebBook.searchBookAwait(this, source, searchWord)
if (books.isEmpty()) {
source.addGroup("搜索失效")
val exs = source.exploreKinds
var url: String? = null
for (ex in exs) {
url = ex.url
if (!url.isNullOrBlank()) {
break
//校验搜索
if (CheckSource.checkSearch) {
books = WebBook.searchBookAwait(this, source, searchWord)
if (books.isEmpty()) source.addGroup("搜索失效") else source.removeGroup("搜索失效")
}
//校验发现
if (CheckSource.checkDiscovery) {
if (books.isEmpty()) {
val exs = source.exploreKinds
var url: String? = null
for (ex in exs) {
url = ex.url
if (!url.isNullOrBlank()) {
break
}
}
if (url.isNullOrBlank()) {
throw NoStackTraceException("搜索内容为空并且没有发现")
}
books = WebBook.exploreBookAwait(this, source, url)
if (books.isEmpty()) {
throw NoStackTraceException("发现书籍为空")
}
}
if (url.isNullOrBlank()) {
throw NoStackTraceException("搜索内容为空并且没有发现")
}
//校验详情
if (CheckSource.checkInfo) {
var book = books.first().toBook()
if (book.tocUrl.isBlank()) {
book = WebBook.getBookInfoAwait(this, source, book)
}
books = WebBook.exploreBookAwait(this, source, url)
if (books.isEmpty()) {
throw NoStackTraceException("发现书籍为空")
//校验目录
if (CheckSource.checkCategory) {
val toc = WebBook.getChapterListAwait(this, source, book)
val nextChapterUrl = toc.getOrNull(1)?.url ?: toc.first().url
//校验正文
if (CheckSource.checkContent) {
val content = WebBook.getContentAwait(
this,
bookSource = source,
book = book,
bookChapter = toc.first(),
nextChapterUrl = nextChapterUrl,
needSave = false
)
if ( !toc.first().isVolume && content.isBlank()) {
throw NoStackTraceException("正文内容为空")
}
}
}
} else {
source.removeGroup("搜索失效")
}
var book = books.first().toBook()
if (book.tocUrl.isBlank()) {
book = WebBook.getBookInfoAwait(this, source, book)
}
val toc = WebBook.getChapterListAwait(this, source, book)
val nextChapterUrl = toc.getOrNull(1)?.url ?: toc.first().url
val content = WebBook.getContentAwait(
this,
bookSource = source,
book = book,
bookChapter = toc.first(),
nextChapterUrl = nextChapterUrl,
needSave = false
)
if ( !toc.first().isVolume && content.isBlank()) {
throw NoStackTraceException("正文内容为空")
}
}.timeout(180000L)
}.timeout(CheckSource.timeout)
.onError(searchCoroutine) {
source.addGroup("失效")
if (source.bookSourceComment?.contains("Error: ") == false) {
@@ -0,0 +1,71 @@
package io.legado.app.ui.config
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import io.legado.app.R
import io.legado.app.base.BaseDialogFragment
import io.legado.app.databinding.DialogCheckSourceConfigBinding
import io.legado.app.model.CheckSource
import io.legado.app.lib.theme.primaryColor
import io.legado.app.utils.setLayout
import io.legado.app.utils.viewbindingdelegate.viewBinding
import io.legado.app.utils.toastOnUi
import splitties.views.onClick
class CheckSourceConfig : BaseDialogFragment(R.layout.dialog_check_source_config) {
private val binding by viewBinding(DialogCheckSourceConfigBinding::bind)
//允许的最小超时时间,秒
private val minTimeout = 60L
override fun onStart() {
super.onStart()
setLayout(
0.9f,
ViewGroup.LayoutParams.WRAP_CONTENT
)
}
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
binding.toolBar.setBackgroundColor(primaryColor)
CheckSource.run {
binding.checkSourceTimeout.setText((timeout / 1000).toString())
binding.checkSearch?.isChecked = checkSearch
binding.checkDiscovery?.isChecked = checkDiscovery
binding.checkInfo?.isChecked = checkInfo
binding.checkCategory?.isChecked = checkCategory
binding.checkContent?.isChecked = checkContent
binding.tvCancel.onClick {
dismiss()
}
binding.tvOk.onClick {
val text = binding.checkSourceTimeout.text.toString()
when {
text.isBlank() -> {
toastOnUi("${getString(R.string.timeout)}${getString(R.string.cannot_empty)}")
return@onClick
}
text.toLong() < minTimeout -> {
toastOnUi("${getString(R.string.timeout)}${getString(R.string.less_than)}${minTimeout}${getString(R.string.seconds)}")
return@onClick
}
else -> timeout = text.toLong() * 1000
}
val _checkSearch = binding.checkSearch?.isChecked
val _checkDiscovery = binding.checkDiscovery?.isChecked
if (!_checkSearch && !_checkDiscovery) {
toastOnUi(getString(R.string.error_check_source_config))
return@onClick
}
checkSearch = _checkSearch
checkDiscovery = _checkDiscovery
checkInfo = binding.checkInfo?.isChecked
checkCategory = binding.checkCategory?.isChecked
checkContent = binding.checkContent?.isChecked
putConfig()
dismiss()
}
}
}
}
@@ -41,11 +41,11 @@ class DirectLinkUploadConfig : BaseDialogFragment(R.layout.dialog_direct_link_up
val uploadUrl = binding.editUploadUrl.text?.toString()
val downloadUrlRule = binding.editDownloadUrlRule.text?.toString()
val summary = binding.editSummary.text?.toString()
uploadUrl ?: let {
if (uploadUrl.isNullOrBlank()) {
toastOnUi("上传Url不能为空")
return@onClick
}
downloadUrlRule ?: let {
if (downloadUrlRule.isNullOrBlank()) {
toastOnUi("下载Url规则不能为空")
return@onClick
}
@@ -70,7 +70,7 @@ class OtherConfigFragment : BasePreferenceFragment(),
when (preference?.key) {
PreferKey.userAgent -> showUserAgentDialog()
PreferKey.defaultBookTreeUri -> localBookTreeSelect.launch {
title = "选择保存书籍的文件夹"
title = getString(R.string.select_book_folder)
mode = HandleFileContract.DIR_SYS
}
PreferKey.preDownloadNum -> NumberPickerDialog(requireContext())
@@ -98,7 +98,8 @@ class OtherConfigFragment : BasePreferenceFragment(),
AppConfig.webPort = it
}
PreferKey.cleanCache -> clearCache()
"uploadRule" -> DirectLinkUploadConfig().show(childFragmentManager, "uploadRuleConfig")
PreferKey.uploadRule -> showDialogFragment<DirectLinkUploadConfig>()
PreferKey.checkSource -> showDialogFragment<CheckSourceConfig>()
}
return super.onPreferenceTreeClick(preference)
}
@@ -155,9 +156,9 @@ class OtherConfigFragment : BasePreferenceFragment(),
@SuppressLint("InflateParams")
private fun showUserAgentDialog() {
alert("UserAgent") {
alert(getString(R.string.user_agent)) {
val alertBinding = DialogEditTextBinding.inflate(layoutInflater).apply {
editView.hint = "UserAgent"
editView.hint = getString(R.string.user_agent)
editView.setText(AppConfig.userAgent)
}
customView { alertBinding.root }