pull/393/head
Jason Yao 4 years ago
parent c32e02e56a
commit bffbf0e46b
  1. 11
      app/src/main/java/io/legado/app/service/help/ReadBook.kt
  2. 5
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  3. 4
      app/src/main/java/io/legado/app/ui/book/searchContent/SearchListFragment.kt

@ -339,9 +339,11 @@ object ReadBook {
index = content.indexOf(query, index + 1); index = content.indexOf(query, index + 1);
count += 1 count += 1
} }
Log.d("h11128", "new index $index")
val contentPosition = index val contentPosition = index
var pageIndex = 0 var pageIndex = 0
var length = pages[pageIndex].text.length var length = pages[pageIndex].text.length
Log.d("h11128", "page size ${pages.size}")
while (length < contentPosition){ while (length < contentPosition){
pageIndex += 1 pageIndex += 1
if (pageIndex >pages.size){ if (pageIndex >pages.size){
@ -349,12 +351,16 @@ object ReadBook {
break break
} }
length += pages[pageIndex].text.length length += pages[pageIndex].text.length
Log.d("h11128", "to next page , add length change to $length")
} }
Log.d("h11128", "at pageindex $pageIndex")
// calculate search result's lineIndex // calculate search result's lineIndex
val currentPage = pages[pageIndex] val currentPage = pages[pageIndex]
var lineIndex = 0 var lineIndex = 0
length = length - currentPage.text.length + currentPage.textLines[lineIndex].text.length length = length - currentPage.text.length + currentPage.textLines[lineIndex].text.length
Log.d("h11128", "currentLine ${currentPage.textLines[lineIndex].text}")
while (length < contentPosition){ while (length < contentPosition){
lineIndex += 1 lineIndex += 1
if (lineIndex >currentPage.textLines.size){ if (lineIndex >currentPage.textLines.size){
@ -362,12 +368,17 @@ object ReadBook {
break break
} }
length += currentPage.textLines[lineIndex].text.length length += currentPage.textLines[lineIndex].text.length
Log.d("h11128", "currentLine ${currentPage.textLines[lineIndex].text}")
} }
// charIndex // charIndex
Log.d("h11128", "currentLine ${currentPage.textLines[lineIndex].text}")
Log.d("h11128", "currentLength $length")
val currentLine = currentPage.textLines[lineIndex] val currentLine = currentPage.textLines[lineIndex]
length -= currentLine.text.length length -= currentLine.text.length
Log.d("h11128", "currentLength $length")
val charIndex = contentPosition - length val charIndex = contentPosition - length
Log.d("h11128", "contentLength $contentPosition")
return arrayOf(pageIndex, lineIndex, charIndex) return arrayOf(pageIndex, lineIndex, charIndex)
} }

@ -780,13 +780,18 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
val indexWithinChapter = data.getIntExtra("indexWithinChapter", 0) val indexWithinChapter = data.getIntExtra("indexWithinChapter", 0)
val query = data.getStringExtra("query") val query = data.getStringExtra("query")
viewModel.openChapter(index) viewModel.openChapter(index)
// block until load correct chapter and pages // block until load correct chapter and pages
var pages = ReadBook.curTextChapter?.pages var pages = ReadBook.curTextChapter?.pages
while (ReadBook.durChapterIndex != index || pages == null ){ while (ReadBook.durChapterIndex != index || pages == null ){
delay(100L) delay(100L)
pages = ReadBook.curTextChapter?.pages pages = ReadBook.curTextChapter?.pages
} }
Log.d("h11128", "Current chapter pages ${pages.size}")
val positions = ReadBook.searchResultPositions(pages, indexWithinChapter, query!!) val positions = ReadBook.searchResultPositions(pages, indexWithinChapter, query!!)
Log.d("h11128", positions[0].toString())
Log.d("h11128", positions[1].toString())
Log.d("h11128", positions[2].toString())
//todo: show selected text //todo: show selected text
val job1 = async(Main){ val job1 = async(Main){
ReadBook.skipToPage(positions[0]) ReadBook.skipToPage(positions[0])

@ -121,12 +121,15 @@ class SearchListFragment : VMBaseFragment<SearchListViewModel>(R.layout.fragment
val job = async(IO){ val job = async(IO){
if (isLocalBook || adapter.cacheFileNames.contains(BookHelp.formatChapterName(chapter))) { if (isLocalBook || adapter.cacheFileNames.contains(BookHelp.formatChapterName(chapter))) {
searchResults = searchChapter(newText, chapter) searchResults = searchChapter(newText, chapter)
//Log.d("h11128", "find ${searchResults.size} results in chapter ${chapter.title}")
} }
} }
job.await() job.await()
if (searchResults.isNotEmpty()){ if (searchResults.isNotEmpty()){
//Log.d("h11128", "load ${searchResults.size} results in chapter ${chapter.title}")
searchResultList.addAll(searchResults) searchResultList.addAll(searchResults)
tv_current_search_info.text = "搜索结果:$searchResultCounts" tv_current_search_info.text = "搜索结果:$searchResultCounts"
//Log.d("h11128", "searchResultList size ${searchResultList.size}")
adapter.addItems(searchResults) adapter.addItems(searchResults)
searchResults = listOf<SearchResult>() searchResults = listOf<SearchResult>()
} }
@ -226,6 +229,7 @@ class SearchListFragment : VMBaseFragment<SearchListViewModel>(R.layout.fragment
searchData.putExtra("contentPosition", searchResult.contentPosition) searchData.putExtra("contentPosition", searchResult.contentPosition)
searchData.putExtra("query", searchResult.query) searchData.putExtra("query", searchResult.query)
searchData.putExtra("indexWithinChapter", searchResult.indexWithinChapter) searchData.putExtra("indexWithinChapter", searchResult.indexWithinChapter)
Log.d("h11128","current chapter index ${searchResult.chapterIndex}")
activity?.setResult(RESULT_OK, searchData) activity?.setResult(RESULT_OK, searchData)
activity?.finish() activity?.finish()

Loading…
Cancel
Save