From d6532eb87e6b6d002f37ae470351e2d3e09ccfc8 Mon Sep 17 00:00:00 2001 From: gedoor Date: Fri, 2 Oct 2020 10:46:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=85=A8=E6=96=87=E6=90=9C?= =?UTF-8?q?=E7=B4=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/book/read/ReadBookActivity.kt | 53 +++++++++++++------ .../app/ui/book/read/ReadBookViewModel.kt | 1 + .../book/searchContent/SearchListActivity.kt | 7 +-- 3 files changed, 39 insertions(+), 22 deletions(-) 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 92df11f6a..e26a982ad 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,7 +9,6 @@ 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 @@ -57,14 +56,16 @@ 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.* +import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.Main +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext 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, @@ -494,6 +495,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo return true } R.id.menu_search_content -> { + viewModel.searchContentQuery = selectedText openSearchActivity(selectedText) return true } @@ -686,7 +688,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo startActivityForResult( requestCodeSearchResult, Pair("bookUrl", it.bookUrl), - Pair("searchWord", searchWord) + Pair("searchWord", searchWord ?: viewModel.searchContentQuery) ) } } @@ -771,33 +773,50 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo requestCodeChapterList -> data?.getIntExtra("index", ReadBook.durChapterIndex)?.let { index -> if (index != ReadBook.durChapterIndex) { - val pageIndex = data.getIntExtra("pageIndex", 0) - viewModel.openChapter(index, pageIndex) - } + val pageIndex = data.getIntExtra("pageIndex", 0) + viewModel.openChapter(index, pageIndex) } + } requestCodeSearchResult -> data?.getIntExtra("index", ReadBook.durChapterIndex)?.let { index -> - launch(IO){ + launch(IO) { val indexWithinChapter = data.getIntExtra("indexWithinChapter", 0) - val query = data.getStringExtra("query") + viewModel.searchContentQuery = data.getStringExtra("query") ?: "" viewModel.openChapter(index) // block until load correct chapter and pages var pages = ReadBook.curTextChapter?.pages - while (ReadBook.durChapterIndex != index || pages == null ){ + while (ReadBook.durChapterIndex != index || pages == null) { delay(100L) pages = ReadBook.curTextChapter?.pages } - val positions = ReadBook.searchResultPositions(pages, indexWithinChapter, query!!) - while (ReadBook.durPageIndex != positions[0]){ + val positions = + ReadBook.searchResultPositions( + pages, + indexWithinChapter, + viewModel.searchContentQuery + ) + while (ReadBook.durPageIndex != positions[0]) { delay(100L) ReadBook.skipToPage(positions[0]) } - withContext(Main){ - page_view.curPage.selectStartMoveIndex(0, positions[1], positions[2]) + withContext(Main) { + 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]) + when (positions[3]) { + 0 -> page_view.curPage.selectEndMoveIndex( + 0, + positions[1], + positions[2] + viewModel.searchContentQuery.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]) } diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt index b8efe1cee..e2ff19199 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt @@ -23,6 +23,7 @@ import kotlinx.coroutines.withContext class ReadBookViewModel(application: Application) : BaseViewModel(application) { var isInitFinish = false + var searchContentQuery = "" fun initData(intent: Intent) { execute { diff --git a/app/src/main/java/io/legado/app/ui/book/searchContent/SearchListActivity.kt b/app/src/main/java/io/legado/app/ui/book/searchContent/SearchListActivity.kt index 6c01801c3..18966496c 100644 --- a/app/src/main/java/io/legado/app/ui/book/searchContent/SearchListActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/searchContent/SearchListActivity.kt @@ -140,11 +140,8 @@ class SearchListActivity : VMBaseActivity(R.layout.activity launch(Dispatchers.Main) { App.db.bookChapterDao().getChapterList(viewModel.bookUrl).map { chapter -> val job = async(Dispatchers.IO) { - if (isLocalBook || adapter.cacheFileNames.contains( - BookHelp.formatChapterName( - chapter - ) - ) + if (isLocalBook + || adapter.cacheFileNames.contains(BookHelp.formatChapterName(chapter)) ) { searchResults = searchChapter(newText, chapter) }