pull/2072/head
kunfei 2 years ago
parent a052c8f0e6
commit 316cd246e2
  1. 1
      .idea/inspectionProfiles/profiles_settings.xml
  2. 1
      app/src/main/assets/updateLog.md
  3. 56
      app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentActivity.kt
  4. 16
      app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentViewModel.kt

@ -1,5 +1,6 @@
<component name="InspectionProjectProfileManager"> <component name="InspectionProjectProfileManager">
<settings> <settings>
<option name="PROJECT_PROFILE" value="Default" />
<option name="USE_PROJECT_PROFILE" value="false" /> <option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" /> <version value="1.0" />
</settings> </settings>

@ -14,6 +14,7 @@
**2022/07/12** **2022/07/12**
* 针对自建webDav进行优化,解决一些问题 by 821938089 * 针对自建webDav进行优化,解决一些问题 by 821938089
* 修复全文搜索新关键词时,老关键词搜索没有停止的bug
**2022/07/10** **2022/07/10**

@ -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)
} }
} }
} }

@ -10,7 +10,9 @@ import io.legado.app.data.entities.BookChapter
import io.legado.app.help.BookHelp import io.legado.app.help.BookHelp
import io.legado.app.help.ContentProcessor import io.legado.app.help.ContentProcessor
import io.legado.app.help.config.AppConfig import io.legado.app.help.config.AppConfig
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
class SearchContentViewModel(application: Application) : BaseViewModel(application) { class SearchContentViewModel(application: Application) : BaseViewModel(application) {
@ -35,11 +37,16 @@ class SearchContentViewModel(application: Application) : BaseViewModel(applicati
} }
} }
suspend fun searchChapter(query: String, chapter: BookChapter?): List<SearchResult> { suspend fun searchChapter(
scope: CoroutineScope,
query: String,
chapter: BookChapter?
): List<SearchResult> {
val searchResultsWithinChapter: MutableList<SearchResult> = mutableListOf() val searchResultsWithinChapter: MutableList<SearchResult> = mutableListOf()
if (chapter != null) { if (chapter != null) {
book?.let { book -> book?.let { book ->
val chapterContent = BookHelp.getContent(book, chapter) val chapterContent = BookHelp.getContent(book, chapter)
scope.ensureActive()
if (chapterContent != null) { if (chapterContent != null) {
//先搜索没有启用净化的正文 //先搜索没有启用净化的正文
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
@ -48,6 +55,7 @@ class SearchContentViewModel(application: Application) : BaseViewModel(applicati
2 -> ChineseUtils.s2t(chapter.title) 2 -> ChineseUtils.s2t(chapter.title)
else -> chapter.title else -> chapter.title
} }
scope.ensureActive()
mContent = contentProcessor!!.getContent( mContent = contentProcessor!!.getContent(
book, chapter, chapterContent, book, chapter, chapterContent,
chineseConvert = true, chineseConvert = true,
@ -55,8 +63,9 @@ class SearchContentViewModel(application: Application) : BaseViewModel(applicati
useReplace = false useReplace = false
).joinToString("") ).joinToString("")
} }
val positions = searchPosition(query) val positions = searchPosition(scope, query)
positions.forEachIndexed { index, position -> positions.forEachIndexed { index, position ->
scope.ensureActive()
val construct = getResultAndQueryIndex(mContent, position, query) val construct = getResultAndQueryIndex(mContent, position, query)
val result = SearchResult( val result = SearchResult(
resultCountWithinChapter = index, resultCountWithinChapter = index,
@ -76,7 +85,7 @@ class SearchContentViewModel(application: Application) : BaseViewModel(applicati
return searchResultsWithinChapter return searchResultsWithinChapter
} }
private suspend fun searchPosition(pattern: String): List<Int> { private suspend fun searchPosition(scope: CoroutineScope, pattern: String): List<Int> {
val position: MutableList<Int> = mutableListOf() val position: MutableList<Int> = mutableListOf()
var index = mContent.indexOf(pattern) var index = mContent.indexOf(pattern)
if (index >= 0) { if (index >= 0) {
@ -86,6 +95,7 @@ class SearchContentViewModel(application: Application) : BaseViewModel(applicati
index = mContent.indexOf(pattern) index = mContent.indexOf(pattern)
} }
while (index >= 0) { while (index >= 0) {
scope.ensureActive()
position.add(index) position.add(index)
index = mContent.indexOf(pattern, index + 1) index = mContent.indexOf(pattern, index + 1)
} }

Loading…
Cancel
Save