From 270030c3be8146ca37d3d8a51d885c5b9da516fc Mon Sep 17 00:00:00 2001 From: 821938089 <821938089@qq.com> Date: Sat, 6 Aug 2022 19:45:29 +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 | 8 +++----- .../io/legado/app/help/coroutine/Coroutine.kt | 10 ++++++++-- .../legado/app/service/CheckSourceService.kt | 10 ++++------ .../book/source/manage/BookSourceActivity.kt | 2 +- .../book/source/manage/BookSourceAdapter.kt | 19 ++++++++----------- 5 files changed, 24 insertions(+), 25 deletions(-) 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 54b409f39..852dc0d5f 100644 --- a/app/src/main/java/io/legado/app/base/BaseService.kt +++ b/app/src/main/java/io/legado/app/base/BaseService.kt @@ -6,10 +6,7 @@ import android.os.IBinder import androidx.annotation.CallSuper import io.legado.app.help.LifecycleHelp import io.legado.app.help.coroutine.Coroutine -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.MainScope -import kotlinx.coroutines.cancel +import kotlinx.coroutines.* import kotlin.coroutines.CoroutineContext abstract class BaseService : Service(), CoroutineScope by MainScope() { @@ -17,8 +14,9 @@ abstract class BaseService : Service(), CoroutineScope by MainScope() { fun execute( scope: CoroutineScope = this, context: CoroutineContext = Dispatchers.IO, + start: CoroutineStart = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() -> T - ) = Coroutine.async(scope, context) { block() } + ) = Coroutine.async(scope, context, start) { block() } @CallSuper override fun onCreate() { diff --git a/app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt b/app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt index 73f8f9edf..a1f1c08cf 100644 --- a/app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt +++ b/app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt @@ -12,6 +12,7 @@ import kotlin.coroutines.CoroutineContext class Coroutine( val scope: CoroutineScope, context: CoroutineContext = Dispatchers.IO, + val startOption: CoroutineStart = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() -> T ) { @@ -22,9 +23,10 @@ class Coroutine( fun async( scope: CoroutineScope = DEFAULT, context: CoroutineContext = Dispatchers.IO, + start: CoroutineStart = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() -> T ): Coroutine { - return Coroutine(scope, context, block) + return Coroutine(scope, context, start, block) } } @@ -135,11 +137,15 @@ class Coroutine( return job.invokeOnCompletion(handler) } + fun start() { + job.start() + } + private fun executeInternal( context: CoroutineContext, block: suspend CoroutineScope.() -> T ): Job { - return (scope + Dispatchers.Main).launch { + return (scope + Dispatchers.Main).launch(start = startOption) { try { start?.let { dispatchVoidCallback(this, it) } ensureActive() 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 62637d428..44c3de4b6 100644 --- a/app/src/main/java/io/legado/app/service/CheckSourceService.kt +++ b/app/src/main/java/io/legado/app/service/CheckSourceService.kt @@ -24,10 +24,8 @@ import io.legado.app.utils.activityPendingIntent import io.legado.app.utils.postEvent import io.legado.app.utils.servicePendingIntent import io.legado.app.utils.toastOnUi +import kotlinx.coroutines.* import kotlinx.coroutines.Dispatchers.IO -import kotlinx.coroutines.TimeoutCancellationException -import kotlinx.coroutines.asCoroutineDispatcher -import kotlinx.coroutines.launch import org.mozilla.javascript.WrappedException import java.util.concurrent.Executors import kotlin.math.min @@ -122,7 +120,7 @@ class CheckSourceService : BaseService() { *校验书源 */ private fun check(source: BookSource) { - execute(context = searchCoroutine) { + execute(context = searchCoroutine, start = CoroutineStart.LAZY) { Debug.startChecking(source) var searchWord = CheckSource.keyword source.ruleSearch?.checkKeyWord?.let { @@ -162,7 +160,7 @@ class CheckSourceService : BaseService() { } } if (url.isNullOrBlank()) { - source.addGroup("发现规则为空") + source.addGroup("发现规则为空") } else { source.removeGroup("发现规则为空") val exploreBooks = WebBook.exploreBookAwait(this, source, url) @@ -194,7 +192,7 @@ class CheckSourceService : BaseService() { source.respondTime = Debug.getRespondTime(source.bookSourceUrl) appDb.bookSourceDao.update(source) onNext(source.bookSourceUrl, source.bookSourceName) - } + }.start() } /** diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt index 37aa77e84..02f750699 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt @@ -531,7 +531,7 @@ class BookSourceActivity : VMBaseActivity() - private val selectedPosition = linkedSetOf() val selection: List get() { @@ -143,10 +142,8 @@ class BookSourceAdapter(context: Context, val callBack: CallBack) : if (view.isPressed) { if (checked) { selected.add(it) - selectedPosition.add(holder.layoutPosition) } else { selected.remove(it) - selectedPosition.remove(holder.layoutPosition) } callBack.upCountView() } @@ -219,22 +216,19 @@ class BookSourceAdapter(context: Context, val callBack: CallBack) : } fun selectAll() { - getItems().forEachIndexed { index, it -> + getItems().forEach { selected.add(it) - selectedPosition.add(index) } notifyItemRangeChanged(0, itemCount, bundleOf(Pair("selected", null))) callBack.upCountView() } fun revertSelection() { - getItems().forEachIndexed { index, it -> + getItems().forEach { if (selected.contains(it)) { selected.remove(it) - selectedPosition.remove(index) } else { selected.add(it) - selectedPosition.add(index) } } notifyItemRangeChanged(0, itemCount, bundleOf(Pair("selected", null))) @@ -242,13 +236,18 @@ class BookSourceAdapter(context: Context, val callBack: CallBack) : } fun checkSelectedInterval() { + val selectedPosition = linkedSetOf() + getItems().forEachIndexed { index, it -> + if (selected.contains(it)) { + selectedPosition.add(index) + } + } val minPosition = Collections.min(selectedPosition) val maxPosition = Collections.max(selectedPosition) val itemCount = maxPosition - minPosition + 1 for (i in minPosition..maxPosition) { getItem(i)?.let { selected.add(it) - selectedPosition.add(i) } } notifyItemRangeChanged(minPosition, itemCount, bundleOf(Pair("selected", null))) @@ -296,10 +295,8 @@ class BookSourceAdapter(context: Context, val callBack: CallBack) : getItem(position)?.let { if (isSelected) { selected.add(it) - selectedPosition.add(position) } else { selected.remove(it) - selectedPosition.remove(position) } notifyItemChanged(position, bundleOf(Pair("selected", null))) callBack.upCountView()