feat(viewer): add context-aware series collapse functionality (#2073)

- Introduced support for context-specific series collapse preferences for libraries, shelves, and magic shelves.
- Improved backward compatibility with legacy preference structures.
- Added JSON aliasing for `seriesCollapse` to maintain API compatibility.
- Extended entity selection in view preferences to include magic shelves.
- Enhanced error handling in app settings service to filter null values.
- Optimized series collapse preference management, ensuring local and global overrides persist accurately.

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>
This commit is contained in:
Balázs Szücs
2026-01-01 00:38:46 -07:00
committed by GitHub
parent ff86a93a9f
commit a72f6e285c
7 changed files with 150 additions and 22 deletions
@@ -261,7 +261,7 @@
class="grid"
#container
[ngStyle]="{'grid-template-columns': 'repeat(auto-fill, minmax(' + gridColumnMinWidth + ', 1fr))'}">
@for (book of scroll.viewPortItems; let i = $index; track book) {
@for (book of scroll.viewPortItems; let i = $index; track book.id) {
<div
class="virtual-scroller-item relative"
[ngStyle]="{width: currentCardSize.width + 'px', height: currentCardSize.height + 'px'}">
@@ -222,6 +222,7 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
this.entityType = entityType;
this.entityType$ = of(entityType);
this.entity$ = of(null);
this.seriesCollapseFilter.setContext(null, null);
this.pageTitle.setPageTitle(currentPath === 'all-books' ? 'All Books' : 'Unshelved Books');
} else {
@@ -235,6 +236,7 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
this.pageTitle.setPageTitle(entity.name);
}
this.entity = entity ?? null;
this.updateSeriesCollapseContext();
this.entityOptions = entity
? this.isLibrary(entity)
? this.libraryShelfMenuService.initializeLibraryMenuItems(entity)
@@ -711,6 +713,30 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
this.dialogHelperService.openFileMoverDialog(this.selectedBooks);
}
private updateSeriesCollapseContext() {
let type: 'LIBRARY' | 'SHELF' | 'MAGIC_SHELF' | null = null;
let id: number | null = null;
if (this.entity && this.entityType) {
switch (this.entityType) {
case EntityType.LIBRARY:
type = 'LIBRARY';
id = this.entity.id ?? 0;
break;
case EntityType.SHELF:
type = 'SHELF';
id = this.entity.id ?? 0;
break;
case EntityType.MAGIC_SHELF:
type = 'MAGIC_SHELF';
id = this.entity.id ?? 0;
break;
}
}
this.seriesCollapseFilter.setContext(type, id);
}
private isLibrary(entity: Library | Shelf | MagicShelf): entity is Library {
return (entity as Library).paths !== undefined;
}
@@ -1,7 +1,7 @@
import {BookFilter} from './BookFilter';
import {BehaviorSubject, Observable, Subject} from 'rxjs';
import {BookState} from '../../../model/state/book-state.model';
import {debounceTime, filter, map, take, takeUntil} from 'rxjs/operators';
import {debounceTime, filter, map, takeUntil} from 'rxjs/operators';
import {Book} from '../../../model/book.model';
import {inject, Injectable, OnDestroy} from '@angular/core';
import {MessageService} from 'primeng/api';
@@ -17,18 +17,16 @@ export class SeriesCollapseFilter implements BookFilter, OnDestroy {
private destroy$ = new Subject<void>();
private hasUserToggled = false;
private currentContext: { type: 'LIBRARY' | 'SHELF' | 'MAGIC_SHELF', id: number } | null = null;
constructor() {
this.userService.userState$
.pipe(
filter(userState => !!userState?.user && userState.loaded),
take(1),
takeUntil(this.destroy$)
)
.subscribe(user => {
const prefs = user.user?.userSettings?.entityViewPreferences;
const initialCollapsed = prefs?.global?.seriesCollapsed ?? false;
this.seriesCollapseSubject.next(initialCollapsed);
.subscribe(() => {
this.applyPreference();
});
this.seriesCollapse$
@@ -40,6 +38,15 @@ export class SeriesCollapseFilter implements BookFilter, OnDestroy {
});
}
setContext(type: 'LIBRARY' | 'SHELF' | 'MAGIC_SHELF' | null, id: number | null): void {
if (type && id) {
this.currentContext = {type, id};
} else {
this.currentContext = null;
}
this.applyPreference();
}
get isSeriesCollapsed(): boolean {
return this.seriesCollapseSubject.value;
}
@@ -49,6 +56,38 @@ export class SeriesCollapseFilter implements BookFilter, OnDestroy {
this.seriesCollapseSubject.next(value);
}
private applyPreference(): void {
const user = this.userService.getCurrentUser();
const prefs = user?.userSettings?.entityViewPreferences;
let collapsed = false;
if (prefs) {
// Backward compatibility: check for old 'seriesCollapse' field
const globalAny = prefs.global as any;
collapsed = prefs.global?.seriesCollapsed ?? globalAny?.seriesCollapse ?? false;
if (this.currentContext) {
const override = prefs.overrides?.find(o =>
o.entityType === this.currentContext?.type && o.entityId === this.currentContext?.id
);
if (override) {
const prefAny = override.preferences as any;
if (override.preferences.seriesCollapsed !== undefined) {
collapsed = override.preferences.seriesCollapsed;
} else if (prefAny?.seriesCollapse !== undefined) {
collapsed = prefAny.seriesCollapse;
}
}
}
}
this.hasUserToggled = false;
if (this.seriesCollapseSubject.value !== collapsed) {
this.seriesCollapseSubject.next(collapsed);
}
}
filter(bookState: BookState, forceExpandSeries?: boolean): Observable<BookState> {
return this.seriesCollapse$.pipe(
map(isCollapsed => {
@@ -93,18 +132,51 @@ export class SeriesCollapseFilter implements BookFilter, OnDestroy {
private persistCollapsePreference(isCollapsed: boolean): void {
const user = this.userService.getCurrentUser();
const prefs = user?.userSettings?.entityViewPreferences;
if (!user || !prefs) return;
if (!user) return;
prefs.global = prefs.global ?? {};
prefs.global.seriesCollapsed = isCollapsed;
const prefs = structuredClone(user.userSettings.entityViewPreferences ?? {
global: {
sortKey: 'addedOn',
sortDir: 'DESC',
view: 'GRID',
coverSize: 1.0,
seriesCollapsed: false
},
overrides: []
});
if (!prefs.overrides) {
prefs.overrides = [];
}
if (this.currentContext) {
let override = prefs.overrides.find(o =>
o.entityType === this.currentContext?.type && o.entityId === this.currentContext?.id
);
if (!override) {
override = {
entityType: this.currentContext.type,
entityId: this.currentContext.id,
preferences: {
...prefs.global,
seriesCollapsed: isCollapsed
}
};
prefs.overrides.push(override);
} else {
override.preferences.seriesCollapsed = isCollapsed;
}
} else {
prefs.global.seriesCollapsed = isCollapsed;
}
this.userService.updateUserSetting(user.id, 'entityViewPreferences', prefs);
this.messageService.add({
severity: 'success',
summary: 'Preference Saved',
detail: `Series collapse set to ${isCollapsed ? 'enabled' : 'disabled'}.`,
detail: `Series collapse set to ${isCollapsed ? 'enabled' : 'disabled'}${this.currentContext ? ' for this view' : ' globally'}.`,
life: 1500
});
}