|
|
@@ -7,6 +7,7 @@ import androidx.activity.viewModels
|
|
|
import androidx.appcompat.widget.SearchView
|
|
|
import io.legado.app.R
|
|
|
import io.legado.app.base.VMBaseActivity
|
|
|
+import io.legado.app.constant.AppLog
|
|
|
import io.legado.app.constant.EventBus
|
|
|
import io.legado.app.data.appDb
|
|
|
import io.legado.app.data.entities.Book
|
|
|
@@ -25,6 +26,8 @@ import io.legado.app.utils.observeEvent
|
|
|
import io.legado.app.utils.postEvent
|
|
|
import io.legado.app.utils.viewbindingdelegate.viewBinding
|
|
|
import kotlinx.coroutines.Dispatchers.IO
|
|
|
+import kotlinx.coroutines.Job
|
|
|
+import kotlinx.coroutines.ensureActive
|
|
|
import kotlinx.coroutines.launch
|
|
|
import kotlinx.coroutines.withContext
|
|
|
|
|
|
@@ -41,6 +44,7 @@ class SearchContentActivity :
|
|
|
binding.titleBar.findViewById(R.id.search_view)
|
|
|
}
|
|
|
private var durChapterIndex = 0
|
|
|
+ private var searchJob: Job? = null
|
|
|
|
|
|
override fun onActivityCreated(savedInstanceState: Bundle?) {
|
|
|
val bbg = bottomBackground
|
|
|
@@ -133,35 +137,43 @@ class SearchContentActivity :
|
|
|
fun startContentSearch(query: String) {
|
|
|
// 按章节搜索内容
|
|
|
if (query.isNotBlank()) {
|
|
|
+ searchJob?.cancel()
|
|
|
adapter.clearItems()
|
|
|
viewModel.searchResultList.clear()
|
|
|
viewModel.searchResultCounts = 0
|
|
|
viewModel.lastQuery = query
|
|
|
- launch {
|
|
|
- withContext(IO) {
|
|
|
- appDb.bookChapterDao.getChapterList(viewModel.bookUrl)
|
|
|
- }.forEach { bookChapter ->
|
|
|
- binding.refreshProgressBar.isAutoLoading = true
|
|
|
- val searchResults = withContext(IO) {
|
|
|
- if (isLocalBook || viewModel.cacheChapterNames.contains(bookChapter.getFileName())) {
|
|
|
- viewModel.searchChapter(query, bookChapter)
|
|
|
- } else {
|
|
|
- null
|
|
|
+ searchJob = launch {
|
|
|
+ kotlin.runCatching {
|
|
|
+ withContext(IO) {
|
|
|
+ appDb.bookChapterDao.getChapterList(viewModel.bookUrl)
|
|
|
+ }.forEach { bookChapter ->
|
|
|
+ ensureActive()
|
|
|
+ binding.refreshProgressBar.isAutoLoading = true
|
|
|
+ val searchResults = withContext(IO) {
|
|
|
+ if (isLocalBook || viewModel.cacheChapterNames.contains(bookChapter.getFileName())) {
|
|
|
+ viewModel.searchChapter(this, query, bookChapter)
|
|
|
+ } else {
|
|
|
+ null
|
|
|
+ }
|
|
|
+ }
|
|
|
+ binding.tvCurrentSearchInfo.text =
|
|
|
+ this@SearchContentActivity.getString(R.string.search_content_size) + ": ${viewModel.searchResultCounts}"
|
|
|
+ ensureActive()
|
|
|
+ if (searchResults != null && searchResults.isNotEmpty()) {
|
|
|
+ viewModel.searchResultList.addAll(searchResults)
|
|
|
+ binding.refreshProgressBar.isAutoLoading = false
|
|
|
+ adapter.addItems(searchResults)
|
|
|
}
|
|
|
}
|
|
|
- binding.tvCurrentSearchInfo.text =
|
|
|
- this@SearchContentActivity.getString(R.string.search_content_size) + ": ${viewModel.searchResultCounts}"
|
|
|
- if (searchResults != null && searchResults.isNotEmpty()) {
|
|
|
- viewModel.searchResultList.addAll(searchResults)
|
|
|
- binding.refreshProgressBar.isAutoLoading = false
|
|
|
- adapter.addItems(searchResults)
|
|
|
+ binding.refreshProgressBar.isAutoLoading = false
|
|
|
+ if (viewModel.searchResultCounts == 0) {
|
|
|
+ val noSearchResult =
|
|
|
+ SearchResult(resultText = getString(R.string.search_content_empty))
|
|
|
+ adapter.addItem(noSearchResult)
|
|
|
}
|
|
|
- }
|
|
|
- binding.refreshProgressBar.isAutoLoading = false
|
|
|
- if (viewModel.searchResultCounts == 0) {
|
|
|
- val noSearchResult =
|
|
|
- SearchResult(resultText = getString(R.string.search_content_empty))
|
|
|
- adapter.addItem(noSearchResult)
|
|
|
+ }.onFailure {
|
|
|
+ binding.refreshProgressBar.isAutoLoading = false
|
|
|
+ AppLog.put("全文搜索出错\n${it.localizedMessage}", it)
|
|
|
}
|
|
|
}
|
|
|
}
|