feat: 优化代码

pull/169/head
kunfei 5 years ago
parent 83e5a5d91f
commit e13f7b0f9c
  1. 23
      app/src/main/java/io/legado/app/model/SearchBookModel.kt
  2. 94
      app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt

@ -1,7 +1,6 @@
package io.legado.app.model package io.legado.app.model
import io.legado.app.App import io.legado.app.App
import io.legado.app.data.entities.BookSource
import io.legado.app.data.entities.SearchBook import io.legado.app.data.entities.SearchBook
import io.legado.app.help.AppConfig import io.legado.app.help.AppConfig
import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.coroutine.Coroutine
@ -22,6 +21,7 @@ class SearchBookModel(private val scope: CoroutineScope, private val callBack: C
fun search(searchId: Long, key: String) { fun search(searchId: Long, key: String) {
if (searchId != mSearchId) { if (searchId != mSearchId) {
task?.cancel() task?.cancel()
callBack.onSearchCancel()
mSearchId = searchId mSearchId = searchId
searchPage = 1 searchPage = 1
if (key.isEmpty()) { if (key.isEmpty()) {
@ -55,19 +55,30 @@ class SearchBookModel(private val scope: CoroutineScope, private val callBack: C
} }
} }
}.onStart { }.onStart {
callBack.onSearchStart()
}.onCancel {
} }
task?.invokeOnCompletion { task?.invokeOnCompletion {
if (searchId == mSearchId) {
callBack.onSearchFinish()
}
} }
} }
private data class SearchEngine(val bookSource: BookSource, var hasMore: Boolean = true) fun cancelSearch() {
task?.cancel()
mSearchId = 0
callBack.onSearchCancel()
}
fun close() {
task?.cancel()
mSearchId = 0
searchPool.close()
}
interface CallBack { interface CallBack {
fun onSearchStart()
fun onSearchSuccess(searchBooks: ArrayList<SearchBook>) fun onSearchSuccess(searchBooks: ArrayList<SearchBook>)
fun onSearchFinish() fun onSearchFinish()
fun onSearchCancel() fun onSearchCancel()

@ -7,84 +7,58 @@ import io.legado.app.base.BaseViewModel
import io.legado.app.constant.PreferKey import io.legado.app.constant.PreferKey
import io.legado.app.data.entities.SearchBook import io.legado.app.data.entities.SearchBook
import io.legado.app.data.entities.SearchKeyword import io.legado.app.data.entities.SearchKeyword
import io.legado.app.help.AppConfig import io.legado.app.model.SearchBookModel
import io.legado.app.help.coroutine.Coroutine
import io.legado.app.model.WebBook
import io.legado.app.utils.getPrefBoolean import io.legado.app.utils.getPrefBoolean
import io.legado.app.utils.getPrefString
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.isActive import kotlinx.coroutines.isActive
import java.util.concurrent.Executors
class SearchViewModel(application: Application) : BaseViewModel(application) { class SearchViewModel(application: Application) : BaseViewModel(application)
private var searchPool = , SearchBookModel.CallBack {
Executors.newFixedThreadPool(AppConfig.threadCount).asCoroutineDispatcher() private val searchBookModel = SearchBookModel(this, this)
private var task: Coroutine<*>? = null
var isSearchLiveData = MutableLiveData<Boolean>() var isSearchLiveData = MutableLiveData<Boolean>()
var searchBookLiveData = MutableLiveData<List<SearchBook>>() var searchBookLiveData = MutableLiveData<List<SearchBook>>()
var searchKey: String = "" var searchKey: String = ""
var searchPage = 1
var isLoading = false var isLoading = false
var searchBooks = arrayListOf<SearchBook>() var searchBooks = arrayListOf<SearchBook>()
private var searchID = 0L
/** /**
* 开始搜索 * 开始搜索
*/ */
fun search(key: String) { fun search(key: String) {
task?.cancel() if (searchKey != key && key.isNotEmpty()) {
if (key.isEmpty() && searchKey.isEmpty()) { searchBookModel.cancelSearch()
return
} else if (key.isEmpty()) {
isLoading = true
searchPage++
} else if (key.isNotEmpty()) {
isLoading = true
searchPage = 1
searchKey = key
searchBooks.clear() searchBooks.clear()
searchBookLiveData.postValue(searchBooks)
searchID = System.currentTimeMillis()
searchKey = key
} }
task = execute { searchBookModel.search(searchID, searchKey)
val searchGroup = context.getPrefString("searchGroup") ?: "" }
val bookSourceList = if (searchGroup.isBlank()) {
App.db.bookSourceDao().allEnabled override fun onSearchStart() {
} else { isSearchLiveData.postValue(true)
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) {
if (context.getPrefBoolean(PreferKey.precisionSearch)) {
precisionSearch(this, it)
} else {
App.db.searchBookDao().insert(*it.toTypedArray())
mergeItems(this, it)
}
}
}
}
}.onStart {
isSearchLiveData.postValue(true)
}.onCancel {
isSearchLiveData.postValue(false)
isLoading = false
}
task?.invokeOnCompletion { override fun onSearchSuccess(searchBooks: ArrayList<SearchBook>) {
isSearchLiveData.postValue(false) if (context.getPrefBoolean(PreferKey.precisionSearch)) {
isLoading = false precisionSearch(this, searchBooks)
} else {
App.db.searchBookDao().insert(*searchBooks.toTypedArray())
mergeItems(this, searchBooks)
} }
} }
override fun onSearchFinish() {
isSearchLiveData.postValue(false)
isLoading = false
}
override fun onSearchCancel() {
isSearchLiveData.postValue(false)
isLoading = false
}
/** /**
* 精确搜索处理 * 精确搜索处理
*/ */
@ -185,7 +159,7 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
* 停止搜索 * 停止搜索
*/ */
fun stop() { fun stop() {
task?.cancel() searchBookModel.cancelSearch()
} }
/** /**
@ -221,7 +195,7 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
override fun onCleared() { override fun onCleared() {
super.onCleared() super.onCleared()
searchPool.close() searchBookModel.close()
} }
} }

Loading…
Cancel
Save