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:
Aditya Chandel
2025-09-13 21:20:54 -06:00
committed by GitHub
parent e99c8a9172
commit 226cffec6d
4 changed files with 130 additions and 23 deletions
@@ -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;
});
}