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() } }