From ca67f585d14eca497c3232062d6cc3c7c0ef9a55 Mon Sep 17 00:00:00 2001 From: kunfei Date: Wed, 18 Mar 2020 19:50:24 +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 --- .../io/legado/app/help/coroutine/Coroutine.kt | 3 +- .../app/ui/book/search/SearchViewModel.kt | 34 ++++++++++++------- 2 files changed, 23 insertions(+), 14 deletions(-) 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 c8ff66a8a..eb0a29375 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 @@ -113,7 +113,7 @@ class Coroutine( fun cancel(cause: CancellationException? = null) { job.cancel(cause) cancel?.let { - scope.plus(Dispatchers.Main).launch { + MainScope().launch { if (null == it.context) { it.block.invoke(scope) } else { @@ -177,6 +177,7 @@ class Coroutine( value: R, callback: Callback ) { + if (!scope.isActive) return if (null == callback.context) { callback.block.invoke(scope, value) } else { diff --git a/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt b/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt index 191ceeafc..f1a4fc858 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt @@ -12,8 +12,10 @@ import io.legado.app.help.coroutine.Coroutine import io.legado.app.model.WebBook import io.legado.app.utils.getPrefBoolean import io.legado.app.utils.getPrefString +import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.asCoroutineDispatcher +import kotlinx.coroutines.isActive import java.util.concurrent.Executors class SearchViewModel(application: Application) : BaseViewModel(application) { @@ -53,19 +55,21 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { for (item in bookSourceList) { //task取消时自动取消 by (scope = this@execute) WebBook(item).searchBook( - searchKey, - searchPage, + searchKey, + searchPage, scope = this, - context = searchPool - ) + context = searchPool + ) .timeout(30000L) .onSuccess(IO) { - it?.let { list -> - if (context.getPrefBoolean(PreferKey.precisionSearch)) { - precisionSearch(list) - } else { - App.db.searchBookDao().insert(*list.toTypedArray()) - mergeItems(list) + if (isActive) { + it?.let { list -> + if (context.getPrefBoolean(PreferKey.precisionSearch)) { + precisionSearch(this, list) + } else { + App.db.searchBookDao().insert(*list.toTypedArray()) + mergeItems(this, list) + } } } } @@ -86,7 +90,7 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { /** * 精确搜索处理 */ - private fun precisionSearch(searchBooks: List) { + private fun precisionSearch(scope: CoroutineScope, searchBooks: List) { val books = arrayListOf() searchBooks.forEach { searchBook -> if (searchBook.name.contains(searchKey, true) @@ -94,14 +98,16 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { ) books.add(searchBook) } App.db.searchBookDao().insert(*books.toTypedArray()) - mergeItems(books) + if (scope.isActive) { + mergeItems(scope, books) + } } /** * 合并搜索结果并排序 */ @Synchronized - private fun mergeItems(newDataS: List) { + private fun mergeItems(scope: CoroutineScope, newDataS: List) { if (newDataS.isNotEmpty()) { val copyDataS = ArrayList(searchBooks) val searchBooksAdd = ArrayList() @@ -145,6 +151,7 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { } } } + if (!scope.isActive) return searchBooks.sortWith(Comparator { o1, o2 -> if (o1.name == searchKey && o2.name != searchKey) { 1 @@ -170,6 +177,7 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { 0 } }) + if (!scope.isActive) return searchBooks = copyDataS searchBookLiveData.postValue(copyDataS) }