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