This commit is contained in:
Horis
2023-09-12 17:25:33 +08:00
parent 9b45ce05e2
commit f0ebfaa443
2 changed files with 8 additions and 2 deletions

View File

@@ -28,6 +28,7 @@ class SearchModel(private val scope: CoroutineScope, private val callBack: CallB
private var tasks = CompositeCoroutine()
private var bookSourceList = arrayListOf<BookSource>()
private var searchBooks = arrayListOf<SearchBook>()
private val emptyBookSource = BookSource()
@Volatile
private var searchIndex = -1
@@ -76,6 +77,7 @@ class SearchModel(private val scope: CoroutineScope, private val callBack: CallB
return
}
searchIndex++
val searchIndex = searchIndex
val source = bookSourceList[searchIndex]
val searchPool = searchPool ?: return
val task = WebBook.searchBook(
@@ -91,7 +93,7 @@ class SearchModel(private val scope: CoroutineScope, private val callBack: CallB
onSuccess(searchId, it)
}
.onFinally {
onFinally(searchId)
onFinally(searchId, searchIndex)
}
tasks.add(task)
}
@@ -107,12 +109,15 @@ class SearchModel(private val scope: CoroutineScope, private val callBack: CallB
}
@Synchronized
private fun onFinally(searchId: Long) {
private fun onFinally(searchId: Long, index: Int) {
if (searchIndex < bookSourceList.lastIndex) {
search(searchId)
} else {
searchIndex++
}
if (index <= bookSourceList.lastIndex) {
bookSourceList[index] = emptyBookSource
}
if (searchIndex >= bookSourceList.lastIndex
+ min(bookSourceList.size, threadCount)
) {

View File

@@ -212,6 +212,7 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
binding.rvHistoryKey.adapter = historyKeyAdapter
binding.recyclerView.layoutManager = LinearLayoutManager(this)
binding.recyclerView.adapter = adapter
binding.recyclerView.itemAnimator = null
adapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
super.onItemRangeInserted(positionStart, itemCount)