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
@@ -2,6 +2,7 @@ package com.adityachandel.booklore.model.dto;
import com.adityachandel.booklore.model.dto.settings.SidebarSortOption;
import com.adityachandel.booklore.model.enums.*;
import com.fasterxml.jackson.annotation.JsonAlias;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
@@ -93,6 +94,7 @@ public class BookLoreUser {
private String sortDir;
private String view;
private Float coverSize;
@JsonAlias("seriesCollapse")
private Boolean seriesCollapsed;
}
@@ -114,7 +116,8 @@ public class BookLoreUser {
private String sortKey;
private String sortDir;
private String view;
private Boolean seriesCollapse;
@JsonAlias("seriesCollapse")
private Boolean seriesCollapsed;
}
@Data
@@ -95,7 +95,9 @@ public class AppSettingService {
}
private Map<String, String> getSettingsMap() {
return settingPersistenceHelper.appSettingsRepository.findAll().stream().collect(Collectors.toMap(AppSettingEntity::getName, AppSettingEntity::getVal));
return settingPersistenceHelper.appSettingsRepository.findAll().stream()
.filter(entity -> entity.getName() != null && entity.getVal() != null)
.collect(Collectors.toMap(AppSettingEntity::getName, AppSettingEntity::getVal));
}
private PublicAppSetting buildPublicSetting() {
@@ -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
});
}
@@ -21,7 +21,7 @@ export interface EntityViewPreference {
}
export interface EntityViewPreferenceOverride {
entityType: 'LIBRARY' | 'SHELF';
entityType: 'LIBRARY' | 'SHELF' | 'MAGIC_SHELF';
entityId: number;
preferences: EntityViewPreference;
}
@@ -7,6 +7,7 @@ import {TableModule} from 'primeng/table';
import {User, UserService} from '../../user-management/user.service';
import {LibraryService} from '../../../book/service/library.service';
import {ShelfService} from '../../../book/service/shelf.service';
import {MagicShelfService} from '../../../magic-shelf/service/magic-shelf.service';
import {combineLatest, Subject} from 'rxjs';
import {FormsModule} from '@angular/forms';
import {ToastModule} from 'primeng/toast';
@@ -52,7 +53,8 @@ export class ViewPreferencesComponent implements OnInit, OnDestroy {
entityTypeOptions = [
{label: 'Library', value: 'LIBRARY'},
{label: 'Shelf', value: 'SHELF'}
{label: 'Shelf', value: 'SHELF'},
{label: 'Magic Shelf', value: 'MAGIC_SHELF'}
];
sortDirectionOptions = [
@@ -67,13 +69,14 @@ export class ViewPreferencesComponent implements OnInit, OnDestroy {
libraryOptions: { label: string; value: number }[] = [];
shelfOptions: { label: string; value: number }[] = [];
magicShelfOptions: { label: string; value: number }[] = [];
selectedSort: string = 'title';
selectedSortDir: 'ASC' | 'DESC' = 'ASC';
selectedView: 'GRID' | 'TABLE' = 'GRID';
overrides: {
entityType: 'LIBRARY' | 'SHELF';
entityType: 'LIBRARY' | 'SHELF' | 'MAGIC_SHELF';
library: number;
sort: string;
sortDir: 'ASC' | 'DESC';
@@ -85,6 +88,7 @@ export class ViewPreferencesComponent implements OnInit, OnDestroy {
private libraryService = inject(LibraryService);
private shelfService = inject(ShelfService);
private magicShelfService = inject(MagicShelfService);
private userService = inject(UserService);
private messageService = inject(MessageService);
@@ -93,9 +97,10 @@ export class ViewPreferencesComponent implements OnInit, OnDestroy {
this.userService.userState$.pipe(filter(userState => !!userState?.user && userState.loaded), take(1)),
this.libraryService.libraryState$.pipe(filter(libraryState => !!libraryState?.libraries && libraryState.loaded), take(1)),
this.shelfService.shelfState$.pipe(filter(shelfState => !!shelfState?.shelves && shelfState.loaded), take(1)),
this.magicShelfService.shelvesState$.pipe(filter(magicState => !!magicState?.shelves && magicState.loaded), take(1))
]).pipe(
takeUntil(this.destroy$)
).subscribe(([userState, librariesState, shelfState]) => {
).subscribe(([userState, librariesState, shelfState, magicState]) => {
this.user = userState.user;
const prefs = userState.user?.userSettings?.entityViewPreferences;
@@ -121,6 +126,11 @@ export class ViewPreferencesComponent implements OnInit, OnDestroy {
label: s.name,
value: s.id!
}));
this.magicShelfOptions = (magicState?.shelves ?? []).filter(s => s.id !== undefined).map(s => ({
label: s.name,
value: s.id!
}));
});
}
@@ -129,19 +139,34 @@ export class ViewPreferencesComponent implements OnInit, OnDestroy {
this.destroy$.complete();
}
getAvailableEntities(index: number, type: 'LIBRARY' | 'SHELF') {
getAvailableEntities(index: number, type: 'LIBRARY' | 'SHELF' | 'MAGIC_SHELF') {
const selected = this.overrides.map((o, i) => i !== index ? o.library : null);
const source = type === 'LIBRARY' ? this.libraryOptions : this.shelfOptions;
let source: { label: string; value: number }[];
switch (type) {
case 'LIBRARY':
source = this.libraryOptions;
break;
case 'SHELF':
source = this.shelfOptions;
break;
case 'MAGIC_SHELF':
source = this.magicShelfOptions;
break;
default:
source = [];
}
return source.filter(opt => !selected.includes(opt.value) || this.overrides[index]?.library === opt.value);
}
get availableLibraries() {
const used = new Set(this.overrides.map(o => `${o.entityType}_${o.library}`));
const withEntityType = (options: { label: string; value: number }[], entityType: 'LIBRARY' | 'SHELF') =>
const withEntityType = (options: { label: string; value: number }[], entityType: 'LIBRARY' | 'SHELF' | 'MAGIC_SHELF') =>
options.map(opt => ({...opt, entityType}));
return [...withEntityType(this.libraryOptions, 'LIBRARY'), ...withEntityType(this.shelfOptions, 'SHELF')]
return [...withEntityType(this.libraryOptions, 'LIBRARY'),
...withEntityType(this.shelfOptions, 'SHELF'),
...withEntityType(this.magicShelfOptions, 'MAGIC_SHELF')]
.filter(opt => !used.has(`${opt.entityType}_${opt.value}`));
}