Skip to content

Commit

Permalink
Rename UISettings to Settings
Browse files Browse the repository at this point in the history
Rename UISettings to Settings to better match purpose.

Author-Change-Id: IB#1141363
Signed-off-by: Pawel Boguslawski <[email protected]>
  • Loading branch information
pboguslawski committed Feb 26, 2024
1 parent 629095e commit 2eaa4e7
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 29 deletions.
16 changes: 8 additions & 8 deletions src/lib/common/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// SPDX-License-Identifier: AGPL-3.0-only
// SPDX-FileCopyrightText: 2024 Informatyka Boguslawski sp. z o.o. sp.k. <https://www.ib.pl>

import { initializeEnvironment, isUISettings, type UISettings } from './index';
import { initializeEnvironment, isSettings, type Settings } from './index';

// Check common routnes.
describe('common', () => {
test('isUISettings recognizes valid UISettings', () => {
const validUISettings = <UISettings>{ darkTheme: true, locale: 'en-US' };
expect(isUISettings(validUISettings)).toBeTruthy();
test('isSettings recognizes valid Settings', () => {
const validSettings = <Settings>{ darkTheme: true, locale: 'en-US' };
expect(isSettings(validSettings)).toBeTruthy();
});

test('isUISettings recognizes invalid UISettings', () => {
expect(isUISettings(1)).toBeFalsy();
expect(isUISettings({ darkTheme: 1, locale: 'en-US' })).toBeFalsy();
expect(isUISettings({ darkTheme: false, locale: 0 })).toBeFalsy();
test('isSettings recognizes invalid Settings', () => {
expect(isSettings(1)).toBeFalsy();
expect(isSettings({ darkTheme: 1, locale: 'en-US' })).toBeFalsy();
expect(isSettings({ darkTheme: false, locale: 0 })).toBeFalsy();
});

test('initializeEnvironment does not throw errors', () => {
Expand Down
31 changes: 15 additions & 16 deletions src/lib/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
// SPDX-FileCopyrightText: 2024 Informatyka Boguslawski sp. z o.o. sp.k. <https://www.ib.pl>

import { loadTranslations } from '$lib/i18n';
import { uiSettings } from '$lib/stores';
import { settings } from '$lib/stores';
import { get } from 'svelte/store';
import { t } from '$lib/i18n';
import { convertToAppError } from '$lib/errors';

// UISettings defines users UI settings.
export type UISettings = {
// Settings defines users UI settings.
export type Settings = {
// darkTheme allows to set application theme mode:
// when true - dark theme is used,convertToAppError
// when false - light theme is used,
Expand All @@ -19,10 +19,10 @@ export type UISettings = {
locale: string;
};

// isUISettings is UISettings type guard.
export function isUISettings(arg: unknown): arg is UISettings {
// isSettings is Settings type guard.
export function isSettings(arg: unknown): arg is Settings {
// Falsy (https://developer.mozilla.org/en-US/docs/Glossary/Falsy)
// or something not object type is not a UISettings.
// or something not object type is not a Settings.
if (!arg || typeof arg !== 'object') {
return false;
}
Expand All @@ -37,19 +37,19 @@ export function isUISettings(arg: unknown): arg is UISettings {
return false;
}

// All checks passed so we assume it's UISettings.
// All checks passed so we assume it's Settings.
return true;
}

// initUISettings initializes UI settings.
function initUISettings() {
// initSettings initializes UI settings.
function initSettings() {
// Use current settings as start point.
const uiSettingsNew = get(uiSettings);
const settingsNew = get(settings);

// Set default browser locale if not specified already.
if (uiSettingsNew.locale === '') {
uiSettingsNew.locale = window.navigator.language;
uiSettings.set(uiSettingsNew);
if (settingsNew.locale === '') {
settingsNew.locale = window.navigator.language;
settings.set(settingsNew);
}
}

Expand All @@ -72,11 +72,10 @@ async function initializeEnvironmentInternal(path: string) {
}

// Initialize UI settings.
initUISettings();
initSettings();

// Load translations based on locale from UI settings.
const settings = get(uiSettings);
await loadTranslations(settings.locale, path);
await loadTranslations(get(settings).locale, path);
}

// initializeEnvironment initializes application environment for given path with
Expand Down
4 changes: 2 additions & 2 deletions src/lib/stores/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-FileCopyrightText: 2024 Informatyka Boguslawski sp. z o.o. sp.k. <https://www.ib.pl>

import { writable } from 'svelte/store';
import type { UISettings } from '$lib/common';
import type { Settings } from '$lib/common';

// progressOpen is true when top progress bar is enabled.
export const progressOpen = writable<boolean>(false);
Expand All @@ -14,7 +14,7 @@ export const progressOpen = writable<boolean>(false);
export const smallWindow = writable<boolean | undefined>(undefined);

// Current UI settings.
export const uiSettings = writable<UISettings>({
export const settings = writable<Settings>({
darkTheme: undefined, // Browser default theme mode.
locale: '' // Browser default locale.
});
Expand Down
6 changes: 3 additions & 3 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ SPDX-FileCopyrightText: 2024 Informatyka Boguslawski sp. z o.o. sp.k. <https://w
// Import components.
import { navigating } from '$app/stores';
import { uiSettings, smallWindow, progressOpen } from '$lib/stores';
import { settings, smallWindow, progressOpen } from '$lib/stores';
import { dir, locale } from '$lib/i18n';
import { onMount } from 'svelte';
import ProgressBar from '$lib/components/common/ProgressBar.svelte';
Expand Down Expand Up @@ -50,10 +50,10 @@ SPDX-FileCopyrightText: 2024 Informatyka Boguslawski sp. z o.o. sp.k. <https://w
<link rel="preload" href="/smui-dark.css" as="style" />
...to avoid FOUC on page load. -->
<svelte:head>
{#if $uiSettings.darkTheme === undefined}
{#if $settings.darkTheme === undefined}
<link rel="stylesheet" href="/smui.css" media="(prefers-color-scheme: light)" />
<link rel="stylesheet" href="/smui-dark.css" media="screen and (prefers-color-scheme: dark)" />
{:else if $uiSettings.darkTheme}
{:else if $settings.darkTheme}
<link rel="stylesheet" href="/smui.css" />
<link rel="stylesheet" href="/smui-dark.css" media="screen" />
{:else}
Expand Down

0 comments on commit 2eaa4e7

Please sign in to comment.