From d6e3349f8cbec1a2ef5beff5b6318b37b9202793 Mon Sep 17 00:00:00 2001 From: gedoor Date: Mon, 24 May 2021 22:23:42 +0800 Subject: [PATCH] =?UTF-8?q?=E7=B2=BE=E7=A1=AE=E6=90=9C=E7=B4=A2=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E5=8C=85=E5=90=AB=E5=85=B3=E9=94=AE=E8=AF=8D=E7=9A=84?= =?UTF-8?q?,=E6=AF=94=E5=A6=82=E6=90=9C=E7=B4=A2=E4=BA=94=E8=A1=8C=20?= =?UTF-8?q?=E4=BA=94=E8=A1=8C=E5=A4=A9=E4=B9=9F=E6=98=BE=E7=A4=BA=E5=87=BA?= =?UTF-8?q?=E6=9D=A5,=20=E4=BA=94=E5=A4=A9=E8=A1=8C=E4=B8=8D=E6=98=BE?= =?UTF-8?q?=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/assets/updateLog.md | 1 + .../app/ui/book/search/SearchViewModel.kt | 46 ++++++++++++------- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/app/src/main/assets/updateLog.md b/app/src/main/assets/updateLog.md index e1016e4e3..53f055054 100644 --- a/app/src/main/assets/updateLog.md +++ b/app/src/main/assets/updateLog.md @@ -6,6 +6,7 @@ **2021/05/24** * 反转目录后刷新内容 * 修复上下滑动会导致左右切换问题 +* 精确搜索增加包含关键词的,比如搜索五行 五行天也显示出来, 五天行不显示 **2021/05/21** * 添加反转目录功能 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 2eee0dbeb..c4b6c5f15 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 @@ -83,23 +83,37 @@ class SearchViewModel(application: Application) : BaseViewModel(application), @Synchronized private fun mergeItems(scope: CoroutineScope, newDataS: List, precision: Boolean) { if (newDataS.isNotEmpty()) { - val prevData = ArrayList(searchBooks) - val precisionData = arrayListOf() - prevData.forEach { + val copyData = ArrayList(searchBooks) + val equalData = arrayListOf() + val containsData = arrayListOf() + val otherData = arrayListOf() + copyData.forEach { if (!scope.isActive) return if (it.name == searchKey || it.author == searchKey) { - precisionData.add(it) + equalData.add(it) + } else if (it.name.contains(searchKey) || it.author.contains(searchKey)) { + containsData.add(it) + } else { + otherData.add(it) } } - repeat(precisionData.size) { - if (!scope.isActive) return - prevData.removeAt(0) - } newDataS.forEach { nBook -> if (!scope.isActive) return if (nBook.name == searchKey || nBook.author == searchKey) { var hasSame = false - precisionData.forEach { pBook -> + equalData.forEach { pBook -> + if (!scope.isActive) return + if (pBook.name == nBook.name && pBook.author == nBook.author) { + pBook.addOrigin(nBook.origin) + hasSame = true + } + } + if (!hasSame) { + equalData.add(nBook) + } + } else if (nBook.name.contains(searchKey) || nBook.author.contains(searchKey)) { + var hasSame = false + containsData.forEach { pBook -> if (!scope.isActive) return if (pBook.name == nBook.name && pBook.author == nBook.author) { pBook.addOrigin(nBook.origin) @@ -107,11 +121,11 @@ class SearchViewModel(application: Application) : BaseViewModel(application), } } if (!hasSame) { - precisionData.add(nBook) + containsData.add(nBook) } } else if (!precision) { var hasSame = false - prevData.forEach { pBook -> + otherData.forEach { pBook -> if (!scope.isActive) return if (pBook.name == nBook.name && pBook.author == nBook.author) { pBook.addOrigin(nBook.origin) @@ -119,17 +133,17 @@ class SearchViewModel(application: Application) : BaseViewModel(application), } } if (!hasSame) { - prevData.add(nBook) + otherData.add(nBook) } } } if (!scope.isActive) return - precisionData.sortByDescending { it.origins.size } - if (!scope.isActive) return + equalData.sortByDescending { it.origins.size } + equalData.addAll(containsData) if (!precision) { - precisionData.addAll(prevData) + equalData.addAll(otherData) } - searchBooks = precisionData + searchBooks = equalData upAdapter() } }