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