refactor(ui): move theme and locale settings location (#1624)
This commit is contained in:
@@ -471,6 +471,13 @@ export class UserService {
|
||||
);
|
||||
}
|
||||
|
||||
async updateCurrentUserProfile(updateData: UserProfileUpdateRequest): Promise<User | null> {
|
||||
const user = this.currentUser();
|
||||
if (!user) return null;
|
||||
|
||||
return lastValueFrom(this.updateUserProfile(user.id, updateData));
|
||||
}
|
||||
|
||||
deleteUser(userId: number): Observable<void> {
|
||||
return this.http.delete<void>(`${this.userUrl}/${userId}`);
|
||||
}
|
||||
|
||||
-122
@@ -22,128 +22,6 @@
|
||||
</div>
|
||||
|
||||
<div class="form-container">
|
||||
<div class="form-section preferences-section">
|
||||
<h3 class="section-title">
|
||||
<i class="pi pi-palette" aria-hidden="true"></i>
|
||||
{{ 'settingsView.theme.sectionTitle' | transloco }}
|
||||
</h3>
|
||||
|
||||
<div class="form-field">
|
||||
<label class="field-label" for="app-appearance">{{ 'layout.theme.appearance' | transloco }}</label>
|
||||
<p-select
|
||||
inputId="app-appearance"
|
||||
[options]="appearanceOptions()"
|
||||
[ngModel]="selectedAppearancePreference()"
|
||||
(ngModelChange)="updateAppearancePreference($event)"
|
||||
optionLabel="label"
|
||||
optionValue="value"
|
||||
appendTo="body"
|
||||
class="preference-select">
|
||||
</p-select>
|
||||
</div>
|
||||
|
||||
@if (showOledDarkModeToggle()) {
|
||||
<div class="theme-toggle-row">
|
||||
<label class="field-label" for="oled-dark-mode">{{ 'settingsView.theme.oledDarkMode' | transloco }}</label>
|
||||
<p-toggleswitch
|
||||
inputId="oled-dark-mode"
|
||||
[ngModel]="oledDarkMode()"
|
||||
(ngModelChange)="updateOledDarkMode($event)">
|
||||
</p-toggleswitch>
|
||||
</div>
|
||||
}
|
||||
|
||||
<div class="form-field">
|
||||
<label class="field-label" for="app-theme">{{ 'layout.theme.theme' | transloco }}</label>
|
||||
<p-select
|
||||
inputId="app-theme"
|
||||
[options]="themeOptions()"
|
||||
[ngModel]="selectedThemePreference()"
|
||||
(ngModelChange)="updateThemePreference($event)"
|
||||
optionLabel="label"
|
||||
optionValue="value"
|
||||
appendTo="body"
|
||||
class="preference-select">
|
||||
<ng-template #selectedItem let-option>
|
||||
@if (option) {
|
||||
<span class="inline-flex items-center gap-2.5 min-w-0">
|
||||
@if (option.value !== 'custom') {
|
||||
<span
|
||||
class="w-3.5 h-3.5 rounded-full shrink-0 ring-1 ring-black/15 ring-inset bg-(--color-primary-500)"
|
||||
[attr.data-app-theme]="option.value"
|
||||
aria-hidden="true">
|
||||
</span>
|
||||
}
|
||||
<span>{{ option.label }}</span>
|
||||
</span>
|
||||
}
|
||||
</ng-template>
|
||||
<ng-template #item let-option>
|
||||
<span class="inline-flex items-center gap-2.5 min-w-0">
|
||||
@if (option.value !== 'custom') {
|
||||
<span
|
||||
class="w-3.5 h-3.5 rounded-full shrink-0 ring-1 ring-black/15 ring-inset bg-(--color-primary-500)"
|
||||
[attr.data-app-theme]="option.value"
|
||||
aria-hidden="true">
|
||||
</span>
|
||||
}
|
||||
<span>{{ option.label }}</span>
|
||||
</span>
|
||||
</ng-template>
|
||||
</p-select>
|
||||
</div>
|
||||
|
||||
<div class="form-field">
|
||||
<label class="field-label" for="theme-sync">{{ 'layout.theme.syncAcrossDevices' | transloco }}</label>
|
||||
<p-toggleswitch
|
||||
inputId="theme-sync"
|
||||
[ngModel]="selectedThemeSyncEnabled()"
|
||||
(ngModelChange)="updateThemeSyncEnabled($event)">
|
||||
</p-toggleswitch>
|
||||
</div>
|
||||
|
||||
@if (selectedThemePreference() === 'custom') {
|
||||
<fieldset class="form-field">
|
||||
<legend class="field-label">{{ 'layout.theme.customPrimary' | transloco }}</legend>
|
||||
<div class="custom-primary-card">
|
||||
<div class="custom-primary-options">
|
||||
@for (palette of customPrimaryOptions; track palette) {
|
||||
<button
|
||||
type="button"
|
||||
[class]="'w-7 h-7 rounded-full cursor-pointer shadow-[inset_0_0_0_1px_rgb(0_0_0_/_0.1)] dark:shadow-[inset_0_0_0_1px_rgb(255_255_255_/_0.1)] ' + (palette === selectedCustomPrimary() ? 'ring-1 ring-black dark:ring-white' : '')"
|
||||
[style.background]="'var(--color-' + palette + '-500)'"
|
||||
[attr.aria-label]="palette"
|
||||
[attr.aria-pressed]="palette === selectedCustomPrimary()"
|
||||
(click)="updateCustomPrimary(palette)">
|
||||
</button>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</fieldset>
|
||||
}
|
||||
</div>
|
||||
|
||||
<div class="gradient-divider"></div>
|
||||
|
||||
<div class="form-section preferences-section">
|
||||
<h3 class="section-title">
|
||||
<i class="pi pi-language" aria-hidden="true"></i>
|
||||
{{ 'settingsView.language.sectionTitle' | transloco }}
|
||||
</h3>
|
||||
|
||||
<p-select
|
||||
[options]="languageOptions"
|
||||
[ngModel]="activeLang()"
|
||||
(ngModelChange)="onLanguageChange($event)"
|
||||
optionLabel="label"
|
||||
optionValue="value"
|
||||
appendTo="body"
|
||||
class="preference-select">
|
||||
</p-select>
|
||||
</div>
|
||||
|
||||
<div class="gradient-divider"></div>
|
||||
|
||||
<div class="form-section">
|
||||
<h3 class="section-title">
|
||||
<i class="pi pi-id-card"></i>
|
||||
|
||||
-39
@@ -67,14 +67,6 @@
|
||||
}
|
||||
}
|
||||
|
||||
.preferences-section {
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
.preference-select {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.form-field {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
@@ -134,37 +126,6 @@
|
||||
}
|
||||
}
|
||||
|
||||
fieldset.form-field {
|
||||
min-width: 0;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
border: 0;
|
||||
}
|
||||
|
||||
.custom-primary-card {
|
||||
padding: 0.75rem;
|
||||
background: var(--color-card);
|
||||
border-radius: 6px;
|
||||
box-shadow: inset 0 0 0 1px var(--color-border);
|
||||
}
|
||||
|
||||
.custom-primary-options {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.theme-toggle-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 1rem;
|
||||
|
||||
.field-label {
|
||||
margin: 0;
|
||||
}
|
||||
}
|
||||
|
||||
.edit-actions {
|
||||
margin-top: 0.5rem;
|
||||
|
||||
|
||||
+1
-120
@@ -1,27 +1,12 @@
|
||||
import {Component, DestroyRef, computed, effect, inject} from '@angular/core';
|
||||
import {Component, effect, inject} from '@angular/core';
|
||||
import {Button} from 'primeng/button';
|
||||
import {AbstractControl, FormBuilder, FormGroup, FormsModule, ReactiveFormsModule, ValidationErrors, ValidatorFn, Validators} from '@angular/forms';
|
||||
import {InputText} from 'primeng/inputtext';
|
||||
import {Password} from 'primeng/password';
|
||||
import {User, UserProfileUpdateRequest, UserService} from '../user-management/user.service';
|
||||
import {ToggleSwitch} from 'primeng/toggleswitch';
|
||||
import {MessageService} from 'primeng/api';
|
||||
import {DynamicDialogRef} from 'primeng/dynamicdialog';
|
||||
import {TranslocoDirective, TranslocoPipe, TranslocoService} from '@jsverse/transloco';
|
||||
import {Select} from 'primeng/select';
|
||||
import {takeUntilDestroyed, toSignal} from '@angular/core/rxjs-interop';
|
||||
import {from, switchMap} from 'rxjs';
|
||||
import {AVAILABLE_LANGS, LANG_LABELS} from '../../../core/config/transloco-loader';
|
||||
import {AppThemeService} from '../../../shared/service/app-theme.service';
|
||||
import {AppLocaleService} from '../../../shared/service/app-locale.service';
|
||||
import {
|
||||
AppearancePreference,
|
||||
AppTheme,
|
||||
CUSTOM_PRIMARY_OPTIONS,
|
||||
CustomPrimary,
|
||||
DEFAULT_APP_THEME,
|
||||
DEFAULT_CUSTOM_PRIMARY,
|
||||
} from '../../../shared/model/app-state.model';
|
||||
|
||||
export const passwordMatchValidator: ValidatorFn = (control: AbstractControl): ValidationErrors | null => {
|
||||
const newPassword = control.get('newPassword');
|
||||
@@ -42,8 +27,6 @@ export const passwordMatchValidator: ValidatorFn = (control: AbstractControl): V
|
||||
ReactiveFormsModule,
|
||||
InputText,
|
||||
Password,
|
||||
Select,
|
||||
ToggleSwitch,
|
||||
TranslocoDirective,
|
||||
TranslocoPipe,
|
||||
],
|
||||
@@ -56,44 +39,12 @@ export class UserProfileDialogComponent {
|
||||
currentUser: User | null = null;
|
||||
editUserData: Partial<User> = {};
|
||||
changePasswordForm: FormGroup;
|
||||
readonly languageOptions = AVAILABLE_LANGS.map(value => ({
|
||||
value,
|
||||
label: LANG_LABELS[value] ?? value,
|
||||
}));
|
||||
|
||||
protected readonly userService = inject(UserService);
|
||||
protected readonly themeService = inject(AppThemeService);
|
||||
protected readonly appLocaleService = inject(AppLocaleService);
|
||||
private readonly messageService = inject(MessageService);
|
||||
private readonly fb = inject(FormBuilder);
|
||||
private readonly dialogRef = inject(DynamicDialogRef);
|
||||
private readonly t = inject(TranslocoService);
|
||||
private readonly destroyRef = inject(DestroyRef);
|
||||
protected readonly activeLang = toSignal(this.t.langChanges$, {initialValue: this.t.getActiveLang()});
|
||||
protected readonly selectedThemePreference = computed(() => this.themeService.appState().themePreference ?? DEFAULT_APP_THEME);
|
||||
protected readonly selectedAppearancePreference = computed(() => this.themeService.appState().appearancePreference ?? 'system');
|
||||
protected readonly oledDarkMode = computed(() => this.themeService.appState().oledDarkMode);
|
||||
protected readonly showOledDarkModeToggle = computed(() => this.themeService.effectiveAppearance() === 'dark');
|
||||
protected readonly selectedCustomPrimary = computed<CustomPrimary>(
|
||||
() => this.themeService.appState().customPrimary ?? DEFAULT_CUSTOM_PRIMARY,
|
||||
);
|
||||
protected readonly selectedThemeSyncEnabled = computed(() => this.themeService.appState().themeSyncEnabled !== false);
|
||||
protected readonly customPrimaryOptions = CUSTOM_PRIMARY_OPTIONS;
|
||||
protected readonly themeOptions = computed(() => {
|
||||
this.activeLang();
|
||||
return this.themeService.themes.map((theme) => ({
|
||||
value: theme.name,
|
||||
label: this.t.translate(theme.labelKey),
|
||||
}));
|
||||
});
|
||||
protected readonly appearanceOptions = computed(() => {
|
||||
this.activeLang();
|
||||
return [
|
||||
{value: 'light' as AppearancePreference, label: this.t.translate('layout.theme.light')},
|
||||
{value: 'dark' as AppearancePreference, label: this.t.translate('layout.theme.dark')},
|
||||
{value: 'system' as AppearancePreference, label: this.t.translate('layout.theme.system')},
|
||||
];
|
||||
});
|
||||
|
||||
constructor() {
|
||||
this.changePasswordForm = this.fb.group(
|
||||
@@ -131,76 +82,6 @@ export class UserProfileDialogComponent {
|
||||
}
|
||||
}
|
||||
|
||||
onLanguageChange(lang: string): void {
|
||||
if (lang === this.activeLang()) return;
|
||||
if (!this.currentUser) return;
|
||||
|
||||
this.userService.updateUserProfile(this.currentUser.id, {locale: lang}).pipe(
|
||||
switchMap(user => from(this.appLocaleService.applyLocale(user.locale))),
|
||||
takeUntilDestroyed(this.destroyRef),
|
||||
).subscribe();
|
||||
}
|
||||
|
||||
updateThemePreference(themePreference: AppTheme): void {
|
||||
if (!this.currentUser) return;
|
||||
|
||||
if (!this.selectedThemeSyncEnabled()) {
|
||||
this.themeService.applyDeviceTheme(themePreference, this.selectedCustomPrimary());
|
||||
return;
|
||||
}
|
||||
|
||||
const updateRequest: UserProfileUpdateRequest = {
|
||||
theme: themePreference,
|
||||
...(themePreference === 'custom' ? {themeAccent: this.selectedCustomPrimary()} : {}),
|
||||
};
|
||||
|
||||
this.userService.updateUserProfile(this.currentUser.id, updateRequest).pipe(takeUntilDestroyed(this.destroyRef)).subscribe({
|
||||
next: (user) => this.themeService.applySyncedTheme(user.theme, user.themeAccent ?? DEFAULT_CUSTOM_PRIMARY),
|
||||
});
|
||||
}
|
||||
|
||||
updateCustomPrimary(customPrimary: CustomPrimary): void {
|
||||
if (!this.currentUser) return;
|
||||
|
||||
if (!this.selectedThemeSyncEnabled()) {
|
||||
this.themeService.applyDeviceTheme('custom', customPrimary);
|
||||
return;
|
||||
}
|
||||
|
||||
this.userService.updateUserProfile(this.currentUser.id, {theme: 'custom', themeAccent: customPrimary}).pipe(takeUntilDestroyed(this.destroyRef)).subscribe({
|
||||
next: (user) => this.themeService.applySyncedTheme(user.theme, user.themeAccent ?? DEFAULT_CUSTOM_PRIMARY),
|
||||
});
|
||||
}
|
||||
|
||||
updateThemeSyncEnabled(themeSyncEnabled: boolean): void {
|
||||
if (!this.currentUser) return;
|
||||
|
||||
if (!themeSyncEnabled) {
|
||||
this.userService.updateUserProfile(this.currentUser.id, {themeSyncEnabled: false}).pipe(takeUntilDestroyed(this.destroyRef)).subscribe({
|
||||
next: () => this.themeService.applyDeviceTheme(this.selectedThemePreference(), this.selectedCustomPrimary()),
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const theme = this.selectedThemePreference();
|
||||
const updateRequest: UserProfileUpdateRequest = {
|
||||
themeSyncEnabled: true,
|
||||
theme,
|
||||
...(theme === 'custom' ? {themeAccent: this.selectedCustomPrimary()} : {}),
|
||||
};
|
||||
this.userService.updateUserProfile(this.currentUser.id, updateRequest).pipe(takeUntilDestroyed(this.destroyRef)).subscribe({
|
||||
next: (user) => this.themeService.applySyncedTheme(user.theme, user.themeAccent ?? DEFAULT_CUSTOM_PRIMARY),
|
||||
});
|
||||
}
|
||||
|
||||
updateAppearancePreference(appearancePreference: AppearancePreference): void {
|
||||
this.themeService.setAppearancePreference(appearancePreference);
|
||||
}
|
||||
|
||||
updateOledDarkMode(oledDarkMode: boolean): void {
|
||||
this.themeService.setOledDarkMode(oledDarkMode);
|
||||
}
|
||||
|
||||
updateProfile(): void {
|
||||
if (!this.currentUser) {
|
||||
this.messageService.add({
|
||||
|
||||
+131
@@ -0,0 +1,131 @@
|
||||
<ng-container *transloco="let t; prefix: 'settingsView'">
|
||||
<div class="flex flex-col gap-5 rounded-lg border border-border bg-card p-6 max-md:gap-4 max-md:p-4">
|
||||
<div class="mb-1">
|
||||
<h3 class="m-0 mb-2 flex items-center gap-2 text-xl font-semibold text-text">
|
||||
<i class="pi pi-palette text-lg text-primary" aria-hidden="true"></i>
|
||||
{{ t('theme.sectionTitle') }}
|
||||
</h3>
|
||||
<p class="m-0 text-sm leading-6 text-text-secondary">{{ t('theme.sectionDesc') }}</p>
|
||||
</div>
|
||||
|
||||
<div class="flex flex-col gap-4 pl-4 max-md:pl-2">
|
||||
<section class="w-full max-w-3xl rounded-md border-l-[3px] border-l-primary bg-[color-mix(in_srgb,var(--color-card)_35%,var(--color-page))] p-4 transition-[background,border-color] duration-200 hover:bg-[color-mix(in_srgb,var(--color-card)_20%,var(--color-page))] max-md:p-3">
|
||||
<div class="mb-1">
|
||||
<label class="text-[0.9375rem] font-medium text-text" for="appearance-preference">{{ 'layout.theme.appearance' | transloco }}</label>
|
||||
</div>
|
||||
<div class="mt-3 border-t border-dashed border-border pt-3">
|
||||
<p-select
|
||||
inputId="appearance-preference"
|
||||
[options]="appearanceOptions()"
|
||||
[ngModel]="selectedAppearancePreference()"
|
||||
(ngModelChange)="updateAppearancePreference($event)"
|
||||
optionLabel="label"
|
||||
optionValue="value"
|
||||
appendTo="body"
|
||||
class="w-80 max-w-full">
|
||||
</p-select>
|
||||
</div>
|
||||
|
||||
@if (showOledDarkModeToggle()) {
|
||||
<div class="mt-4 flex max-w-xl items-center gap-3 border-t border-dashed border-border pt-3">
|
||||
<p-toggleswitch
|
||||
inputId="oled-dark-mode"
|
||||
[ngModel]="oledDarkMode()"
|
||||
(ngModelChange)="updateOledDarkMode($event)">
|
||||
</p-toggleswitch>
|
||||
<label class="text-[0.9375rem] font-medium text-text" for="oled-dark-mode">{{ t('theme.oledDarkMode') }}</label>
|
||||
</div>
|
||||
}
|
||||
</section>
|
||||
|
||||
<section class="w-full max-w-3xl rounded-md border-l-[3px] border-l-primary bg-[color-mix(in_srgb,var(--color-card)_35%,var(--color-page))] p-4 transition-[background,border-color] duration-200 hover:bg-[color-mix(in_srgb,var(--color-card)_20%,var(--color-page))] max-md:p-3">
|
||||
<div class="mb-1">
|
||||
<label class="text-[0.9375rem] font-medium text-text" for="app-theme">{{ 'layout.theme.theme' | transloco }}</label>
|
||||
</div>
|
||||
<div class="mt-3 border-t border-dashed border-border pt-3">
|
||||
<p-select
|
||||
inputId="app-theme"
|
||||
[options]="themeOptions()"
|
||||
[ngModel]="selectedThemePreference()"
|
||||
(ngModelChange)="updateThemePreference($event)"
|
||||
optionLabel="label"
|
||||
optionValue="value"
|
||||
appendTo="body"
|
||||
class="w-80 max-w-full">
|
||||
<ng-template #selectedItem let-option>
|
||||
@if (option) {
|
||||
<span class="inline-flex min-w-0 items-center gap-2.5">
|
||||
@if (option.value !== 'custom') {
|
||||
<span
|
||||
class="h-3.5 w-3.5 shrink-0 rounded-full bg-(--color-primary-500) ring-1 ring-inset ring-black/15"
|
||||
[attr.data-app-theme]="option.value"
|
||||
aria-hidden="true">
|
||||
</span>
|
||||
}
|
||||
<span>{{ option.label }}</span>
|
||||
</span>
|
||||
}
|
||||
</ng-template>
|
||||
<ng-template #item let-option>
|
||||
<span class="inline-flex min-w-0 items-center gap-2.5">
|
||||
@if (option.value !== 'custom') {
|
||||
<span
|
||||
class="h-3.5 w-3.5 shrink-0 rounded-full bg-(--color-primary-500) ring-1 ring-inset ring-black/15"
|
||||
[attr.data-app-theme]="option.value"
|
||||
aria-hidden="true">
|
||||
</span>
|
||||
}
|
||||
<span>{{ option.label }}</span>
|
||||
</span>
|
||||
</ng-template>
|
||||
</p-select>
|
||||
</div>
|
||||
|
||||
@if (selectedThemePreference() === 'custom') {
|
||||
<div class="mt-4 max-w-xl">
|
||||
<div class="flex flex-wrap gap-2">
|
||||
@for (option of customPrimaryOptions(); track option.value) {
|
||||
<button
|
||||
type="button"
|
||||
class="size-7 cursor-pointer rounded-full border-0 p-0 shadow-[inset_0_0_0_1px_rgb(0_0_0_/_10%)] aria-pressed:ring-2 aria-pressed:ring-[var(--color-text)] aria-pressed:ring-offset-2 aria-pressed:ring-offset-[var(--color-page)] focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary"
|
||||
[style.background]="'var(--color-' + option.value + '-500)'"
|
||||
[attr.aria-label]="option.ariaLabel"
|
||||
[attr.aria-pressed]="option.value === selectedCustomPrimary()"
|
||||
(click)="updateCustomPrimary(option.value)">
|
||||
</button>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
|
||||
<div class="mt-4 flex max-w-xl items-center gap-3 border-t border-dashed border-border pt-3">
|
||||
<p-toggleswitch
|
||||
inputId="theme-sync"
|
||||
[ngModel]="selectedThemeSyncEnabled()"
|
||||
(ngModelChange)="updateThemeSyncEnabled($event)">
|
||||
</p-toggleswitch>
|
||||
<label class="text-[0.9375rem] font-medium text-text" for="theme-sync">{{ 'layout.theme.syncAcrossDevices' | transloco }}</label>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section class="w-full max-w-3xl rounded-md border-l-[3px] border-l-primary bg-[color-mix(in_srgb,var(--color-card)_35%,var(--color-page))] p-4 transition-[background,border-color] duration-200 hover:bg-[color-mix(in_srgb,var(--color-card)_20%,var(--color-page))] max-md:p-3">
|
||||
<div class="mb-1">
|
||||
<label class="text-[0.9375rem] font-medium text-text" for="interface-language">{{ t('language.label') }}</label>
|
||||
</div>
|
||||
<div class="mt-3 border-t border-dashed border-border pt-3">
|
||||
<p-select
|
||||
inputId="interface-language"
|
||||
[options]="languageOptions"
|
||||
[ngModel]="activeLang()"
|
||||
(ngModelChange)="onLanguageChange($event)"
|
||||
optionLabel="label"
|
||||
optionValue="value"
|
||||
appendTo="body"
|
||||
class="w-80 max-w-full">
|
||||
</p-select>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</ng-container>
|
||||
+173
@@ -0,0 +1,173 @@
|
||||
import {ChangeDetectionStrategy, Component, computed, inject} from '@angular/core';
|
||||
import {FormsModule} from '@angular/forms';
|
||||
import {toSignal} from '@angular/core/rxjs-interop';
|
||||
import {Select} from 'primeng/select';
|
||||
import {ToggleSwitch} from 'primeng/toggleswitch';
|
||||
import {MessageService} from 'primeng/api';
|
||||
import {TranslocoDirective, TranslocoPipe, TranslocoService} from '@jsverse/transloco';
|
||||
|
||||
import {AVAILABLE_LANGS, LANG_LABELS} from '../../../../core/config/transloco-loader';
|
||||
import {AppLocaleService} from '../../../../shared/service/app-locale.service';
|
||||
import {AppThemeService} from '../../../../shared/service/app-theme.service';
|
||||
import {
|
||||
CUSTOM_PRIMARY_OPTIONS,
|
||||
DEFAULT_CUSTOM_PRIMARY,
|
||||
} from '../../../../shared/model/app-state.model';
|
||||
import type {AppearancePreference, AppTheme, CustomPrimary} from '../../../../shared/model/app-state.model';
|
||||
import {APPEARANCE_OPTIONS} from '../../../../shared/layout/theme/appearance-options';
|
||||
import {UserService} from '../../user-management/user.service';
|
||||
import type {User, UserProfileUpdateRequest} from '../../user-management/user.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-display-preferences',
|
||||
standalone: true,
|
||||
imports: [
|
||||
FormsModule,
|
||||
Select,
|
||||
ToggleSwitch,
|
||||
TranslocoDirective,
|
||||
TranslocoPipe,
|
||||
],
|
||||
templateUrl: './display-preferences.component.html',
|
||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||
})
|
||||
export class DisplayPreferencesComponent {
|
||||
private readonly themeService = inject(AppThemeService);
|
||||
private readonly localeService = inject(AppLocaleService);
|
||||
private readonly userService = inject(UserService);
|
||||
private readonly messageService = inject(MessageService);
|
||||
private readonly t = inject(TranslocoService);
|
||||
|
||||
protected readonly activeLang = toSignal(this.t.langChanges$, {initialValue: this.t.getActiveLang()});
|
||||
protected readonly appearanceOptions = computed(() => {
|
||||
const activeLang = this.activeLang();
|
||||
return APPEARANCE_OPTIONS.map((option) => ({
|
||||
value: option.value,
|
||||
label: this.t.translate(option.labelKey, {}, activeLang),
|
||||
}));
|
||||
});
|
||||
protected readonly languageOptions = AVAILABLE_LANGS.map(value => ({
|
||||
value,
|
||||
label: LANG_LABELS[value] ?? value,
|
||||
}));
|
||||
protected readonly selectedAppearancePreference = this.themeService.appearancePreference;
|
||||
protected readonly selectedThemePreference = this.themeService.themePreference;
|
||||
protected readonly oledDarkMode = this.themeService.oledDarkMode;
|
||||
protected readonly showOledDarkModeToggle = computed(() => this.themeService.effectiveAppearance() === 'dark');
|
||||
protected readonly selectedCustomPrimary = this.themeService.customPrimary;
|
||||
protected readonly selectedThemeSyncEnabled = this.themeService.themeSyncEnabled;
|
||||
protected readonly customPrimaryOptions = computed(() => {
|
||||
const activeLang = this.activeLang();
|
||||
return CUSTOM_PRIMARY_OPTIONS.map((value) => {
|
||||
const color = this.t.translate(`settingsView.theme.customPrimaryColors.${value}`, {}, activeLang);
|
||||
return {
|
||||
value,
|
||||
ariaLabel: this.t.translate('settingsView.theme.customPrimaryOptionLabel', {color}, activeLang),
|
||||
};
|
||||
});
|
||||
});
|
||||
protected readonly themeOptions = computed(() => {
|
||||
const activeLang = this.activeLang();
|
||||
return this.themeService.themes.map((theme) => ({
|
||||
value: theme.name,
|
||||
label: this.t.translate(theme.labelKey, {}, activeLang),
|
||||
}));
|
||||
});
|
||||
|
||||
protected async onLanguageChange(lang: string): Promise<void> {
|
||||
if (lang === this.activeLang()) return;
|
||||
|
||||
const updatedUser = await this.updateCurrentUserProfile({locale: lang});
|
||||
if (!updatedUser) return;
|
||||
|
||||
await this.localeService.applyLocale(updatedUser.locale);
|
||||
this.showSavedToast('settingsView.language.savedDetail');
|
||||
}
|
||||
|
||||
protected async updateThemePreference(themePreference: AppTheme): Promise<void> {
|
||||
if (!this.selectedThemeSyncEnabled()) {
|
||||
this.themeService.applyDeviceTheme(themePreference, this.selectedCustomPrimary());
|
||||
return;
|
||||
}
|
||||
|
||||
const updateRequest: UserProfileUpdateRequest = {
|
||||
theme: themePreference,
|
||||
...(themePreference === 'custom' ? {themeAccent: this.selectedCustomPrimary()} : {}),
|
||||
};
|
||||
|
||||
const updatedUser = await this.updateCurrentUserProfile(updateRequest);
|
||||
if (!updatedUser) return;
|
||||
|
||||
this.applySyncedUserTheme(updatedUser);
|
||||
this.showSavedToast();
|
||||
}
|
||||
|
||||
protected updateAppearancePreference(appearancePreference: AppearancePreference): void {
|
||||
this.themeService.setAppearancePreference(appearancePreference);
|
||||
}
|
||||
|
||||
protected async updateCustomPrimary(customPrimary: CustomPrimary): Promise<void> {
|
||||
if (!this.selectedThemeSyncEnabled()) {
|
||||
this.themeService.applyDeviceTheme('custom', customPrimary);
|
||||
return;
|
||||
}
|
||||
|
||||
const updatedUser = await this.updateCurrentUserProfile({theme: 'custom', themeAccent: customPrimary});
|
||||
if (!updatedUser) return;
|
||||
|
||||
this.applySyncedUserTheme(updatedUser);
|
||||
this.showSavedToast();
|
||||
}
|
||||
|
||||
protected async updateThemeSyncEnabled(themeSyncEnabled: boolean): Promise<void> {
|
||||
if (!themeSyncEnabled) {
|
||||
const updatedUser = await this.updateCurrentUserProfile({themeSyncEnabled: false});
|
||||
if (!updatedUser) return;
|
||||
|
||||
this.themeService.applyDeviceTheme(this.selectedThemePreference(), this.selectedCustomPrimary());
|
||||
this.showSavedToast('settingsView.theme.syncSavedDetail');
|
||||
return;
|
||||
}
|
||||
|
||||
const theme = this.selectedThemePreference();
|
||||
const updateRequest: UserProfileUpdateRequest = {
|
||||
themeSyncEnabled: true,
|
||||
theme,
|
||||
...(theme === 'custom' ? {themeAccent: this.selectedCustomPrimary()} : {}),
|
||||
};
|
||||
const updatedUser = await this.updateCurrentUserProfile(updateRequest);
|
||||
if (!updatedUser) return;
|
||||
|
||||
this.applySyncedUserTheme(updatedUser);
|
||||
this.showSavedToast('settingsView.theme.syncSavedDetail');
|
||||
}
|
||||
|
||||
protected updateOledDarkMode(oledDarkMode: boolean): void {
|
||||
this.themeService.setOledDarkMode(oledDarkMode);
|
||||
}
|
||||
|
||||
private applySyncedUserTheme(user: User): void {
|
||||
this.themeService.applySyncedTheme(user.theme, user.themeAccent ?? DEFAULT_CUSTOM_PRIMARY);
|
||||
}
|
||||
|
||||
private async updateCurrentUserProfile(updateRequest: UserProfileUpdateRequest): Promise<User | null> {
|
||||
try {
|
||||
return await this.userService.updateCurrentUserProfile(updateRequest);
|
||||
} catch {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: this.t.translate('common.error'),
|
||||
detail: this.t.translate('settingsView.saveFailedDetail'),
|
||||
});
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private showSavedToast(detailKey = 'settingsView.theme.savedDetail'): void {
|
||||
this.messageService.add({
|
||||
severity: 'success',
|
||||
summary: this.t.translate('common.success'),
|
||||
detail: this.t.translate(detailKey),
|
||||
});
|
||||
}
|
||||
}
|
||||
+2
@@ -10,6 +10,8 @@
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<app-display-preferences></app-display-preferences>
|
||||
<div class="section-divider"><hr></div>
|
||||
<app-view-preferences></app-view-preferences>
|
||||
<div class="section-divider"><hr></div>
|
||||
<app-filter-preferences></app-filter-preferences>
|
||||
|
||||
+2
@@ -6,6 +6,7 @@ import {ViewPreferencesComponent} from './view-preferences/view-preferences.comp
|
||||
import {SidebarSortingPreferencesComponent} from './sidebar-sorting-preferences/sidebar-sorting-preferences.component';
|
||||
import {MetaCenterViewModeComponent} from './meta-center-view-mode/meta-center-view-mode-component';
|
||||
import {FilterPreferencesComponent} from './filter-preferences/filter-preferences.component';
|
||||
import {DisplayPreferencesComponent} from './display-preferences/display-preferences.component';
|
||||
import {TranslocoDirective, TranslocoService} from '@jsverse/transloco';
|
||||
import {Slider} from 'primeng/slider';
|
||||
import {MessageService} from 'primeng/api';
|
||||
@@ -21,6 +22,7 @@ import {LayoutService, SIDEBAR_MAX_WIDTH, SIDEBAR_MIN_WIDTH} from '../../../shar
|
||||
SidebarSortingPreferencesComponent,
|
||||
MetaCenterViewModeComponent,
|
||||
FilterPreferencesComponent,
|
||||
DisplayPreferencesComponent,
|
||||
TranslocoDirective,
|
||||
Slider,
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user