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
@@ -0,0 +1,104 @@
<div class="timeline-container">
<div class="timeline-header">
<div class="header-title">
<h3>Reading Session Timeline</h3>
<p class="timeline-subtitle">Your reading schedule throughout the week</p>
</div>
<div class="week-selector">
<button type="button"
class="week-nav-btn"
(click)="changeWeek(-1)"
title="Previous week">
<i class="pi pi-chevron-left"></i>
</button>
<div class="week-info">
<span class="week-label">Week {{ currentWeek }}</span>
<span class="week-dates">{{ getWeekDateRange() }}</span>
</div>
<button type="button"
class="week-nav-btn"
(click)="changeWeek(1)"
title="Next week">
<i class="pi pi-chevron-right"></i>
</button>
</div>
</div>
<div class="timeline-content">
<div class="hour-markers">
<div class="day-label-spacer"></div>
<div class="hour-grid">
@for (hour of hourLabels; track $index) {
<div class="hour-marker" [class.major]="$index % 3 === 0">
<span class="hour-label">{{ hour }}</span>
</div>
}
</div>
</div>
<div class="timeline-rows">
@for (dayData of timelineData; track dayData.dayOfWeek) {
<div class="timeline-row">
<div class="day-label">{{ dayData.day }}</div>
<div class="timeline-track">
<div class="grid-lines">
@for (hour of hourLabels; track $index) {
<div class="grid-line" [class.major]="$index % 6 === 0"></div>
}
</div>
<div class="sessions">
@for (session of dayData.sessions; track $index) {
<div class="session-block"
[ngClass]="'book-type-' + (session.bookType || 'default').toLowerCase()"
[style.left.%]="session.left"
[style.width.%]="session.width"
[style.top]="session.totalLevels > 1 ? 'calc(' + session.level + ' / ' + session.totalLevels + ' * 100% + ' + session.level * 2 + 'px)' : '0'"
[style.height]="session.totalLevels > 1 ? 'calc((1 / ' + session.totalLevels + ' * 100%) - ' + (session.totalLevels - 1) * 2 / session.totalLevels + 'px)' : '100%'">
<div class="session-content">
<span class="session-time">
{{ formatTime(session.startHour, session.startMinute) }}
</span>
<span class="session-duration">
{{ formatDuration(session.duration) }}
</span>
</div>
<div class="session-tooltip">
<div class="tooltip-content">
<div class="tooltip-cover">
<img [src]="getCoverUrl(session.bookId)" alt="Book Cover">
</div>
<div class="tooltip-details">
<div class="tooltip-header">
<i class="pi pi-book"></i>
<span class="tooltip-title">{{ session.bookTitle || 'Reading Session' }}</span>
</div>
<div class="tooltip-divider"></div>
<div class="tooltip-body">
<div class="tooltip-row">
<i class="pi pi-clock"></i>
<span class="tooltip-label">Time:</span>
<span class="tooltip-value">
{{ formatTime(session.startHour, session.startMinute) }} -
{{ formatTime(session.endHour, session.endMinute) }}
</span>
</div>
<div class="tooltip-row">
<i class="pi pi-hourglass"></i>
<span class="tooltip-label">Duration:</span>
<span class="tooltip-value">{{ formatDuration(session.duration) }}</span>
</div>
</div>
</div>
</div>
</div>
</div>
}
</div>
</div>
</div>
}
</div>
</div>
</div>
@@ -0,0 +1,483 @@
.timeline-container {
width: 100%;
}
.timeline-header {
display: flex;
justify-content: space-between;
align-items: flex-start;
margin-bottom: 0.5rem;
gap: 1rem;
.header-title {
flex: 1;
h3 {
color: var(--text-color, #ffffff);
font-size: 1.25rem;
font-weight: 500;
margin: 0 0 0.5rem 0;
}
.timeline-subtitle {
color: var(--text-secondary-color);
font-size: 0.9rem;
margin: 0;
line-height: 1.4;
}
}
.week-selector {
display: flex;
align-items: center;
gap: 1rem;
flex-shrink: 0;
.week-nav-btn {
background: rgba(255, 255, 255, 0.1);
border: 1px solid rgba(255, 255, 255, 0.2);
border-radius: 6px;
padding: 0.5rem 0.75rem;
color: #ffffff;
cursor: pointer;
transition: all 0.3s ease;
display: flex;
align-items: center;
justify-content: center;
&:hover {
background: rgba(255, 255, 255, 0.2);
border-color: rgba(255, 255, 255, 0.3);
transform: translateY(-1px);
}
&:active {
transform: translateY(0);
}
i {
font-size: 0.875rem;
}
}
.week-info {
display: flex;
flex-direction: column;
align-items: center;
min-width: 100px;
.week-label {
font-size: 1.125rem;
font-weight: 600;
color: #ffffff;
white-space: nowrap;
}
.week-dates {
font-size: 0.8rem;
color: var(--text-secondary-color);
white-space: nowrap;
}
}
}
}
.timeline-content {
position: relative;
width: 100%;
padding: 1rem;
}
.hour-markers {
display: flex;
margin-bottom: 0.5rem;
.day-label-spacer {
width: 100px;
flex-shrink: 0;
}
.hour-grid {
flex: 1;
display: grid;
grid-template-columns: repeat(24, 1fr);
position: relative;
}
.hour-marker {
position: relative;
height: 20px;
&.major {
.hour-label {
display: block;
}
}
}
.hour-label {
display: none;
font-size: 0.7rem;
color: rgba(255, 255, 255, 0.6);
position: absolute;
left: 0;
transform: translateX(-50%);
white-space: nowrap;
}
}
.timeline-rows {
display: flex;
flex-direction: column;
gap: 8px;
}
.timeline-row {
display: flex;
align-items: stretch;
.day-label {
width: 50px;
flex-shrink: 0;
display: flex;
align-items: center;
font-size: 0.875rem;
font-weight: 500;
color: rgba(255, 255, 255, 0.9);
padding-right: 1rem;
}
.timeline-track {
flex: 1;
position: relative;
background: rgba(255, 255, 255, 0.02);
border-radius: 4px;
border: 1px solid rgba(255, 255, 255, 0.1);
min-height: 40px;
}
}
.grid-lines {
position: absolute;
inset: 0;
display: grid;
grid-template-columns: repeat(24, 1fr);
pointer-events: none;
.grid-line {
border-right: 1px solid rgba(255, 255, 255, 0.05);
&:last-child {
border-right: none;
}
}
}
.sessions {
position: absolute;
inset: 4px;
pointer-events: none;
.session-block {
position: absolute;
border-radius: 4px;
pointer-events: auto;
cursor: pointer;
transition: all 0.2s ease;
overflow: visible;
box-sizing: border-box;
&.book-type-epub, &.book-type-default {
background: linear-gradient(135deg, #4a90e2 0%, #357abd 100%);
box-shadow: 0 2px 8px rgba(74, 144, 226, 0.3);
&:hover {
box-shadow: 0 4px 16px rgba(74, 144, 226, 0.5);
}
}
&.book-type-pdf {
background: linear-gradient(135deg, #e24a4a 0%, #bd3535 100%);
box-shadow: 0 2px 8px rgba(226, 74, 74, 0.3);
&:hover {
box-shadow: 0 4px 16px rgba(226, 74, 74, 0.5);
}
}
&.book-type-cbx {
background: linear-gradient(135deg, #e2b74a 0%, #bd9635 100%);
box-shadow: 0 2px 8px rgba(226, 183, 74, 0.3);
&:hover {
box-shadow: 0 4px 16px rgba(226, 183, 74, 0.5);
}
}
&:hover {
transform: translateY(-2px);
z-index: 10;
.session-tooltip {
opacity: 1;
transform: translate(-50%, 0);
pointer-events: auto;
}
}
.session-content {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100%;
padding: 4px 8px;
color: white;
font-size: 0.7rem;
font-weight: 500;
text-align: center;
gap: 2px;
overflow: hidden;
.session-time {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
max-width: 100%;
line-height: 1;
}
.session-duration {
font-size: 0.65rem;
opacity: 0.9;
white-space: nowrap;
line-height: 1;
}
}
}
}
.session-tooltip {
position: absolute;
bottom: calc(100% + 15px);
left: 50%;
transform: translate(-50%, 10px);
background: rgba(30, 30, 30, 0.98);
border: 1px solid rgba(255, 255, 255, 0.2);
border-radius: 8px;
padding: 0;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.5);
z-index: 1000;
pointer-events: none;
min-width: 320px;
max-width: 350px;
backdrop-filter: blur(10px);
opacity: 0;
transition: opacity 0.2s ease, transform 0.2s ease;
.tooltip-content {
display: flex;
gap: 1rem;
padding: 1rem;
}
.tooltip-cover {
flex-shrink: 0;
width: 80px;
height: 120px;
img {
width: 100%;
height: 100%;
object-fit: cover;
border-radius: 4px;
background-color: rgba(255, 255, 255, 0.1);
}
}
.tooltip-details {
flex: 1;
display: flex;
flex-direction: column;
min-width: 0;
}
.tooltip-header {
display: flex;
align-items: center;
gap: 0.75rem;
padding: 0;
background: none;
i {
font-size: 1.1rem;
color: #4a90e2;
}
.tooltip-title {
color: #ffffff;
font-weight: 600;
font-size: 0.95rem;
flex: 1;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
}
.tooltip-divider {
height: 1px;
background: linear-gradient(90deg,
transparent 0%,
rgba(255, 255, 255, 0.1) 50%,
transparent 100%);
margin: 0.75rem 0;
}
.tooltip-body {
padding: 0;
display: flex;
flex-direction: column;
gap: 0.625rem;
}
.tooltip-row {
display: grid;
grid-template-columns: auto auto 1fr;
align-items: center;
gap: 0.625rem;
font-size: 0.875rem;
i {
font-size: 0.875rem;
color: rgba(255, 255, 255, 0.6);
width: 16px;
text-align: center;
}
.tooltip-label {
color: rgba(255, 255, 255, 0.7);
font-weight: 500;
}
.tooltip-value {
color: #ffffff;
text-align: right;
font-weight: 500;
}
}
&::after {
content: '';
position: absolute;
top: 100%;
left: 50%;
transform: translateX(-50%);
width: 0;
height: 0;
border-left: 8px solid transparent;
border-right: 8px solid transparent;
border-top: 8px solid rgba(30, 30, 30, 0.98);
filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.2));
}
}
@media (max-width: 768px) {
.timeline-header {
flex-direction: column;
align-items: stretch;
.week-selector {
justify-content: center;
margin-top: 0.5rem;
.week-info {
min-width: 120px;
}
}
}
.timeline-content {
min-height: 300px;
padding: 0.75rem;
}
.timeline-row {
.day-label {
width: 70px;
font-size: 0.75rem;
padding-right: 0.5rem;
}
}
.hour-markers {
.day-label-spacer {
width: 70px;
}
}
.hour-marker {
.hour-label {
font-size: 0.6rem;
}
}
.session-block {
.session-content {
font-size: 0.65rem;
.session-duration {
display: none;
}
}
}
.session-tooltip {
min-width: 240px;
max-width: 280px;
font-size: 0.85rem;
.tooltip-header {
padding: 0.75rem;
.tooltip-title {
font-size: 0.875rem;
}
}
.tooltip-body {
padding: 0.75rem;
}
.tooltip-row {
font-size: 0.8rem;
gap: 0.5rem;
}
}
}
@media (max-width: 480px) {
.timeline-header {
.header-title {
h3 {
font-size: 1.1rem;
}
.timeline-subtitle {
font-size: 0.85rem;
}
}
}
.week-selector {
.week-info {
.week-label {
font-size: 1rem;
}
}
.week-nav-btn {
padding: 0.4rem 0.6rem;
}
}
}
@@ -0,0 +1,305 @@
import {Component, inject, Input, OnInit} from '@angular/core';
import {CommonModule} from '@angular/common';
import {UserStatsService, ReadingSessionTimelineResponse} from '../../../settings/user-management/user-stats.service';
import {UrlHelperService} from '../../../../shared/service/url-helper.service';
import {BookType} from '../../../book/model/book.model';
interface ReadingSession {
startTime: Date;
endTime: Date;
duration: number;
bookTitle?: string;
bookId: number;
bookType: BookType;
}
interface TimelineSession {
startHour: number;
startMinute: number;
endHour: number;
endMinute: number;
duration: number;
left: number;
width: number;
bookTitle?: string;
bookId: number;
bookType: BookType;
level: number;
totalLevels: number;
}
interface DayTimeline {
day: string;
dayOfWeek: number;
sessions: TimelineSession[];
}
@Component({
selector: 'app-reading-session-timeline',
standalone: true,
imports: [CommonModule],
templateUrl: './reading-session-timeline.component.html',
styleUrls: ['./reading-session-timeline.component.scss']
})
export class ReadingSessionTimelineComponent implements OnInit {
@Input() initialYear: number = new Date().getFullYear();
@Input() weekNumber: number = this.getCurrentWeekNumber();
private userStatsService = inject(UserStatsService);
private urlHelperService = inject(UrlHelperService);
public daysOfWeek = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
public hourLabels: string[] = [];
public timelineData: DayTimeline[] = [];
public currentYear: number = new Date().getFullYear();
public currentWeek: number = this.getCurrentWeekNumber();
ngOnInit(): void {
this.currentYear = this.initialYear;
this.currentWeek = this.weekNumber;
this.initializeHourLabels();
this.loadReadingSessions();
}
private initializeHourLabels(): void {
for (let i = 0; i < 24; i++) {
const hour = i === 0 ? 12 : i > 12 ? i - 12 : i;
const period = i < 12 ? 'AM' : 'PM';
this.hourLabels.push(`${hour} ${period}`);
}
}
private loadReadingSessions(): void {
this.userStatsService.getTimelineForWeek(this.currentYear, this.currentWeek)
.subscribe({
next: (response) => {
const sessions = this.convertResponseToSessions(response);
this.processSessionData(sessions);
},
error: (error) => {
console.error('Error loading reading sessions:', error);
this.processSessionData([]);
}
});
}
private convertResponseToSessions(response: ReadingSessionTimelineResponse[]): ReadingSession[] {
const sessions: ReadingSession[] = [];
response.forEach((item) => {
const startTime = new Date(item.startDate);
const endTime = new Date(item.endDate);
const duration = Math.floor((endTime.getTime() - startTime.getTime()) / (1000 * 60));
sessions.push({
startTime,
endTime,
duration,
bookId: item.bookId,
bookTitle: item.bookTitle,
bookType: item.bookType
});
});
return sessions.sort((a, b) => a.startTime.getTime() - b.startTime.getTime());
}
private getCurrentWeekNumber(): number {
const now = new Date();
const startOfYear = new Date(now.getFullYear(), 0, 1);
const days = Math.floor((now.getTime() - startOfYear.getTime()) / (24 * 60 * 60 * 1000));
return Math.ceil((days + startOfYear.getDay() + 1) / 7);
}
public changeWeek(delta: number): void {
this.currentWeek += delta;
const weeksInYear = this.getWeeksInYear(this.currentYear);
if (this.currentWeek > weeksInYear) {
this.currentWeek = 1;
this.currentYear++;
} else if (this.currentWeek < 1) {
this.currentYear--;
this.currentWeek = this.getWeeksInYear(this.currentYear);
}
this.loadReadingSessions();
}
private getWeeksInYear(year: number): number {
const lastDay = new Date(year, 11, 31);
const startOfYear = new Date(year, 0, 1);
const days = Math.floor((lastDay.getTime() - startOfYear.getTime()) / (24 * 60 * 60 * 1000));
return Math.ceil((days + startOfYear.getDay() + 1) / 7);
}
public getWeekDateRange(): string {
const startOfYear = new Date(this.currentYear, 0, 1);
const daysToAdd = (this.currentWeek - 1) * 7;
const weekStart = new Date(startOfYear.getTime() + daysToAdd * 24 * 60 * 60 * 1000);
const dayOfWeek = weekStart.getDay();
weekStart.setDate(weekStart.getDate() - dayOfWeek);
const weekEnd = new Date(weekStart);
weekEnd.setDate(weekStart.getDate() + 6);
const formatDate = (date: Date) => {
const month = date.toLocaleDateString('en-US', {month: 'short'});
const day = date.getDate();
return `${month} ${day}`;
};
return `${formatDate(weekStart)} - ${formatDate(weekEnd)}`;
}
private processSessionData(sessions: ReadingSession[]): void {
const dayMap = new Map<number, ReadingSession[]>();
sessions.forEach(session => {
const sessionStart = new Date(session.startTime);
const sessionEnd = new Date(session.endTime);
if (sessionStart.getDate() === sessionEnd.getDate()) {
const dayOfWeek = sessionStart.getDay();
if (!dayMap.has(dayOfWeek)) {
dayMap.set(dayOfWeek, []);
}
dayMap.get(dayOfWeek)!.push(session);
} else {
let currentStart = new Date(sessionStart);
while (currentStart < sessionEnd) {
const dayOfWeek = currentStart.getDay();
const endOfDay = new Date(currentStart);
endOfDay.setHours(23, 59, 59, 999);
const segmentEnd = sessionEnd < endOfDay ? sessionEnd : endOfDay;
const segmentDuration = Math.floor((segmentEnd.getTime() - currentStart.getTime()) / (1000 * 60));
if (!dayMap.has(dayOfWeek)) {
dayMap.set(dayOfWeek, []);
}
dayMap.get(dayOfWeek)!.push({
startTime: new Date(currentStart),
endTime: new Date(segmentEnd),
duration: segmentDuration,
bookTitle: session.bookTitle,
bookId: session.bookId,
bookType: session.bookType
});
currentStart = new Date(segmentEnd);
currentStart.setDate(currentStart.getDate() + 1);
currentStart.setHours(0, 0, 0, 0);
}
}
});
this.timelineData = [];
for (let i = 0; i < 7; i++) {
const sessionsForDay = dayMap.get(i) || [];
const timelineSessions = this.layoutSessionsForDay(sessionsForDay);
this.timelineData.push({
day: this.daysOfWeek[i],
dayOfWeek: i,
sessions: timelineSessions
});
}
}
private layoutSessionsForDay(sessions: ReadingSession[]): TimelineSession[] {
if (sessions.length === 0) {
return [];
}
sessions.sort((a, b) => {
if (a.startTime.getTime() !== b.startTime.getTime()) {
return a.startTime.getTime() - b.startTime.getTime();
}
return b.endTime.getTime() - a.endTime.getTime();
});
const tracks: ReadingSession[][] = [];
sessions.forEach(session => {
let placed = false;
for (let i = 0; i < tracks.length; i++) {
const lastSessionInTrack = tracks[i][tracks[i].length - 1];
if (session.startTime >= lastSessionInTrack.endTime) {
tracks[i].push(session);
placed = true;
break;
}
}
if (!placed) {
tracks.push([session]);
}
});
const totalLevels = tracks.length;
const timelineSessions: TimelineSession[] = [];
tracks.forEach((track, level) => {
track.forEach(session => {
timelineSessions.push(this.convertToTimelineSession(session, level, totalLevels));
});
});
return timelineSessions;
}
private convertToTimelineSession(session: ReadingSession, level: number, totalLevels: number): TimelineSession {
const startHour = session.startTime.getHours();
const startMinute = session.startTime.getMinutes();
const endHour = session.endTime.getHours();
const endMinute = session.endTime.getMinutes();
const startDecimal = startHour + startMinute / 60;
const endDecimal = endHour + endMinute / 60;
const left = (startDecimal / 24) * 100;
let width = ((endDecimal - startDecimal) / 24) * 100;
if (width < 0.5) {
width = 0.5;
}
return {
startHour,
startMinute,
endHour,
endMinute,
duration: session.duration,
left,
width,
bookTitle: session.bookTitle,
bookId: session.bookId,
bookType: session.bookType,
level,
totalLevels
};
}
public formatTime(hour: number, minute: number): string {
const displayHour = hour === 0 ? 12 : hour > 12 ? hour - 12 : hour;
const period = hour < 12 ? 'AM' : 'PM';
const displayMinute = minute.toString().padStart(2, '0');
return `${displayHour}:${displayMinute} ${period}`;
}
public formatDuration(minutes: number): string {
const hours = Math.floor(minutes / 60);
const mins = minutes % 60;
if (hours > 0) {
return mins > 0 ? `${hours}h ${mins}m` : `${hours}h`;
}
return `${mins}m`;
}
public getCoverUrl(bookId: number): string {
return this.urlHelperService.getThumbnailUrl1(bookId);
}
}