From bffbf0e46bae1933bcd42b7fd23e436f6e8e503b Mon Sep 17 00:00:00 2001 From: Jason Yao Date: Mon, 14 Sep 2020 21:33:52 -0400 Subject: [PATCH 1/3] revert log --- .../main/java/io/legado/app/service/help/ReadBook.kt | 11 +++++++++++ .../io/legado/app/ui/book/read/ReadBookActivity.kt | 5 +++++ .../app/ui/book/searchContent/SearchListFragment.kt | 4 ++++ 3 files changed, 20 insertions(+) diff --git a/app/src/main/java/io/legado/app/service/help/ReadBook.kt b/app/src/main/java/io/legado/app/service/help/ReadBook.kt index 2e96221f3..dfa337ae7 100644 --- a/app/src/main/java/io/legado/app/service/help/ReadBook.kt +++ b/app/src/main/java/io/legado/app/service/help/ReadBook.kt @@ -339,9 +339,11 @@ object ReadBook { index = content.indexOf(query, index + 1); count += 1 } + Log.d("h11128", "new index $index") val contentPosition = index var pageIndex = 0 var length = pages[pageIndex].text.length + Log.d("h11128", "page size ${pages.size}") while (length < contentPosition){ pageIndex += 1 if (pageIndex >pages.size){ @@ -349,12 +351,16 @@ object ReadBook { break } 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 val currentPage = pages[pageIndex] var lineIndex = 0 length = length - currentPage.text.length + currentPage.textLines[lineIndex].text.length + Log.d("h11128", "currentLine ${currentPage.textLines[lineIndex].text}") while (length < contentPosition){ lineIndex += 1 if (lineIndex >currentPage.textLines.size){ @@ -362,12 +368,17 @@ object ReadBook { break } length += currentPage.textLines[lineIndex].text.length + Log.d("h11128", "currentLine ${currentPage.textLines[lineIndex].text}") } // charIndex + Log.d("h11128", "currentLine ${currentPage.textLines[lineIndex].text}") + Log.d("h11128", "currentLength $length") val currentLine = currentPage.textLines[lineIndex] length -= currentLine.text.length + Log.d("h11128", "currentLength $length") val charIndex = contentPosition - length + Log.d("h11128", "contentLength $contentPosition") return arrayOf(pageIndex, lineIndex, charIndex) } diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt index 7120e047a..7abcdfc3a 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt @@ -780,13 +780,18 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo val indexWithinChapter = data.getIntExtra("indexWithinChapter", 0) val query = data.getStringExtra("query") viewModel.openChapter(index) + // block until load correct chapter and pages var pages = ReadBook.curTextChapter?.pages while (ReadBook.durChapterIndex != index || pages == null ){ delay(100L) pages = ReadBook.curTextChapter?.pages } + Log.d("h11128", "Current chapter pages ${pages.size}") 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 val job1 = async(Main){ ReadBook.skipToPage(positions[0]) diff --git a/app/src/main/java/io/legado/app/ui/book/searchContent/SearchListFragment.kt b/app/src/main/java/io/legado/app/ui/book/searchContent/SearchListFragment.kt index 38ac013df..646153b6c 100644 --- a/app/src/main/java/io/legado/app/ui/book/searchContent/SearchListFragment.kt +++ b/app/src/main/java/io/legado/app/ui/book/searchContent/SearchListFragment.kt @@ -121,12 +121,15 @@ class SearchListFragment : VMBaseFragment(R.layout.fragment val job = async(IO){ if (isLocalBook || adapter.cacheFileNames.contains(BookHelp.formatChapterName(chapter))) { searchResults = searchChapter(newText, chapter) + //Log.d("h11128", "find ${searchResults.size} results in chapter ${chapter.title}") } } job.await() if (searchResults.isNotEmpty()){ + //Log.d("h11128", "load ${searchResults.size} results in chapter ${chapter.title}") searchResultList.addAll(searchResults) tv_current_search_info.text = "搜索结果:$searchResultCounts" + //Log.d("h11128", "searchResultList size ${searchResultList.size}") adapter.addItems(searchResults) searchResults = listOf() } @@ -226,6 +229,7 @@ class SearchListFragment : VMBaseFragment(R.layout.fragment searchData.putExtra("contentPosition", searchResult.contentPosition) searchData.putExtra("query", searchResult.query) searchData.putExtra("indexWithinChapter", searchResult.indexWithinChapter) + Log.d("h11128","current chapter index ${searchResult.chapterIndex}") activity?.setResult(RESULT_OK, searchData) activity?.finish() From 8c377c2ba50c55f4b558c8a84b124c0dc01528c7 Mon Sep 17 00:00:00 2001 From: Jason Yao Date: Mon, 21 Sep 2020 20:59:10 -0400 Subject: [PATCH 2/3] =?UTF-8?q?=E9=80=89=E6=8B=A9=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=E6=96=87=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/service/help/ReadBook.kt | 16 ++++++++-- .../app/ui/book/read/ReadBookActivity.kt | 32 +++++++++++++------ 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/io/legado/app/service/help/ReadBook.kt b/app/src/main/java/io/legado/app/service/help/ReadBook.kt index dfa337ae7..c52f75afe 100644 --- a/app/src/main/java/io/legado/app/service/help/ReadBook.kt +++ b/app/src/main/java/io/legado/app/service/help/ReadBook.kt @@ -378,8 +378,20 @@ object ReadBook { length -= currentLine.text.length Log.d("h11128", "currentLength $length") val charIndex = contentPosition - length - Log.d("h11128", "contentLength $contentPosition") - return arrayOf(pageIndex, lineIndex, charIndex) + var addLine = 0 + var charIndex2 = 0 + // change line + if ((charIndex + query.length) > currentLine.text.length){ + addLine = 1 + charIndex2 = charIndex + query.length - currentLine.text.length - 1 + } + // changePage + if ((lineIndex + addLine + 1) > currentPage.textLines.size){ + addLine = -1 + charIndex2 = charIndex + query.length - currentLine.text.length - 1 + } + Log.d("h11128", "charIndex2 $charIndex $addLine $charIndex2") + return arrayOf(pageIndex, lineIndex, charIndex, addLine, charIndex2) } /** diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt index b6bb4503c..8ea3cd1c6 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt @@ -9,6 +9,7 @@ import android.graphics.drawable.ColorDrawable import android.net.Uri import android.os.Bundle import android.os.Handler +import android.util.Log import android.view.* import android.view.ViewGroup.LayoutParams.WRAP_CONTENT import androidx.core.view.get @@ -56,16 +57,14 @@ import io.legado.app.ui.widget.dialog.TextDialog import io.legado.app.utils.* import kotlinx.android.synthetic.main.activity_book_read.* import kotlinx.android.synthetic.main.view_read_menu.* -import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.* import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.Main -import kotlinx.coroutines.async -import kotlinx.coroutines.delay -import kotlinx.coroutines.launch import org.jetbrains.anko.sdk27.listeners.onClick import org.jetbrains.anko.startActivity import org.jetbrains.anko.startActivityForResult import org.jetbrains.anko.toast +import java.lang.Runnable class ReadBookActivity : VMBaseActivity(R.layout.activity_book_read), View.OnTouchListener, @@ -794,14 +793,29 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo Log.d("h11128", positions[0].toString()) Log.d("h11128", positions[1].toString()) Log.d("h11128", positions[2].toString()) - //todo: show selected text - val job1 = async(Main){ + Log.d("h11128", positions[3].toString()) + + while (ReadBook.durPageIndex != positions[0]){ + delay(100L) ReadBook.skipToPage(positions[0]) - page_view.curPage.selectStartMoveIndex(positions[0], positions[1], 0) - page_view.curPage.selectEndMoveIndex(positions[0], positions[1], 0 + query.length ) + } + withContext(Main){ + Log.d("h11128", "currentpage ${ReadBook.durPageIndex}") + Log.d("h11128", "currentpage ${page_view.pageFactory.currentPage.index}") + Log.d("h11128", "${positions[0]} ${positions[1]} ${positions[2]}") + + page_view.curPage.selectStartMoveIndex(0, positions[1], positions[2]) + delay(20L) + when (positions[3]){ + 0 -> page_view.curPage.selectEndMoveIndex(0, positions[1], positions[2] + query.length - 1) + 1 -> page_view.curPage.selectEndMoveIndex(0, positions[1] + 1, positions[4]) + //todo: consider change page, jump to scroll position + -1 -> page_view.curPage.selectEndMoveIndex(1, 0, positions[4]) + } page_view.isTextSelected = true + delay(100L) + Log.d("h11128", "asdasd $selectedText") } - job1.await() } } requestCodeReplace -> onReplaceRuleSave() From 65b7bb3d41eaed371d1aebd1001f4baf18bd1e17 Mon Sep 17 00:00:00 2001 From: Jason Yao Date: Mon, 21 Sep 2020 21:04:28 -0400 Subject: [PATCH 3/3] clean log --- .../main/java/io/legado/app/service/help/ReadBook.kt | 11 ----------- .../io/legado/app/ui/book/read/ReadBookActivity.kt | 12 ------------ 2 files changed, 23 deletions(-) diff --git a/app/src/main/java/io/legado/app/service/help/ReadBook.kt b/app/src/main/java/io/legado/app/service/help/ReadBook.kt index c52f75afe..479850620 100644 --- a/app/src/main/java/io/legado/app/service/help/ReadBook.kt +++ b/app/src/main/java/io/legado/app/service/help/ReadBook.kt @@ -339,11 +339,9 @@ object ReadBook { index = content.indexOf(query, index + 1); count += 1 } - Log.d("h11128", "new index $index") val contentPosition = index var pageIndex = 0 var length = pages[pageIndex].text.length - Log.d("h11128", "page size ${pages.size}") while (length < contentPosition){ pageIndex += 1 if (pageIndex >pages.size){ @@ -351,16 +349,12 @@ object ReadBook { break } 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 val currentPage = pages[pageIndex] var lineIndex = 0 length = length - currentPage.text.length + currentPage.textLines[lineIndex].text.length - Log.d("h11128", "currentLine ${currentPage.textLines[lineIndex].text}") while (length < contentPosition){ lineIndex += 1 if (lineIndex >currentPage.textLines.size){ @@ -368,15 +362,11 @@ object ReadBook { break } length += currentPage.textLines[lineIndex].text.length - Log.d("h11128", "currentLine ${currentPage.textLines[lineIndex].text}") } // charIndex - Log.d("h11128", "currentLine ${currentPage.textLines[lineIndex].text}") - Log.d("h11128", "currentLength $length") val currentLine = currentPage.textLines[lineIndex] length -= currentLine.text.length - Log.d("h11128", "currentLength $length") val charIndex = contentPosition - length var addLine = 0 var charIndex2 = 0 @@ -390,7 +380,6 @@ object ReadBook { addLine = -1 charIndex2 = charIndex + query.length - currentLine.text.length - 1 } - Log.d("h11128", "charIndex2 $charIndex $addLine $charIndex2") return arrayOf(pageIndex, lineIndex, charIndex, addLine, charIndex2) } diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt index 8ea3cd1c6..92df11f6a 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt @@ -781,29 +781,18 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo val indexWithinChapter = data.getIntExtra("indexWithinChapter", 0) val query = data.getStringExtra("query") viewModel.openChapter(index) - // block until load correct chapter and pages var pages = ReadBook.curTextChapter?.pages while (ReadBook.durChapterIndex != index || pages == null ){ delay(100L) pages = ReadBook.curTextChapter?.pages } - Log.d("h11128", "Current chapter pages ${pages.size}") 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()) - Log.d("h11128", positions[3].toString()) - while (ReadBook.durPageIndex != positions[0]){ delay(100L) ReadBook.skipToPage(positions[0]) } withContext(Main){ - Log.d("h11128", "currentpage ${ReadBook.durPageIndex}") - Log.d("h11128", "currentpage ${page_view.pageFactory.currentPage.index}") - Log.d("h11128", "${positions[0]} ${positions[1]} ${positions[2]}") - page_view.curPage.selectStartMoveIndex(0, positions[1], positions[2]) delay(20L) when (positions[3]){ @@ -814,7 +803,6 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo } page_view.isTextSelected = true delay(100L) - Log.d("h11128", "asdasd $selectedText") } } }