diff --git a/app/src/main/java/io/legado/app/service/CheckSourceService.kt b/app/src/main/java/io/legado/app/service/CheckSourceService.kt index aa3bd6799..539c6c1ac 100644 --- a/app/src/main/java/io/legado/app/service/CheckSourceService.kt +++ b/app/src/main/java/io/legado/app/service/CheckSourceService.kt @@ -8,9 +8,11 @@ import io.legado.app.base.BaseService import io.legado.app.constant.AppConst import io.legado.app.constant.EventBus import io.legado.app.constant.IntentAction +import io.legado.app.data.entities.BookSource import io.legado.app.help.AppConfig import io.legado.app.help.IntentHelp import io.legado.app.help.coroutine.CompositeCoroutine +import io.legado.app.model.webBook.WebBook import io.legado.app.service.help.CheckSource import io.legado.app.ui.book.source.manage.BookSourceActivity import io.legado.app.utils.postEvent @@ -21,7 +23,7 @@ import kotlin.math.min class CheckSourceService : BaseService() { private var threadCount = AppConfig.threadCount - private var searchPool = Executors.newFixedThreadPool(threadCount).asCoroutineDispatcher() + private var searchCoroutine = Executors.newFixedThreadPool(threadCount).asCoroutineDispatcher() private var tasks = CompositeCoroutine() private val allIds = ArrayList() private val checkedIds = ArrayList() @@ -62,7 +64,7 @@ class CheckSourceService : BaseService() { override fun onDestroy() { super.onDestroy() tasks.clear() - searchPool.close() + searchCoroutine.close() postEvent(EventBus.CHECK_SOURCE_DONE, 0) } @@ -96,18 +98,34 @@ class CheckSourceService : BaseService() { if (index < allIds.size) { val sourceUrl = allIds[index] App.db.bookSourceDao.getBookSource(sourceUrl)?.let { source -> - if (source.searchUrl.isNullOrEmpty()) { - onNext(sourceUrl, source.bookSourceName) - } else { - CheckSource(source).check(this, searchPool) { - onNext(it, source.bookSourceName) - } - } + check(source) } ?: onNext(sourceUrl, "") } } } + fun check(source: BookSource) { + execute(context = searchCoroutine) { + val webBook = WebBook(source) + val books = webBook.searchBookAwait(this, CheckSource.keyword) + val book = webBook.getBookInfoAwait(this, books.first().toBook()) + val toc = webBook.getChapterListAwait(this, book) + val content = webBook.getContentAwait(this, book, toc.first()) + if (content.isBlank()) { + throw Exception("正文内容为空") + } + }.timeout(60000L) + .onError { + source.addGroup("失效") + App.db.bookSourceDao.update(source) + }.onSuccess { + source.removeGroup("失效") + App.db.bookSourceDao.update(source) + }.onFinally { + onNext(source.bookSourceUrl, source.bookSourceName) + } + } + private fun onNext(sourceUrl: String, sourceName: String) { synchronized(this) { check() diff --git a/app/src/main/java/io/legado/app/service/help/CheckSource.kt b/app/src/main/java/io/legado/app/service/help/CheckSource.kt index 596662429..d38e6c225 100644 --- a/app/src/main/java/io/legado/app/service/help/CheckSource.kt +++ b/app/src/main/java/io/legado/app/service/help/CheckSource.kt @@ -2,64 +2,35 @@ package io.legado.app.service.help import android.content.Context import android.content.Intent -import io.legado.app.App import io.legado.app.R import io.legado.app.constant.IntentAction import io.legado.app.data.entities.BookSource -import io.legado.app.help.coroutine.Coroutine -import io.legado.app.model.webBook.WebBook import io.legado.app.service.CheckSourceService -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers import org.jetbrains.anko.toast -import kotlin.coroutines.CoroutineContext -class CheckSource(val source: BookSource) { +object CheckSource { + var keyword = "我的" - companion object { - var keyword = "我的" - - fun start(context: Context, sources: List) { - if (sources.isEmpty()) { - context.toast(R.string.non_select) - return - } - val selectedIds: ArrayList = arrayListOf() - sources.map { - selectedIds.add(it.bookSourceUrl) - } - Intent(context, CheckSourceService::class.java).let { - it.action = IntentAction.start - it.putExtra("selectIds", selectedIds) - context.startService(it) - } + fun start(context: Context, sources: List) { + if (sources.isEmpty()) { + context.toast(R.string.non_select) + return } - - fun stop(context: Context) { - Intent(context, CheckSourceService::class.java).let { - it.action = IntentAction.stop - context.startService(it) - } + val selectedIds: ArrayList = arrayListOf() + sources.map { + selectedIds.add(it.bookSourceUrl) + } + Intent(context, CheckSourceService::class.java).let { + it.action = IntentAction.start + it.putExtra("selectIds", selectedIds) + context.startService(it) } } - fun check( - scope: CoroutineScope, - context: CoroutineContext, - onNext: (sourceUrl: String) -> Unit - ): Coroutine<*> { - val webBook = WebBook(source) - return webBook - .searchBook(scope, keyword, context = context) - .timeout(60000L) - .onError(Dispatchers.IO) { - source.addGroup("失效") - App.db.bookSourceDao.update(source) - }.onSuccess(Dispatchers.IO) { - source.removeGroup("失效") - App.db.bookSourceDao.update(source) - }.onFinally(Dispatchers.IO) { - onNext(source.bookSourceUrl) - } + fun stop(context: Context) { + Intent(context, CheckSourceService::class.java).let { + it.action = IntentAction.stop + context.startService(it) + } } } \ No newline at end of file