pull/1251/head
gedoor 3 years ago
parent ac48cf91cf
commit 08e4b64d07
  1. 6
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  2. 13
      app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt

@ -855,11 +855,7 @@ class ReadBookActivity : ReadBookBaseActivity(),
private fun skipToSearch(index: Int, indexWithinChapter: Int) {
viewModel.openChapter(index) {
val pages = ReadBook.curTextChapter?.pages ?: return@openChapter
val positions = viewModel.searchResultPositions(
pages,
indexWithinChapter,
viewModel.searchContentQuery
)
val positions = viewModel.searchResultPositions(pages, indexWithinChapter)
ReadBook.skipToPage(positions[0]) {
launch {
binding.readView.curPage.selectStartMoveIndex(0, positions[1], positions[2])

@ -311,8 +311,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
*/
fun searchResultPositions(
pages: List<TextPage>,
indexWithinChapter: Int,
query: String
indexWithinChapter: Int
): Array<Int> {
// calculate search result's pageIndex
var content = ""
@ -320,9 +319,9 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
content += it.text
}
var count = 1
var index = content.indexOf(query)
var index = content.indexOf(searchContentQuery)
while (count != indexWithinChapter) {
index = content.indexOf(query, index + 1)
index = content.indexOf(searchContentQuery, index + 1)
count += 1
}
val contentPosition = index
@ -357,14 +356,14 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
var addLine = 0
var charIndex2 = 0
// change line
if ((charIndex + query.length) > currentLine.text.length) {
if ((charIndex + searchContentQuery.length) > currentLine.text.length) {
addLine = 1
charIndex2 = charIndex + query.length - currentLine.text.length - 1
charIndex2 = charIndex + searchContentQuery.length - currentLine.text.length - 1
}
// changePage
if ((lineIndex + addLine + 1) > currentPage.textLines.size) {
addLine = -1
charIndex2 = charIndex + query.length - currentLine.text.length - 1
charIndex2 = charIndex + searchContentQuery.length - currentLine.text.length - 1
}
return arrayOf(pageIndex, lineIndex, charIndex, addLine, charIndex2)
}

Loading…
Cancel
Save