feat: 优化代码

pull/103/head
kunfei 5 years ago
parent 70b7b4b1dc
commit 7446caa772
  1. 52
      app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt

@ -14,7 +14,6 @@ import io.legado.app.utils.getPrefBoolean
import io.legado.app.utils.getPrefString import io.legado.app.utils.getPrefString
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.asCoroutineDispatcher import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.withContext
import java.util.concurrent.Executors import java.util.concurrent.Executors
class SearchViewModel(application: Application) : BaseViewModel(application) { class SearchViewModel(application: Application) : BaseViewModel(application) {
@ -28,6 +27,9 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
var isLoading = false var isLoading = false
var searchBooks = arrayListOf<SearchBook>() var searchBooks = arrayListOf<SearchBook>()
/**
* 开始搜索
*/
fun search(key: String) { fun search(key: String) {
task?.cancel() task?.cancel()
if (key.isEmpty() && searchKey.isEmpty()) { if (key.isEmpty() && searchKey.isEmpty()) {
@ -58,9 +60,14 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
context = searchPool context = searchPool
) )
.timeout(30000L) .timeout(30000L)
.onSuccess { .onSuccess(IO) {
it?.let { list -> it?.let { list ->
searchSuccess(list) if (context.getPrefBoolean(PreferKey.precisionSearch)) {
precisionSearch(list)
} else {
App.db.searchBookDao().insert(*list.toTypedArray())
mergeItems(list)
}
} }
} }
} }
@ -72,22 +79,23 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
} }
} }
private suspend fun searchSuccess(searchBooks: List<SearchBook>) { /**
withContext(IO) { * 精确搜索处理
val books = arrayListOf<SearchBook>() */
searchBooks.forEach { searchBook -> private fun precisionSearch(searchBooks: List<SearchBook>) {
if (context.getPrefBoolean(PreferKey.precisionSearch)) { val books = arrayListOf<SearchBook>()
if (searchBook.name.equals(searchKey, true) searchBooks.forEach { searchBook ->
|| searchBook.author.equals(searchKey, true) if (searchBook.name.equals(searchKey, true)
) books.add(searchBook) || searchBook.author.equals(searchKey, true)
} else ) books.add(searchBook)
books.add(searchBook)
}
App.db.searchBookDao().insert(*books.toTypedArray())
mergeItems(books)
} }
App.db.searchBookDao().insert(*books.toTypedArray())
mergeItems(books)
} }
/**
* 合并搜索结果并排序
*/
@Synchronized @Synchronized
private fun mergeItems(newDataS: List<SearchBook>) { private fun mergeItems(newDataS: List<SearchBook>) {
if (newDataS.isNotEmpty()) { if (newDataS.isNotEmpty()) {
@ -138,10 +146,16 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
} }
} }
/**
* 停止搜索
*/
fun stop() { fun stop() {
task?.cancel() task?.cancel()
} }
/**
* 按书名和作者获取书源排序最前的搜索结果
*/
fun getSearchBook(name: String, author: String, success: ((searchBook: SearchBook?) -> Unit)?) { fun getSearchBook(name: String, author: String, success: ((searchBook: SearchBook?) -> Unit)?) {
execute { execute {
val searchBook = App.db.searchBookDao().getFirstByNameAuthor(name, author) val searchBook = App.db.searchBookDao().getFirstByNameAuthor(name, author)
@ -149,6 +163,9 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
} }
} }
/**
* 保存搜索关键字
*/
fun saveSearchKey(key: String) { fun saveSearchKey(key: String) {
execute { execute {
App.db.searchKeywordDao().get(key)?.let { App.db.searchKeywordDao().get(key)?.let {
@ -158,6 +175,9 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
} }
} }
/**
* 清楚搜索关键字
*/
fun clearHistory() { fun clearHistory() {
execute { execute {
App.db.searchKeywordDao().deleteAll() App.db.searchKeywordDao().deleteAll()

Loading…
Cancel
Save