From 7fff0dd5080b797ea3e1ed46fcdcdbc75e334465 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 29 Mar 2020 21:33:10 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../legado/app/service/CheckSourceService.kt | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) 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 0b2b7329f..a9e132afb 100644 --- a/app/src/main/java/io/legado/app/service/CheckSourceService.kt +++ b/app/src/main/java/io/legado/app/service/CheckSourceService.kt @@ -10,7 +10,7 @@ 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.Coroutine +import io.legado.app.help.coroutine.CompositeCoroutine import io.legado.app.model.WebBook import io.legado.app.ui.book.source.manage.BookSourceActivity import kotlinx.coroutines.Dispatchers.IO @@ -21,7 +21,7 @@ import java.util.concurrent.Executors class CheckSourceService : BaseService() { private val threadCount = AppConfig.threadCount private var searchPool = Executors.newFixedThreadPool(threadCount).asCoroutineDispatcher() - private var task: Coroutine<*>? = null + private var tasks = CompositeCoroutine() private val allIds = ArrayList() private val checkedIds = ArrayList() private var processIndex = 0 @@ -43,23 +43,23 @@ class CheckSourceService : BaseService() { override fun onDestroy() { super.onDestroy() - task?.cancel() + tasks.clear() searchPool.close() } private fun check(ids: List) { - task?.cancel() + if (allIds.isNotEmpty()) { + toast("已有书源在校验,等完成后再试") + return + } + tasks.clear() allIds.clear() checkedIds.clear() allIds.addAll(ids) processIndex = 0 updateNotification(0, getString(R.string.progress_show, 0, allIds.size)) - task = execute { - for (i in 0 until threadCount) { - check() - } - }.onError { - toast("校验书源出错:${it.localizedMessage}") + for (i in 0 until threadCount) { + check() } } @@ -78,7 +78,7 @@ class CheckSourceService : BaseService() { private fun check(source: BookSource) { val webBook = WebBook(source) - webBook.searchBook("我的", scope = this, context = searchPool) + tasks.add(webBook.searchBook("我的", scope = this, context = searchPool) .onError(IO) { source.addGroup("失效") App.db.bookSourceDao().update(source) @@ -94,7 +94,7 @@ class CheckSourceService : BaseService() { stopSelf() } } - } + }) } /**