修复崩溃,高亮的时候。
This commit is contained in:
parent
79a95aceb2
commit
82f37cc4c8
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user