Display and correct calculation of reading progress of a story
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
@@ -132,7 +133,15 @@ public class StorySearchDto {
|
||||
public void setReadingPosition(Integer readingPosition) {
|
||||
this.readingPosition = readingPosition;
|
||||
}
|
||||
|
||||
|
||||
public Integer getReadingProgressPercentage() {
|
||||
return readingProgressPercentage;
|
||||
}
|
||||
|
||||
public void setReadingProgressPercentage(Integer readingProgressPercentage) {
|
||||
this.readingProgressPercentage = readingProgressPercentage;
|
||||
}
|
||||
|
||||
public UUID getAuthorId() {
|
||||
return authorId;
|
||||
}
|
||||
|
||||
@@ -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")));
|
||||
|
||||
Reference in New Issue
Block a user