2026-04-08 14:02:12 +00:00
|
|
|
import StorageModel, { StorageModelSubPaths, filePaths } from "./storageModel";
|
2026-09-27 01:58:06 +02:00
|
|
|
import { UUID } from "../../react";
|
2024-07-20 00:22:53 +02:00
|
|
|
|
|
|
|
|
export default class SettingsModel {
|
2026-04-01 08:29:39 +00:00
|
|
|
// data
|
2026-09-09 00:04:27 +02:00
|
|
|
username: string = "";
|
2026-09-27 01:46:55 +02:00
|
|
|
userid: string = "";
|
2026-09-09 00:04:27 +02:00
|
|
|
firstDayOfWeek: string = "";
|
|
|
|
|
language: string = "";
|
|
|
|
|
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-08-29 02:10:26 +02:00
|
|
|
};
|
2026-04-09 17:32:17 +00:00
|
|
|
|
2026-09-27 01:46:55 +02:00
|
|
|
readonly setId = (newValue: string): void => {
|
|
|
|
|
this.userid = newValue;
|
|
|
|
|
this.storeSetting("userid", newValue);
|
|
|
|
|
};
|
2026-09-27 01:58:06 +02:00
|
|
|
|
2026-04-18 11:55:01 +00:00
|
|
|
readonly setName = (newValue: string): void => {
|
2026-04-09 17:32:17 +00:00
|
|
|
this.username = newValue;
|
2026-09-27 03:05:01 +02:00
|
|
|
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-09-27 03:05:01 +02:00
|
|
|
this.firstDayOfWeek = newValue;
|
|
|
|
|
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-09-27 03:05:01 +02:00
|
|
|
this.language = newValue;
|
|
|
|
|
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-09-27 03:05:01 +02: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-09-27 03:05:01 +02:00
|
|
|
pathName: keyof typeof filePaths.settingsModel,
|
2026-09-23 00:09:09 +02:00
|
|
|
): string | null => {
|
2026-09-27 03:05:01 +02:00
|
|
|
const path = StorageModel.getPath(
|
|
|
|
|
StorageModelSubPaths.SettingsModel,
|
|
|
|
|
filePaths.settingsModel[pathName],
|
|
|
|
|
);
|
|
|
|
|
return this.storageModel.read(path);
|
2026-04-13 11:23:17 +00:00
|
|
|
};
|
2026-09-27 01:58:06 +02:00
|
|
|
|
2026-09-27 01:48:41 +02:00
|
|
|
readonly loadId = (): void => {
|
2026-09-27 03:05:01 +02:00
|
|
|
const content: string | null = this.readSetting("userid");
|
|
|
|
|
const id = content ?? UUID();
|
|
|
|
|
if (content) this.userid = content;
|
|
|
|
|
else this.setId(id);
|
2026-09-27 01:46:55 +02:00
|
|
|
};
|
2026-04-09 17:32:17 +00:00
|
|
|
|
2026-04-18 11:55:01 +00:00
|
|
|
readonly loadUsername = (): void => {
|
2026-09-27 03:05:01 +02:00
|
|
|
const content: string | null = this.readSetting("username");
|
|
|
|
|
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-09-27 03:05:01 +02:00
|
|
|
const content: string | null = this.readSetting("firstDayOfWeek");
|
|
|
|
|
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-09-27 03:05:01 +02:00
|
|
|
const content: string | null = this.readSetting("language");
|
|
|
|
|
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-09-27 03:05:01 +02:00
|
|
|
const content: string | null = this.readSetting("theme");
|
|
|
|
|
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
|
2026-09-28 15:07:02 +02:00
|
|
|
constructor(public storageModel: StorageModel) {
|
2026-09-27 03:05:01 +02:00
|
|
|
this.loadId();
|
|
|
|
|
this.loadUsername();
|
|
|
|
|
this.loadFirstDayofWeek();
|
|
|
|
|
this.loadLanguage();
|
|
|
|
|
this.loadTheme();
|
2026-04-01 08:29:39 +00:00
|
|
|
}
|
2026-04-09 16:48:22 +00:00
|
|
|
|
|
|
|
|
static getSystemLanguage(): Languages {
|
2026-09-27 03:05:01 +02:00
|
|
|
switch (navigator.language.substring(0, 2)) {
|
|
|
|
|
case "de":
|
|
|
|
|
return Languages.German;
|
|
|
|
|
case "es":
|
|
|
|
|
return Languages.Spanish;
|
|
|
|
|
default:
|
|
|
|
|
return Languages.English;
|
|
|
|
|
}
|
2026-04-09 16:48:22 +00:00
|
|
|
}
|
2024-07-20 00:22:53 +02:00
|
|
|
}
|
2026-04-09 17:32:17 +00:00
|
|
|
|
|
|
|
|
export enum Languages {
|
|
|
|
|
English = "en",
|
2026-09-27 03:05:01 +02:00
|
|
|
German = "de",
|
|
|
|
|
Spanish = "es",
|
2026-04-09 17:32:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export enum ThemeSettings {
|
|
|
|
|
Dark = "dark",
|
2026-09-27 03:05:01 +02:00
|
|
|
Light = "light",
|
|
|
|
|
System = "system",
|
|
|
|
|
Dynamic = "dynamic",
|
|
|
|
|
Black = "black",
|
2026-04-09 18:09:26 +00:00
|
|
|
}
|