feat(ui): sync profile locale and theme preferences (#1604)
This commit is contained in:
@@ -12,8 +12,9 @@ export {
|
||||
export type {AppTheme, CustomPrimary} from './theme-registry';
|
||||
|
||||
export interface AppState {
|
||||
themePreference: AppTheme;
|
||||
appearancePreference: AppearancePreference;
|
||||
customPrimary: CustomPrimary;
|
||||
themePreference?: AppTheme;
|
||||
appearancePreference?: AppearancePreference;
|
||||
customPrimary?: CustomPrimary;
|
||||
themeSyncEnabled?: boolean;
|
||||
oledDarkMode: boolean;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
import {TestBed} from '@angular/core/testing';
|
||||
import {TranslocoService} from '@jsverse/transloco';
|
||||
import {of} from 'rxjs';
|
||||
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
|
||||
|
||||
import {AppLocaleService} from './app-locale.service';
|
||||
|
||||
describe('AppLocaleService', () => {
|
||||
const translocoService = {
|
||||
load: vi.fn(),
|
||||
setActiveLang: vi.fn(),
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
localStorage.clear();
|
||||
translocoService.load.mockReset();
|
||||
translocoService.setActiveLang.mockReset();
|
||||
translocoService.load.mockReturnValue(of({}));
|
||||
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
AppLocaleService,
|
||||
{provide: TranslocoService, useValue: translocoService},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
localStorage.clear();
|
||||
TestBed.resetTestingModule();
|
||||
});
|
||||
|
||||
it('defaults the display locale to english', () => {
|
||||
const service = TestBed.inject(AppLocaleService);
|
||||
|
||||
expect(service.getDisplayLocale()).toBe('en');
|
||||
});
|
||||
|
||||
it('uses a supported stored display locale', () => {
|
||||
localStorage.setItem('appLocale', 'de');
|
||||
const service = TestBed.inject(AppLocaleService);
|
||||
|
||||
expect(service.getDisplayLocale()).toBe('de');
|
||||
});
|
||||
|
||||
it('ignores an unsupported stored display locale', () => {
|
||||
localStorage.setItem('appLocale', 'zz');
|
||||
const service = TestBed.inject(AppLocaleService);
|
||||
|
||||
expect(service.getDisplayLocale()).toBe('en');
|
||||
});
|
||||
|
||||
it('loads, activates, and stores an applied locale', async () => {
|
||||
const service = TestBed.inject(AppLocaleService);
|
||||
|
||||
await service.applyLocale('de');
|
||||
|
||||
expect(translocoService.load).toHaveBeenCalledWith('de');
|
||||
expect(translocoService.setActiveLang).toHaveBeenCalledWith('de');
|
||||
expect(localStorage.getItem('appLocale')).toBe('de');
|
||||
});
|
||||
|
||||
it('falls back to english for unsupported applied locales', async () => {
|
||||
const service = TestBed.inject(AppLocaleService);
|
||||
|
||||
await service.applyLocale('zz');
|
||||
|
||||
expect(translocoService.load).toHaveBeenCalledWith('en');
|
||||
expect(translocoService.setActiveLang).toHaveBeenCalledWith('en');
|
||||
expect(localStorage.getItem('appLocale')).toBe('en');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,24 @@
|
||||
import {Injectable, inject} from '@angular/core';
|
||||
import {TranslocoService} from '@jsverse/transloco';
|
||||
import {firstValueFrom} from 'rxjs';
|
||||
import {AVAILABLE_LANGS} from '../../core/config/transloco-loader';
|
||||
|
||||
const DEFAULT_LOCALE = 'en';
|
||||
const STORAGE_KEY = 'appLocale';
|
||||
|
||||
@Injectable({providedIn: 'root'})
|
||||
export class AppLocaleService {
|
||||
private translocoService = inject(TranslocoService);
|
||||
|
||||
getDisplayLocale(): string {
|
||||
const locale = localStorage.getItem(STORAGE_KEY);
|
||||
return locale && AVAILABLE_LANGS.includes(locale) ? locale : DEFAULT_LOCALE;
|
||||
}
|
||||
|
||||
async applyLocale(locale: string): Promise<void> {
|
||||
const nextLocale = AVAILABLE_LANGS.includes(locale) ? locale : DEFAULT_LOCALE;
|
||||
await firstValueFrom(this.translocoService.load(nextLocale));
|
||||
this.translocoService.setActiveLang(nextLocale);
|
||||
localStorage.setItem(STORAGE_KEY, nextLocale);
|
||||
}
|
||||
}
|
||||
+49
-10
@@ -1,6 +1,6 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { AppConfigService } from './app-config.service';
|
||||
import { AppThemeService } from './app-theme.service';
|
||||
import { FaviconService } from '../layout/theme/favicon-service';
|
||||
|
||||
function createLocalStorageMock() {
|
||||
@@ -44,8 +44,8 @@ function createMatchMediaMock(matches: boolean) {
|
||||
}) as unknown as MediaQueryList);
|
||||
}
|
||||
|
||||
describe('AppConfigService', () => {
|
||||
let service: AppConfigService;
|
||||
describe('AppThemeService', () => {
|
||||
let service: AppThemeService;
|
||||
let localStorageMock: ReturnType<typeof createLocalStorageMock>;
|
||||
let faviconServiceMock: { updateFavicon: ReturnType<typeof vi.fn> };
|
||||
const root = document.documentElement;
|
||||
@@ -72,12 +72,12 @@ describe('AppConfigService', () => {
|
||||
TestBed.resetTestingModule();
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
AppConfigService,
|
||||
AppThemeService,
|
||||
{ provide: FaviconService, useValue: faviconServiceMock },
|
||||
],
|
||||
});
|
||||
|
||||
service = TestBed.inject(AppConfigService);
|
||||
service = TestBed.inject(AppThemeService);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
@@ -95,6 +95,7 @@ describe('AppConfigService', () => {
|
||||
themePreference: 'grimmory',
|
||||
appearancePreference: 'system',
|
||||
customPrimary: 'orange',
|
||||
themeSyncEnabled: true,
|
||||
oledDarkMode: false,
|
||||
});
|
||||
expect(localStorageMock.getItem('appConfigState')).toBeNull();
|
||||
@@ -111,10 +112,10 @@ describe('AppConfigService', () => {
|
||||
});
|
||||
|
||||
it('updates the root theme attributes without writing palette tokens inline', () => {
|
||||
service.setThemePreference('grimmory');
|
||||
service.applySyncedTheme('cobalt');
|
||||
service.setAppearancePreference('light');
|
||||
|
||||
expect(root.dataset['appTheme']).toBe('grimmory');
|
||||
expect(root.dataset['appTheme']).toBe('cobalt');
|
||||
expect(root.classList.contains('dark')).toBe(false);
|
||||
expect(rootStyle.getPropertyValue('color-scheme')).toBe('light');
|
||||
expect(rootStyle.getPropertyValue('--color-primary')).toBe('');
|
||||
@@ -137,24 +138,62 @@ describe('AppConfigService', () => {
|
||||
TestBed.resetTestingModule();
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
AppConfigService,
|
||||
AppThemeService,
|
||||
{ provide: FaviconService, useValue: faviconServiceMock },
|
||||
],
|
||||
});
|
||||
|
||||
service = TestBed.inject(AppConfigService);
|
||||
service = TestBed.inject(AppThemeService);
|
||||
|
||||
expect(service.appState()).toEqual({
|
||||
themePreference: 'grimmory',
|
||||
appearancePreference: 'system',
|
||||
customPrimary: 'orange',
|
||||
themeSyncEnabled: true,
|
||||
oledDarkMode: false,
|
||||
});
|
||||
expect(root.dataset['appTheme']).toBe('grimmory');
|
||||
expect(root.classList.contains('dark')).toBe(false);
|
||||
expect(localStorageMock.getItem('appConfigState')).toBe(JSON.stringify({
|
||||
themePreference: 'grimmory',
|
||||
appearancePreference: 'system',
|
||||
themePreference: 'grimmory',
|
||||
customPrimary: 'orange',
|
||||
oledDarkMode: false,
|
||||
}));
|
||||
});
|
||||
|
||||
it('persists device theme and disabled sync state when theme sync is disabled', () => {
|
||||
service.applyDeviceTheme('custom', 'teal');
|
||||
|
||||
expect(service.appState()).toEqual({
|
||||
themePreference: 'custom',
|
||||
appearancePreference: 'system',
|
||||
customPrimary: 'teal',
|
||||
themeSyncEnabled: false,
|
||||
oledDarkMode: false,
|
||||
});
|
||||
expect(localStorageMock.getItem('appConfigState')).toBe(JSON.stringify({
|
||||
appearancePreference: 'system',
|
||||
themePreference: 'custom',
|
||||
customPrimary: 'teal',
|
||||
oledDarkMode: false,
|
||||
themeSyncEnabled: false,
|
||||
}));
|
||||
});
|
||||
|
||||
it('persists the last synced theme as a local display fallback', () => {
|
||||
service.applySyncedTheme('cobalt');
|
||||
|
||||
expect(service.appState()).toEqual({
|
||||
themePreference: 'cobalt',
|
||||
appearancePreference: 'system',
|
||||
customPrimary: 'orange',
|
||||
themeSyncEnabled: true,
|
||||
oledDarkMode: false,
|
||||
});
|
||||
expect(localStorageMock.getItem('appConfigState')).toBe(JSON.stringify({
|
||||
appearancePreference: 'system',
|
||||
themePreference: 'cobalt',
|
||||
customPrimary: 'orange',
|
||||
oledDarkMode: false,
|
||||
}));
|
||||
+36
-7
@@ -32,7 +32,7 @@ interface LoadedAppState {
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class AppConfigService {
|
||||
export class AppThemeService {
|
||||
private readonly STORAGE_KEY = 'appConfigState';
|
||||
readonly themes = APP_THEME_OPTIONS;
|
||||
private readonly appStateSignal = signal<AppState>(this.withDefaults({}));
|
||||
@@ -53,12 +53,24 @@ export class AppConfigService {
|
||||
this.applyAppState(initialState.state);
|
||||
}
|
||||
|
||||
setThemePreference(themePreference: AppTheme): void {
|
||||
this.updateAppState({themePreference});
|
||||
applySyncedTheme(themePreference: AppTheme, customPrimary: CustomPrimary = DEFAULT_CUSTOM_PRIMARY): void {
|
||||
this.updateAppState({
|
||||
themePreference,
|
||||
customPrimary,
|
||||
themeSyncEnabled: true,
|
||||
});
|
||||
}
|
||||
|
||||
setCustomPrimary(customPrimary: CustomPrimary): void {
|
||||
this.updateAppState({customPrimary});
|
||||
applyDeviceTheme(themePreference: AppTheme, customPrimary: CustomPrimary = DEFAULT_CUSTOM_PRIMARY): void {
|
||||
this.updateAppState({
|
||||
themePreference,
|
||||
customPrimary,
|
||||
themeSyncEnabled: false,
|
||||
});
|
||||
}
|
||||
|
||||
useStoredDeviceTheme(): void {
|
||||
this.updateAppState({themeSyncEnabled: false});
|
||||
}
|
||||
|
||||
setAppearancePreference(appearancePreference: AppearancePreference): void {
|
||||
@@ -87,7 +99,7 @@ export class AppConfigService {
|
||||
const parsedState = JSON.parse(storedState) as StoredAppState;
|
||||
return {
|
||||
state: this.normalizeStoredState(parsedState),
|
||||
shouldPersist: this.isLegacyPaletteState(parsedState),
|
||||
shouldPersist: this.shouldRewriteStoredState(parsedState),
|
||||
};
|
||||
} catch {
|
||||
return {state: this.withDefaults({}), shouldPersist: false};
|
||||
@@ -107,10 +119,12 @@ export class AppConfigService {
|
||||
});
|
||||
}
|
||||
|
||||
const themeSyncEnabled = state.themeSyncEnabled === false ? false : true;
|
||||
return this.withDefaults({
|
||||
themePreference: state.themePreference,
|
||||
appearancePreference: state.appearancePreference,
|
||||
customPrimary: state.customPrimary,
|
||||
themeSyncEnabled,
|
||||
oledDarkMode: state.oledDarkMode,
|
||||
});
|
||||
}
|
||||
@@ -119,11 +133,16 @@ export class AppConfigService {
|
||||
return 'preset' in state || 'primary' in state || 'surface' in state;
|
||||
}
|
||||
|
||||
private shouldRewriteStoredState(state: StoredAppState): boolean {
|
||||
return this.isLegacyPaletteState(state);
|
||||
}
|
||||
|
||||
private withDefaults(state: AppStatePatch): AppState {
|
||||
return {
|
||||
themePreference: this.resolveThemePreference(state.themePreference),
|
||||
appearancePreference: this.resolveAppearancePreference(state.appearancePreference),
|
||||
customPrimary: this.resolveCustomPrimary(state.customPrimary),
|
||||
themeSyncEnabled: state.themeSyncEnabled !== false,
|
||||
oledDarkMode: state.oledDarkMode === true,
|
||||
};
|
||||
}
|
||||
@@ -162,7 +181,17 @@ export class AppConfigService {
|
||||
|
||||
private saveAppState(state: AppState): void {
|
||||
if (isPlatformBrowser(this.platformId)) {
|
||||
localStorage.setItem(this.STORAGE_KEY, JSON.stringify(state));
|
||||
const defaults = this.withDefaults(state);
|
||||
const storedState: AppState = {
|
||||
appearancePreference: defaults.appearancePreference,
|
||||
themePreference: defaults.themePreference,
|
||||
customPrimary: defaults.customPrimary,
|
||||
oledDarkMode: defaults.oledDarkMode,
|
||||
};
|
||||
if (!defaults.themeSyncEnabled) {
|
||||
storedState.themeSyncEnabled = false;
|
||||
}
|
||||
localStorage.setItem(this.STORAGE_KEY, JSON.stringify(storedState));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,33 @@
|
||||
import {signal} from '@angular/core';
|
||||
import {TestBed} from '@angular/core/testing';
|
||||
import {describe, expect, it, vi} from 'vitest';
|
||||
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
|
||||
import {QueryClient, queryOptions} from '@tanstack/angular-query-experimental';
|
||||
import {of} from 'rxjs';
|
||||
|
||||
import {AuthService} from './auth.service';
|
||||
import {StartupService} from './startup.service';
|
||||
import {UserService} from '../../features/settings/user-management/user.service';
|
||||
import {AppThemeService} from './app-theme.service';
|
||||
import {AppLocaleService} from './app-locale.service';
|
||||
|
||||
describe('StartupService', () => {
|
||||
beforeEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
localStorage.clear();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
localStorage.clear();
|
||||
TestBed.resetTestingModule();
|
||||
});
|
||||
|
||||
it('loads the current user when a token is present', async () => {
|
||||
const fetchQuery = vi.fn().mockResolvedValue({username: 'admin'});
|
||||
const fetchQuery = vi.fn().mockResolvedValue({id: 1, username: 'admin', locale: 'en', theme: 'grimmory', themeAccent: null, themeSyncEnabled: true});
|
||||
const applySyncedTheme = vi.fn();
|
||||
const applyDeviceTheme = vi.fn();
|
||||
const useStoredDeviceTheme = vi.fn();
|
||||
const applyLocale = vi.fn().mockResolvedValue(undefined);
|
||||
const getDisplayLocale = vi.fn().mockReturnValue('en');
|
||||
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
@@ -25,6 +43,8 @@ describe('StartupService', () => {
|
||||
},
|
||||
},
|
||||
{provide: QueryClient, useValue: {fetchQuery}},
|
||||
{provide: AppThemeService, useValue: {applySyncedTheme, applyDeviceTheme, useStoredDeviceTheme, appState: () => ({themeSyncEnabled: true})}},
|
||||
{provide: AppLocaleService, useValue: {applyLocale, getDisplayLocale}},
|
||||
]
|
||||
});
|
||||
|
||||
@@ -32,10 +52,16 @@ describe('StartupService', () => {
|
||||
|
||||
await expect(service.load()).resolves.toBeUndefined();
|
||||
expect(fetchQuery).toHaveBeenCalledOnce();
|
||||
expect(applySyncedTheme).toHaveBeenCalledWith('grimmory', 'orange');
|
||||
expect(applyDeviceTheme).not.toHaveBeenCalled();
|
||||
expect(applyLocale).toHaveBeenCalledWith('en');
|
||||
});
|
||||
|
||||
it('resolves immediately when there is no token', async () => {
|
||||
it('loads the local display locale when there is no token', async () => {
|
||||
const fetchQuery = vi.fn();
|
||||
const applySyncedTheme = vi.fn();
|
||||
const applyLocale = vi.fn().mockResolvedValue(undefined);
|
||||
const getDisplayLocale = vi.fn().mockReturnValue('de');
|
||||
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
@@ -51,6 +77,8 @@ describe('StartupService', () => {
|
||||
},
|
||||
},
|
||||
{provide: QueryClient, useValue: {fetchQuery}},
|
||||
{provide: AppThemeService, useValue: {applySyncedTheme, appState: () => ({themeSyncEnabled: true})}},
|
||||
{provide: AppLocaleService, useValue: {applyLocale, getDisplayLocale}},
|
||||
]
|
||||
});
|
||||
|
||||
@@ -58,5 +86,111 @@ describe('StartupService', () => {
|
||||
|
||||
await expect(service.load()).resolves.toBeUndefined();
|
||||
expect(fetchQuery).not.toHaveBeenCalled();
|
||||
expect(getDisplayLocale).toHaveBeenCalledOnce();
|
||||
expect(applyLocale).toHaveBeenCalledWith('de');
|
||||
});
|
||||
|
||||
it('uses the backend locale for authenticated users', async () => {
|
||||
const fetchQuery = vi.fn().mockResolvedValue({id: 1, username: 'admin', locale: 'en', theme: 'custom', themeAccent: 'teal', themeSyncEnabled: true});
|
||||
const updateUserProfile = vi.fn().mockReturnValue(of({id: 1, username: 'admin', locale: 'de', theme: 'custom', themeAccent: 'teal', themeSyncEnabled: true}));
|
||||
const applySyncedTheme = vi.fn();
|
||||
const applyLocale = vi.fn().mockResolvedValue(undefined);
|
||||
const getDisplayLocale = vi.fn().mockReturnValue('de');
|
||||
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
StartupService,
|
||||
{provide: AuthService, useValue: {token: signal('token')}},
|
||||
{
|
||||
provide: UserService,
|
||||
useValue: {
|
||||
getUserQueryOptions: () => queryOptions({
|
||||
queryKey: ['user'],
|
||||
queryFn: async () => ({id: 1, username: 'admin', locale: 'en', theme: 'custom', themeAccent: 'teal', themeSyncEnabled: true}),
|
||||
}),
|
||||
updateUserProfile,
|
||||
},
|
||||
},
|
||||
{provide: QueryClient, useValue: {fetchQuery}},
|
||||
{provide: AppThemeService, useValue: {applySyncedTheme, appState: () => ({themeSyncEnabled: true})}},
|
||||
{provide: AppLocaleService, useValue: {applyLocale, getDisplayLocale}},
|
||||
]
|
||||
});
|
||||
|
||||
const service = TestBed.inject(StartupService);
|
||||
|
||||
await service.load();
|
||||
|
||||
expect(updateUserProfile).not.toHaveBeenCalled();
|
||||
expect(applySyncedTheme).toHaveBeenCalledWith('custom', 'teal');
|
||||
expect(applyLocale).toHaveBeenCalledWith('en');
|
||||
});
|
||||
|
||||
it('uses a local device theme when theme sync is disabled', async () => {
|
||||
const fetchQuery = vi.fn().mockResolvedValue({id: 1, username: 'admin', locale: 'en', theme: 'cobalt', themeAccent: null, themeSyncEnabled: false});
|
||||
const applySyncedTheme = vi.fn();
|
||||
const applyDeviceTheme = vi.fn();
|
||||
const useStoredDeviceTheme = vi.fn();
|
||||
const applyLocale = vi.fn().mockResolvedValue(undefined);
|
||||
const getDisplayLocale = vi.fn().mockReturnValue('en');
|
||||
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
StartupService,
|
||||
{provide: AuthService, useValue: {token: signal('token')}},
|
||||
{
|
||||
provide: UserService,
|
||||
useValue: {
|
||||
getUserQueryOptions: () => queryOptions({
|
||||
queryKey: ['user'],
|
||||
queryFn: async () => ({id: 1, username: 'admin', locale: 'en', theme: 'cobalt', themeAccent: null, themeSyncEnabled: false}),
|
||||
}),
|
||||
},
|
||||
},
|
||||
{provide: QueryClient, useValue: {fetchQuery}},
|
||||
{provide: AppThemeService, useValue: {applySyncedTheme, applyDeviceTheme, useStoredDeviceTheme, appState: () => ({themeSyncEnabled: false})}},
|
||||
{provide: AppLocaleService, useValue: {applyLocale, getDisplayLocale}},
|
||||
]
|
||||
});
|
||||
|
||||
const service = TestBed.inject(StartupService);
|
||||
|
||||
await service.load();
|
||||
|
||||
expect(applySyncedTheme).not.toHaveBeenCalled();
|
||||
expect(applyDeviceTheme).not.toHaveBeenCalled();
|
||||
expect(useStoredDeviceTheme).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it('seeds the local device theme from the backend when sync is disabled without a local override', async () => {
|
||||
const fetchQuery = vi.fn().mockResolvedValue({id: 1, username: 'admin', locale: 'en', theme: 'cobalt', themeAccent: null, themeSyncEnabled: false});
|
||||
const applyDeviceTheme = vi.fn();
|
||||
const applyLocale = vi.fn().mockResolvedValue(undefined);
|
||||
const getDisplayLocale = vi.fn().mockReturnValue('en');
|
||||
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
StartupService,
|
||||
{provide: AuthService, useValue: {token: signal('token')}},
|
||||
{
|
||||
provide: UserService,
|
||||
useValue: {
|
||||
getUserQueryOptions: () => queryOptions({
|
||||
queryKey: ['user'],
|
||||
queryFn: async () => ({id: 1, username: 'admin', locale: 'en', theme: 'cobalt', themeAccent: null, themeSyncEnabled: false}),
|
||||
}),
|
||||
},
|
||||
},
|
||||
{provide: QueryClient, useValue: {fetchQuery}},
|
||||
{provide: AppThemeService, useValue: {applyDeviceTheme, appState: () => ({themeSyncEnabled: true})}},
|
||||
{provide: AppLocaleService, useValue: {applyLocale, getDisplayLocale}},
|
||||
]
|
||||
});
|
||||
|
||||
const service = TestBed.inject(StartupService);
|
||||
|
||||
await service.load();
|
||||
|
||||
expect(applyDeviceTheme).toHaveBeenCalledWith('cobalt', 'orange');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,19 +1,46 @@
|
||||
import {Injectable, inject} from '@angular/core';
|
||||
import {AuthService} from './auth.service';
|
||||
import {UserService} from '../../features/settings/user-management/user.service';
|
||||
import {User, UserService} from '../../features/settings/user-management/user.service';
|
||||
import {QueryClient} from '@tanstack/angular-query-experimental';
|
||||
import {AppThemeService} from './app-theme.service';
|
||||
import {DEFAULT_CUSTOM_PRIMARY} from '../model/app-state.model';
|
||||
import {AppLocaleService} from './app-locale.service';
|
||||
|
||||
@Injectable({providedIn: 'root'})
|
||||
export class StartupService {
|
||||
private authService = inject(AuthService);
|
||||
private userService = inject(UserService);
|
||||
private queryClient = inject(QueryClient);
|
||||
private appThemeService = inject(AppThemeService);
|
||||
private appLocaleService = inject(AppLocaleService);
|
||||
|
||||
load(): Promise<void> {
|
||||
async load(): Promise<void> {
|
||||
if (this.authService.token()) {
|
||||
return this.queryClient.fetchQuery(this.userService.getUserQueryOptions()).then(() => undefined);
|
||||
const user = await this.queryClient.fetchQuery(this.userService.getUserQueryOptions());
|
||||
await this.applyUserPreferences(user);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return Promise.resolve();
|
||||
await this.appLocaleService.applyLocale(this.appLocaleService.getDisplayLocale());
|
||||
}
|
||||
|
||||
private async applyUserPreferences(user: User): Promise<void> {
|
||||
this.applyTheme(user);
|
||||
await this.appLocaleService.applyLocale(user.locale);
|
||||
}
|
||||
|
||||
private applyTheme(user: User): void {
|
||||
const customPrimary = user.themeAccent ?? DEFAULT_CUSTOM_PRIMARY;
|
||||
if (user.themeSyncEnabled) {
|
||||
this.appThemeService.applySyncedTheme(user.theme, customPrimary);
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.appThemeService.appState().themeSyncEnabled === false) {
|
||||
this.appThemeService.useStoredDeviceTheme();
|
||||
return;
|
||||
}
|
||||
|
||||
this.appThemeService.applyDeviceTheme(user.theme, customPrimary);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user