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