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
|
@Transactional
|
||||||
public void updateReadProgress(ReadProgressRequest request) {
|
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();
|
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")));
|
UserBookProgressEntity progress = userBookProgressRepository
|
||||||
userBookProgress.setBook(book);
|
.findByUserIdAndBookId(user.getId(), book.getId())
|
||||||
userBookProgress.setLastReadTime(Instant.now());
|
.orElseGet(UserBookProgressEntity::new);
|
||||||
if (book.getBookType() == BookFileType.EPUB && request.getEpubProgress() != null) {
|
|
||||||
userBookProgress.setEpubProgress(request.getEpubProgress().getCfi());
|
BookLoreUserEntity userEntity = userRepository.findById(user.getId())
|
||||||
userBookProgress.setEpubProgressPercent(request.getEpubProgress().getPercentage());
|
.orElseThrow(() -> new UsernameNotFoundException("User not found"));
|
||||||
} else if (book.getBookType() == BookFileType.PDF && request.getPdfProgress() != null) {
|
progress.setUser(userEntity);
|
||||||
userBookProgress.setPdfProgress(request.getPdfProgress().getPage());
|
|
||||||
userBookProgress.setPdfProgressPercent(request.getPdfProgress().getPercentage());
|
progress.setBook(book);
|
||||||
} else if (book.getBookType() == BookFileType.CBX && request.getCbxProgress() != null) {
|
progress.setLastReadTime(Instant.now());
|
||||||
userBookProgress.setCbxProgress(request.getCbxProgress().getPage());
|
|
||||||
userBookProgress.setCbxProgressPercent(request.getCbxProgress().getPercentage());
|
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) {
|
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
|
@Transactional
|
||||||
|
|||||||
+9
-2
@@ -7,8 +7,11 @@
|
|||||||
@if (!isLoading) {
|
@if (!isLoading) {
|
||||||
<div>
|
<div>
|
||||||
<div class="epub-header">
|
<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>
|
<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">
|
<p-drawer [(visible)]="isDrawerVisible" [modal]="true" [position]="'left'" header="Chapters">
|
||||||
<ul class="chapter-list">
|
<ul class="chapter-list">
|
||||||
@for (chapter of chapters; track chapter) {
|
@for (chapter of chapters; track chapter) {
|
||||||
@@ -19,8 +22,12 @@
|
|||||||
</ul>
|
</ul>
|
||||||
</p-drawer>
|
</p-drawer>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="header-center">
|
||||||
<p class="text-sm font-medium">{{ currentChapter }}</p>
|
<p class="text-sm font-medium">{{ currentChapter }}</p>
|
||||||
<div>
|
</div>
|
||||||
|
|
||||||
|
<div class="header-right">
|
||||||
<div class="flex gap-4">
|
<div class="flex gap-4">
|
||||||
@if (!locationsReady) {
|
@if (!locationsReady) {
|
||||||
<div class="location-indicator" pTooltip="Saving progress not ready yet">
|
<div class="location-indicator" pTooltip="Saving progress not ready yet">
|
||||||
|
|||||||
@@ -5,6 +5,49 @@
|
|||||||
justify-content: space-between;
|
justify-content: space-between;
|
||||||
padding-left: 1rem;
|
padding-left: 1rem;
|
||||||
padding-right: 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 {
|
.epub-viewer-container {
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
|
|||||||
public locationsReady = false;
|
public locationsReady = false;
|
||||||
public approxProgress = 0;
|
public approxProgress = 0;
|
||||||
public exactProgress = 0;
|
public exactProgress = 0;
|
||||||
|
public progressPercentage = 0;
|
||||||
|
|
||||||
private book: any;
|
private book: any;
|
||||||
private rendition: any;
|
private rendition: any;
|
||||||
@@ -309,22 +310,39 @@ export class EpubViewerComponent implements OnInit, OnDestroy {
|
|||||||
const currentIndex = location.start.index;
|
const currentIndex = location.start.index;
|
||||||
const totalSpineItems = this.book.spine.items.length;
|
const totalSpineItems = this.book.spine.items.length;
|
||||||
let percentage: number;
|
let percentage: number;
|
||||||
if (this.locationsReady) {
|
|
||||||
|
if (this.locationsReady && this.book.locations.total > 0) {
|
||||||
percentage = this.book.locations.percentageFromCfi(cfi);
|
percentage = this.book.locations.percentageFromCfi(cfi);
|
||||||
this.exactProgress = Math.round(percentage * 1000) / 10;
|
this.exactProgress = Math.round(percentage * 1000) / 10;
|
||||||
|
this.progressPercentage = Math.round(percentage * 1000) / 10;
|
||||||
} else {
|
} else {
|
||||||
if (totalSpineItems > 0) {
|
if (totalSpineItems > 0) {
|
||||||
percentage = currentIndex / totalSpineItems;
|
percentage = (currentIndex + 1) / totalSpineItems;
|
||||||
} else {
|
} else {
|
||||||
percentage = 0;
|
percentage = 0;
|
||||||
}
|
}
|
||||||
this.approxProgress = Math.round(percentage * 1000) / 10;
|
this.approxProgress = Math.round(percentage * 1000) / 10;
|
||||||
|
this.progressPercentage = Math.round(percentage * 1000) / 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.currentChapter = getChapter(this.book, location)?.label;
|
this.currentChapter = getChapter(this.book, location)?.label;
|
||||||
this.bookService.saveEpubProgress(this.epub.id, cfi, Math.round(percentage * 1000) / 10).subscribe();
|
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;
|
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