2026-04-18 11:55:01 +00:00
|
|
|
// cleanup: Phase A
|
2024-07-20 00:22:53 +02:00
|
|
|
|
2026-04-08 14:02:12 +00:00
|
|
|
import StorageModel, { StorageModelSubPaths, filePaths } from "./storageModel";
|
2024-07-20 00:22:53 +02:00
|
|
|
|
|
|
|
|
export default class SettingsModel {
|
2026-04-13 10:58:21 +00:00
|
|
|
readonly storageModel: StorageModel;
|
2026-04-01 08:29:39 +00:00
|
|
|
|
|
|
|
|
// data
|
|
|
|
|
username: string;
|
|
|
|
|
firstDayOfWeek: string;
|
2026-04-09 16:48:22 +00:00
|
|
|
language: string;
|
2026-04-09 17:32:17 +00:00
|
|
|
theme: string;
|
2026-04-01 08:29:39 +00:00
|
|
|
|
|
|
|
|
// storage
|
2026-04-18 11:55:01 +00:00
|
|
|
private readonly storeSetting = (
|
2026-04-09 18:09:26 +00:00
|
|
|
pathName: keyof typeof filePaths.settingsModel,
|
|
|
|
|
value: string,
|
2026-04-18 11:55:01 +00:00
|
|
|
): void => {
|
2026-04-01 08:29:39 +00:00
|
|
|
const path = StorageModel.getPath(
|
2026-04-08 14:02:12 +00:00
|
|
|
StorageModelSubPaths.SettingsModel,
|
2026-04-09 17:32:17 +00:00
|
|
|
filePaths.settingsModel[pathName],
|
2026-04-01 08:29:39 +00:00
|
|
|
);
|
2026-04-09 17:32:17 +00:00
|
|
|
this.storageModel.write(path, value);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-18 11:55:01 +00:00
|
|
|
readonly setName = (newValue: string): void => {
|
2026-04-09 17:32:17 +00:00
|
|
|
this.username = newValue;
|
|
|
|
|
this.storeSetting("username", newValue);
|
2026-04-13 11:23:17 +00:00
|
|
|
};
|
2026-04-01 08:29:39 +00:00
|
|
|
|
2026-04-18 11:55:01 +00:00
|
|
|
readonly setFirstDayOfWeek = (newValue: string): void => {
|
2026-04-01 08:29:39 +00:00
|
|
|
this.firstDayOfWeek = newValue;
|
2026-04-09 17:32:17 +00:00
|
|
|
this.storeSetting("firstDayOfWeek", newValue);
|
2026-04-13 11:23:17 +00:00
|
|
|
};
|
2026-04-01 08:29:39 +00:00
|
|
|
|
2026-04-18 11:55:01 +00:00
|
|
|
readonly setLanguage = (newValue: string): void => {
|
2026-04-09 16:48:22 +00:00
|
|
|
this.language = newValue;
|
2026-04-09 17:32:17 +00:00
|
|
|
this.storeSetting("language", newValue);
|
2026-04-13 11:23:17 +00:00
|
|
|
};
|
2026-04-09 17:32:17 +00:00
|
|
|
|
2026-04-18 11:55:01 +00:00
|
|
|
readonly setTheme = (newValue: string): void => {
|
2026-04-09 17:32:17 +00:00
|
|
|
this.theme = newValue;
|
|
|
|
|
this.storeSetting("theme", newValue);
|
2026-04-13 11:23:17 +00:00
|
|
|
};
|
2026-04-09 16:48:22 +00:00
|
|
|
|
2026-04-01 08:29:39 +00:00
|
|
|
// load
|
2026-04-18 11:55:01 +00:00
|
|
|
private readonly readSetting = (
|
2026-04-09 18:09:26 +00:00
|
|
|
pathName: keyof typeof filePaths.settingsModel,
|
2026-04-13 10:51:03 +00:00
|
|
|
): string => {
|
2026-04-01 08:29:39 +00:00
|
|
|
const path = StorageModel.getPath(
|
2026-04-08 14:02:12 +00:00
|
|
|
StorageModelSubPaths.SettingsModel,
|
2026-04-09 17:32:17 +00:00
|
|
|
filePaths.settingsModel[pathName],
|
2026-04-01 08:29:39 +00:00
|
|
|
);
|
2026-04-09 17:32:17 +00:00
|
|
|
return this.storageModel.read(path);
|
2026-04-13 11:23:17 +00:00
|
|
|
};
|
2026-04-09 17:32:17 +00:00
|
|
|
|
2026-04-18 11:55:01 +00:00
|
|
|
readonly loadUsername = (): void => {
|
2026-04-13 11:23:17 +00:00
|
|
|
const content: string | null = this.readSetting("username");
|
2026-04-01 08:29:39 +00:00
|
|
|
this.username = content ?? "";
|
2026-04-13 11:23:17 +00:00
|
|
|
};
|
2026-04-01 08:29:39 +00:00
|
|
|
|
2026-04-18 11:55:01 +00:00
|
|
|
readonly loadFirstDayofWeek = (): void => {
|
2026-04-13 11:23:17 +00:00
|
|
|
const content: string | null = this.readSetting("firstDayOfWeek");
|
2026-04-01 08:29:39 +00:00
|
|
|
this.firstDayOfWeek = content ?? "0";
|
2026-04-13 11:23:17 +00:00
|
|
|
};
|
2026-04-01 08:29:39 +00:00
|
|
|
|
2026-04-18 11:55:01 +00:00
|
|
|
readonly loadLanguage = (): void => {
|
2026-04-13 11:23:17 +00:00
|
|
|
const content: string | null = this.readSetting("language");
|
2026-04-09 16:48:22 +00:00
|
|
|
this.language = content ?? SettingsModel.getSystemLanguage();
|
2026-04-13 11:23:17 +00:00
|
|
|
};
|
2026-04-09 16:48:22 +00:00
|
|
|
|
2026-04-18 11:55:01 +00:00
|
|
|
readonly loadTheme = (): void => {
|
2026-04-13 11:23:17 +00:00
|
|
|
const content: string | null = this.readSetting("theme");
|
2026-04-09 17:32:17 +00:00
|
|
|
this.theme = content ?? ThemeSettings.System;
|
2026-04-13 11:23:17 +00:00
|
|
|
};
|
2026-04-09 17:32:17 +00:00
|
|
|
|
2026-04-01 08:29:39 +00:00
|
|
|
// init
|
|
|
|
|
constructor(storageModel: StorageModel) {
|
|
|
|
|
this.storageModel = storageModel;
|
|
|
|
|
|
2026-04-09 17:14:33 +00:00
|
|
|
this.loadUsername();
|
2026-04-01 08:29:39 +00:00
|
|
|
this.loadFirstDayofWeek();
|
2026-04-09 17:14:33 +00:00
|
|
|
this.loadLanguage();
|
2026-04-09 18:09:26 +00:00
|
|
|
this.loadTheme();
|
2026-04-01 08:29:39 +00:00
|
|
|
}
|
2026-04-09 16:48:22 +00:00
|
|
|
|
|
|
|
|
static getSystemLanguage(): Languages {
|
|
|
|
|
switch (navigator.language.substring(0, 2)) {
|
|
|
|
|
case "de":
|
|
|
|
|
return Languages.German;
|
|
|
|
|
case "es":
|
|
|
|
|
return Languages.Spanish;
|
|
|
|
|
default:
|
|
|
|
|
return Languages.English;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-20 00:22:53 +02:00
|
|
|
}
|
2026-04-09 17:32:17 +00:00
|
|
|
|
|
|
|
|
export enum Languages {
|
|
|
|
|
English = "en",
|
|
|
|
|
German = "de",
|
|
|
|
|
Spanish = "es",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export enum ThemeSettings {
|
|
|
|
|
Dark = "dark",
|
|
|
|
|
Light = "light",
|
|
|
|
|
System = "system",
|
2026-04-09 18:09:26 +00:00
|
|
|
}
|