pull/1705/head
kunfei 3 years ago
parent 3e3125d9a2
commit 424b55a97f
  1. 35
      app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentActivity.kt

@ -24,7 +24,7 @@ import io.legado.app.utils.applyTint
import io.legado.app.utils.observeEvent import io.legado.app.utils.observeEvent
import io.legado.app.utils.postEvent import io.legado.app.utils.postEvent
import io.legado.app.utils.viewbindingdelegate.viewBinding import io.legado.app.utils.viewbindingdelegate.viewBinding
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
@ -98,7 +98,8 @@ class SearchContentActivity :
@SuppressLint("SetTextI18n") @SuppressLint("SetTextI18n")
private fun initBook() { private fun initBook() {
binding.tvCurrentSearchInfo.text = this.getString(R.string.search_content_size) +": ${viewModel.searchResultCounts}" binding.tvCurrentSearchInfo.text =
this.getString(R.string.search_content_size) + ": ${viewModel.searchResultCounts}"
viewModel.book?.let { viewModel.book?.let {
initCacheFileNames(it) initCacheFileNames(it)
durChapterIndex = it.durChapterIndex durChapterIndex = it.durChapterIndex
@ -109,11 +110,11 @@ class SearchContentActivity :
} }
private fun initCacheFileNames(book: Book) { private fun initCacheFileNames(book: Book) {
launch(Dispatchers.IO) { launch {
viewModel.cacheChapterNames.addAll(BookHelp.getChapterFiles(book)) withContext(IO) {
withContext(Dispatchers.Main) { viewModel.cacheChapterNames.addAll(BookHelp.getChapterFiles(book))
adapter.notifyItemRangeChanged(0, adapter.itemCount, true)
} }
adapter.notifyItemRangeChanged(0, adapter.itemCount, true)
} }
} }
@ -136,26 +137,30 @@ class SearchContentActivity :
viewModel.searchResultList.clear() viewModel.searchResultList.clear()
viewModel.searchResultCounts = 0 viewModel.searchResultCounts = 0
viewModel.lastQuery = query viewModel.lastQuery = query
var searchResults = listOf<SearchResult>() launch {
launch(Dispatchers.Main) { withContext(IO) {
appDb.bookChapterDao.getChapterList(viewModel.bookUrl).map { bookChapter -> appDb.bookChapterDao.getChapterList(viewModel.bookUrl)
}.forEach { bookChapter ->
binding.refreshProgressBar.isAutoLoading = true binding.refreshProgressBar.isAutoLoading = true
withContext(Dispatchers.IO) { val searchResults = withContext(IO) {
if (isLocalBook || viewModel.cacheChapterNames.contains(bookChapter.getFileName())) { if (isLocalBook || viewModel.cacheChapterNames.contains(bookChapter.getFileName())) {
searchResults = viewModel.searchChapter(query, bookChapter) viewModel.searchChapter(query, bookChapter)
} else {
null
} }
} }
binding.tvCurrentSearchInfo.text = this@SearchContentActivity.getString(R.string.search_content_size) +": ${viewModel.searchResultCounts}" binding.tvCurrentSearchInfo.text =
if (searchResults.isNotEmpty()) { this@SearchContentActivity.getString(R.string.search_content_size) + ": ${viewModel.searchResultCounts}"
if (searchResults != null && searchResults.isNotEmpty()) {
viewModel.searchResultList.addAll(searchResults) viewModel.searchResultList.addAll(searchResults)
binding.refreshProgressBar.isAutoLoading = false binding.refreshProgressBar.isAutoLoading = false
adapter.addItems(searchResults) adapter.addItems(searchResults)
searchResults = listOf()
} }
} }
binding.refreshProgressBar.isAutoLoading = false binding.refreshProgressBar.isAutoLoading = false
if (viewModel.searchResultCounts == 0) { if (viewModel.searchResultCounts == 0) {
val noSearchResult = SearchResult(resultText = getString(R.string.search_content_empty)) val noSearchResult =
SearchResult(resultText = getString(R.string.search_content_empty))
adapter.addItem(noSearchResult) adapter.addItem(noSearchResult)
} }
} }

Loading…
Cancel
Save