Add EPUB reader progress tracking and auto-update read status (#1148)
- Track EPUB reading progress (CFI) - Calculate and update read status based on progress percentage - Handle EPUB, PDF, and CBX file types consistently
This commit is contained in:
@@ -297,29 +297,68 @@ public class BookService {
|
||||
|
||||
@Transactional
|
||||
public void updateReadProgress(ReadProgressRequest request) {
|
||||
BookEntity book = bookRepository.findById(request.getBookId()).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(request.getBookId()));
|
||||
BookEntity book = bookRepository.findById(request.getBookId())
|
||||
.orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(request.getBookId()));
|
||||
|
||||
BookLoreUser user = authenticationService.getAuthenticatedUser();
|
||||
UserBookProgressEntity userBookProgress = userBookProgressRepository.findByUserIdAndBookId(user.getId(), book.getId()).orElse(new UserBookProgressEntity());
|
||||
userBookProgress.setUser(userRepository.findById(user.getId()).orElseThrow(() -> new UsernameNotFoundException("User not found")));
|
||||
userBookProgress.setBook(book);
|
||||
userBookProgress.setLastReadTime(Instant.now());
|
||||
if (book.getBookType() == BookFileType.EPUB && request.getEpubProgress() != null) {
|
||||
userBookProgress.setEpubProgress(request.getEpubProgress().getCfi());
|
||||
userBookProgress.setEpubProgressPercent(request.getEpubProgress().getPercentage());
|
||||
} else if (book.getBookType() == BookFileType.PDF && request.getPdfProgress() != null) {
|
||||
userBookProgress.setPdfProgress(request.getPdfProgress().getPage());
|
||||
userBookProgress.setPdfProgressPercent(request.getPdfProgress().getPercentage());
|
||||
} else if (book.getBookType() == BookFileType.CBX && request.getCbxProgress() != null) {
|
||||
userBookProgress.setCbxProgress(request.getCbxProgress().getPage());
|
||||
userBookProgress.setCbxProgressPercent(request.getCbxProgress().getPercentage());
|
||||
|
||||
UserBookProgressEntity progress = userBookProgressRepository
|
||||
.findByUserIdAndBookId(user.getId(), book.getId())
|
||||
.orElseGet(UserBookProgressEntity::new);
|
||||
|
||||
BookLoreUserEntity userEntity = userRepository.findById(user.getId())
|
||||
.orElseThrow(() -> new UsernameNotFoundException("User not found"));
|
||||
progress.setUser(userEntity);
|
||||
|
||||
progress.setBook(book);
|
||||
progress.setLastReadTime(Instant.now());
|
||||
|
||||
Float percentage = null;
|
||||
switch (book.getBookType()) {
|
||||
case EPUB -> {
|
||||
if (request.getEpubProgress() != null) {
|
||||
progress.setEpubProgress(request.getEpubProgress().getCfi());
|
||||
percentage = request.getEpubProgress().getPercentage();
|
||||
}
|
||||
}
|
||||
case PDF -> {
|
||||
if (request.getPdfProgress() != null) {
|
||||
progress.setPdfProgress(request.getPdfProgress().getPage());
|
||||
percentage = request.getPdfProgress().getPercentage();
|
||||
}
|
||||
}
|
||||
case CBX -> {
|
||||
if (request.getCbxProgress() != null) {
|
||||
progress.setCbxProgress(request.getCbxProgress().getPage());
|
||||
percentage = request.getCbxProgress().getPercentage();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (percentage != null) {
|
||||
progress.setReadStatus(getStatus(percentage));
|
||||
setProgressPercent(progress, book.getBookType(), percentage);
|
||||
}
|
||||
|
||||
// Update dateFinished if provided
|
||||
if (request.getDateFinished() != null) {
|
||||
userBookProgress.setDateFinished(request.getDateFinished());
|
||||
progress.setDateFinished(request.getDateFinished());
|
||||
}
|
||||
|
||||
userBookProgressRepository.save(userBookProgress);
|
||||
userBookProgressRepository.save(progress);
|
||||
}
|
||||
|
||||
private void setProgressPercent(UserBookProgressEntity progress, BookFileType type, Float percentage) {
|
||||
switch (type) {
|
||||
case EPUB -> progress.setEpubProgressPercent(percentage);
|
||||
case PDF -> progress.setPdfProgressPercent(percentage);
|
||||
case CBX -> progress.setCbxProgressPercent(percentage);
|
||||
}
|
||||
}
|
||||
|
||||
private ReadStatus getStatus(Float percentage) {
|
||||
if (percentage >= 99.5f) return ReadStatus.READ;
|
||||
if (percentage > 0.5f) return ReadStatus.READING;
|
||||
return ReadStatus.UNREAD;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
|
||||
+10
-3
@@ -7,8 +7,11 @@
|
||||
@if (!isLoading) {
|
||||
<div>
|
||||
<div class="epub-header">
|
||||
<div>
|
||||
<div class="header-left">
|
||||
<p-button size="small" class="menu-toggle-button" (click)="toggleDrawer()" icon="pi pi-bars" severity="secondary"></p-button>
|
||||
<div class="progress-info">
|
||||
<span class="progress-percentage"><span class="progress-label">Progress: </span>{{ progressPercentage }}%</span>
|
||||
</div>
|
||||
<p-drawer [(visible)]="isDrawerVisible" [modal]="true" [position]="'left'" header="Chapters">
|
||||
<ul class="chapter-list">
|
||||
@for (chapter of chapters; track chapter) {
|
||||
@@ -19,8 +22,12 @@
|
||||
</ul>
|
||||
</p-drawer>
|
||||
</div>
|
||||
<p class="text-sm font-medium">{{ currentChapter }}</p>
|
||||
<div>
|
||||
|
||||
<div class="header-center">
|
||||
<p class="text-sm font-medium">{{ currentChapter }}</p>
|
||||
</div>
|
||||
|
||||
<div class="header-right">
|
||||
<div class="flex gap-4">
|
||||
@if (!locationsReady) {
|
||||
<div class="location-indicator" pTooltip="Saving progress not ready yet">
|
||||
|
||||
@@ -5,6 +5,49 @@
|
||||
justify-content: space-between;
|
||||
padding-left: 1rem;
|
||||
padding-right: 1rem;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.header-left {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 1rem;
|
||||
flex: 0 0 auto;
|
||||
}
|
||||
|
||||
.header-center {
|
||||
position: absolute;
|
||||
left: 50%;
|
||||
top: 50%;
|
||||
transform: translate(-50%, -50%);
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
.header-right {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
flex: 0 0 auto;
|
||||
margin-left: auto;
|
||||
}
|
||||
|
||||
.progress-info {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.progress-percentage {
|
||||
font-size: 0.85rem;
|
||||
color: var(--text-color);
|
||||
font-weight: 500;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.progress-label {
|
||||
@media (max-width: 768px) {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
|
||||
.epub-viewer-container {
|
||||
|
||||
@@ -38,6 +38,7 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
|
||||
public locationsReady = false;
|
||||
public approxProgress = 0;
|
||||
public exactProgress = 0;
|
||||
public progressPercentage = 0;
|
||||
|
||||
private book: any;
|
||||
private rendition: any;
|
||||
@@ -309,22 +310,39 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
|
||||
const currentIndex = location.start.index;
|
||||
const totalSpineItems = this.book.spine.items.length;
|
||||
let percentage: number;
|
||||
if (this.locationsReady) {
|
||||
|
||||
if (this.locationsReady && this.book.locations.total > 0) {
|
||||
percentage = this.book.locations.percentageFromCfi(cfi);
|
||||
this.exactProgress = Math.round(percentage * 1000) / 10;
|
||||
this.progressPercentage = Math.round(percentage * 1000) / 10;
|
||||
} else {
|
||||
if (totalSpineItems > 0) {
|
||||
percentage = currentIndex / totalSpineItems;
|
||||
percentage = (currentIndex + 1) / totalSpineItems;
|
||||
} else {
|
||||
percentage = 0;
|
||||
}
|
||||
this.approxProgress = Math.round(percentage * 1000) / 10;
|
||||
this.progressPercentage = Math.round(percentage * 1000) / 10;
|
||||
}
|
||||
|
||||
this.currentChapter = getChapter(this.book, location)?.label;
|
||||
this.bookService.saveEpubProgress(this.epub.id, cfi, Math.round(percentage * 1000) / 10).subscribe();
|
||||
});
|
||||
this.book.ready.then(() => this.book.locations.generate(10000)).then(() => {
|
||||
|
||||
this.book.ready.then(() => {
|
||||
return this.book.locations.generate(1600);
|
||||
}).then(() => {
|
||||
this.locationsReady = true;
|
||||
// Recalculate progress with new locations
|
||||
if (this.rendition.currentLocation()) {
|
||||
const location = this.rendition.currentLocation();
|
||||
const cfi = location.end.cfi;
|
||||
const percentage = this.book.locations.percentageFromCfi(cfi);
|
||||
this.progressPercentage = Math.round(percentage * 1000) / 10;
|
||||
}
|
||||
}).catch(() => {
|
||||
// If location generation fails, keep using spine-based calculation
|
||||
this.locationsReady = false;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user