From a72f6e285c1614aa1ef1cf1a547ce6034df5509d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Sz=C3=BCcs?= <127139797+balazs-szucs@users.noreply.github.com> Date: Thu, 1 Jan 2026 08:38:46 +0100 Subject: [PATCH] feat(viewer): add context-aware series collapse functionality (#2073) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- .../booklore/model/dto/BookLoreUser.java | 5 +- .../appsettings/AppSettingService.java | 4 +- .../book-browser/book-browser.component.html | 2 +- .../book-browser/book-browser.component.ts | 26 +++++ .../filters/SeriesCollapseFilter.ts | 94 ++++++++++++++++--- .../settings/user-management/user.service.ts | 2 +- .../view-preferences.component.ts | 39 ++++++-- 7 files changed, 150 insertions(+), 22 deletions(-) diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/model/dto/BookLoreUser.java b/booklore-api/src/main/java/com/adityachandel/booklore/model/dto/BookLoreUser.java index e04074e7a..03d798be5 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/model/dto/BookLoreUser.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/model/dto/BookLoreUser.java @@ -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 diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/appsettings/AppSettingService.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/appsettings/AppSettingService.java index 86bc212cf..139ef7dd3 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/appsettings/AppSettingService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/appsettings/AppSettingService.java @@ -95,7 +95,9 @@ public class AppSettingService { } private Map 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() { diff --git a/booklore-ui/src/app/features/book/components/book-browser/book-browser.component.html b/booklore-ui/src/app/features/book/components/book-browser/book-browser.component.html index 48663c95c..6e38b76df 100644 --- a/booklore-ui/src/app/features/book/components/book-browser/book-browser.component.html +++ b/booklore-ui/src/app/features/book/components/book-browser/book-browser.component.html @@ -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) {
diff --git a/booklore-ui/src/app/features/book/components/book-browser/book-browser.component.ts b/booklore-ui/src/app/features/book/components/book-browser/book-browser.component.ts index cc618bf89..502d8ba13 100644 --- a/booklore-ui/src/app/features/book/components/book-browser/book-browser.component.ts +++ b/booklore-ui/src/app/features/book/components/book-browser/book-browser.component.ts @@ -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; } diff --git a/booklore-ui/src/app/features/book/components/book-browser/filters/SeriesCollapseFilter.ts b/booklore-ui/src/app/features/book/components/book-browser/filters/SeriesCollapseFilter.ts index 207a6d9f6..0a82440ce 100644 --- a/booklore-ui/src/app/features/book/components/book-browser/filters/SeriesCollapseFilter.ts +++ b/booklore-ui/src/app/features/book/components/book-browser/filters/SeriesCollapseFilter.ts @@ -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(); 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 { 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 }); } diff --git a/booklore-ui/src/app/features/settings/user-management/user.service.ts b/booklore-ui/src/app/features/settings/user-management/user.service.ts index 2be4b81e0..6d91c5605 100644 --- a/booklore-ui/src/app/features/settings/user-management/user.service.ts +++ b/booklore-ui/src/app/features/settings/user-management/user.service.ts @@ -21,7 +21,7 @@ export interface EntityViewPreference { } export interface EntityViewPreferenceOverride { - entityType: 'LIBRARY' | 'SHELF'; + entityType: 'LIBRARY' | 'SHELF' | 'MAGIC_SHELF'; entityId: number; preferences: EntityViewPreference; } diff --git a/booklore-ui/src/app/features/settings/view-preferences-parent/view-preferences/view-preferences.component.ts b/booklore-ui/src/app/features/settings/view-preferences-parent/view-preferences/view-preferences.component.ts index 043494672..bc319c552 100644 --- a/booklore-ui/src/app/features/settings/view-preferences-parent/view-preferences/view-preferences.component.ts +++ b/booklore-ui/src/app/features/settings/view-preferences-parent/view-preferences/view-preferences.component.ts @@ -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}`)); }