General improvements (#151)

* Updated folder synchronization logic and redesigned the file type filter dialog.

* Fixed CFI generation and navigation stability in the EPUB reader.

* Improved CFI scrolling and position calculation in the EPUB reader by implementing text node traversal using `TreeWalker`. This ensures accurate positioning and scrolling when a CFI offset spans multiple fragmented text nodes.

* fix fb2 multiline titles, retain footnotes, and prevent stream leaks

- Fix FB2 titles with multiple paragraphs by inserting breaks/spaces
- Prevent resource leaks by properly closing InputStreams in all importers
- Retain "notes" and "comments" sections in FB2 instead of skipping them
- Add support for FB2 poem, stanza, cite, and link tags

* Implement persistence for zoom and pan states when pan lock is enabled in the PDF reader.

* Added `FileTypeBadge` to home and library screens

* Bump version to 1.0.41(42)
This commit is contained in:
Aryan 2026-04-05 10:36:44 +05:30 committed by GitHub
parent 65e0570d0e
commit 26692d2c05
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 747 additions and 664 deletions

View file

@ -30,8 +30,8 @@ android {
applicationId = "com.aryan.reader"
minSdk = 26
targetSdk = 35
versionCode = 41
versionName = "1.0.40"
versionCode = 42
versionName = "1.0.41"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
externalNativeBuild {

View file

@ -588,7 +588,9 @@
300,
);
if (window.reportScrollState) {
if (window.triggerInitialScrollStateReport) {
window.triggerInitialScrollStateReport();
} else if (window.reportScrollState) {
setTimeout(window.reportScrollState, 60);
}
};
@ -693,26 +695,20 @@
window.triggerInitialScrollStateReport = function () {
var attempts = 0;
var maxAttempts = 7;
var baseInterval = 100;
var maxAttempts = 15;
function tryReport() {
attempts++;
if (window.reportScrollState) {
window.reportScrollState();
var currentClientHeight = document.documentElement.clientHeight || window.innerHeight || 0;
if (currentClientHeight > 0) {
setTimeout(window.reportScrollState, 50);
return;
}
if (attempts < maxAttempts) {
var retryDelay = baseInterval + attempts * 50;
setTimeout(tryReport, retryDelay);
setTimeout(tryReport, 200);
}
}
setTimeout(tryReport, baseInterval);
setTimeout(tryReport, 50);
};
window.scrollToChapterStart = function () {
@ -1418,223 +1414,131 @@
}
function resolveCfiPath(rootElement, path) {
log(`Attempting to resolve path '${path}' from root <$ {
rootElement.tagName
}
>`);
let currentNode = rootElement;
const steps = path.substring(1).split("/").map(Number);
for (let i = 0; i < steps.length; i++) {
const cfiIndex = steps[i];
if (!currentNode) return null;
if (!currentNode) {
log(`Traversal failed: currentNode became null before step $ {
i
// Handle virtualized content container specially
if (currentNode.id === 'content-container') {
const childNodeIndex = (cfiIndex - 2) / 2;
let chunkIndex = Math.floor(childNodeIndex / 20);
let indexInChunk = childNodeIndex % 20;
let chunkElement = currentNode.querySelector(`.chunk-container[data-chunk-index="${chunkIndex}"]`);
if (chunkElement) {
let elementsInChunk = Array.from(chunkElement.childNodes).filter(n => n.nodeType === Node.ELEMENT_NODE);
if (indexInChunk >= 0 && indexInChunk < elementsInChunk.length) {
currentNode = elementsInChunk[indexInChunk];
continue;
}
}
(CFI index $ {
cfiIndex
}).`);
return null;
}
const elementChildren = Array.from(currentNode.childNodes).filter((node) => node.nodeType === Node.ELEMENT_NODE);
let elementChildren = Array.from(currentNode.childNodes).filter((node) => node.nodeType === Node.ELEMENT_NODE);
const childNodeIndex = (cfiIndex - 2) / 2;
if (childNodeIndex >= 0 && childNodeIndex < elementChildren.length) {
currentNode = elementChildren[childNodeIndex];
} else {
const childrenTags = elementChildren
.map(
(node) => `<$ {
node.tagName || "TEXT"
}
>`,
)
.join(", ");
log(`Step $ {
i
}
: FAILED. CFI index $ {
cfiIndex
}
(child index $ {
childNodeIndex
}) is out of bounds for $ {
elementChildren.length
}
element children: [$ {
childrenTags
}
]`);
log(`Parent Node HTML at failure point (<$ {
currentNode.tagName
}
>): $ {
currentNode.innerHTML.substring(0, 300)
}
...`); // ADD THIS LINE
return null; // Path is invalid from this root
return null;
}
}
return currentNode;
}
window.getNodeAndOffsetFromCfi = function (cfi) {
log(`getNodeAndOffsetFromCfi called with: $ {
cfi
}
`);
try {
var pathParts = cfi.split(":");
var nodePath = pathParts[0];
var charOffset = pathParts.length > 1 ? parseInt(pathParts[1], 10) : 0;
log(`Parsed CFI: path=$ {
nodePath
}
, offset=$ {
charOffset
}
`);
let cfiRoot = document.getElementById("content-container") || document.body;
let pathToResolve = nodePath;
const firstChunk = cfiRoot.querySelector("[data-chunk-index]");
if (firstChunk && pathToResolve.startsWith("/4/")) {
log(`Paginator CFI detected. Adjusting root to first chunk and stripping '/4' from path.`);
cfiRoot = firstChunk;
// Strip the /4 prefix because cfiRoot (content-container or body) represents /4
if (pathToResolve.startsWith("/4/")) {
pathToResolve = "/" + pathToResolve.substring(3);
} else if (pathToResolve === "/4") {
pathToResolve = "";
}
log(`CFI Root is <$ {
cfiRoot.tagName
if (!pathToResolve) {
return { node: cfiRoot, offset: charOffset };
}
>, Path to resolve is $ {
pathToResolve
}
`);
let resolvedNode = resolveCfiPath(cfiRoot, pathToResolve);
log(`Resolution attempt #1 (from CFI root) result: $ {
resolvedNode ? resolvedNode.tagName : 'null'
}
`);
if (!resolvedNode) {
log("Resolution failed from all roots.");
return null;
}
if (!resolvedNode) return null;
let currentNode = resolvedNode;
log(`Successfully resolved containing element: <$ {
currentNode.tagName || 'TEXT_NODE'
}
>`);
if (currentNode.nodeType === Node.ELEMENT_NODE) {
const treeWalker = document.createTreeWalker(currentNode, NodeFilter.SHOW_TEXT, null, false);
const firstTextNode = treeWalker.nextNode();
if (firstTextNode) {
log(`Found first text node inside element to apply offset.`);
currentNode = firstTextNode;
} else {
log(`Could not find a text node inside the target element. Using the element itself.`);
}
}
return { node: currentNode, offset: charOffset };
} catch (e) {
log(`ERROR in getNodeAndOffsetFromCfi: $ {
e.message
}
`);
return null;
}
};
window.getCfiPathForElement = function (element, charOffset) {
const logStack = [];
try {
var path =[];
var currentNode = element;
if (currentNode.nodeType === Node.TEXT_NODE) {
logStack.push(`Initial node is a TEXT_NODE. Calculating cumulative offset.`);
// --- FIX: Accumulate offsets from previous siblings ---
var accumulatedOffset = charOffset || 0;
var sibling = currentNode.previousSibling;
while (sibling) {
if (sibling.nodeType === Node.TEXT_NODE) {
accumulatedOffset += sibling.nodeValue.length;
} else if (sibling.nodeType === Node.ELEMENT_NODE) {
// Elements like <em>, <b> contribute text content to the flow
accumulatedOffset += (sibling.textContent || "").length;
}
sibling = sibling.previousSibling;
}
logStack.push(`Original offset: $ {
charOffset
}
, Cumulative offset: $ {
accumulatedOffset
}
`);
charOffset = accumulatedOffset;
// -----------------------------------------------------
logStack.push(`Using its parent <$ {
currentNode.parentNode.tagName
}
> for path generation.`);
currentNode = currentNode.parentNode;
}
const root = document.getElementById("content-container") || document.body;
logStack.push(`Using <$ {
root.tagName
while (currentNode && currentNode !== root && currentNode.parentNode) {
let parentNode = currentNode.parentNode;
if (parentNode.classList && parentNode.classList.contains('chunk-container')) {
let trueParent = parentNode.parentNode;
let elementSiblingsInChunk = Array.from(parentNode.childNodes).filter(node => node.nodeType === Node.ELEMENT_NODE);
let indexInChunk = elementSiblingsInChunk.indexOf(currentNode);
if (indexInChunk === -1) {
currentNode = trueParent;
continue;
}
id='${root.id}' class='${root.className}' > as the consistent CFI root.`);
let chunkIndex = parseInt(parentNode.dataset.chunkIndex, 10);
let elementsInPrecedingChunks = chunkIndex * 20;
let trueIndex = elementsInPrecedingChunks + indexInChunk;
let cfiIndex = trueIndex * 2 + 2;
path.unshift(cfiIndex);
logStack.push(`Chunk ${chunkIndex}, IdxInChunk ${indexInChunk}, TrueIdx ${trueIndex} -> CFI /${cfiIndex}`);
currentNode = trueParent;
continue;
}
while (currentNode && currentNode !== root && currentNode.parentNode) {
const parentNode = currentNode.parentNode;
const elementSiblings = Array.from(parentNode.childNodes).filter((node) => node.nodeType === Node.ELEMENT_NODE);
const nodeIndex = elementSiblings.indexOf(currentNode);
@ -1645,110 +1549,56 @@
const cfiIndex = nodeIndex * 2 + 2;
path.unshift(cfiIndex);
const childrenTags = elementSiblings
.map(
(node) => `<$ {
node.tagName || "TEXT"
}
>`,
)
.join(", ");
logStack.push(`GENERATION: Parent <$ {
parentNode.tagName
}
> has $ {
elementSiblings.length
}
element children: [$ {
childrenTags
}
]. Current node <$ {
currentNode.tagName
}
> is at index $ {
nodeIndex
}
, becoming CFI step $ {
cfiIndex
}
. Path: /$ {
path.join('/')
}
`);
currentNode = parentNode;
}
var cfi = `/` + path.join("/");
var cfi = "/4";
if (path.length > 0) {
cfi += "/" + path.join("/");
}
if (charOffset !== undefined && charOffset > 0) {
cfi += ":" + charOffset;
}
logStack.push(`Final CFI generated: $ {
cfi
}
`);
logStack.push(`Generated CFI: ${cfi}`);
return { cfi: cfi, log: logStack };
} catch (e) {
logStack.push(`ERROR in getCfiPathForElement: $ {
e.message
}
`);
return { cfi: `/2`, log: logStack };
logStack.push("Error: " + e.message);
return { cfi: "/4/2", log: logStack };
}
};
window.getCurrentCfi = function() {
const debugLog =[];
let finalCfi = "/2"; // Fallback to root
function logCfi(m) { debugLog.push(m); }
let finalCfi = "/4/2";
try {
const viewportX = window.innerWidth / 2;
// Probe slightly further down to avoid headers/padding issues
const viewportY = window.VIEWPORT_PADDING_TOP + 50;
let viewportY = window.VIEWPORT_PADDING_TOP + 5;
let topElement = null;
logCfi("Probing for CFI at " + viewportX + "," + viewportY);
for (let i = 0; i < 10; i++) {
let el = document.elementFromPoint(viewportX, viewportY);
if (el && el.id !== 'content-container' &&
!(el.classList && el.classList.contains('chunk-container') && el.innerText.trim() === "")) {
topElement = el;
break;
}
viewportY += 15;
}
let topElement = document.elementFromPoint(viewportX, viewportY);
// FIX: Ignore empty chunk containers or the container wrapper itself
if (topElement && (topElement.id === 'content-container' ||
(topElement.classList && topElement.classList.contains('chunk-container') && topElement.innerText.trim() === ""))) {
logCfi("Hit empty chunk or container. scanning for first visible content...");
// Fallback: Query specifically for content elements
const elements = document.querySelectorAll('p, h1, h2, h3, h4, h5, h6, li, span, div.chunk-container');
if (!topElement || topElement.id === 'content-container' || (topElement.classList && topElement.classList.contains('chunk-container'))) {
const elements = document.querySelectorAll('p, h1, h2, h3, h4, h5, h6, li, span');
for (let i = 0; i < elements.length; i++) {
const el = elements[i];
const rect = el.getBoundingClientRect();
// Find first element that is effectively visible below the top padding
if (rect.bottom > window.VIEWPORT_PADDING_TOP && el.innerText.trim().length > 0) {
topElement = el;
logCfi("Found alternative content element: <" + el.tagName + ">");
break;
}
}
}
if (!topElement) {
logCfi("No element found. Fallback to body first child.");
topElement = document.body.firstElementChild;
}
@ -1756,9 +1606,6 @@
return JSON.stringify({ cfi: finalCfi, log: debugLog });
}
logCfi("Selected Reference Element: <" + topElement.tagName + "> ID:" + topElement.id);
// Try caret range for precision
let range = null;
if (document.caretRangeFromPoint) {
range = document.caretRangeFromPoint(viewportX, viewportY);
@ -1770,27 +1617,22 @@
if (range && range.startContainer && range.startContainer.nodeType === Node.TEXT_NODE) {
nodeForCfi = range.startContainer;
offsetForCfi = range.startOffset;
logCfi("Precision match via caretRangeFromPoint.");
debugLog.push(`Caret range hit successfully at offset ${offsetForCfi}`);
} else {
// Walker fallback
const treeWalker = document.createTreeWalker(topElement, NodeFilter.SHOW_TEXT, null, false);
let firstTextNode = treeWalker.nextNode();
nodeForCfi = (firstTextNode && firstTextNode.textContent.trim().length > 0) ? firstTextNode : topElement;
offsetForCfi = 0;
logCfi("Fallback match via TreeWalker.");
}
const cfiResult = window.getCfiPathForElement(nodeForCfi, offsetForCfi);
finalCfi = cfiResult.cfi;
// Merge logs
if (cfiResult.log) debugLog.push(...cfiResult.log);
} catch (e) {
debugLog.push("Error in getCurrentCfi: " + e.message);
}
// We intentionally do not use TAG_BM here as this is called frequently on scroll.
// The CfiBridge in Kotlin logs this separately.
return JSON.stringify({ cfi: finalCfi, log: debugLog });
};
@ -1801,15 +1643,15 @@
}
window.scrollToCfi = function(cfi) {
logBm("scrollToCfi called with: " + cfi);
let cleanCfi = cfi;
if (cfi && cfi.includes('@')) {
cleanCfi = cfi.substring(cfi.indexOf('@') + 1);
}
console.log("PosSaveDiag: JS scrollToCfi called with cleanCfi=" + cleanCfi);
if (!cleanCfi || !cleanCfi.startsWith('/')) {
logBm("Invalid CFI format, aborting scroll.");
if (window.CfiBridge && window.CfiBridge.onScrollFinished) {
window.CfiBridge.onScrollFinished(false);
}
@ -1818,77 +1660,103 @@
let attempts = 0;
const maxAttempts = 20;
let stabilizingFrames = 0;
const maxStabilizingFrames = 8;
function attemptScroll() {
attempts++;
logBm("Scroll Attempt " + attempts + "/" + maxAttempts + " for " + cleanCfi);
try {
const location = window.getNodeAndOffsetFromCfi(cleanCfi);
if (location && location.node) {
logBm("Target node FOUND. Node: " + location.node.nodeName);
if (!document.body.contains(location.node)) {
logBm("Node found but detached. Retrying...");
if (attempts < maxAttempts) setTimeout(attemptScroll, 100);
return;
}
let targetScrollY = 0;
if (location.node.nodeType === Node.TEXT_NODE && location.offset > 0) {
try {
console.log("PosSaveDiag: Initial text node length=" + location.node.nodeValue.length + ", location.offset=" + location.offset);
let currentNode = location.node;
let remainingOffset = location.offset;
// Traverse sibling text nodes to find the exact offset
const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT, null, false);
walker.currentNode = currentNode;
while (currentNode && remainingOffset > currentNode.nodeValue.length) {
remainingOffset -= currentNode.nodeValue.length;
const next = walker.nextNode();
if (!next) {
console.log("PosSaveDiag: Reached end of text nodes while traversing.");
break;
}
currentNode = next;
}
console.log("PosSaveDiag: Traversed to node with text: '" + currentNode.nodeValue.substring(0, 20) + "', remainingOffset=" + remainingOffset + ", actual length=" + currentNode.nodeValue.length);
const range = document.createRange();
const validOffset = Math.min(location.offset, location.node.nodeValue.length);
range.setStart(location.node, validOffset);
const validOffset = Math.min(remainingOffset, currentNode.nodeValue.length);
range.setStart(currentNode, validOffset);
range.collapse(true);
const rect = range.getBoundingClientRect();
if (rect.top !== 0 || rect.bottom !== 0) {
const targetScrollY = window.scrollY + rect.top - window.VIEWPORT_PADDING_TOP;
window.scrollTo({ top: targetScrollY, behavior: 'auto' });
setTimeout(() => {
window.reportScrollState();
if (window.CfiBridge && window.CfiBridge.onScrollFinished) {
window.CfiBridge.onScrollFinished(true);
}
}, 150);
return;
targetScrollY = window.scrollY + rect.top - (window.VIEWPORT_PADDING_TOP + 5);
}
} catch (e) {
logBm("Precise scroll failed: " + e.message);
console.log("PosSaveDiag: Error calculating range bounding rect: " + e.message);
}
}
if (targetScrollY === 0) {
const targetElement = (location.node.nodeType === Node.TEXT_NODE) ? location.node.parentNode : location.node;
targetElement.scrollIntoView({ behavior: 'auto', block: 'start', inline: 'nearest' });
const rect = targetElement.getBoundingClientRect();
targetScrollY = window.scrollY + rect.top - (window.VIEWPORT_PADDING_TOP + 5);
}
if (Math.abs(window.scrollY - targetScrollY) > 1) {
window.scrollTo({ top: targetScrollY, behavior: 'auto' });
}
stabilizingFrames++;
if (stabilizingFrames < maxStabilizingFrames) {
setTimeout(attemptScroll, 100);
} else {
setTimeout(() => {
if (window.VIEWPORT_PADDING_TOP > 0) window.scrollBy(0, -window.VIEWPORT_PADDING_TOP);
window.reportScrollState();
if (window.CfiBridge && window.CfiBridge.onScrollFinished) {
window.CfiBridge.onScrollFinished(true);
}
}, 150);
}, 50);
}
} else {
if (attempts < maxAttempts) {
setTimeout(attemptScroll, 100);
} else {
logBm("Max attempts reached. Scroll failed.");
if (window.CfiBridge && window.CfiBridge.onScrollFinished) {
window.CfiBridge.onScrollFinished(false);
}
}
}
} catch (e) {
logBm("Fatal error: " + e.message);
if (window.CfiBridge && window.CfiBridge.onScrollFinished) {
window.CfiBridge.onScrollFinished(false);
}
}
}
if (document.fonts && document.fonts.ready) {
document.fonts.ready.then(function() {
attemptScroll();
});
} else {
attemptScroll();
}
};
window.getElementByCfi = function(cfi) {
@ -1927,6 +1795,15 @@
const treeWalker = document.createTreeWalker(location.node, NodeFilter.SHOW_TEXT, null, false);
textNode = treeWalker.nextNode();
offset = 0;
} else if (offset > 0) {
const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT, null, false);
walker.currentNode = textNode;
while (textNode && offset > textNode.nodeValue.length) {
offset -= textNode.nodeValue.length;
const next = walker.nextNode();
if (!next) break;
textNode = next;
}
}
if (textNode) {
@ -2026,10 +1903,9 @@
if (container) {
container.querySelectorAll(".chunk-container").forEach((div) => {
let idx = parseInt(div.dataset.chunkIndex, 10);
let content = div.innerHTML; // Don't trim, keep original HTML structure
let content = div.innerHTML;
if (content.trim().length > 0) {
// FIX: Assign to specific index, don't overwrite the whole array
this.chunksData[idx] = content;
this.chunkHeights[idx] = div.getBoundingClientRect().height;
}
@ -2045,6 +1921,7 @@
this.observer = new IntersectionObserver(
(entries) => {
let scrollAdjust = 0;
let domChanged = false;
entries.forEach((entry) => {
let div = entry.target;
@ -2062,6 +1939,7 @@
let newHeight = div.getBoundingClientRect().height;
this.chunkHeights[idx] = newHeight;
domChanged = true;
if (div.getBoundingClientRect().top < 0) {
scrollAdjust += (newHeight - oldHeight);
@ -2076,6 +1954,7 @@
this.chunkHeights[idx] = oldHeight;
div.style.height = oldHeight + "px";
div.innerHTML = "";
domChanged = true;
}
}
});
@ -2083,8 +1962,12 @@
if (scrollAdjust !== 0) {
window.scrollBy(0, scrollAdjust);
}
if (domChanged && window.reportScrollState) {
setTimeout(window.reportScrollState, 50);
}
},
{ rootMargin: "2500px 0px" } // Keep large margin for smooth scrolling
{ rootMargin: "2500px 0px" }
);
document.querySelectorAll(".chunk-container").forEach((div) => {
@ -2114,6 +1997,11 @@
if (div.getBoundingClientRect().bottom < 0) {
window.scrollBy(0, newHeight - oldHeight);
}
if (window.reportScrollState) {
setTimeout(window.reportScrollState, 50);
}
if (window.CURRENT_HIGHLIGHTS) {
window.HighlightBridgeHelper.restoreHighlights(window.CURRENT_HIGHLIGHTS);
}

View file

@ -205,6 +205,9 @@ class FolderSyncWorker(
val file = fileQueue.removeAt(0)
if (file.isDirectory) {
if (file.name?.startsWith(".") == true) {
continue
}
file.listFiles().let { fileQueue.addAll(it) }
} else if (file.isFile) {
val name = file.name ?: ""

View file

@ -630,21 +630,15 @@ fun RecentFileCard(
.fallback(placeholder).crossfade(true).build(),
contentDescription = item.displayName,
contentScale = ContentScale.Crop,
modifier = Modifier
.height(160.dp)
.fillMaxWidth(),
modifier = Modifier.height(160.dp).fillMaxWidth(),
)
if (item.sourceFolderUri != null) {
Box(
modifier = Modifier
.align(Alignment.TopEnd)
.padding(8.dp)
.background(
modifier = Modifier.align(Alignment.TopEnd).padding(8.dp).background(
color = MaterialTheme.colorScheme.secondaryContainer,
shape = CircleShape
)
.padding(4.dp)
).padding(4.dp)
) {
Icon(
imageVector = Icons.Default.Folder,
@ -658,14 +652,10 @@ fun RecentFileCard(
val isOpdsStream = item.uriString?.startsWith("opds-pse://") == true
if (isOpdsStream) {
Box(
modifier = Modifier
.align(Alignment.TopEnd)
.padding(8.dp)
.background(
modifier = Modifier.align(Alignment.TopEnd).padding(8.dp).background(
color = MaterialTheme.colorScheme.tertiaryContainer,
shape = CircleShape
)
.padding(4.dp)
).padding(4.dp)
) {
Icon(
imageVector = Icons.Default.Cloud,
@ -678,14 +668,10 @@ fun RecentFileCard(
if (isPinned) {
Box(
modifier = Modifier
.align(Alignment.TopStart)
.padding(8.dp)
.background(
modifier = Modifier.align(Alignment.TopStart).padding(8.dp).background(
color = MaterialTheme.colorScheme.primaryContainer,
shape = CircleShape
)
.padding(4.dp)
).padding(4.dp)
) {
Icon(
imageVector = Icons.Default.PushPin,
@ -715,6 +701,11 @@ fun RecentFileCard(
}
}
}
Box(
modifier = Modifier.align(Alignment.BottomEnd).padding(8.dp)
) {
FileTypeBadge(type = item.type, overlay = true)
}
}
Column(

View file

@ -1404,8 +1404,19 @@ private fun LibraryListItem(
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
item.progressPercentage?.let {
Spacer(modifier = Modifier.height(8.dp))
Row(verticalAlignment = Alignment.CenterVertically) {
FileTypeBadge(type = item.type, overlay = false)
item.progressPercentage?.let {
Spacer(modifier = Modifier.width(8.dp))
Text(
text = "",
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
Spacer(modifier = Modifier.width(8.dp))
Text(
text = "${it.toInt()}% complete",
style = MaterialTheme.typography.bodySmall,
@ -1416,6 +1427,7 @@ private fun LibraryListItem(
}
}
}
}
@Composable
private fun RenameShelfDialog(
@ -1743,6 +1755,7 @@ private fun FolderCard(
}
}
@OptIn(androidx.compose.foundation.layout.ExperimentalLayoutApi::class)
@Composable
private fun EditFolderFiltersDialog(
folder: SyncedFolder,
@ -1753,44 +1766,74 @@ private fun EditFolderFiltersDialog(
AlertDialog(
onDismissRequest = onDismiss,
title = { Text(stringResource(R.string.filter_file_types)) },
text = {
title = {
Column {
Text(
stringResource(R.string.filter_file_types_desc),
style = MaterialTheme.typography.bodyMedium
text = stringResource(R.string.filter_file_types),
style = MaterialTheme.typography.headlineSmall,
fontWeight = FontWeight.Bold
)
Text(
text = stringResource(R.string.filter_file_types_desc),
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
Spacer(modifier = Modifier.height(8.dp))
FileType.entries.forEach { type ->
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier
.fillMaxWidth()
.clickable {
selectedTypes = if (type in selectedTypes) selectedTypes - type else selectedTypes + type
}
.padding(vertical = 4.dp)
},
text = {
Column(modifier = Modifier.fillMaxWidth()) {
HorizontalDivider(modifier = Modifier.padding(bottom = 16.dp))
androidx.compose.foundation.layout.FlowRow(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
androidx.compose.material3.Checkbox(
checked = type in selectedTypes,
onCheckedChange = { checked ->
selectedTypes = if (checked) selectedTypes + type else selectedTypes - type
FileType.entries.forEach { type ->
val isSelected = type in selectedTypes
FilterChip(
selected = isSelected,
onClick = {
selectedTypes = if (isSelected) {
selectedTypes - type
} else {
selectedTypes + type
}
},
label = {
Text(
text = type.name,
style = MaterialTheme.typography.labelLarge
)
},
leadingIcon = if (isSelected) {
{
Icon(
imageVector = Icons.Default.Check,
contentDescription = null,
modifier = Modifier.size(16.dp)
)
}
} else null,
shape = MaterialTheme.shapes.medium
)
Spacer(modifier = Modifier.width(8.dp))
Text(type.name)
}
}
}
},
confirmButton = {
TextButton(
androidx.compose.material3.Button(
onClick = { onConfirm(selectedTypes) },
enabled = selectedTypes.isNotEmpty()
) { Text(stringResource(R.string.action_save)) }
enabled = selectedTypes.isNotEmpty(),
shape = MaterialTheme.shapes.medium
) {
Text(stringResource(R.string.action_save))
}
},
dismissButton = {
TextButton(onClick = onDismiss) { Text(stringResource(R.string.action_cancel)) }
TextButton(onClick = onDismiss) {
Text(stringResource(R.string.action_cancel))
}
}
)
}

View file

@ -765,7 +765,7 @@ open class MainViewModel(application: Application) : AndroidViewModel(applicatio
remoteConfigRepository.init()
if (_internalState.value.syncedFolders.isNotEmpty()) {
syncFolderMetadata()
triggerFolderSyncWorker(metadataOnly = false, showFeedback = false)
}
sweepOrphanedCache()

View file

@ -21,12 +21,13 @@ class Fb2Parser(private val context: Context) {
bookId: String,
originalBookNameHint: String,
parseContent: Boolean = true
): EpubBook {
): EpubBook = withContext(Dispatchers.IO) {
val extractionDir = File(context.cacheDir, "imported_file_$bookId").apply {
if (!exists()) mkdirs()
}
var streamToParse = inputStream
try {
if (originalBookNameHint.endsWith(".zip", ignoreCase = true)) {
val zis = ZipInputStream(inputStream)
var entry = zis.nextEntry
@ -56,12 +57,11 @@ class Fb2Parser(private val context: Context) {
val images = mutableListOf<EpubImage>() // Keep track of extracted images
var currentChapterHtml = StringBuilder()
var currentChapterTitle = "Chapter"
var currentChapterTitle = "Chapter 1"
var chapterCount = 0
var inSection = false
var inBody = false
var inTitle = false
var skipElement = false
val titleBuilder = java.lang.StringBuilder() // Buffer to handle <p> tags inside <title>
val cssStyle = """
@ -71,6 +71,9 @@ class Fb2Parser(private val context: Context) {
.empty-line { height: 1.5em; }
img { max-width: 100%; height: auto; display: block; margin: 1em auto; }
.epigraph { margin-left: 2em; font-style: italic; margin-bottom: 1.5em; }
.cite { border-left: 4px solid currentColor; padding-left: 1em; margin-left: 0; opacity: 0.8; font-style: italic; }
.poem { margin: 1.5em 0; padding-left: 2em; }
.stanza { margin-bottom: 1em; }
""".trimIndent()
fun saveChapter() {
@ -128,15 +131,10 @@ class Fb2Parser(private val context: Context) {
}
}
"body" -> {
val nameAttr = parser.getAttributeValue(null, "name")
if (nameAttr == "notes" || nameAttr == "comments") {
skipElement = true
} else {
inBody = true
}
}
"section" -> {
if (inBody && !skipElement) {
if (inBody) {
if (currentChapterHtml.isNotBlank()) {
saveChapter()
}
@ -150,16 +148,52 @@ class Fb2Parser(private val context: Context) {
}
currentChapterHtml.append("<h2>")
}
"p" -> if (!inTitle) currentChapterHtml.append("<p>")
"v" -> if (!inTitle) currentChapterHtml.append("<p style='text-indent: 0;'>")
"p" -> {
if (!inTitle) {
currentChapterHtml.append("<p>")
} else if (titleBuilder.isNotEmpty()) {
titleBuilder.append(" ")
currentChapterHtml.append("<br>")
}
}
"v" -> {
if (!inTitle) {
currentChapterHtml.append("<p style='text-indent: 0; text-align: left;'>")
} else if (titleBuilder.isNotEmpty()) {
titleBuilder.append(" ")
currentChapterHtml.append("<br>")
}
}
"subtitle" -> currentChapterHtml.append("<h3>")
"empty-line" -> currentChapterHtml.append("<div class='empty-line'></div>")
"empty-line" -> {
if (!inTitle) {
currentChapterHtml.append("<div class='empty-line'></div>")
} else if (titleBuilder.isNotEmpty()) {
titleBuilder.append(" ")
currentChapterHtml.append("<br>")
}
}
"strong" -> currentChapterHtml.append("<b>")
"emphasis" -> currentChapterHtml.append("<i>")
"strikethrough" -> currentChapterHtml.append("<s>")
"sup" -> currentChapterHtml.append("<sup>")
"sub" -> currentChapterHtml.append("<sub>")
"epigraph" -> currentChapterHtml.append("<div class='epigraph'>")
"cite" -> currentChapterHtml.append("<blockquote class='cite'>")
"poem" -> currentChapterHtml.append("<div class='poem'>")
"stanza" -> currentChapterHtml.append("<div class='stanza'>")
"a" -> {
val href = parser.getAttributeValue(null, "l:href")
?: parser.getAttributeValue(null, "xlink:href")
?: parser.getAttributeValue("http://www.w3.org/1999/xlink", "href")
if (!inTitle) {
if (href != null) {
currentChapterHtml.append("<a href=\"$href\">")
} else {
currentChapterHtml.append("<a>")
}
}
}
"image" -> {
// Safely extract href checking all possible namespace stripped versions
val href = parser.getAttributeValue(null, "l:href")
@ -170,7 +204,7 @@ class Fb2Parser(private val context: Context) {
if (href != null) {
val id = href.removePrefix("#")
if (!inBody) {
coverImageId = id
if (coverImageId == null) coverImageId = id
} else {
currentChapterHtml.append("<img src=\"$id\" />")
}
@ -184,10 +218,8 @@ class Fb2Parser(private val context: Context) {
val bytes = Base64.decode(base64Data, Base64.DEFAULT)
if (parseContent) {
val imgFile = File(extractionDir, id)
withContext(Dispatchers.IO) {
FileOutputStream(imgFile).use { it.write(bytes) }
}
}
images.add(EpubImage(absPath = id))
@ -208,7 +240,7 @@ class Fb2Parser(private val context: Context) {
if (inTitle) {
titleBuilder.append(text) // Append to buffer since it could be split by <p> tags
currentChapterHtml.append(text)
} else if (inBody && !skipElement) {
} else if (inBody) {
currentChapterHtml.append(text)
}
}
@ -217,12 +249,14 @@ class Fb2Parser(private val context: Context) {
val name = parser.name.lowercase()
when (name) {
"body" -> {
skipElement = false
inBody = false
}
"title" -> {
if (inTitle) {
currentChapterTitle = titleBuilder.toString().trim()
currentChapterTitle = titleBuilder.toString().replace("\\s+".toRegex(), " ").trim()
if (currentChapterTitle.isBlank()) {
currentChapterTitle = "Chapter ${chapterCount + 1}"
}
inTitle = false
}
currentChapterHtml.append("</h2>\n")
@ -235,12 +269,13 @@ class Fb2Parser(private val context: Context) {
"sup" -> currentChapterHtml.append("</sup>")
"sub" -> currentChapterHtml.append("</sub>")
"epigraph" -> currentChapterHtml.append("</div>\n")
"cite" -> currentChapterHtml.append("</blockquote>\n")
"poem", "stanza" -> currentChapterHtml.append("</div>\n")
"a" -> if (!inTitle) currentChapterHtml.append("</a>")
}
}
}
// Calling nextText() moves the parser directly to END_TAG.
// We ensure we don't accidentally read past the EOF.
if (eventType != XmlPullParser.END_DOCUMENT) {
eventType = parser.next()
}
@ -265,7 +300,7 @@ class Fb2Parser(private val context: Context) {
}
}
return EpubBook(
return@withContext EpubBook(
fileName = originalBookNameHint,
title = title,
author = author,
@ -273,11 +308,18 @@ class Fb2Parser(private val context: Context) {
coverImage = coverBitmap,
chapters = chapters,
chaptersForPagination = chapters,
images = images, // Extracted images attached!
images = images,
pageList = emptyList(),
tableOfContents = emptyList(),
extractionBasePath = extractionDir.absolutePath,
css = emptyMap()
)
} finally {
try {
streamToParse.close()
} catch (e: Exception) {
Timber.e(e, "Error closing FB2 stream")
}
}
}
}

View file

@ -306,9 +306,9 @@ class SingleFileImporter(private val context: Context) {
.replace(">", "&gt;")
}
val reader = inputStream.bufferedReader()
var inParagraph = false
inputStream.bufferedReader().use { reader ->
while (true) {
val line = reader.readLine()
if (line == null) {
@ -344,6 +344,7 @@ class SingleFileImporter(private val context: Context) {
}
}
}
}
flushChapter()
@ -590,9 +591,10 @@ class SingleFileImporter(private val context: Context) {
val parseStart = System.currentTimeMillis()
Timber.tag("FileOpenPerf").d("[DOCX] parseDocx START | file=$originalBookNameHint")
val htmlContent = inputStream.use { stream ->
val converter = DocumentConverter()
val result = converter.convertToHtml(inputStream)
val htmlContent = result.value ?: ""
converter.convertToHtml(stream).value ?: ""
}
Timber.tag("FileOpenPerf").d("[DOCX] parseDocx: mammoth conversion done | elapsed=${System.currentTimeMillis() - parseStart}ms")

View file

@ -560,6 +560,11 @@ fun ChapterWebView(
)
}
message.startsWith("PosSaveDiag:") -> {
Timber.tag("PosSaveDiag")
.d("JS -> ${message.substringAfter("PosSaveDiag: ")}")
}
message.startsWith("HIGHLIGHT_DEBUG:") -> {
Timber.d(
"JS -> ${message.substringAfter("HIGHLIGHT_DEBUG: ")}"

View file

@ -483,7 +483,7 @@ fun EpubReaderHost(
var showJustifyWarningDialog by remember { mutableStateOf(false) }
var isNavigatingByToc by remember { mutableStateOf(false) }
var chunkTargetOverride by remember { mutableStateOf<Int?>(null) }
var chunkTargetOverride by remember { mutableStateOf(initialLocator?.let { it.blockIndex / 20 }) }
val snackbarHostState = remember { SnackbarHostState() }
@ -957,6 +957,8 @@ fun EpubReaderHost(
skipChapterRequest = false
if (ttsShouldStartOnChapterLoad && currentChapterIndex < chapters.size - 1) {
Timber.d("Executing skip chapter request for continuous TTS.")
currentScrollYPosition = 0
currentScrollHeightValue = 0
currentChapterIndex++
} else {
ttsShouldStartOnChapterLoad = false
@ -1212,6 +1214,7 @@ fun EpubReaderHost(
initialScrollTargetForChapter = ChapterScrollPosition.START
cfiToLoad = null
currentScrollYPosition = 0
currentScrollHeightValue = 0
currentChapterIndex = nextIndex
},
onToggleTtsStartOnLoad = { shouldStart -> ttsShouldStartOnChapterLoad = shouldStart },
@ -1362,7 +1365,17 @@ fun EpubReaderHost(
chapterHead = result.head
chapterChunks = result.chunks
isChapterParsing = false
if (initialScrollTargetForChapter == ChapterScrollPosition.END) {
loadUpToChunkIndex = max(0, result.chunks.size - 1)
loadedChunkCount = result.chunks.size
topVisibleChunkIndex = loadUpToChunkIndex
} else {
loadUpToChunkIndex = result.startChunkIndex
loadedChunkCount = min(result.chunks.size, result.startChunkIndex + 2)
topVisibleChunkIndex = 0
}
Timber.tag("ReflowPaginationDiag").d("EpubReaderScreen: loadChapterContent finished. chapterChunks.size=${chapterChunks.size}, isChapterParsing=$isChapterParsing")
if (chunkTargetOverride != null) {
@ -1371,9 +1384,6 @@ fun EpubReaderHost(
if (isInitialCfiLoad) {
isInitialCfiLoad = false
}
loadedChunkCount = 1
topVisibleChunkIndex = 0
}
EpubReaderSystemUiController(
@ -1604,6 +1614,8 @@ fun EpubReaderHost(
coroutineScope = scope,
onVerticalChapterChange = { chapterIdx, chunkIdx, result ->
initialScrollTargetForChapter = ChapterScrollPosition.START
currentScrollYPosition = 0
currentScrollHeightValue = 0
currentChapterIndex = chapterIdx
searchHighlightTarget = result
loadUpToChunkIndex = chunkIdx
@ -1674,6 +1686,7 @@ fun EpubReaderHost(
if (targetChapterIndex != currentChapterIndex) {
initialScrollTargetForChapter = null
currentScrollYPosition = 0
currentScrollHeightValue = 0
currentChapterIndex = targetChapterIndex
} else {
if (entry.fragmentId != null) {
@ -1718,6 +1731,7 @@ fun EpubReaderHost(
if (index != currentChapterIndex) {
initialScrollTargetForChapter = ChapterScrollPosition.START
currentScrollYPosition = 0
currentScrollHeightValue = 0
currentChapterIndex = index
pullToNextProgress = 0f
pullToPrevProgress = 0f
@ -1773,6 +1787,8 @@ fun EpubReaderHost(
} else {
0
}
currentScrollYPosition = 0
currentScrollHeightValue = 0
currentChapterIndex = bookmark.chapterIndex
}
else {
@ -1876,6 +1892,8 @@ fun EpubReaderHost(
if (highlight.chapterIndex != currentChapterIndex) {
chunkTargetOverride = if (targetChunk != null && targetChunk >= 0) targetChunk else 0
currentScrollYPosition = 0
currentScrollHeightValue = 0
currentChapterIndex = highlight.chapterIndex
} else {
if (targetChunk != null && targetChunk >= 0) {
@ -2069,7 +2087,8 @@ fun EpubReaderHost(
onNavigateChapter = { offset, target ->
scope.launch {
initialScrollTargetForChapter = target
if (target == ChapterScrollPosition.START) currentScrollYPosition = 0
currentScrollYPosition = 0
currentScrollHeightValue = 0
currentChapterIndex += offset
}
},
@ -2147,10 +2166,10 @@ fun EpubReaderHost(
CircularProgressIndicator()
}
} else if (chapterChunks.isNotEmpty()) {
val initialContentToLoad =
remember(loadUpToChunkIndex, chapterChunks) {
val startIdx = maxOf(0, loadUpToChunkIndex - 1)
val endIdx = minOf(chapterChunks.lastIndex, loadUpToChunkIndex + 1)
val initialContentToLoad = remember(loadUpToChunkIndex, chapterChunks) {
val targetIdx = loadUpToChunkIndex
val startIdx = maxOf(0, targetIdx - 1)
val endIdx = minOf(chapterChunks.lastIndex, targetIdx + 1)
chapterChunks.indices.joinToString(separator = "\n") { index ->
if (index in startIdx..endIdx) {
@ -2364,6 +2383,7 @@ fun EpubReaderHost(
Timber.d("Screen: Moving to next chapter (${currentChapterIndex + 1}).")
initialScrollTargetForChapter = ChapterScrollPosition.START
currentScrollYPosition = 0
currentScrollHeightValue = 0
currentChapterIndex++
isAutoScrollPlaying = true
} else {
@ -2384,6 +2404,8 @@ fun EpubReaderHost(
scope.launch {
delay(20)
initialScrollTargetForChapter = ChapterScrollPosition.END
currentScrollYPosition = 0
currentScrollHeightValue = 0
currentChapterIndex--
if (showBars) showBars = false
delay(300)
@ -2405,6 +2427,7 @@ fun EpubReaderHost(
delay(20)
initialScrollTargetForChapter = ChapterScrollPosition.START
currentScrollYPosition = 0
currentScrollHeightValue = 0
currentChapterIndex++
if (showBars) showBars = false
delay(300)
@ -2423,12 +2446,12 @@ fun EpubReaderHost(
)
scope.launch {
delay(50)
initialScrollTargetForChapter =
ChapterScrollPosition.END
initialScrollTargetForChapter = ChapterScrollPosition.END
currentScrollYPosition = 0
currentScrollHeightValue = 0
currentChapterIndex--
if (showBars) showBars = false
Timber.d("Changed to previous chapter: $currentChapterIndex, will scroll to END"
)
Timber.d("Changed to previous chapter: $currentChapterIndex, will scroll to END")
}
}
pullToPrevProgress = 0f
@ -2443,9 +2466,9 @@ fun EpubReaderHost(
)
scope.launch {
delay(50)
initialScrollTargetForChapter =
ChapterScrollPosition.START
initialScrollTargetForChapter = ChapterScrollPosition.START
currentScrollYPosition = 0
currentScrollHeightValue = 0
currentChapterIndex++
if (showBars) showBars = false
}
@ -2604,7 +2627,7 @@ fun EpubReaderHost(
showDictionaryUpsellDialog = true
},
onCfiGenerated = { cfi ->
Timber.tag("POS_DIAG").d("JS generated CFI: '$cfi'")
Timber.tag("PosSaveDiag").d("JS generated CFI string: '$cfi'")
if (cfi.isBlank() || !cfi.startsWith('/')) {
if (isSavingAndExiting) {
@ -2623,6 +2646,7 @@ fun EpubReaderHost(
)
if (locator != null) {
Timber.tag("PosSaveDiag").d("✅ Converted CFI to Locator successfully: chapter=${locator.chapterIndex}, block=${locator.blockIndex}, charOffset=${locator.charOffset}")
lastKnownLocator = locator
val progressWithinChapter =
@ -3131,8 +3155,11 @@ fun EpubReaderHost(
val chapterTitle =
chapters.getOrNull(currentChapterIndex)?.title?.take(30)?.trim()
?: "Chapter"
val displayPageInfo = if (currentScrollHeightValue <= 0 || isChapterParsing) "" else " ($currentPageInChapter/$totalPagesInCurrentChapter)"
Text(
text = "$chapterTitle ($currentPageInChapter/$totalPagesInCurrentChapter)",
text = "$chapterTitle$displayPageInfo",
style = MaterialTheme.typography.bodySmall,
color = effectiveText.copy(alpha = 0.8f),
textAlign = TextAlign.Center,
@ -3143,7 +3170,7 @@ fun EpubReaderHost(
.padding(horizontal = 48.dp)
)
if (totalBookLengthChars > 0) {
if (totalBookLengthChars > 0 && currentScrollHeightValue > 0 && !isChapterParsing) {
Text(
text = "%.1f%%".format(currentBookProgress),
style = MaterialTheme.typography.bodySmall,
@ -3484,10 +3511,14 @@ fun EpubReaderHost(
val targetChunk = locator.blockIndex / 20
chunkTargetOverride = targetChunk
if (currentChapterIndex != locator.chapterIndex) {
currentScrollYPosition = 0
currentScrollHeightValue = 0
currentChapterIndex = locator.chapterIndex
}
cfiToLoad = cfi
} else {
currentScrollYPosition = 0
currentScrollHeightValue = 0
currentChapterIndex = locator.chapterIndex
cfiToLoad = null
}

View file

@ -167,12 +167,14 @@ class LocatorConverter(
val bestMatch = findBestMatchingBlock(allBlocks, baseCfiPath)
if (bestMatch != null) {
Timber.tag("PosSaveDiag").d("Found best match for baseCfiPath $baseCfiPath -> blockIndex=${bestMatch.blockIndex}, actualBlockCfi=${bestMatch.cfi}")
Locator(
chapterIndex = chapterIndex,
blockIndex = bestMatch.blockIndex,
charOffset = charOffset
)
} else {
Timber.tag("PosSaveDiag").e("No semantic block match found for baseCfiPath $baseCfiPath inside ${allBlocks.size} parsed blocks")
null
}
}

View file

@ -432,6 +432,7 @@ internal fun PdfPageComposable(
draggingBoxId: String? = null,
isScrollLocked: Boolean = false,
isVisible: Boolean = true,
isActivePage: Boolean = true,
isStylusOnlyMode: Boolean = false,
isHighlighterSnapEnabled: Boolean = false,
userHighlights: List<PdfUserHighlight> = emptyList(),
@ -442,7 +443,9 @@ internal fun PdfPageComposable(
onTts: (Int, Int) -> Unit = { _, _ -> },
activeToolThickness: Float = 0f,
customHighlightColors: Map<PdfHighlightColor, Color> = emptyMap(),
onPaletteClick: (() -> Unit)? = null
onPaletteClick: (() -> Unit)? = null,
lockedState: Triple<Float, Float, Float>? = null,
onZoomAndPanChanged: ((Float, Offset) -> Unit)? = null
) {
val pdfDocumentItem = pdfDocument.item
var bitmapState by remember { mutableStateOf(PdfThumbnailCache.get(pageIndex)) }
@ -474,6 +477,10 @@ internal fun PdfPageComposable(
var scale by remember { mutableFloatStateOf(1f) }
var offset by remember { mutableStateOf(Offset.Zero) }
LaunchedEffect(scale, offset) {
onZoomAndPanChanged?.invoke(scale, offset)
}
val currentOnSingleTap by rememberUpdatedState(onSingleTap)
val currentOnDoubleTap by rememberUpdatedState(onDoubleTap)
@ -720,12 +727,6 @@ internal fun PdfPageComposable(
}
}
LaunchedEffect(pageIndex) {
scale = 1f
offset = Offset.Zero
onScaleChanged(1f)
}
LaunchedEffect(isPerformingOcrForSelection) { onOcrStateChange(isPerformingOcrForSelection) }
LaunchedEffect(
@ -1067,9 +1068,10 @@ internal fun PdfPageComposable(
canvasHeightPx.floatValue,
isVerticalScroll,
isScrolling,
virtualPage
virtualPage,
isActivePage
) {
val needsTiling = effectiveScale > 1f || actualBitmapWidthPx > 3000 || actualBitmapHeightPx > 3000
val needsTiling = (effectiveScale > 1f || actualBitmapWidthPx > 3000 || actualBitmapHeightPx > 3000) && (isVerticalScroll || isActivePage)
if (!needsTiling) {
if (tiles.isNotEmpty()) {
val oldTiles = tiles
@ -2594,7 +2596,7 @@ internal fun PdfPageComposable(
}
}
}, onDoubleTap = { tapOffset ->
if (isZoomEnabled && !isVerticalScroll) {
if (isZoomEnabled && !isVerticalScroll && !isScrollLocked) {
if (actualBitmapWidthPx == 0) return@detectTapGestures
coroutineScope.launch {
val startScale = scale
@ -2689,7 +2691,11 @@ internal fun PdfPageComposable(
if (!canceled) {
val rawPanChange = event.calculatePan()
val panChange = if (isScrollLocked) Offset(0f, rawPanChange.y) else rawPanChange
val panChange = if (isScrollLocked && pointerCount == 1) {
if (isVerticalScroll) Offset(0f, rawPanChange.y) else Offset.Zero
} else {
rawPanChange
}
val zoomChange = event.calculateZoom()
if (scale > 1f) {
@ -3112,14 +3118,21 @@ internal fun PdfPageComposable(
}
LaunchedEffect(
this@BoxWithConstraints.maxWidth, this@BoxWithConstraints.maxHeight
pageIndex, this@BoxWithConstraints.maxWidth, this@BoxWithConstraints.maxHeight,
isScrollLocked, lockedState
) {
if (isScrollLocked && !isVerticalScroll && lockedState != null) {
scale = lockedState.first
offset = Offset(lockedState.second, lockedState.third)
onScaleChanged(scale)
} else if (!isScrollLocked && !isVerticalScroll) {
scale = 1f
offset = Offset.Zero
onScaleChanged(1f)
}
Timber.d(
"PdfPageComposable Page $pageIndex | Constraints: maxWidth=${this@BoxWithConstraints.maxWidth}, maxHeight=${this@BoxWithConstraints.maxHeight}"
"PdfPageComposable Page $pageIndex initialized/resized/locked. scale=$scale, offset=$offset"
)
}

View file

@ -234,7 +234,9 @@ internal fun PdfVerticalReader(
onTts: (Int, Int) -> Unit = { _, _ -> },
activeToolThickness: Float = 0f,
customHighlightColors: Map<PdfHighlightColor, Color> = emptyMap(),
onPaletteClick: () -> Unit = {}
onPaletteClick: () -> Unit = {},
lockedState: Triple<Float, Float, Float>? = null,
onZoomAndPanChanged: ((Float, Offset) -> Unit)? = null
) {
SideEffect { Timber.tag("PdfDrawPerf").v("LIST: PdfVerticalReader Recomposing.") }
DisposableEffect(state) {
@ -336,6 +338,10 @@ internal fun PdfVerticalReader(
val panXAnimatable = remember { Animatable(if ((screenWidth * fitZoom) < screenWidth) (screenWidth - (screenWidth * fitZoom)) / 2f else 0f) }
val panYAnimatable = remember { Animatable(0f) }
LaunchedEffect(zoomAnimatable.value, panXAnimatable.value, panYAnimatable.value) {
onZoomAndPanChanged?.invoke(zoomAnimatable.value, Offset(panXAnimatable.value, panYAnimatable.value))
}
var isResizing by remember { mutableStateOf(false) }
var previousScreenWidth by remember { mutableFloatStateOf(0f) }
var previousScreenHeight by remember { mutableFloatStateOf(0f) }
@ -401,6 +407,12 @@ internal fun PdfVerticalReader(
delay(50)
isResizing = false
targetPageDuringResize.intValue = -1
} else if (isScrollLocked && lockedState != null) {
val (savedScale, savedPanX, _) = lockedState
coroutineScope {
launch { zoomAnimatable.snapTo(savedScale) }
launch { panXAnimatable.snapTo(savedPanX) }
}
}
isInitialLayout = false
}
@ -768,6 +780,7 @@ internal fun PdfVerticalReader(
}
val onDoubleTapToZoom: (Offset) -> Unit = { tapScreenOffset ->
if (!isScrollLocked) {
val currentZoom = zoomAnimatable.value
val targetZoom = when {
@ -827,6 +840,7 @@ internal fun PdfVerticalReader(
panYAnimatable.updateBounds(lowerBound = minScrollY, upperBound = headerHeightPx)
}
}
}
val globalDrawingModifier = Modifier.pointerInput(
isEditMode,
@ -1042,7 +1056,7 @@ internal fun PdfVerticalReader(
val zoomChange = event.calculateZoom()
val rawPanChange = event.calculatePan()
val panChange = if (isScrollLocked) Offset(0f, rawPanChange.y) else rawPanChange
val panChange = if (isScrollLocked && !isMultiTouch) Offset(0f, rawPanChange.y) else rawPanChange
val centroid = event.calculateCentroid(useCurrent = false)
val panMagnitude = panChange.getDistance()

View file

@ -583,6 +583,25 @@ private fun getSuggestedFilename(originalName: String?, isAnnotated: Boolean): S
return "${safeBase}${suffix}_${shortId}.pdf"
}
private fun savePdfLockedState(context: Context, bookId: String, scale: Float, offsetX: Float, offsetY: Float) {
val prefs = context.getSharedPreferences(SETTINGS_PREFS_NAME, Context.MODE_PRIVATE)
prefs.edit {
putFloat("pdf_locked_scale_$bookId", scale)
putFloat("pdf_locked_offset_x_$bookId", offsetX)
putFloat("pdf_locked_offset_y_$bookId", offsetY)
}
}
private fun loadPdfLockedState(context: Context, bookId: String): Triple<Float, Float, Float>? {
val prefs = context.getSharedPreferences(SETTINGS_PREFS_NAME, Context.MODE_PRIVATE)
if (!prefs.contains("pdf_locked_scale_$bookId")) return null
return Triple(
prefs.getFloat("pdf_locked_scale_$bookId", 1f),
prefs.getFloat("pdf_locked_offset_x_$bookId", 0f),
prefs.getFloat("pdf_locked_offset_y_$bookId", 0f)
)
}
private enum class SaveMode {
ORIGINAL, ANNOTATED
}
@ -1181,6 +1200,9 @@ fun PdfViewerScreen(
var documentPassword by rememberSaveable { mutableStateOf<String?>(null) }
var pendingRestorePage by rememberSaveable { mutableStateOf(initialPage) }
var isScrollLocked by remember { mutableStateOf(false) }
var lockedState by remember { mutableStateOf<Triple<Float, Float, Float>?>(null) }
var currentActiveScale by remember { mutableFloatStateOf(1f) }
var currentActiveOffset by remember { mutableStateOf(Offset.Zero) }
var showPasswordDialog by remember { mutableStateOf(false) }
var isPasswordError by remember { mutableStateOf(false) }
LocalView.current
@ -1239,6 +1261,7 @@ fun PdfViewerScreen(
LaunchedEffect(bookId) {
isScrollLocked = loadPdfScrollLocked(context, bookId)
isFullScreen = loadPdfFullScreen(context, bookId)
lockedState = loadPdfLockedState(context, bookId)
}
var isAutoScrollModeActive by remember { mutableStateOf(false) }
@ -1504,6 +1527,14 @@ fun PdfViewerScreen(
LaunchedEffect(displayMode) { saveDisplayMode(context, displayMode) }
LaunchedEffect(currentActiveScale, currentActiveOffset, isScrollLocked) {
if (isScrollLocked) {
delay(500)
lockedState = Triple(currentActiveScale, currentActiveOffset.x, currentActiveOffset.y)
savePdfLockedState(context, bookId, currentActiveScale, currentActiveOffset.x, currentActiveOffset.y)
}
}
val annotationSettingsRepo = remember(context) { AnnotationSettingsRepository(context) }
val toolSettings by annotationSettingsRepo.settings.collectAsState()
var showToolSettings by rememberSaveable { mutableStateOf(false) }
@ -4367,7 +4398,7 @@ fun PdfViewerScreen(
key = { it },
beyondViewportPageCount = dynamicBeyondViewportPageCount,
userScrollEnabled = run {
val enabled = currentPageScale == 1f && !(ttsState.isPlaying || ttsState.isLoading || searchState.isSearchActive) && !isPageSliderVisible && paginationDraggingBoxId == null
val enabled = (currentPageScale == 1f || (isScrollLocked && displayMode == DisplayMode.PAGINATION)) && !(ttsState.isPlaying || ttsState.isLoading || searchState.isSearchActive) && !isPageSliderVisible && paginationDraggingBoxId == null
SideEffect {
Timber.tag("PdfZoomDebug").v("Pager Scroll Enabled: $enabled (Scale: $currentPageScale, Playing: ${ttsState.isPlaying}, Slider: $isPageSliderVisible, DraggingBox: $paginationDraggingBoxId)")
}
@ -4619,6 +4650,13 @@ fun PdfViewerScreen(
onNoteRequested = onNoteRequested,
onTts = { pageIdx, charIdx -> startTtsWithPermissionCheck(pageIdx, charIdx) },
activeToolThickness = currentStrokeWidthState,
lockedState = lockedState,
onZoomAndPanChanged = { newScale, newOffset ->
if (pagerState.currentPage == pageIndex) {
currentActiveScale = newScale
currentActiveOffset = newOffset
}
},
onTwoFingerSwipe = { direction ->
coroutineScope.launch {
val targetPage =
@ -4774,6 +4812,8 @@ fun PdfViewerScreen(
},
onDragPageTurn = { /* Handled in onTextBoxDrag */ },
isVisible = isVisiblePage,
isActivePage = pagerState.currentPage == pageIndex,
isScrolling = pagerState.isScrollInProgress
)
}
@ -5025,7 +5065,12 @@ fun PdfViewerScreen(
isAutoScrollPlaying = isAutoScrollPlaying,
isAutoScrollTempPaused = isAutoScrollTempPaused,
autoScrollSpeed = autoScrollSpeed * 0.5f,
onInteractionListener = onAutoScrollInteraction
onInteractionListener = onAutoScrollInteraction,
lockedState = lockedState,
onZoomAndPanChanged = { newScale, newOffset ->
currentActiveScale = newScale
currentActiveOffset = newOffset
}
)
}
}
@ -5529,6 +5574,10 @@ fun PdfViewerScreen(
onClick = {
isScrollLocked = !isScrollLocked
savePdfScrollLocked(context, bookId, isScrollLocked)
if (isScrollLocked) {
savePdfLockedState(context, bookId, currentActiveScale, currentActiveOffset.x, currentActiveOffset.y)
lockedState = Triple(currentActiveScale, currentActiveOffset.x, currentActiveOffset.y)
}
}) {
Icon(
imageVector = if (isScrollLocked) Icons.Default.Lock else Icons.Default.LockOpen,