From a286831fd17736839d489769fe0049c5fb353113 Mon Sep 17 00:00:00 2001 From: kunfei Date: Wed, 5 Feb 2020 19:58:15 +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 --- .../java/io/legado/app/base/BaseService.kt | 12 ++++++- .../io/legado/app/data/entities/BookSource.kt | 10 ++++++ .../legado/app/service/CheckSourceService.kt | 31 +++++++++++++++++++ .../io/legado/app/service/help/CheckSource.kt | 5 +++ app/src/main/res/values/strings.xml | 1 + 5 files changed, 58 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/io/legado/app/base/BaseService.kt b/app/src/main/java/io/legado/app/base/BaseService.kt index c67acb95c..163ac09ec 100644 --- a/app/src/main/java/io/legado/app/base/BaseService.kt +++ b/app/src/main/java/io/legado/app/base/BaseService.kt @@ -3,17 +3,27 @@ package io.legado.app.base import android.app.Service import android.content.Intent import android.os.IBinder +import io.legado.app.help.coroutine.Coroutine import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.MainScope import kotlinx.coroutines.cancel +import kotlin.coroutines.CoroutineContext abstract class BaseService : Service(), CoroutineScope by MainScope() { + fun execute( + scope: CoroutineScope = this, + context: CoroutineContext = Dispatchers.IO, + block: suspend CoroutineScope.() -> T + ): Coroutine { + return Coroutine.async(scope, context) { block() } + } + override fun onBind(intent: Intent?): IBinder? { return null } - override fun onDestroy() { super.onDestroy() cancel() 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 39b569565..bab3a7b5e 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 @@ -126,6 +126,16 @@ data class BookSource( return contentRuleV!! } + fun addGroup(group: String) { + bookSourceGroup?.let { + if (!it.contains(group)) { + bookSourceGroup = "$it;$group" + } + } ?: let { + bookSourceGroup = group + } + } + fun getExploreKinds(): ArrayList? { val exploreKinds = arrayListOf() exploreUrl?.let { 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 549d7fee2..bf4a268a8 100644 --- a/app/src/main/java/io/legado/app/service/CheckSourceService.kt +++ b/app/src/main/java/io/legado/app/service/CheckSourceService.kt @@ -2,19 +2,24 @@ package io.legado.app.service import android.content.Intent import androidx.core.app.NotificationCompat +import io.legado.app.App import io.legado.app.R import io.legado.app.base.BaseService import io.legado.app.constant.AppConst import io.legado.app.constant.IntentAction import io.legado.app.help.AppConfig import io.legado.app.help.IntentHelp +import io.legado.app.help.coroutine.Coroutine +import io.legado.app.model.WebBook import io.legado.app.ui.book.source.manage.BookSourceActivity +import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.asCoroutineDispatcher import java.util.concurrent.Executors class CheckSourceService : BaseService() { private var searchPool = Executors.newFixedThreadPool(AppConfig.threadCount).asCoroutineDispatcher() + private var task: Coroutine<*>? = null private val allIds = LinkedHashSet() private val unCheckIds = LinkedHashSet() @@ -35,13 +40,39 @@ class CheckSourceService : BaseService() { override fun onDestroy() { super.onDestroy() + task?.cancel() searchPool.close() } private fun check(ids: List) { + task?.cancel() + allIds.clear() + unCheckIds.clear() allIds.addAll(ids) unCheckIds.addAll(ids) + task = execute { + unCheckIds.forEach { sourceUrl -> + App.db.bookSourceDao().getBookSource(sourceUrl)?.let { source -> + val webBook = WebBook(source) + webBook.searchBook("我的", scope = this, context = searchPool) + .onError(IO) { + source.addGroup("失效") + App.db.bookSourceDao().update(source) + }.onFinally { + unCheckIds.remove(sourceUrl) + val checkedCount = allIds.size - unCheckIds.size + updateNotification( + checkedCount, + getString(R.string.progress_show, checkedCount, unCheckIds.size) + ) + } + } + } + } + task?.invokeOnCompletion { + stopSelf() + } } /** 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 47aa593f9..11e140b10 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,12 +2,17 @@ package io.legado.app.service.help import android.content.Context import android.content.Intent +import io.legado.app.R import io.legado.app.constant.IntentAction import io.legado.app.service.CheckSourceService +import org.jetbrains.anko.toast object CheckSource { fun start(context: Context, selectedIds: ArrayList) { + if (selectedIds.isEmpty()) { + context.toast(R.string.non_select) + } Intent(context, CheckSourceService::class.java).let { it.action = IntentAction.start it.putExtra("selectIds", selectedIds) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8b283bb42..ee33406df 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -252,6 +252,7 @@ 文字颜色和背景(长按自定义) 沉浸式状态栏 还剩%d章未下载 + 没有选择 长按输入颜色值 加载中… 追更区