修复可能小概率出现的数据操作问题,传入的进度计算异常导致的传入不正常的数值得崩溃。
This commit is contained in:
parent
1ae6a12395
commit
9a882bc4e1
@ -84,12 +84,16 @@ class PdfRepository private constructor(context: Context) {
|
||||
}
|
||||
|
||||
suspend fun updateReadingProgress(filePath: String, page: Int, progress: Float) {
|
||||
val document = pdfDao.getByPath(filePath)?.copy(
|
||||
lastOpenedTime = System.currentTimeMillis(),
|
||||
val old = pdfDao.getByPath(filePath) ?: return
|
||||
|
||||
// 修复旧数据里可能为 null 的字段
|
||||
val safeDocument = old.copy(
|
||||
readingProgress = progress.takeIf { !it.isNaN() } ?: 0f,
|
||||
lastReadPage = page,
|
||||
readingProgress = progress
|
||||
lastOpenedTime = System.currentTimeMillis()
|
||||
)
|
||||
document?.let { pdfDao.update(it) }
|
||||
|
||||
pdfDao.update(safeDocument)
|
||||
}
|
||||
|
||||
//复制更新不会更新flow
|
||||
|
||||
@ -334,7 +334,8 @@ class PdfViewActivity : BaseActivity(), OnLoadCompleteListener, OnPageChangeList
|
||||
override fun onPageChanged(page: Int, pageCount: Int) {
|
||||
// 保存阅读进度到内存
|
||||
pdfDocument = pdfDocument.copy(
|
||||
lastReadPage = page, readingProgress = (page.toFloat() / pageCount.toFloat()) * 100
|
||||
lastReadPage = page,
|
||||
readingProgress = calcProgress(page, pageCount)
|
||||
)
|
||||
saveReadingProgress()
|
||||
|
||||
@ -364,6 +365,19 @@ class PdfViewActivity : BaseActivity(), OnLoadCompleteListener, OnPageChangeList
|
||||
}
|
||||
}
|
||||
|
||||
private fun calcProgress(page: Int, pageCount: Int): Float {
|
||||
if (pageCount <= 0) return 0f
|
||||
|
||||
val raw = (page.toFloat() / pageCount.toFloat()) * 100f
|
||||
|
||||
return when {
|
||||
raw.isNaN() || raw.isInfinite() -> 0f
|
||||
raw < 0f -> 0f
|
||||
raw > 100f -> 100f
|
||||
else -> raw
|
||||
}
|
||||
}
|
||||
|
||||
private fun showPasswordDialog(file: File) {
|
||||
val pdfPasswordProtectionDialogFragment =
|
||||
PdfPasswordProtectionDialogFragment.newInstance(file.absolutePath)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user