Display paginated reading sessions in the book metadata view (#2003)

Co-authored-by: acx10 <acx10@users.noreply.github.com>
This commit is contained in:
ACX
2025-12-26 16:44:44 -07:00
committed by GitHub
co-authored by acx10
parent 2c1e8a99e3
commit 4712f53b8e
13 changed files with 533 additions and 117 deletions
@@ -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,