精确搜索增加包含关键词的,比如搜索五行 五行天也显示出来, 五天行不显示

pull/1013/head
gedoor 4 years ago
parent 7bf6eef2aa
commit d6e3349f8c
  1. 1
      app/src/main/assets/updateLog.md
  2. 46
      app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt

@ -6,6 +6,7 @@
**2021/05/24** **2021/05/24**
* 反转目录后刷新内容 * 反转目录后刷新内容
* 修复上下滑动会导致左右切换问题 * 修复上下滑动会导致左右切换问题
* 精确搜索增加包含关键词的,比如搜索五行 五行天也显示出来, 五天行不显示
**2021/05/21** **2021/05/21**
* 添加反转目录功能 * 添加反转目录功能

@ -83,23 +83,25 @@ class SearchViewModel(application: Application) : BaseViewModel(application),
@Synchronized @Synchronized
private fun mergeItems(scope: CoroutineScope, newDataS: List<SearchBook>, precision: Boolean) { private fun mergeItems(scope: CoroutineScope, newDataS: List<SearchBook>, precision: Boolean) {
if (newDataS.isNotEmpty()) { if (newDataS.isNotEmpty()) {
val prevData = ArrayList(searchBooks) val copyData = ArrayList(searchBooks)
val precisionData = arrayListOf<SearchBook>() val equalData = arrayListOf<SearchBook>()
prevData.forEach { val containsData = arrayListOf<SearchBook>()
val otherData = arrayListOf<SearchBook>()
copyData.forEach {
if (!scope.isActive) return if (!scope.isActive) return
if (it.name == searchKey || it.author == searchKey) { 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 -> newDataS.forEach { nBook ->
if (!scope.isActive) return if (!scope.isActive) return
if (nBook.name == searchKey || nBook.author == searchKey) { if (nBook.name == searchKey || nBook.author == searchKey) {
var hasSame = false var hasSame = false
precisionData.forEach { pBook -> equalData.forEach { pBook ->
if (!scope.isActive) return if (!scope.isActive) return
if (pBook.name == nBook.name && pBook.author == nBook.author) { if (pBook.name == nBook.name && pBook.author == nBook.author) {
pBook.addOrigin(nBook.origin) pBook.addOrigin(nBook.origin)
@ -107,11 +109,23 @@ class SearchViewModel(application: Application) : BaseViewModel(application),
} }
} }
if (!hasSame) { if (!hasSame) {
precisionData.add(nBook) 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)
hasSame = true
}
}
if (!hasSame) {
containsData.add(nBook)
} }
} else if (!precision) { } else if (!precision) {
var hasSame = false var hasSame = false
prevData.forEach { pBook -> otherData.forEach { pBook ->
if (!scope.isActive) return if (!scope.isActive) return
if (pBook.name == nBook.name && pBook.author == nBook.author) { if (pBook.name == nBook.name && pBook.author == nBook.author) {
pBook.addOrigin(nBook.origin) pBook.addOrigin(nBook.origin)
@ -119,17 +133,17 @@ class SearchViewModel(application: Application) : BaseViewModel(application),
} }
} }
if (!hasSame) { if (!hasSame) {
prevData.add(nBook) otherData.add(nBook)
} }
} }
} }
if (!scope.isActive) return if (!scope.isActive) return
precisionData.sortByDescending { it.origins.size } equalData.sortByDescending { it.origins.size }
if (!scope.isActive) return equalData.addAll(containsData)
if (!precision) { if (!precision) {
precisionData.addAll(prevData) equalData.addAll(otherData)
} }
searchBooks = precisionData searchBooks = equalData
upAdapter() upAdapter()
} }
} }

Loading…
Cancel
Save