From e34884b3f18de9d8525ac457483829b1299b103d Mon Sep 17 00:00:00 2001 From: Xwite <1797350009@qq.com> Date: Wed, 28 Sep 2022 19:00:57 +0800 Subject: [PATCH] fix: search nothing when query is chapter title - enable reSegment - revert https://github.com/gedoor/legado/commit/b181d81f1728b725e8bd0b6a802a4b3d2911cd97 --- .../searchContent/SearchContentViewModel.kt | 29 ++++++------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentViewModel.kt b/app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentViewModel.kt index e14fb31e1..f93f67714 100644 --- a/app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentViewModel.kt +++ b/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 val cacheChapterNames = hashSetOf() val searchResultList: MutableList = mutableListOf() - var mContent: String = "" fun initBook(bookUrl: String, success: () -> Unit) { this.bookUrl = bookUrl @@ -45,9 +44,9 @@ class SearchContentViewModel(application: Application) : BaseViewModel(applicati if (chapter != null) { book?.let { book -> val chapterContent = BookHelp.getContent(book, chapter) + val mContent: String coroutineContext.ensureActive() if (chapterContent != null) { - //先搜索没有启用净化的正文 withContext(Dispatchers.IO) { chapter.title = when (AppConfig.chineseConverterType) { 1 -> ChineseUtils.t2s(chapter.title) @@ -56,13 +55,10 @@ class SearchContentViewModel(application: Application) : BaseViewModel(applicati } coroutineContext.ensureActive() mContent = contentProcessor!!.getContent( - book, chapter, chapterContent, - chineseConvert = true, - reSegment = false, - useReplace = false + book, chapter, chapterContent ).joinToString("") } - val positions = searchPosition(query) + val positions = searchPosition(mContent, query) positions.forEachIndexed { index, position -> coroutineContext.ensureActive() val construct = getResultAndQueryIndex(mContent, position, query) @@ -84,20 +80,13 @@ class SearchContentViewModel(application: Application) : BaseViewModel(applicati return searchResultsWithinChapter } - private suspend fun searchPosition(pattern: String): List { + private suspend fun searchPosition(content: String, pattern: String): List { val position: MutableList = mutableListOf() - var index = mContent.indexOf(pattern) - if (index >= 0) { - //搜索到内容允许净化 - if (book!!.getUseReplaceRule()) { - mContent = contentProcessor!!.replaceContent(mContent) - index = mContent.indexOf(pattern) - } - while (index >= 0) { - coroutineContext.ensureActive() - position.add(index) - index = mContent.indexOf(pattern, index + pattern.length) - } + var index = content.indexOf(pattern) + while (index >= 0) { + coroutineContext.ensureActive() + position.add(index) + index = content.indexOf(pattern, index + pattern.length) } return position }