Persist cover scale preference per device using LocalStorage

- Store cover scale preference locally to allow different settings per device (desktop, mobile)
- Disabled backend saving for cover scale but kept code commented for future reference
- Load scale preference from LocalStorage on init for faster and device-specific UX
This commit is contained in:
aditya.chandel
2025-07-21 17:27:32 -06:00
committed by Aditya Chandel
parent ca5a09f740
commit 49c4519cf7
4 changed files with 54 additions and 19 deletions
@@ -275,7 +275,7 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
this.entityViewPreferences = user.userSettings?.entityViewPreferences;
const globalPrefs = this.entityViewPreferences?.global;
const currentEntityTypeStr = this.entityType ? this.entityType.toString().toUpperCase() : undefined;
this.coverScalePreferenceService.initScaleValue(user?.userSettings?.entityViewPreferences?.global?.coverSize);
this.coverScalePreferenceService.initScaleValue(this.coverScalePreferenceService.scaleFactor);
this.filterSortPreferenceService.initValue(user?.userSettings?.filterSortingMode);
this.columnPreferenceService.initPreferences(user.userSettings?.tableColumnPreference);
this.visibleColumns = this.columnPreferenceService.visibleColumns;
@@ -1,8 +1,8 @@
import {Injectable, inject} from '@angular/core';
import {inject, Injectable} from '@angular/core';
import {Subject} from 'rxjs';
import {debounceTime} from 'rxjs/operators';
import {MessageService} from 'primeng/api';
import {UserService} from '../../../settings/user-management/user.service';
import {LocalStorageService} from '../../../core/service/local-storage-service';
@Injectable({
providedIn: 'root'
@@ -12,9 +12,10 @@ export class CoverScalePreferenceService {
private readonly BASE_WIDTH = 135;
private readonly BASE_HEIGHT = 220;
private readonly DEBOUNCE_MS = 1000;
private readonly STORAGE_KEY = 'coverScalePreference';
private readonly messageService = inject(MessageService);
private readonly userService = inject(UserService);
private readonly localStorageService = inject(LocalStorageService);
private readonly scaleChangeSubject = new Subject<number>();
readonly scaleChange$ = this.scaleChangeSubject.asObservable();
@@ -22,6 +23,8 @@ export class CoverScalePreferenceService {
scaleFactor = 1.0;
constructor() {
this.loadScaleFromStorage();
this.scaleChange$
.pipe(debounceTime(this.DEBOUNCE_MS))
.subscribe(scale => this.saveScalePreference(scale));
@@ -48,19 +51,28 @@ export class CoverScalePreferenceService {
}
private saveScalePreference(scale: number): void {
const user = this.userService.getCurrentUser();
const prefs = user?.userSettings?.entityViewPreferences;
try {
this.localStorageService.set(this.STORAGE_KEY, scale);
this.messageService.add({
severity: 'success',
summary: 'Cover Size Saved',
detail: `Cover size set to ${scale.toFixed(2)}x.`,
life: 1500
});
} catch (e) {
this.messageService.add({
severity: 'error',
summary: 'Save Failed',
detail: 'Could not save cover size preference locally.',
life: 3000
});
}
}
if (!user || !prefs) return;
prefs.global = {...prefs.global, coverSize: scale};
this.userService.updateUserSetting(user.id, 'entityViewPreferences', prefs);
this.messageService.add({
severity: 'success',
summary: 'Cover Size Saved',
detail: `Cover size set to ${scale.toFixed(2)}x.`,
life: 1500
});
private loadScaleFromStorage(): void {
const saved = this.localStorageService.get<number>(this.STORAGE_KEY);
if (saved !== null && !isNaN(saved)) {
this.scaleFactor = saved;
}
}
}
@@ -0,0 +1,25 @@
import {Injectable} from '@angular/core';
@Injectable({providedIn: 'root'})
export class LocalStorageService {
get<T>(key: string): T | null {
try {
const value = localStorage.getItem(key);
return value ? JSON.parse(value) as T : null;
} catch {
return null;
}
}
set<T>(key: string, value: T): void {
try {
localStorage.setItem(key, JSON.stringify(value));
} catch {
}
}
remove(key: string): void {
localStorage.removeItem(key);
}
}
@@ -15,7 +15,6 @@ import {BookService} from '../../book/service/book.service';
import {AppSettingKey, AppSettings} from '../../core/model/app-settings.model';
import {filter, take} from 'rxjs/operators';
import {FileUploadPatternComponent} from './file-upload-pattern/file-upload-pattern.component';
import {OpdsSettingsComponent} from './opds-settings/opds-settings.component';
import {InputText} from 'primeng/inputtext';
@Component({
@@ -30,7 +29,6 @@ import {InputText} from 'primeng/inputtext';
ToggleSwitch,
FormsModule,
FileUploadPatternComponent,
OpdsSettingsComponent,
InputText
],
templateUrl: './global-preferences.component.html',