Skip to content

Open-source Android PDF and document toolkit with PDF tools, Office viewing, XLSX editing, scanning, and image editing. Built with Kotlin and Jetpack Compose.

fix: undo first-attempt no-op in continuous viewer (BUG A)

Chethan6162d9269dfb280
diff --git a/app/src/main/java/com/chethan616/clearpdf/ui/screen/PdfViewerScreen.kt b/app/src/main/java/com/chethan616/clearpdf/ui/screen/PdfViewerScreen.kt
index c1f3410..a34265c 100644
--- a/app/src/main/java/com/chethan616/clearpdf/ui/screen/PdfViewerScreen.kt
+++ b/app/src/main/java/com/chethan616/clearpdf/ui/screen/PdfViewerScreen.kt
@@ -709,7 +709,7 @@ fun PdfViewerScreen(
surfaceColor = chromeGlass
) {
BasicText(
- stringResource(R.string.viewer_page_of, state.currentPage + 1, safePageCount),
+ stringResource(R.string.viewer_page_of, currentPageIndex + 1, safePageCount),
style = TextStyle(chromeFg, 13.sp, FontWeight.SemiBold)
)
}
@@ -752,11 +752,17 @@ fun PdfViewerScreen(
exportError = state.exportError,
exportMessage = state.exportMessage,
lastExportedUri = state.lastExportedUri,
- selectedTextCount = state.selectedOcrBlockIdsByPage[state.currentPage]?.size ?: 0,
- currentSelectedIds = state.selectedOcrBlockIdsByPage[state.currentPage].orEmpty(),
+ selectedTextCount = state.selectedOcrBlockIdsByPage[currentPageIndex]?.size ?: 0,
+ currentSelectedIds = state.selectedOcrBlockIdsByPage[currentPageIndex].orEmpty(),
activeIsSignature = activeItem?.isSignature == true,
- currentPageMarks = getPageMarks(state.currentPage),
- onSetActiveTool = { activeTool = it; if (it == PdfEditTool.None) activeImageId = null; selectedAnnoPage = null; selectedAnnoIndex = -1; viewModel.clearOcrSelection(state.currentPage) },
+ // Undo + all page-scoped edit actions must target the SYNCHRONOUS visible
+ // page (currentPageIndex = listState.firstVisibleItemIndex), NOT the async
+ // state.currentPage mirror updated via snapshotFlow. Drawing/placement inside
+ // the LazyColumn item uses that same synchronous index, so keying the toolbar
+ // to state.currentPage caused a transient mismatch right after open/scroll →
+ // undo removed from the wrong (empty) page list and no-op'd on the first try.
+ currentPageMarks = getPageMarks(currentPageIndex),
+ onSetActiveTool = { activeTool = it; if (it == PdfEditTool.None) activeImageId = null; selectedAnnoPage = null; selectedAnnoIndex = -1; viewModel.clearOcrSelection(currentPageIndex) },
onToggleFindBar = {
showFindBar = !showFindBar
if (showFindBar) viewModel.triggerOcrForAllPages(context)
@@ -777,35 +783,35 @@ fun PdfViewerScreen(
activeImageId = null; activeTool = PdfEditTool.None
},
onSelectAllText = {
- val ids = state.ocrBlocksByPage[state.currentPage].orEmpty().map { it.id }.toSet()
- if (ids.isNotEmpty()) viewModel.selectOcrBlocks(state.currentPage, ids, false)
+ val ids = state.ocrBlocksByPage[currentPageIndex].orEmpty().map { it.id }.toSet()
+ if (ids.isNotEmpty()) viewModel.selectOcrBlocks(currentPageIndex, ids, false)
},
onCopyText = {
- viewModel.getSelectedOcrText(state.currentPage).takeIf { it.isNotBlank() }
+ viewModel.getSelectedOcrText(currentPageIndex).takeIf { it.isNotBlank() }
?.let { clipboard.setText(AnnotatedString(it)) }
},
onHighlightSelected = {
- val m = getPageMarks(state.currentPage)
- state.selectedOcrBlockIdsByPage[state.currentPage].orEmpty().forEach { id ->
+ val m = getPageMarks(currentPageIndex)
+ state.selectedOcrBlockIdsByPage[currentPageIndex].orEmpty().forEach { id ->
if (!m.any { it is PdfMarkup.TextBlockHighlightMarkup && it.blockId == id })
m.add(PdfMarkup.TextBlockHighlightMarkup(id, Color(currentColorLong), 0.30f))
}
},
onUnderlineSelected = {
- val m = getPageMarks(state.currentPage)
- state.selectedOcrBlockIdsByPage[state.currentPage].orEmpty().forEach { id ->
+ val m = getPageMarks(currentPageIndex)
+ state.selectedOcrBlockIdsByPage[currentPageIndex].orEmpty().forEach { id ->
if (!m.any { it is PdfMarkup.TextBlockLineMarkup && it.blockId == id && !it.strikeThrough })
m.add(PdfMarkup.TextBlockLineMarkup(id, Color(currentColorLong), 3f, 1f, false))
}
},
onStrikeSelected = {
- val m = getPageMarks(state.currentPage)
- state.selectedOcrBlockIdsByPage[state.currentPage].orEmpty().forEach { id ->
+ val m = getPageMarks(currentPageIndex)
+ state.selectedOcrBlockIdsByPage[currentPageIndex].orEmpty().forEach { id ->
if (!m.any { it is PdfMarkup.TextBlockLineMarkup && it.blockId == id && it.strikeThrough })
m.add(PdfMarkup.TextBlockLineMarkup(id, Color(currentColorLong), 3f, 1f, true))
}
},
- onClearTextSelection = { viewModel.clearOcrSelection(state.currentPage) },
+ onClearTextSelection = { viewModel.clearOcrSelection(currentPageIndex) },
onSetColorLong = { currentColorLong = it },
onSetStrokeWidth = { currentStrokeWidth = it },
onDismissExportFeedback = { viewModel.clearExportFeedback() },
diff --git a/plan.txt b/plan.txt
index b49fbab..9c649c3 100644
--- a/plan.txt
+++ b/plan.txt
@@ -73,14 +73,20 @@ ITEM 5 — PDF_TOOLS FEATURE-GAP PORTING [ ]
=====================================================================
BUGS TO FIX (from user) — do alongside the items above [ ]
---------------------------------------------------------------------
-BUG A — UNDO first-attempt failure:
+BUG A — UNDO first-attempt failure [x] DONE:
"sometimes on first attempt the undo doesnt work at all for anything any modification unless i
come back to homescreen and reopen the pdf."
- → Undo (per-page currentPageMarks.removeAt(lastIndex)) sometimes no-ops until the PDF is
- reopened. Investigate: annotationsByPage state / getPageMarks keying / whether the first
- edit's mark list is a different instance than what undo mutates / recomposition not observing
- the SnapshotStateList. Likely getPageMarks returns a fresh list vs the one the page edited,
- or mutableStateListOf identity issue across recompositions. Fix so undo works on first try.
+ → ROOT CAUSE: the bottom toolbar's undo + all page-scoped OCR/highlight actions targeted
+ getPageMarks(state.currentPage), where state.currentPage is an ASYNC mirror updated via
+ snapshotFlow{ firstVisibleItemIndex }.collect{ onPageChanged }. Drawing/placement inside the
+ LazyColumn item targets getPageMarks(page) with the SYNCHRONOUS item index. Right after
+ open/scroll (before the flow settles) the two diverge, so undo removed from the wrong (empty)
+ page list = no-op; reopening resynced them.
+ → FIX: switched currentPageMarks + selectedTextCount/currentSelectedIds + onSelectAllText/
+ onCopyText/onHighlight/onUnderline/onStrike/onClearTextSelection + onSetActiveTool's
+ clearOcrSelection + the "Page X / Y" pill from state.currentPage to the synchronous
+ currentPageIndex (= listState.firstVisibleItemIndex, same source the drawing uses).
+ state.currentPage is kept only for the async OCR-extraction trigger. BUILD SUCCESSFUL.
BUG B — Signature resize [x] DONE: PdfContinuousPage image selection redesigned — rounded
accent frame, 3 passive corner dots, and a large (22px) bottom-right resize handle with a