From 78a2f6ce2f55f7a10ac406650d5143910b62980b Mon Sep 17 00:00:00 2001 From: kunfei Date: Wed, 18 Mar 2020 20:56:09 +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 --- .../app/ui/book/search/SearchViewModel.kt | 93 +++++++++---------- 1 file changed, 45 insertions(+), 48 deletions(-) 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 6577c88fe..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 @@ -16,7 +16,6 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.asCoroutineDispatcher import kotlinx.coroutines.isActive -import kotlinx.coroutines.launch import java.util.concurrent.Executors class SearchViewModel(application: Application) : BaseViewModel(application) { @@ -34,59 +33,57 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { * 开始搜索 */ fun search(key: String) { - launch { - task?.cancel() - if (key.isEmpty() && searchKey.isEmpty()) { - return@launch - } else if (key.isEmpty()) { - isLoading = true - searchPage++ - } else if (key.isNotEmpty()) { - isLoading = true - searchPage = 1 - searchKey = key - searchBooks.clear() + task?.cancel() + if (key.isEmpty() && searchKey.isEmpty()) { + return + } else if (key.isEmpty()) { + isLoading = true + searchPage++ + } else if (key.isNotEmpty()) { + isLoading = true + searchPage = 1 + searchKey = key + searchBooks.clear() + } + task = execute { + val searchGroup = context.getPrefString("searchGroup") ?: "" + val bookSourceList = if (searchGroup.isBlank()) { + App.db.bookSourceDao().allEnabled + } else { + App.db.bookSourceDao().getEnabledByGroup(searchGroup) } - task = execute { - val searchGroup = context.getPrefString("searchGroup") ?: "" - val bookSourceList = if (searchGroup.isBlank()) { - App.db.bookSourceDao().allEnabled - } else { - App.db.bookSourceDao().getEnabledByGroup(searchGroup) - } - for (item in bookSourceList) { - //task取消时自动取消 by (scope = this@execute) - WebBook(item).searchBook( - searchKey, - searchPage, - scope = this, - context = searchPool - ) - .timeout(30000L) - .onSuccess(IO) { - if (isActive) { - it?.let { list -> - if (context.getPrefBoolean(PreferKey.precisionSearch)) { - precisionSearch(this, list) - } else { - App.db.searchBookDao().insert(*list.toTypedArray()) - mergeItems(this, list) - } + for (item in bookSourceList) { + //task取消时自动取消 by (scope = this@execute) + WebBook(item).searchBook( + searchKey, + searchPage, + scope = this, + context = searchPool + ) + .timeout(30000L) + .onSuccess(IO) { + if (isActive) { + it?.let { list -> + if (context.getPrefBoolean(PreferKey.precisionSearch)) { + precisionSearch(this, list) + } else { + App.db.searchBookDao().insert(*list.toTypedArray()) + mergeItems(this, list) } } } - } - }.onStart { - isSearchLiveData.postValue(true) - }.onCancel { - isSearchLiveData.postValue(false) - isLoading = false + } } + }.onStart { + isSearchLiveData.postValue(true) + }.onCancel { + isSearchLiveData.postValue(false) + isLoading = false + } - task?.invokeOnCompletion { - isSearchLiveData.postValue(false) - isLoading = false - } + task?.invokeOnCompletion { + isSearchLiveData.postValue(false) + isLoading = false } }