Introduce reading session tracking with visual insights (#1957)

* Introduce reading session tracking with visual insights (#1957)

---------

Co-authored-by: acx10 <acx10@users.noreply.github.com>
This commit is contained in:
ACX
2025-12-21 21:14:42 -07:00
committed by GitHub
co-authored by acx10
parent b12fc82414
commit b5ada2fff0
39 changed files with 2526 additions and 406 deletions
@@ -97,6 +97,9 @@
<button class="view-button background-toggle" (click)="toggleBackground()" title="Toggle Background Color">
<span>{{ backgroundColorIcon }}</span>
</button>
<button class="view-button close-button" (click)="closeReader()" title="Close Reader">
<span>✕</span>
</button>
</div>
<div class="mobile-controls">
@@ -160,6 +163,10 @@
<span class="option-icon">{{ backgroundColorIcon }}</span>
<span class="option-label">Background</span>
</button>
<button class="mobile-option" (click)="closeReader()">
<span class="option-icon">✕</span>
<span class="option-label">Close Reader</span>
</button>
</div>
}
</div>
@@ -311,6 +311,18 @@
background: #4a4a4a;
border-color: #666;
}
&.close-button {
span {
font-size: 16px;
font-weight: bold;
}
&:hover {
background: rgba(239, 68, 68, 0.2);
border-color: rgba(239, 68, 68, 0.4);
}
}
}
.fit-mode-dropdown {
@@ -387,11 +399,11 @@
background: #0f0f0f;
min-height: 0;
position: relative;
&:not(.two-page-view):not(.infinite-scroll) {
flex-direction: column;
align-items: stretch;
.pages-wrapper {
flex: 1;
display: flex;
@@ -399,7 +411,7 @@
align-items: center;
min-height: 0;
}
.end-of-comic-action {
flex: 0 0 auto;
margin-top: 1rem;
@@ -777,7 +789,7 @@
.action-icon { font-size: 2rem; }
.action-text { font-size: 1rem; }
.book-title {
.book-title {
font-size: 0.85rem;
max-width: 250px;
}
@@ -1,5 +1,6 @@
import {Component, HostListener, inject, OnInit} from '@angular/core';
import {Component, HostListener, inject, OnInit, OnDestroy} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import {CommonModule, Location} from '@angular/common';
import {PageTitleService} from "../../../shared/service/page-title.service";
import {CbxReaderService} from '../../book/service/cbx-reader.service';
import {BookService} from '../../book/service/book.service';
@@ -21,6 +22,7 @@ import {BookState} from '../../book/model/state/book-state.model';
import {ProgressSpinner} from 'primeng/progressspinner';
import {FormsModule} from "@angular/forms";
import {NewPdfReaderService} from '../../book/service/new-pdf-reader.service';
import {ReadingSessionService} from '../../../shared/service/reading-session.service';
@Component({
@@ -30,7 +32,7 @@ import {NewPdfReaderService} from '../../book/service/new-pdf-reader.service';
templateUrl: './cbx-reader.component.html',
styleUrl: './cbx-reader.component.scss'
})
export class CbxReaderComponent implements OnInit {
export class CbxReaderComponent implements OnInit, OnDestroy {
bookType!: BookType;
goToPageInput: number | null = null;
@@ -53,12 +55,14 @@ export class CbxReaderComponent implements OnInit {
private route = inject(ActivatedRoute);
private router = inject(Router);
private location = inject(Location);
private cbxReaderService = inject(CbxReaderService);
private pdfReaderService = inject(NewPdfReaderService);
private bookService = inject(BookService);
private userService = inject(UserService);
private messageService = inject(MessageService);
private pageTitle = inject(PageTitleService);
private readingSessionService = inject(ReadingSessionService);
showFitModeDropdown: boolean = false;
@@ -164,6 +168,9 @@ export class CbxReaderComponent implements OnInit {
}
this.alignCurrentPageToParity();
this.isLoading = false;
const percentage = this.pages.length > 0 ? Math.round(((this.currentPage + 1) / this.pages.length) * 1000) / 10 : 0;
this.readingSessionService.startSession(this.bookId, "CBX", (this.currentPage + 1).toString(), percentage);
},
error: (err) => {
const errorMessage = err?.error?.message || 'Failed to load pages';
@@ -253,6 +260,7 @@ export class CbxReaderComponent implements OnInit {
this.currentPage++;
this.scrollToPage(this.currentPage);
this.updateProgress();
this.updateSessionProgress();
}
return;
}
@@ -269,6 +277,7 @@ export class CbxReaderComponent implements OnInit {
if (this.currentPage !== previousPage) {
this.updateProgress();
this.updateSessionProgress();
}
}
@@ -278,6 +287,7 @@ export class CbxReaderComponent implements OnInit {
this.currentPage--;
this.scrollToPage(this.currentPage);
this.updateProgress();
this.updateSessionProgress();
}
return;
}
@@ -288,6 +298,7 @@ export class CbxReaderComponent implements OnInit {
this.currentPage = Math.max(0, this.currentPage - 1);
}
this.updateProgress();
this.updateSessionProgress();
}
private alignCurrentPageToParity() {
@@ -420,6 +431,7 @@ export class CbxReaderComponent implements OnInit {
if (newPage !== this.currentPage) {
this.currentPage = newPage;
this.updateProgress();
this.updateSessionProgress();
}
break;
}
@@ -483,6 +495,16 @@ export class CbxReaderComponent implements OnInit {
}
}
private updateSessionProgress(): void {
const percentage = this.pages.length > 0
? Math.round(((this.currentPage + 1) / this.pages.length) * 1000) / 10
: 0;
this.readingSessionService.updateProgress(
(this.currentPage + 1).toString(),
percentage
);
}
goToPage(page: number): void {
if (page < 1 || page > this.pages.length) return;
@@ -495,9 +517,11 @@ export class CbxReaderComponent implements OnInit {
this.ensurePageLoaded(targetIndex);
this.scrollToPage(targetIndex);
this.updateProgress();
this.updateSessionProgress();
} else {
this.alignCurrentPageToParity();
this.updateProgress();
this.updateSessionProgress();
}
}
@@ -586,12 +610,14 @@ export class CbxReaderComponent implements OnInit {
navigateToPreviousBook(): void {
if (this.previousBookInSeries) {
this.endReadingSession();
this.router.navigate(['/cbx-reader/book', this.previousBookInSeries.id]);
}
}
navigateToNextBook(): void {
if (this.nextBookInSeries) {
this.endReadingSession();
this.router.navigate(['/cbx-reader/book', this.nextBookInSeries.id]);
}
}
@@ -672,4 +698,20 @@ export class CbxReaderComponent implements OnInit {
if (!this.nextBookInSeries) return 'No Next Book';
return `Next Book: ${this.getBookDisplayTitle(this.nextBookInSeries)}`;
}
ngOnDestroy(): void {
this.endReadingSession();
}
private endReadingSession(): void {
if (this.readingSessionService.isSessionActive()) {
const percentage = this.pages.length > 0 ? Math.round(((this.currentPage + 1) / this.pages.length) * 1000) / 10 : 0;
this.readingSessionService.endSession((this.currentPage + 1).toString(), percentage);
}
}
closeReader(): void {
this.endReadingSession();
this.location.back();
}
}
@@ -284,6 +284,14 @@
</div>
</p-drawer>
</div>
<p-button
size="small"
icon="pi pi-times"
(click)="closeReader()"
severity="secondary"
pTooltip="Close Reader"
tooltipPosition="bottom">
</p-button>
</div>
</div>
</div>
@@ -4,7 +4,7 @@ import {Drawer} from 'primeng/drawer';
import {forkJoin, Subscription} from 'rxjs';
import {Button} from 'primeng/button';
import {InputText} from 'primeng/inputtext';
import {CommonModule} from '@angular/common';
import {CommonModule, Location} from '@angular/common';
import {FormsModule} from '@angular/forms';
import {ActivatedRoute} from '@angular/router';
import {Book, BookSetting} from '../../../book/model/book.model';
@@ -17,7 +17,8 @@ import {BookMark, BookMarkService, UpdateBookMarkRequest} from '../../../../shar
import {Tooltip} from 'primeng/tooltip';
import {Slider} from 'primeng/slider';
import {FALLBACK_EPUB_SETTINGS, getChapter} from '../epub-reader-helper';
import {EpubThemeUtil, EpubTheme} from '../epub-theme-util';
import {ReadingSessionService} from '../../../../shared/service/reading-session.service';
import {EpubTheme, EpubThemeUtil} from '../epub-theme-util';
import {PageTitleService} from "../../../../shared/service/page-title.service";
import {Tab, TabList, TabPanel, TabPanels, Tabs} from 'primeng/tabs';
import {IconField} from 'primeng/iconfield';
@@ -29,27 +30,7 @@ import {BookmarkViewDialogComponent} from './bookmark-view-dialog.component';
selector: 'app-epub-reader',
templateUrl: './epub-reader.component.html',
styleUrls: ['./epub-reader.component.scss'],
imports: [
CommonModule,
FormsModule,
Drawer,
Button,
Select,
ProgressSpinner,
Tooltip,
Slider,
PrimeTemplate,
Tabs,
TabList,
Tab,
TabPanels,
TabPanel,
IconField,
InputIcon,
BookmarkEditDialogComponent,
BookmarkViewDialogComponent,
InputText
],
imports: [CommonModule, FormsModule, Drawer, Button, Select, ProgressSpinner, Tooltip, Slider, PrimeTemplate, Tabs, TabList, Tab, TabPanels, TabPanel, IconField, InputIcon, BookmarkEditDialogComponent, BookmarkViewDialogComponent, InputText],
standalone: true
})
export class EpubReaderComponent implements OnInit, OnDestroy {
@@ -67,10 +48,8 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
isAddingBookmark = false;
isDeletingBookmark = false;
isEditingBookmark = false;
isUpdatingPosition = false;
private routeSubscription?: Subscription;
// Bookmark Filter & View
filterText = '';
viewDialogVisible = false;
selectedBookmark: BookMark | null = null;
@@ -87,7 +66,6 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
private isMouseInTopRegion = false;
private headerShownByMobileTouch = false;
// Properties for bookmark editing
editingBookmark: BookMark | null = null;
showEditBookmarkDialog = false;
@@ -132,12 +110,14 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
];
private route = inject(ActivatedRoute);
private location = inject(Location);
private userService = inject(UserService);
private bookService = inject(BookService);
private messageService = inject(MessageService);
private ngZone = inject(NgZone);
private pageTitle = inject(PageTitleService);
private bookMarkService = inject(BookMarkService);
private readingSessionService = inject(ReadingSessionService);
epub!: Book;
@@ -249,7 +229,6 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
get filteredBookmarks(): BookMark[] {
let filtered = this.bookmarks;
// Filter
if (this.filterText && this.filterText.trim()) {
const lowerFilter = this.filterText.toLowerCase().trim();
filtered = filtered.filter(b =>
@@ -258,9 +237,8 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
);
}
// Sort: Priority ASC (1 is high), then CreatedAt DESC
return [...filtered].sort((a, b) => {
const priorityA = a.priority ?? 3; // Default to 3 (Normal) if undefined
const priorityA = a.priority ?? 3;
const priorityB = b.priority ?? 3;
if (priorityA !== priorityB) {
@@ -430,19 +408,37 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
prevPage(): void {
if (this.rendition) {
this.rendition.prev();
this.rendition.prev().then(() => {
const location = this.rendition.currentLocation();
this.readingSessionService.updateProgress(
location?.start?.cfi,
this.progressPercentage
);
});
}
}
nextPage(): void {
if (this.rendition) {
this.rendition.next();
this.rendition.next().then(() => {
const location = this.rendition.currentLocation();
this.readingSessionService.updateProgress(
location?.start?.cfi,
this.progressPercentage
);
});
}
}
navigateToChapter(chapter: { label: string; href: string; level: number }): void {
if (this.book && chapter.href) {
this.book.rendition.display(chapter.href);
this.book.rendition.display(chapter.href).then(() => {
const location = this.rendition.currentLocation();
this.readingSessionService.updateProgress(
location?.start?.cfi,
this.progressPercentage
);
});
}
}
@@ -498,6 +494,11 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
}
this.bookService.saveEpubProgress(this.epub.id, cfi, Math.round(percentage * 1000) / 10).subscribe();
this.readingSessionService.updateProgress(
location.start.cfi,
this.progressPercentage
);
});
this.book.ready.then(() => {
@@ -509,13 +510,26 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
const cfi = location.end.cfi;
const percentage = this.book.locations.percentageFromCfi(cfi);
this.progressPercentage = Math.round(percentage * 1000) / 10;
this.readingSessionService.startSession(this.epub.id, "EPUB", location.start.cfi, this.progressPercentage);
}
}).catch(() => {
this.locationsReady = false;
const location = this.rendition.currentLocation();
if (location) {
this.readingSessionService.startSession(this.epub.id, "EPUB", location.start.cfi, this.progressPercentage);
}
});
}
ngOnDestroy(): void {
if (this.readingSessionService.isSessionActive()) {
this.readingSessionService.endSession(
this.currentCfi || undefined,
this.progressPercentage
);
}
this.routeSubscription?.unsubscribe();
if (this.rendition) {
@@ -555,18 +569,15 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
const isBottomClick = clickY > screenHeight * 0.2;
if (isTopClick && !this.showHeader) {
// Touch top 20% - show header and mark as shown by mobile touch
this.showHeader = true;
this.headerShownByMobileTouch = true;
this.clearHeaderTimeout();
} else if (isBottomClick && this.showHeader && this.headerShownByMobileTouch) {
// Touch lower 80% - hide header
this.showHeader = false;
this.headerShownByMobileTouch = false;
this.clearHeaderTimeout();
}
} else {
// Desktop behavior
const isTopClick = clickY < screenHeight * 0.1;
if (isTopClick) {
this.showHeader = true;
@@ -615,7 +626,6 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
return;
}
// Don't auto-hide on mobile if header was shown by touch
if (this.isMobileDevice() && this.headerShownByMobileTouch) {
return;
}
@@ -690,7 +700,6 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
this.bookMarkService.createBookmark(request).subscribe({
next: (bookmark) => {
this.bookmarks.push(bookmark);
// Force array update for change detection if needed, but simple push works with getter usually if ref is stable
this.bookmarks = [...this.bookmarks];
this.updateBookmarkStatus();
this.messageService.add({
@@ -715,9 +724,8 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
if (this.isDeletingBookmark) {
return;
}
// Simple confirmation using window.confirm for now, as consistent with UserManagementComponent behavior seen in linting
if (!confirm('Are you sure you want to delete this bookmark?')) {
return;
return;
}
this.isDeletingBookmark = true;
@@ -745,7 +753,13 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
navigateToBookmark(bookmark: BookMark): void {
if (this.rendition && bookmark.cfi) {
this.rendition.display(bookmark.cfi);
this.rendition.display(bookmark.cfi).then(() => {
const location = this.rendition.currentLocation();
this.readingSessionService.updateProgress(
location?.start?.cfi,
this.progressPercentage
);
});
}
}
@@ -761,7 +775,7 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
}
openEditBookmarkDialog(bookmark: BookMark): void {
this.editingBookmark = { ...bookmark };
this.editingBookmark = {...bookmark};
this.showEditBookmarkDialog = true;
}
@@ -777,7 +791,7 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
const index = this.bookmarks.findIndex(b => b.id === this.editingBookmark!.id);
if (index !== -1) {
this.bookmarks[index] = updatedBookmark;
this.bookmarks = [...this.bookmarks]; // Trigger change detection for getter
this.bookmarks = [...this.bookmarks];
}
this.messageService.add({
severity: 'success',
@@ -785,7 +799,7 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
detail: 'Bookmark updated successfully',
});
this.showEditBookmarkDialog = false;
this.editingBookmark = null; // Reset the editing bookmark after successful save
this.editingBookmark = null;
this.isEditingBookmark = false;
},
error: () => {
@@ -795,7 +809,7 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
detail: 'Failed to update bookmark',
});
this.showEditBookmarkDialog = false;
this.editingBookmark = null; // Reset the editing bookmark even on error
this.editingBookmark = null;
this.isEditingBookmark = false;
}
});
@@ -803,41 +817,16 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
onBookmarkCancel(): void {
this.showEditBookmarkDialog = false;
this.editingBookmark = null; // Reset the editing bookmark when dialog is cancelled
this.editingBookmark = null;
}
updateBookmarkPosition(bookmarkId: number): void {
if (!this.currentCfi || this.isUpdatingPosition) {
return;
closeReader(): void {
if (this.readingSessionService.isSessionActive()) {
this.readingSessionService.endSession(
this.currentCfi || undefined,
this.progressPercentage
);
}
this.isUpdatingPosition = true;
const updateRequest = {
cfi: this.currentCfi
};
this.bookMarkService.updateBookmark(bookmarkId, updateRequest).subscribe({
next: (updatedBookmark) => {
const index = this.bookmarks.findIndex(b => b.id === bookmarkId);
if (index !== -1) {
this.bookmarks[index] = updatedBookmark;
this.bookmarks = [...this.bookmarks];
}
this.messageService.add({
severity: 'success',
summary: 'Success',
detail: 'Bookmark position updated successfully',
});
this.isUpdatingPosition = false;
},
error: () => {
this.messageService.add({
severity: 'error',
summary: 'Error',
detail: 'Failed to update bookmark position',
});
this.isUpdatingPosition = false;
}
});
this.location.back();
}
}
@@ -9,19 +9,54 @@
[src]="bookData"
[textLayer]="true"
[showHandToolButton]="true"
[handTool]="false"
[height]="'auto'"
[page]="page"
[rotation]="rotation"
[sidebarVisible]="false"
[zoom]="zoom"
[spread]="spread"
[showBookModeButton]="false"
[showDownloadButton]="showDownloadButton"
[showPrintButton]="showPrintButton"
[customToolbar]="additionalButtons"
(pagesLoaded)="onPdfPagesLoaded($event)"
(pageChange)="onPageChange($event)"
(zoomChange)="onZoomChange($event)"
(spreadChange)="onSpreadChange($event)">
</ngx-extended-pdf-viewer>
<ng-template #additionalButtons>
<div id="toolbarViewer">
<div id="toolbarViewerLeft">
<pdf-toggle-sidebar></pdf-toggle-sidebar>
<pdf-find-button></pdf-find-button>
<pdf-paging-area></pdf-paging-area>
</div>
<pdf-zoom-toolbar></pdf-zoom-toolbar>
<div id="toolbarViewerRight">
<pdf-shy-button
[cssClass]="'lg' | responsiveCSSClass"
class="newTab"
title="Close PDF Reader"
primaryToolbarId="closePdfReaderButton"
[action]="closeReader"
[order]="5"
image="<svg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24' fill='none' stroke='orange' stroke-width='2' stroke-linecap='round' stroke-linejoin='round' class='lucide lucide-circle-x-icon lucide-circle-x'><circle cx='12' cy='12' r='10'/><path d='m15 9-6 6'/><path d='m9 9 6 6'/></svg>"
>
</pdf-shy-button>
<pdf-hand-tool></pdf-hand-tool>
<pdf-select-tool></pdf-select-tool>
<pdf-rotate-page></pdf-rotate-page>
<pdf-print></pdf-print>
<pdf-no-spread></pdf-no-spread>
<pdf-even-spread></pdf-even-spread>
<pdf-odd-spread></pdf-odd-spread>
<pdf-infinite-scroll></pdf-infinite-scroll>
<pdf-horizontal-scroll></pdf-horizontal-scroll>
<pdf-vertical-scroll-mode></pdf-vertical-scroll-mode>
<pdf-wrapped-scroll-mode></pdf-wrapped-scroll-mode>
<pdf-single-page-mode></pdf-single-page-mode>
<pdf-book-mode></pdf-book-mode>
<div class="verticalToolbarSeparator hiddenSmallView"></div>
<pdf-toggle-secondary-toolbar></pdf-toggle-secondary-toolbar>
</div>
</div>
</ng-template>
}
@@ -9,6 +9,8 @@ import {UserService} from '../../settings/user-management/user.service';
import {ProgressSpinner} from 'primeng/progressspinner';
import {MessageService} from 'primeng/api';
import {ReadingSessionService} from '../../../shared/service/reading-session.service';
import {Location} from '@angular/common';
@Component({
selector: 'app-pdf-reader',
@@ -26,9 +28,6 @@ export class PdfReaderComponent implements OnInit, OnDestroy {
spread!: 'off' | 'even' | 'odd';
zoom!: ZoomType;
showDownloadButton = false;
showPrintButton = false;
bookData!: string | Blob;
bookId!: number;
private appSettingsSubscription!: Subscription;
@@ -38,6 +37,8 @@ export class PdfReaderComponent implements OnInit, OnDestroy {
private messageService = inject(MessageService);
private route = inject(ActivatedRoute);
private pageTitle = inject(PageTitleService);
private readingSessionService = inject(ReadingSessionService);
private location = inject(Location);
ngOnInit(): void {
this.route.paramMap.subscribe((params) => {
@@ -58,9 +59,6 @@ export class PdfReaderComponent implements OnInit, OnDestroy {
this.pageTitle.setBookPageTitle(pdfMeta);
this.showDownloadButton = myself.permissions.canDownload || myself.permissions.admin;
this.showPrintButton = myself.permissions.canDownload || myself.permissions.admin;
const globalOrIndividual = myself.userSettings.perBookSetting.pdf;
if (globalOrIndividual === 'Global') {
this.zoom = myself.userSettings.pdfReaderSetting.pageZoom || 'page-fit';
@@ -85,6 +83,8 @@ export class PdfReaderComponent implements OnInit, OnDestroy {
if (page !== this.page) {
this.page = page;
this.updateProgress();
const percentage = this.totalPages > 0 ? Math.round((this.page / this.totalPages) * 1000) / 10 : 0;
this.readingSessionService.updateProgress(this.page.toString(), percentage);
}
}
@@ -113,21 +113,34 @@ export class PdfReaderComponent implements OnInit, OnDestroy {
}
updateProgress(): void {
const percentage = this.totalPages > 0
? Math.round((this.page / this.totalPages) * 1000) / 10
: 0;
const percentage = this.totalPages > 0 ? Math.round((this.page / this.totalPages) * 1000) / 10 : 0;
this.bookService.savePdfProgress(this.bookId, this.page, percentage).subscribe();
}
onPdfPagesLoaded(event: any): void {
this.totalPages = event.pagesCount;
const percentage = this.totalPages > 0 ? Math.round((this.page / this.totalPages) * 1000) / 10 : 0;
this.readingSessionService.startSession(this.bookId, "PDF", this.page.toString(), percentage);
this.readingSessionService.updateProgress(this.page.toString(), percentage);
}
ngOnDestroy(): void {
if (this.readingSessionService.isSessionActive()) {
const percentage = this.totalPages > 0 ? Math.round((this.page / this.totalPages) * 1000) / 10 : 0;
this.readingSessionService.endSession(this.page.toString(), percentage);
}
if (this.appSettingsSubscription) {
this.appSettingsSubscription.unsubscribe();
}
this.updateProgress();
}
closeReader = (): void => {
if (this.readingSessionService.isSessionActive()) {
const percentage = this.totalPages > 0 ? Math.round((this.page / this.totalPages) * 1000) / 10 : 0;
this.readingSessionService.endSession(this.page.toString(), percentage);
}
this.location.back();
}
}