|
|
@ -62,12 +62,8 @@ class SearchViewModel(application: Application) : BaseViewModel(application), |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
override fun onSearchSuccess(searchBooks: ArrayList<SearchBook>) { |
|
|
|
override fun onSearchSuccess(searchBooks: ArrayList<SearchBook>) { |
|
|
|
if (context.getPrefBoolean(PreferKey.precisionSearch)) { |
|
|
|
val precision = context.getPrefBoolean(PreferKey.precisionSearch) |
|
|
|
precisionSearch(this, searchBooks) |
|
|
|
mergeItems(this, searchBooks, precision) |
|
|
|
} else { |
|
|
|
|
|
|
|
App.db.searchBookDao.insert(*searchBooks.toTypedArray()) |
|
|
|
|
|
|
|
mergeItems(this, searchBooks) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
override fun onSearchFinish() { |
|
|
|
override fun onSearchFinish() { |
|
|
@ -80,98 +76,59 @@ class SearchViewModel(application: Application) : BaseViewModel(application), |
|
|
|
isLoading = false |
|
|
|
isLoading = false |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 精确搜索处理 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private fun precisionSearch(scope: CoroutineScope, searchBooks: List<SearchBook>) { |
|
|
|
|
|
|
|
val books = arrayListOf<SearchBook>() |
|
|
|
|
|
|
|
searchBooks.forEach { searchBook -> |
|
|
|
|
|
|
|
if (searchBook.name.contains(searchKey, true) |
|
|
|
|
|
|
|
|| searchBook.author.contains(searchKey, true) |
|
|
|
|
|
|
|
) books.add(searchBook) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
App.db.searchBookDao.insert(*books.toTypedArray()) |
|
|
|
|
|
|
|
if (scope.isActive) { |
|
|
|
|
|
|
|
mergeItems(scope, books) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 合并搜索结果并排序 |
|
|
|
* 合并搜索结果并排序 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@Synchronized |
|
|
|
@Synchronized |
|
|
|
private fun mergeItems(scope: CoroutineScope, newDataS: List<SearchBook>) { |
|
|
|
private fun mergeItems(scope: CoroutineScope, newDataS: List<SearchBook>, precision: Boolean) { |
|
|
|
if (newDataS.isNotEmpty()) { |
|
|
|
if (newDataS.isNotEmpty()) { |
|
|
|
val copyDataS = ArrayList(searchBooks) |
|
|
|
val prevData = ArrayList(searchBooks) |
|
|
|
val searchBooksAdd = ArrayList<SearchBook>() |
|
|
|
val precisionData = arrayListOf<SearchBook>() |
|
|
|
if (copyDataS.size == 0) { |
|
|
|
prevData.forEach { |
|
|
|
copyDataS.addAll(newDataS) |
|
|
|
if (!scope.isActive) return |
|
|
|
} else { |
|
|
|
if (it.name == searchKey || it.author == searchKey) { |
|
|
|
//存在 |
|
|
|
precisionData.add(it) |
|
|
|
newDataS.forEach { item -> |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
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 |
|
|
|
var hasSame = false |
|
|
|
for (searchBook in copyDataS) { |
|
|
|
precisionData.forEach { pBook -> |
|
|
|
if (item.name == searchBook.name |
|
|
|
if (!scope.isActive) return |
|
|
|
&& item.author == searchBook.author |
|
|
|
if (pBook.name == nBook.name && pBook.author == nBook.author) { |
|
|
|
) { |
|
|
|
pBook.addOrigin(nBook.origin) |
|
|
|
hasSame = true |
|
|
|
hasSame = true |
|
|
|
searchBook.addOrigin(item.origin) |
|
|
|
|
|
|
|
break |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if (!hasSame) { |
|
|
|
if (!hasSame) { |
|
|
|
searchBooksAdd.add(item) |
|
|
|
precisionData.add(nBook) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} else if (!precision) { |
|
|
|
//添加 |
|
|
|
var hasSame = false |
|
|
|
searchBooksAdd.forEach { item -> |
|
|
|
prevData.forEach { pBook -> |
|
|
|
if (searchKey == item.name) { |
|
|
|
if (!scope.isActive) return |
|
|
|
for ((index, searchBook) in copyDataS.withIndex()) { |
|
|
|
if (pBook.name == nBook.name && pBook.author == nBook.author) { |
|
|
|
if (searchKey != searchBook.name) { |
|
|
|
pBook.addOrigin(nBook.origin) |
|
|
|
copyDataS.add(index, item) |
|
|
|
hasSame = true |
|
|
|
break |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else if (searchKey == item.author) { |
|
|
|
|
|
|
|
for ((index, searchBook) in copyDataS.withIndex()) { |
|
|
|
|
|
|
|
if (searchKey != searchBook.name && searchKey == searchBook.author) { |
|
|
|
|
|
|
|
copyDataS.add(index, item) |
|
|
|
|
|
|
|
break |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
|
|
|
|
copyDataS.add(item) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if (!hasSame) { |
|
|
|
} |
|
|
|
prevData.add(nBook) |
|
|
|
if (!scope.isActive) return |
|
|
|
|
|
|
|
searchBooks.sortWith { o1, o2 -> |
|
|
|
|
|
|
|
if (o1.name == searchKey && o2.name != searchKey) { |
|
|
|
|
|
|
|
1 |
|
|
|
|
|
|
|
} else if (o1.name != searchKey && o2.name == searchKey) { |
|
|
|
|
|
|
|
-1 |
|
|
|
|
|
|
|
} else if (o1.author == searchKey && o2.author != searchKey) { |
|
|
|
|
|
|
|
1 |
|
|
|
|
|
|
|
} else if (o1.author != searchKey && o2.author == searchKey) { |
|
|
|
|
|
|
|
-1 |
|
|
|
|
|
|
|
} else if (o1.name == o2.name) { |
|
|
|
|
|
|
|
when { |
|
|
|
|
|
|
|
o1.origins.size > o2.origins.size -> { |
|
|
|
|
|
|
|
1 |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
o1.origins.size < o2.origins.size -> { |
|
|
|
|
|
|
|
-1 |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else -> { |
|
|
|
|
|
|
|
0 |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
|
|
|
|
0 |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if (!scope.isActive) return |
|
|
|
if (!scope.isActive) return |
|
|
|
searchBooks = copyDataS |
|
|
|
precisionData.sortByDescending { it.origins.size } |
|
|
|
|
|
|
|
if (!scope.isActive) return |
|
|
|
|
|
|
|
if (!precision) { |
|
|
|
|
|
|
|
precisionData.addAll(prevData) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
searchBooks = precisionData |
|
|
|
upAdapter() |
|
|
|
upAdapter() |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|