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