diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/BookService.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/BookService.java index 7213e4225..4b6a14aa5 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/BookService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/BookService.java @@ -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 diff --git a/booklore-ui/src/app/book/components/epub-viewer/component/epub-viewer.component.html b/booklore-ui/src/app/book/components/epub-viewer/component/epub-viewer.component.html index 9ce7edd5a..7ba5ce6e7 100644 --- a/booklore-ui/src/app/book/components/epub-viewer/component/epub-viewer.component.html +++ b/booklore-ui/src/app/book/components/epub-viewer/component/epub-viewer.component.html @@ -7,8 +7,11 @@ @if (!isLoading) {
-
+
+
+ Progress: {{ progressPercentage }}% +
    @for (chapter of chapters; track chapter) { @@ -19,8 +22,12 @@
-

{{ currentChapter }}

-
+ +
+

{{ currentChapter }}

+
+ +
@if (!locationsReady) {
diff --git a/booklore-ui/src/app/book/components/epub-viewer/component/epub-viewer.component.scss b/booklore-ui/src/app/book/components/epub-viewer/component/epub-viewer.component.scss index e43d62e1f..3145406c8 100644 --- a/booklore-ui/src/app/book/components/epub-viewer/component/epub-viewer.component.scss +++ b/booklore-ui/src/app/book/components/epub-viewer/component/epub-viewer.component.scss @@ -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 { diff --git a/booklore-ui/src/app/book/components/epub-viewer/component/epub-viewer.component.ts b/booklore-ui/src/app/book/components/epub-viewer/component/epub-viewer.component.ts index 03e60e539..ab7981794 100644 --- a/booklore-ui/src/app/book/components/epub-viewer/component/epub-viewer.component.ts +++ b/booklore-ui/src/app/book/components/epub-viewer/component/epub-viewer.component.ts @@ -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; }); }