feat: 优化代码

pull/167/head
kunfei 5 years ago
parent bed5c2e34a
commit 78a2f6ce2f
  1. 93
      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.Dispatchers.IO
import kotlinx.coroutines.asCoroutineDispatcher import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.isActive import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import java.util.concurrent.Executors import java.util.concurrent.Executors
class SearchViewModel(application: Application) : BaseViewModel(application) { class SearchViewModel(application: Application) : BaseViewModel(application) {
@ -34,59 +33,57 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
* 开始搜索 * 开始搜索
*/ */
fun search(key: String) { fun search(key: String) {
launch { task?.cancel()
task?.cancel() if (key.isEmpty() && searchKey.isEmpty()) {
if (key.isEmpty() && searchKey.isEmpty()) { return
return@launch } else if (key.isEmpty()) {
} else if (key.isEmpty()) { isLoading = true
isLoading = true searchPage++
searchPage++ } else if (key.isNotEmpty()) {
} else if (key.isNotEmpty()) { isLoading = true
isLoading = true searchPage = 1
searchPage = 1 searchKey = key
searchKey = key searchBooks.clear()
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 { for (item in bookSourceList) {
val searchGroup = context.getPrefString("searchGroup") ?: "" //task取消时自动取消 by (scope = this@execute)
val bookSourceList = if (searchGroup.isBlank()) { WebBook(item).searchBook(
App.db.bookSourceDao().allEnabled searchKey,
} else { searchPage,
App.db.bookSourceDao().getEnabledByGroup(searchGroup) scope = this,
} context = searchPool
for (item in bookSourceList) { )
//task取消时自动取消 by (scope = this@execute) .timeout(30000L)
WebBook(item).searchBook( .onSuccess(IO) {
searchKey, if (isActive) {
searchPage, it?.let { list ->
scope = this, if (context.getPrefBoolean(PreferKey.precisionSearch)) {
context = searchPool precisionSearch(this, list)
) } else {
.timeout(30000L) App.db.searchBookDao().insert(*list.toTypedArray())
.onSuccess(IO) { mergeItems(this, list)
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 { task?.invokeOnCompletion {
isSearchLiveData.postValue(false) isSearchLiveData.postValue(false)
isLoading = false isLoading = false
}
} }
} }

Loading…
Cancel
Save