WIP: App Settings

This commit is contained in:
aditya.chandel
2025-01-15 17:38:45 -07:00
parent 9313dad2c3
commit f0123c9776
23 changed files with 497 additions and 15 deletions
@@ -0,0 +1,5 @@
<div class="flex flex-col min-w-20">
<p>{{latestEvent.timestamp}}</p>
<p>{{latestEvent.message}}</p>
</div>
@@ -0,0 +1,25 @@
import {Component, Input} from '@angular/core';
import {LogNotification} from '../../../shared/websocket/model/log-notification.model';
import {EventService} from '../../../shared/websocket/event.service';
@Component({
selector: 'app-live-notification-box',
standalone: true,
templateUrl: './live-notification-box.component.html',
styleUrl: './live-notification-box.component.scss',
host: {
class: 'config-panel hidden'
},
})
export class LiveNotificationBoxComponent {
latestEvent: LogNotification = {message: 'No recent notifications...'};
constructor(private eventService: EventService) {
this.eventService.latestEvent$.subscribe(event => {
this.latestEvent = event;
});
}
}
@@ -0,0 +1,83 @@
<p class="text-3xl font-semibold">Preferences</p>
<p-divider></p-divider>
<p class="text-xl font-medium pb-4 pt-6">PDF Reader Settings</p>
<div class="grid grid-cols-[auto,1fr] pl-6 gap-y-4 gap-x-4 items-center">
<p class="text-lg font-medium py-1">Page Spread:</p>
<div class="flex gap-4 justify-start">
<div *ngFor="let spread of spreads" class="flex items-center gap-2">
<p-radiobutton
[inputId]="spread.key"
name="spread"
[value]="spread.key"
[(ngModel)]="selectedSpread"
(ngModelChange)="onSpreadChange()">
</p-radiobutton>
<label [for]="spread.key">{{ spread.name }}</label>
</div>
</div>
<p class="text-lg font-medium py-1">Page Zoom:</p>
<div class="flex gap-4 justify-start">
<div *ngFor="let zoom of zooms" class="flex items-center gap-2">
<p-radiobutton
[inputId]="zoom.key"
name="zoom"
[value]="zoom.key"
[(ngModel)]="selectedZoom"
(ngModelChange)="onZoomChange()">
</p-radiobutton>
<label [for]="zoom.key">{{ zoom.name }}</label>
</div>
</div>
<p class="text-lg font-medium py-1">Show Sidebar:</p>
<div class="flex justify-start">
<p-toggleswitch
[(ngModel)]="showSidebar"
(ngModelChange)="onSidebarChange()">
</p-toggleswitch>
</div>
</div>
<p-divider></p-divider>
<p class="text-xl font-medium pb-4 pt-4">ePUB Reader Settings</p>
<div class="grid grid-cols-[auto,1fr] pl-6 gap-y-4 gap-x-4 items-center">
<p class="text-lg font-medium">Theme:</p>
<div class="flex gap-4 justify-start">
<p-select
[options]="themes"
[(ngModel)]="selectedTheme"
optionLabel="name"
optionValue="key"
placeholder="Select a Theme"
class="w-full md:w-56"
(ngModelChange)="onThemeChange()">
</p-select>
</div>
<p class="text-lg font-medium">Font:</p>
<div class="flex gap-4 justify-start">
<p-select
[options]="fonts"
[(ngModel)]="selectedFont"
optionLabel="name"
optionValue="key"
placeholder="Select a Font"
class="w-full md:w-56"
(ngModelChange)="onFontChange()">
</p-select>
</div>
<p class="text-lg font-medium py-1">Font Size:</p>
<div class="flex gap-4 justify-start items-center">
<p-button icon="pi pi-minus" rounded="true" (click)="decreaseFontSize()"></p-button>
<p>{{ fontSize }}%</p>
<p-button icon="pi pi-plus" rounded="true" (click)="increaseFontSize()"></p-button>
</div>
</div>
@@ -0,0 +1,132 @@
import {Component, OnInit} from '@angular/core';
import {AppSettingsService} from '../../service/app-settings.service';
import {Observable} from 'rxjs';
import {AppSettings} from '../../model/app-settings.model';
import {Select} from 'primeng/select';
import {FormsModule} from '@angular/forms';
import {NgForOf} from '@angular/common';
import {ToggleSwitch} from 'primeng/toggleswitch';
import {RadioButton} from 'primeng/radiobutton';
import {Divider} from 'primeng/divider';
import {Button} from 'primeng/button';
@Component({
selector: 'app-settings',
templateUrl: './settings.component.html',
imports: [
Select,
FormsModule,
NgForOf,
ToggleSwitch,
RadioButton,
Divider,
Button
],
styleUrls: ['./settings.component.scss']
})
export class SettingsComponent implements OnInit {
spreads = [
{name: 'Even Spread', key: 'even_spread'},
{name: 'Odd Spread', key: 'odd_spread'},
{name: 'No Spread', key: 'no_spread'}
];
zooms = [
{name: 'Auto Zoom', key: 'auto_zoom'},
{name: 'Page Fit', key: 'page_fit'},
{name: 'Page Width', key: 'page_width'},
{name: 'Actual Size', key: 'actual_size'}
];
themes = [
{name: 'White', key: 'white'},
{name: 'Black', key: 'black'},
{name: 'Grey', key: 'grey'},
{name: 'Sepia', key: 'sepia'}
];
fonts = [
{name: 'Serif', key: 'serif'},
{name: 'Sans Serif', key: 'sans-serif'},
{name: 'Roboto', key: 'roboto'},
{name: 'Cursive', key: 'cursive'},
{name: 'Monospace', key: 'monospace'}
];
selectedSpread: any;
selectedZoom: any;
showSidebar = false;
selectedTheme!: any;
fontSize: number = 100;
selectedFont: any;
appSettings$!: Observable<AppSettings | null>;
constructor(private appSettingsService: AppSettingsService) {
}
ngOnInit(): void {
this.appSettings$ = this.appSettingsService.appSettings$;
this.appSettings$.subscribe(settings => {
if (settings) {
this.populateSettings(settings);
}
});
}
populateSettings(settings: AppSettings): void {
if (settings.pdf) {
const pdfSettings = settings.pdf;
console.log(pdfSettings)
this.selectedSpread = this.spreads.find(s => s.key === pdfSettings.spread)?.key;
this.selectedZoom = this.zooms.find(z => z.key === pdfSettings.zoom)?.key;
this.showSidebar = pdfSettings.sidebar;
}
if (settings.epub) {
const epubSettings = settings.epub;
this.selectedTheme = this.themes.find(t => t.key === epubSettings.theme)?.key;
console.log(epubSettings.fontSize)
this.fontSize = parseInt(epubSettings.fontSize, 10);
this.selectedFont = this.fonts.find(f => f.key === epubSettings.font)?.key;
}
}
onThemeChange(): void {
this.appSettingsService.saveAppSetting('epub', 'theme', this.selectedTheme);
}
onFontChange(): void {
this.appSettingsService.saveAppSetting('epub', 'font', this.selectedFont);
}
onSpreadChange(): void {
this.appSettingsService.saveAppSetting('pdf', 'spread', this.selectedSpread);
}
onZoomChange(): void {
this.appSettingsService.saveAppSetting('pdf', 'zoom', this.selectedZoom);
}
onSidebarChange(): void {
this.appSettingsService.saveAppSetting('pdf', 'sidebar', this.showSidebar.toString());
}
onFontSizeChange(): void {
this.appSettingsService.saveAppSetting('epub', 'fontSize', this.fontSize.toString());
}
increaseFontSize(): void {
if (this.fontSize < 250) {
this.fontSize += 10;
this.onFontSizeChange();
}
}
decreaseFontSize(): void {
if (this.fontSize > 50) {
this.fontSize -= 10;
this.onFontSizeChange();
}
}
}
@@ -0,0 +1,16 @@
export interface EpubSettings {
theme: string;
fontSize: string;
font: string;
}
export interface PdfSettings {
spread: string;
zoom: string;
sidebar: boolean;
}
export interface AppSettings {
epub?: EpubSettings;
pdf?: PdfSettings;
}
@@ -0,0 +1,46 @@
import {Injectable} from '@angular/core';
import {HttpClient, HttpParams} from '@angular/common/http';
import {BehaviorSubject} from 'rxjs';
import {AppSettings} from '../model/app-settings.model';
@Injectable({
providedIn: 'root'
})
export class AppSettingsService {
private apiUrl = 'http://localhost:8080/api/settings';
private appSettingsSubject = new BehaviorSubject<AppSettings | null>(null);
appSettings$ = this.appSettingsSubject.asObservable();
constructor(private http: HttpClient) {
this.loadAppSettings();
}
loadAppSettings(): void {
this.http.get<AppSettings>(this.apiUrl).subscribe({
next: (settings: AppSettings) => {
this.appSettingsSubject.next(settings);
},
error: (error) => {
console.error('Error loading app settings:', error);
this.appSettingsSubject.next(null);
}
});
}
saveAppSetting(category: string, key: string, newValue: string): void {
const params = new HttpParams()
.set('category', category)
.set('key', key)
.set('newValue', newValue);
this.http.put(this.apiUrl, null, { params }).subscribe({
next: () => {
console.log('Setting saved successfully');
},
error: (error) => {
console.error('Error saving setting:', error);
}
});
}
}