Display paginated reading sessions in the book metadata view (#2003)
Co-authored-by: acx10 <acx10@users.noreply.github.com>
This commit is contained in:
+65
@@ -0,0 +1,65 @@
|
||||
<div class="reading-sessions-container">
|
||||
@if (loading && sessions.length === 0) {
|
||||
<div class="loading-state">
|
||||
<p-progressSpinner strokeWidth="4" fill="transparent" animationDuration=".8s"></p-progressSpinner>
|
||||
<p class="loading-text">Loading reading sessions...</p>
|
||||
</div>
|
||||
} @else if (sessions.length === 0) {
|
||||
<div class="empty-state">
|
||||
<i class="pi pi-clock" style="font-size: 3rem; color: var(--text-color-secondary);"></i>
|
||||
<p>No reading sessions recorded yet</p>
|
||||
</div>
|
||||
} @else {
|
||||
<p-table
|
||||
[value]="sessions"
|
||||
[lazy]="true"
|
||||
[paginator]="true"
|
||||
[rows]="rows"
|
||||
[totalRecords]="totalRecords"
|
||||
[loading]="loading"
|
||||
[first]="first"
|
||||
(onLazyLoad)="onPageChange($event)"
|
||||
[showCurrentPageReport]="true"
|
||||
currentPageReportTemplate="Showing {first} to {last} of {totalRecords} sessions"
|
||||
class="p-datatable-sm">
|
||||
<ng-template #header>
|
||||
<tr>
|
||||
<th>Start Time</th>
|
||||
<th>End Time</th>
|
||||
<th>Duration</th>
|
||||
<th>Progress</th>
|
||||
<th>Progress Delta</th>
|
||||
<th>Location</th>
|
||||
</tr>
|
||||
</ng-template>
|
||||
<ng-template #body let-session>
|
||||
<tr>
|
||||
<td>{{ formatDate(session.startTime) }}</td>
|
||||
<td>{{ formatDate(session.endTime) }}</td>
|
||||
<td>{{ formatDuration(session.durationSeconds) }}</td>
|
||||
<td>
|
||||
@if (session.startProgress !== null && session.startProgress !== undefined && session.endProgress !== null && session.endProgress !== undefined) {
|
||||
<span>{{ session.startProgress }}% → {{ session.endProgress }}%</span>
|
||||
} @else {
|
||||
<span class="text-color-secondary">-</span>
|
||||
}
|
||||
</td>
|
||||
<td>
|
||||
@if (session.progressDelta !== null && session.progressDelta !== undefined) {
|
||||
<p-tag [value]="(session.progressDelta > 0 ? '+' : '') + session.progressDelta + '%'" [severity]="getProgressColor(session.progressDelta)"></p-tag>
|
||||
} @else {
|
||||
<span class="text-color-secondary">N/A</span>
|
||||
}
|
||||
</td>
|
||||
<td>
|
||||
@if (session.startLocation && session.endLocation && isPageNumber(session.startLocation)) {
|
||||
<span class="location-text">Page {{ session.startLocation }} → Page {{ session.endLocation }}</span>
|
||||
} @else {
|
||||
<span class="text-color-secondary">-</span>
|
||||
}
|
||||
</td>
|
||||
</tr>
|
||||
</ng-template>
|
||||
</p-table>
|
||||
}
|
||||
</div>
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
.reading-sessions-container {
|
||||
padding: 1rem 0;
|
||||
}
|
||||
|
||||
.loading-state,
|
||||
.empty-state {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 3rem;
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
.loading-text {
|
||||
color: var(--text-color-secondary);
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.empty-state p {
|
||||
color: var(--text-color-secondary);
|
||||
font-size: 1rem;
|
||||
}
|
||||
|
||||
.location-text {
|
||||
font-size: 0.875rem;
|
||||
color: var(--text-color);
|
||||
}
|
||||
|
||||
:host ::ng-deep {
|
||||
.p-datatable {
|
||||
.p-datatable-thead > tr > th {
|
||||
background-color: var(--card-background);
|
||||
color: var(--text-color);
|
||||
font-weight: 600;
|
||||
font-size: 0.875rem;
|
||||
white-space: nowrap;
|
||||
border-color: var(--border-color);
|
||||
}
|
||||
|
||||
.p-datatable-tbody > tr > td {
|
||||
padding: 0.35rem 0.65rem;
|
||||
font-size: 0.875rem;
|
||||
border-color: var(--border-color);
|
||||
}
|
||||
|
||||
.p-datatable-wrapper {
|
||||
overflow-x: auto;
|
||||
}
|
||||
}
|
||||
}
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
import {Component, inject, Input, OnInit, OnChanges, SimpleChanges} from '@angular/core';
|
||||
import {CommonModule} from '@angular/common';
|
||||
import {ReadingSessionApiService, ReadingSessionResponse} from '../../../../../shared/service/reading-session-api.service';
|
||||
import {TableModule} from 'primeng/table';
|
||||
import {ProgressSpinnerModule} from 'primeng/progressspinner';
|
||||
import {TagModule} from 'primeng/tag';
|
||||
|
||||
@Component({
|
||||
selector: 'app-book-reading-sessions',
|
||||
standalone: true,
|
||||
imports: [CommonModule, TableModule, ProgressSpinnerModule, TagModule],
|
||||
templateUrl: './book-reading-sessions.component.html',
|
||||
styleUrls: ['./book-reading-sessions.component.scss']
|
||||
})
|
||||
export class BookReadingSessionsComponent implements OnInit, OnChanges {
|
||||
@Input() bookId!: number;
|
||||
|
||||
private readonly readingSessionService = inject(ReadingSessionApiService);
|
||||
|
||||
sessions: ReadingSessionResponse[] = [];
|
||||
totalRecords = 0;
|
||||
first = 0;
|
||||
rows = 5;
|
||||
loading = false;
|
||||
|
||||
ngOnInit() {
|
||||
this.loadSessions();
|
||||
}
|
||||
|
||||
ngOnChanges(changes: SimpleChanges) {
|
||||
if (changes['bookId'] && !changes['bookId'].firstChange) {
|
||||
this.first = 0;
|
||||
this.loadSessions();
|
||||
}
|
||||
}
|
||||
|
||||
loadSessions(page: number = 0) {
|
||||
this.loading = true;
|
||||
this.readingSessionService.getSessionsByBookId(this.bookId, page, this.rows)
|
||||
.subscribe({
|
||||
next: (response) => {
|
||||
this.sessions = response.content;
|
||||
this.totalRecords = response.totalElements;
|
||||
this.loading = false;
|
||||
},
|
||||
error: () => {
|
||||
this.loading = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
onPageChange(event: any) {
|
||||
this.first = event.first;
|
||||
const page = Math.floor(event.first / event.rows);
|
||||
this.loadSessions(page);
|
||||
}
|
||||
|
||||
formatDuration(seconds: number): string {
|
||||
const hours = Math.floor(seconds / 3600);
|
||||
const minutes = Math.floor((seconds % 3600) / 60);
|
||||
const secs = seconds % 60;
|
||||
|
||||
if (hours > 0) {
|
||||
return `${hours}h ${minutes}m`;
|
||||
} else if (minutes > 0) {
|
||||
return `${minutes}m ${secs}s`;
|
||||
}
|
||||
return `${secs}s`;
|
||||
}
|
||||
|
||||
formatDate(dateString: string): string {
|
||||
return new Date(dateString).toLocaleString();
|
||||
}
|
||||
|
||||
getProgressColor(delta: number): 'success' | 'secondary' | 'danger' {
|
||||
if (delta > 0) return 'success';
|
||||
if (delta < 0) return 'danger';
|
||||
return 'secondary';
|
||||
}
|
||||
|
||||
isPageNumber(location: string | undefined): boolean {
|
||||
if (!location) return false;
|
||||
return !isNaN(Number(location)) && location.trim() !== '';
|
||||
}
|
||||
}
|
||||
+7
-1
@@ -19,7 +19,7 @@
|
||||
</div>
|
||||
}
|
||||
|
||||
<div
|
||||
<div
|
||||
class="cover-progress-bar-container"
|
||||
[style.--progress-count]="getProgressCount(book)">
|
||||
@let progress = getProgressPercent(book);
|
||||
@@ -605,6 +605,9 @@
|
||||
<i class="pi pi-pen-to-square"></i> Notes
|
||||
</p-tab>
|
||||
<p-tab [value]="4">
|
||||
<i class="pi pi-clock"></i> Reading Sessions
|
||||
</p-tab>
|
||||
<p-tab [value]="5">
|
||||
<i class="pi pi-comments"></i> Reviews
|
||||
</p-tab>
|
||||
</p-tablist>
|
||||
@@ -641,6 +644,9 @@
|
||||
<app-book-notes-component [bookId]="book.id"></app-book-notes-component>
|
||||
</p-tabpanel>
|
||||
<p-tabpanel [value]="4">
|
||||
<app-book-reading-sessions [bookId]="book.id"></app-book-reading-sessions>
|
||||
</p-tabpanel>
|
||||
<p-tabpanel [value]="5">
|
||||
<app-book-reviews [bookId]="book.id"></app-book-reviews>
|
||||
</p-tabpanel>
|
||||
</p-tabpanels>
|
||||
|
||||
+2
-1
@@ -41,13 +41,14 @@ import {
|
||||
import {BookNavigationService} from '../../../../book/service/book-navigation.service';
|
||||
import {Divider} from 'primeng/divider';
|
||||
import {BookMetadataHostService} from '../../../../../shared/service/book-metadata-host-service';
|
||||
import { BookReadingSessionsComponent } from '../book-reading-sessions/book-reading-sessions.component';
|
||||
|
||||
@Component({
|
||||
selector: 'app-metadata-viewer',
|
||||
standalone: true,
|
||||
templateUrl: './metadata-viewer.component.html',
|
||||
styleUrl: './metadata-viewer.component.scss',
|
||||
imports: [Button, AsyncPipe, Rating, FormsModule, SplitButton, NgClass, Tooltip, DecimalPipe, Editor, ProgressBar, Menu, InfiniteScrollDirective, BookCardLiteComponent, DatePicker, Tab, TabList, TabPanel, TabPanels, Tabs, BookReviewsComponent, BookNotesComponent, ProgressSpinner, TieredMenu, Image, TagComponent, UpperCasePipe, Divider]
|
||||
imports: [Button, AsyncPipe, Rating, FormsModule, SplitButton, NgClass, Tooltip, DecimalPipe, Editor, ProgressBar, Menu, InfiniteScrollDirective, BookCardLiteComponent, DatePicker, Tab, TabList, TabPanel, TabPanels, Tabs, BookReviewsComponent, BookNotesComponent, ProgressSpinner, TieredMenu, Image, TagComponent, UpperCasePipe, Divider, BookReadingSessionsComponent]
|
||||
})
|
||||
export class MetadataViewerComponent implements OnInit, OnChanges {
|
||||
@Input() book$!: Observable<Book | null>;
|
||||
|
||||
@@ -53,30 +53,33 @@ export interface PeakHoursResponse {
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class UserStatsService {
|
||||
private readonly readingSessionsUrl = `${API_CONFIG.BASE_URL}/api/v1/reading-sessions`;
|
||||
private readonly readingSessionsUrl = `${API_CONFIG.BASE_URL}/api/v1/user-stats`;
|
||||
private http = inject(HttpClient);
|
||||
|
||||
getHeatmapForYear(year: number): Observable<ReadingSessionHeatmapResponse[]> {
|
||||
return this.http.get<ReadingSessionHeatmapResponse[]>(
|
||||
`${this.readingSessionsUrl}/heatmap/year/${year}`
|
||||
`${this.readingSessionsUrl}/heatmap`,
|
||||
{ params: { year: year.toString() } }
|
||||
);
|
||||
}
|
||||
|
||||
getTimelineForWeek(year: number, month: number, week: number): Observable<ReadingSessionTimelineResponse[]> {
|
||||
return this.http.get<ReadingSessionTimelineResponse[]>(
|
||||
`${this.readingSessionsUrl}/timeline/week/${year}/${month}/${week}`
|
||||
`${this.readingSessionsUrl}/timeline`,
|
||||
{ params: { year: year.toString(), month: month.toString(), week: week.toString() } }
|
||||
);
|
||||
}
|
||||
|
||||
getGenreStats(): Observable<GenreStatsResponse[]> {
|
||||
return this.http.get<GenreStatsResponse[]>(
|
||||
`${this.readingSessionsUrl}/stats/genres`
|
||||
`${this.readingSessionsUrl}/genres`
|
||||
);
|
||||
}
|
||||
|
||||
getCompletionTimelineForYear(year: number): Observable<CompletionTimelineResponse[]> {
|
||||
return this.http.get<CompletionTimelineResponse[]>(
|
||||
`${this.readingSessionsUrl}/stats/completion-timeline/year/${year}`
|
||||
`${this.readingSessionsUrl}/completion-timeline`,
|
||||
{ params: { year: year.toString() } }
|
||||
);
|
||||
}
|
||||
|
||||
@@ -90,7 +93,7 @@ export class UserStatsService {
|
||||
}
|
||||
|
||||
return this.http.get<FavoriteDaysResponse[]>(
|
||||
`${this.readingSessionsUrl}/stats/favorite-days`,
|
||||
`${this.readingSessionsUrl}/favorite-days`,
|
||||
{params}
|
||||
);
|
||||
}
|
||||
@@ -105,7 +108,7 @@ export class UserStatsService {
|
||||
}
|
||||
|
||||
return this.http.get<PeakHoursResponse[]>(
|
||||
`${this.readingSessionsUrl}/stats/peak-hours`,
|
||||
`${this.readingSessionsUrl}/peak-hours`,
|
||||
{params}
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
import {inject, Injectable} from '@angular/core';
|
||||
import {HttpClient, HttpParams} from '@angular/common/http';
|
||||
import {Observable} from 'rxjs';
|
||||
import {API_CONFIG} from '../../core/config/api-config';
|
||||
import {BookType} from '../../features/book/model/book.model';
|
||||
|
||||
export interface ReadingSessionResponse {
|
||||
id: number;
|
||||
bookId: number;
|
||||
bookTitle: string;
|
||||
bookType: BookType;
|
||||
startTime: string;
|
||||
endTime: string;
|
||||
durationSeconds: number;
|
||||
startProgress: number;
|
||||
endProgress: number;
|
||||
progressDelta: number;
|
||||
startLocation?: string;
|
||||
endLocation?: string;
|
||||
createdAt: string;
|
||||
}
|
||||
|
||||
export interface PageableResponse<T> {
|
||||
content: T[];
|
||||
pageable: {
|
||||
pageNumber: number;
|
||||
pageSize: number;
|
||||
sort: {
|
||||
empty: boolean;
|
||||
sorted: boolean;
|
||||
unsorted: boolean;
|
||||
};
|
||||
offset: number;
|
||||
paged: boolean;
|
||||
unpaged: boolean;
|
||||
};
|
||||
totalElements: number;
|
||||
last: boolean;
|
||||
totalPages: number;
|
||||
numberOfElements: number;
|
||||
first: boolean;
|
||||
size: number;
|
||||
number: number;
|
||||
sort: {
|
||||
empty: boolean;
|
||||
sorted: boolean;
|
||||
unsorted: boolean;
|
||||
};
|
||||
empty: boolean;
|
||||
}
|
||||
|
||||
export interface CreateReadingSessionDto {
|
||||
bookId: number;
|
||||
bookType: BookType;
|
||||
startTime: string;
|
||||
endTime: string;
|
||||
durationSeconds: number;
|
||||
durationFormatted: string;
|
||||
startProgress?: number;
|
||||
endProgress?: number;
|
||||
progressDelta?: number;
|
||||
startLocation?: string;
|
||||
endLocation?: string;
|
||||
}
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class ReadingSessionApiService {
|
||||
private readonly http = inject(HttpClient);
|
||||
private readonly url = `${API_CONFIG.BASE_URL}/api/v1/reading-sessions`;
|
||||
|
||||
createSession(sessionData: CreateReadingSessionDto): Observable<void> {
|
||||
return this.http.post<void>(this.url, sessionData);
|
||||
}
|
||||
|
||||
getSessionsByBookId(bookId: number, page: number = 0, size: number = 5): Observable<PageableResponse<ReadingSessionResponse>> {
|
||||
const params = new HttpParams()
|
||||
.set('page', page.toString())
|
||||
.set('size', size.toString());
|
||||
|
||||
return this.http.get<PageableResponse<ReadingSessionResponse>>(
|
||||
`${this.url}/book/${bookId}`,
|
||||
{params}
|
||||
);
|
||||
}
|
||||
|
||||
sendSessionBeacon(sessionData: CreateReadingSessionDto): boolean {
|
||||
try {
|
||||
const blob = new Blob([JSON.stringify(sessionData)], {type: 'application/json'});
|
||||
return navigator.sendBeacon(this.url, blob);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
import { inject, Injectable } from '@angular/core';
|
||||
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
|
||||
import { HttpErrorResponse } from '@angular/common/http';
|
||||
import { fromEvent, merge, Subscription } from 'rxjs';
|
||||
import { debounceTime } from 'rxjs/operators';
|
||||
import { API_CONFIG } from '../../core/config/api-config';
|
||||
import {BookType} from '../../features/book/model/book.model';
|
||||
import { BookType } from '../../features/book/model/book.model';
|
||||
import {
|
||||
ReadingSessionApiService,
|
||||
CreateReadingSessionDto
|
||||
} from './reading-session-api.service';
|
||||
|
||||
export interface ReadingSession {
|
||||
bookId: number;
|
||||
@@ -22,8 +25,7 @@ export interface ReadingSession {
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class ReadingSessionService {
|
||||
private readonly http = inject(HttpClient);
|
||||
private readonly url = `${API_CONFIG.BASE_URL}/api/v1/reading-sessions`;
|
||||
private readonly apiService = inject(ReadingSessionApiService);
|
||||
|
||||
private currentSession: ReadingSession | null = null;
|
||||
private idleTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
@@ -139,15 +141,9 @@ export class ReadingSessionService {
|
||||
|
||||
this.log('Reading session ended (sync)', sessionData);
|
||||
|
||||
try {
|
||||
const blob = new Blob([JSON.stringify(sessionData)], { type: 'application/json' });
|
||||
const success = navigator.sendBeacon(this.url, blob);
|
||||
|
||||
if (!success) {
|
||||
this.logError('sendBeacon failed, request may not have been queued');
|
||||
}
|
||||
} catch (error) {
|
||||
this.logError('Failed to send session data', error);
|
||||
const success = this.apiService.sendSessionBeacon(sessionData);
|
||||
if (!success) {
|
||||
this.logError('sendBeacon failed, request may not have been queued');
|
||||
}
|
||||
|
||||
this.cleanup();
|
||||
@@ -167,13 +163,17 @@ export class ReadingSessionService {
|
||||
|
||||
this.log('Reading session completed', sessionData);
|
||||
|
||||
this.http.post<void>(this.url, sessionData).subscribe({
|
||||
this.apiService.createSession(sessionData).subscribe({
|
||||
next: () => this.log('Session saved to backend'),
|
||||
error: (err: HttpErrorResponse) => this.logError('Failed to save session', err)
|
||||
});
|
||||
}
|
||||
|
||||
private buildSessionData(session: ReadingSession, endTime: Date, durationSeconds: number) {
|
||||
private buildSessionData(
|
||||
session: ReadingSession,
|
||||
endTime: Date,
|
||||
durationSeconds: number
|
||||
): CreateReadingSessionDto {
|
||||
return {
|
||||
bookId: session.bookId,
|
||||
bookType: session.bookType,
|
||||
|
||||
Reference in New Issue
Block a user