fix: search nothing when query is chapter title

- enable reSegment

- revert b181d81f17
pull/2352/head
Xwite 2 years ago committed by Xwite
parent d7da433075
commit e34884b3f1
  1. 29
      app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentViewModel.kt

@ -23,7 +23,6 @@ class SearchContentViewModel(application: Application) : BaseViewModel(applicati
var searchResultCounts = 0 var searchResultCounts = 0
val cacheChapterNames = hashSetOf<String>() val cacheChapterNames = hashSetOf<String>()
val searchResultList: MutableList<SearchResult> = mutableListOf() val searchResultList: MutableList<SearchResult> = mutableListOf()
var mContent: String = ""
fun initBook(bookUrl: String, success: () -> Unit) { fun initBook(bookUrl: String, success: () -> Unit) {
this.bookUrl = bookUrl this.bookUrl = bookUrl
@ -45,9 +44,9 @@ class SearchContentViewModel(application: Application) : BaseViewModel(applicati
if (chapter != null) { if (chapter != null) {
book?.let { book -> book?.let { book ->
val chapterContent = BookHelp.getContent(book, chapter) val chapterContent = BookHelp.getContent(book, chapter)
val mContent: String
coroutineContext.ensureActive() coroutineContext.ensureActive()
if (chapterContent != null) { if (chapterContent != null) {
//先搜索没有启用净化的正文
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
chapter.title = when (AppConfig.chineseConverterType) { chapter.title = when (AppConfig.chineseConverterType) {
1 -> ChineseUtils.t2s(chapter.title) 1 -> ChineseUtils.t2s(chapter.title)
@ -56,13 +55,10 @@ class SearchContentViewModel(application: Application) : BaseViewModel(applicati
} }
coroutineContext.ensureActive() coroutineContext.ensureActive()
mContent = contentProcessor!!.getContent( mContent = contentProcessor!!.getContent(
book, chapter, chapterContent, book, chapter, chapterContent
chineseConvert = true,
reSegment = false,
useReplace = false
).joinToString("") ).joinToString("")
} }
val positions = searchPosition(query) val positions = searchPosition(mContent, query)
positions.forEachIndexed { index, position -> positions.forEachIndexed { index, position ->
coroutineContext.ensureActive() coroutineContext.ensureActive()
val construct = getResultAndQueryIndex(mContent, position, query) val construct = getResultAndQueryIndex(mContent, position, query)
@ -84,20 +80,13 @@ class SearchContentViewModel(application: Application) : BaseViewModel(applicati
return searchResultsWithinChapter return searchResultsWithinChapter
} }
private suspend fun searchPosition(pattern: String): List<Int> { private suspend fun searchPosition(content: String, pattern: String): List<Int> {
val position: MutableList<Int> = mutableListOf() val position: MutableList<Int> = mutableListOf()
var index = mContent.indexOf(pattern) var index = content.indexOf(pattern)
if (index >= 0) { while (index >= 0) {
//搜索到内容允许净化 coroutineContext.ensureActive()
if (book!!.getUseReplaceRule()) { position.add(index)
mContent = contentProcessor!!.replaceContent(mContent) index = content.indexOf(pattern, index + pattern.length)
index = mContent.indexOf(pattern)
}
while (index >= 0) {
coroutineContext.ensureActive()
position.add(index)
index = mContent.indexOf(pattern, index + pattern.length)
}
} }
return position return position
} }

Loading…
Cancel
Save