Display and correct calculation of reading progress of a story

This commit is contained in:
Stefan Hardegger
2025-10-31 08:07:12 +01:00
parent 0e1ed7c92e
commit 715fb4e48a
5 changed files with 47 additions and 14 deletions

View File

@@ -620,11 +620,9 @@ public class StoryController {
return 0;
}
// Determine the total content length
// ALWAYS use contentHtml for consistency (frontend uses contentHtml for position tracking)
int totalLength = 0;
if (story.getContentPlain() != null && !story.getContentPlain().isEmpty()) {
totalLength = story.getContentPlain().length();
} else if (story.getContentHtml() != null && !story.getContentHtml().isEmpty()) {
if (story.getContentHtml() != null && !story.getContentHtml().isEmpty()) {
totalLength = story.getContentHtml().length();
}
@@ -633,7 +631,8 @@ public class StoryController {
}
// Calculate percentage and round to nearest integer
return Math.round((float) story.getReadingPosition() * 100 / totalLength);
int percentage = Math.round((float) story.getReadingPosition() * 100 / totalLength);
return Math.min(100, percentage);
}
private StoryReadingDto convertToReadingDto(Story story) {

View File

@@ -18,6 +18,7 @@ public class StorySearchDto {
// Reading status
private Boolean isRead;
private Integer readingPosition;
private Integer readingProgressPercentage; // Pre-calculated percentage (0-100)
private LocalDateTime lastReadAt;
// Author info
@@ -133,6 +134,14 @@ public class StorySearchDto {
this.readingPosition = readingPosition;
}
public Integer getReadingProgressPercentage() {
return readingProgressPercentage;
}
public void setReadingProgressPercentage(Integer readingProgressPercentage) {
this.readingProgressPercentage = readingProgressPercentage;
}
public UUID getAuthorId() {
return authorId;
}

View File

@@ -347,6 +347,7 @@ public class SolrService {
doc.addField("volume", story.getVolume());
doc.addField("isRead", story.getIsRead());
doc.addField("readingPosition", story.getReadingPosition());
doc.addField("readingProgressPercentage", calculateReadingProgressPercentage(story));
if (story.getLastReadAt() != null) {
doc.addField("lastReadAt", formatDateTime(story.getLastReadAt()));
@@ -544,6 +545,26 @@ public class SolrService {
return dateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) + "Z";
}
private Integer calculateReadingProgressPercentage(Story story) {
if (story.getReadingPosition() == null || story.getReadingPosition() == 0) {
return 0;
}
// ALWAYS use contentHtml for consistency (frontend uses contentHtml for position tracking)
int totalLength = 0;
if (story.getContentHtml() != null && !story.getContentHtml().isEmpty()) {
totalLength = story.getContentHtml().length();
}
if (totalLength == 0) {
return 0;
}
// Calculate percentage and round to nearest integer
int percentage = Math.round((float) story.getReadingPosition() * 100 / totalLength);
return Math.min(100, percentage);
}
// ===============================
// UTILITY METHODS
// ===============================
@@ -1039,6 +1060,7 @@ public class SolrService {
story.setVolume((Integer) doc.getFieldValue("volume"));
story.setIsRead((Boolean) doc.getFieldValue("isRead"));
story.setReadingPosition((Integer) doc.getFieldValue("readingPosition"));
story.setReadingProgressPercentage((Integer) doc.getFieldValue("readingProgressPercentage"));
// Handle dates
story.setLastReadAt(parseDateTimeFromSolr(doc.getFieldValue("lastReadAt")));

View File

@@ -107,8 +107,8 @@ export default function StoryReadingPage() {
(scrolled - contentTop + windowHeight * 0.3) / contentHeight
));
// Convert to character position in the plain text content
const textLength = story.contentPlain?.length || story.contentHtml?.length || 0;
// Convert to character position in the HTML content (ALWAYS use contentHtml for consistency)
const textLength = story.contentHtml?.length || 0;
return Math.floor(scrollRatio * textLength);
}, [story]);
@@ -116,7 +116,8 @@ export default function StoryReadingPage() {
const calculateReadingPercentage = useCallback((currentPosition: number): number => {
if (!story) return 0;
const totalLength = story.contentPlain?.length || story.contentHtml?.length || 0;
// ALWAYS use contentHtml for consistency with position calculation
const totalLength = story.contentHtml?.length || 0;
if (totalLength === 0) return 0;
return Math.round((currentPosition / totalLength) * 100);
@@ -126,7 +127,8 @@ export default function StoryReadingPage() {
const scrollToCharacterPosition = useCallback((position: number) => {
if (!contentRef.current || !story || hasScrolledToPosition) return;
const textLength = story.contentPlain?.length || story.contentHtml?.length || 0;
// ALWAYS use contentHtml for consistency with position calculation
const textLength = story.contentHtml?.length || 0;
if (textLength === 0 || position === 0) return;
const ratio = position / textLength;

View File

@@ -86,6 +86,7 @@
<!-- Reading Status Fields -->
<field name="isRead" type="boolean" indexed="true" stored="true"/>
<field name="readingPosition" type="pint" indexed="true" stored="true"/>
<field name="readingProgressPercentage" type="pint" indexed="true" stored="true"/>
<field name="lastReadAt" type="pdate" indexed="true" stored="true"/>
<field name="lastRead" type="pdate" indexed="true" stored="true"/>