kunfei 4 лет назад
Родитель
Сommit
e9db5d967f
1 измененных файлов с 20 добавлено и 15 удалено
  1. 20 15
      app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentActivity.kt

+ 20 - 15
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) {
-            viewModel.cacheChapterNames.addAll(BookHelp.getChapterFiles(book))
-            withContext(Dispatchers.Main) {
-                adapter.notifyItemRangeChanged(0, adapter.itemCount, true)
+        launch {
+            withContext(IO) {
+                viewModel.cacheChapterNames.addAll(BookHelp.getChapterFiles(book))
             }
             }
+            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(Dispatchers.Main) {
-                appDb.bookChapterDao.getChapterList(viewModel.bookUrl).map { bookChapter ->
+            launch {
+                withContext(IO) {
+                    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}"
-                    if (searchResults.isNotEmpty()) {
+                    binding.tvCurrentSearchInfo.text =
+                        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)
                 }
                 }
             }
             }