修复崩溃,高亮的时候。

This commit is contained in:
ocean 2025-12-15 09:37:19 +08:00
parent 79a95aceb2
commit 82f37cc4c8

View File

@ -9,6 +9,7 @@ import android.view.MotionEvent
import android.view.View import android.view.View
import android.view.inputmethod.EditorInfo import android.view.inputmethod.EditorInfo
import androidx.activity.OnBackPressedCallback import androidx.activity.OnBackPressedCallback
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import com.all.pdfreader.pdf.reader.R 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.Dispatchers
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import java.io.File import java.io.File
@ -344,8 +346,12 @@ class PdfViewActivity : BaseActivity(), OnLoadCompleteListener, OnPageChangeList
pageCheckJob?.cancel() pageCheckJob?.cancel()
// 启动新的防抖任务 // 启动新的防抖任务
pageCheckJob = lifecycleScope.launch(Dispatchers.IO) { pageCheckJob = lifecycleScope.launch(Dispatchers.IO) {
highlighter.clearCurrentHighlights()//新的之前先清除 // ✅ 切回主线程清理高亮
withContext(Dispatchers.Main) {
highlighter.clearCurrentHighlights()//新的之前先清除
}
delay(120) // 防抖 120ms delay(120) // 防抖 120ms
if (!isActive) return@launch
val hasText = checkPageHasText(doc, page) val hasText = checkPageHasText(doc, page)
if (hasText && searchManager.hasSearched && !searchManager.currentSearchText.isNullOrEmpty()) { if (hasText && searchManager.hasSearched && !searchManager.currentSearchText.isNullOrEmpty()) {
searchTextIng( searchTextIng(
@ -558,22 +564,22 @@ class PdfViewActivity : BaseActivity(), OnLoadCompleteListener, OnPageChangeList
} }
} }
private fun searchTextIng(pdfFile: File, key: String, targetPage: Int? = null) { private suspend fun searchTextIng(
lifecycleScope.launch(Dispatchers.IO) { pdfFile: File,
try { key: String,
val results = searchManager.searchText(pdfFile, key, targetPage, currentPassword) targetPage: Int? = null
) {
val results = withContext(Dispatchers.IO) {
searchManager.searchText(pdfFile, key, targetPage, currentPassword)
}
withContext(Dispatchers.Main) { if (!lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED)) return
if (!results.isEmpty()) {
binding.selectNextBtn.visibility = View.VISIBLE if (results.isNotEmpty()) {
binding.selectPreviousBtn.visibility = View.VISIBLE binding.selectNextBtn.visibility = View.VISIBLE
results.firstOrNull()?.let { firstResult -> binding.selectPreviousBtn.visibility = View.VISIBLE
onSearchResultClicked(firstResult) results.firstOrNull()?.let {
} onSearchResultClicked(it)
}
}
} catch (e: Exception) {
e.printStackTrace()
} }
} }
} }