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

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/21**
* 添加反转目录功能

@ -83,23 +83,37 @@ class SearchViewModel(application: Application) : BaseViewModel(application),
@Synchronized
private fun mergeItems(scope: CoroutineScope, newDataS: List<SearchBook>, precision: Boolean) {
if (newDataS.isNotEmpty()) {
val prevData = ArrayList(searchBooks)
val precisionData = arrayListOf<SearchBook>()
prevData.forEach {
val copyData = ArrayList(searchBooks)
val equalData = arrayListOf<SearchBook>()
val containsData = arrayListOf<SearchBook>()
val otherData = arrayListOf<SearchBook>()
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()
}
}

Loading…
Cancel
Save