From 82f37cc4c887a32c8ef8e39911c65da12ce31d44 Mon Sep 17 00:00:00 2001 From: ocean <503259349@qq.com> Date: Mon, 15 Dec 2025 09:37:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=B4=A9=E6=BA=83=EF=BC=8C?= =?UTF-8?q?=E9=AB=98=E4=BA=AE=E7=9A=84=E6=97=B6=E5=80=99=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pdf/reader/ui/act/PdfViewActivity.kt | 38 +++++++++++-------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/com/all/pdfreader/pdf/reader/ui/act/PdfViewActivity.kt b/app/src/main/java/com/all/pdfreader/pdf/reader/ui/act/PdfViewActivity.kt index aeb24a9..6fd2d6f 100644 --- a/app/src/main/java/com/all/pdfreader/pdf/reader/ui/act/PdfViewActivity.kt +++ b/app/src/main/java/com/all/pdfreader/pdf/reader/ui/act/PdfViewActivity.kt @@ -9,6 +9,7 @@ import android.view.MotionEvent import android.view.View import android.view.inputmethod.EditorInfo import androidx.activity.OnBackPressedCallback +import androidx.lifecycle.Lifecycle import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.lifecycleScope import com.all.pdfreader.pdf.reader.R @@ -41,6 +42,7 @@ import com.tom_roush.pdfbox.pdmodel.PDDocument import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job import kotlinx.coroutines.delay +import kotlinx.coroutines.isActive import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import java.io.File @@ -344,8 +346,12 @@ class PdfViewActivity : BaseActivity(), OnLoadCompleteListener, OnPageChangeList pageCheckJob?.cancel() // 启动新的防抖任务 pageCheckJob = lifecycleScope.launch(Dispatchers.IO) { - highlighter.clearCurrentHighlights()//新的之前先清除 + // ✅ 切回主线程清理高亮 + withContext(Dispatchers.Main) { + highlighter.clearCurrentHighlights()//新的之前先清除 + } delay(120) // 防抖 120ms + if (!isActive) return@launch val hasText = checkPageHasText(doc, page) if (hasText && searchManager.hasSearched && !searchManager.currentSearchText.isNullOrEmpty()) { searchTextIng( @@ -558,22 +564,22 @@ class PdfViewActivity : BaseActivity(), OnLoadCompleteListener, OnPageChangeList } } - private fun searchTextIng(pdfFile: File, key: String, targetPage: Int? = null) { - lifecycleScope.launch(Dispatchers.IO) { - try { - val results = searchManager.searchText(pdfFile, key, targetPage, currentPassword) + private suspend fun searchTextIng( + pdfFile: File, + key: String, + targetPage: Int? = null + ) { + val results = withContext(Dispatchers.IO) { + searchManager.searchText(pdfFile, key, targetPage, currentPassword) + } - withContext(Dispatchers.Main) { - if (!results.isEmpty()) { - binding.selectNextBtn.visibility = View.VISIBLE - binding.selectPreviousBtn.visibility = View.VISIBLE - results.firstOrNull()?.let { firstResult -> - onSearchResultClicked(firstResult) - } - } - } - } catch (e: Exception) { - e.printStackTrace() + if (!lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED)) return + + if (results.isNotEmpty()) { + binding.selectNextBtn.visibility = View.VISIBLE + binding.selectPreviousBtn.visibility = View.VISIBLE + results.firstOrNull()?.let { + onSearchResultClicked(it) } } }