Files
CT/src/Model/Global/settingsModel.ts
T

121 lines
3.0 KiB
TypeScript
Raw Normal View History

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,
filePaths.settingsModel[pathName],
2026-04-01 08:29:39 +00:00
);
this.storageModel.write(path, value);
2026-08-29 02:10:26 +02: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 => {
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 => {
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 => {
this.language = newValue;
this.storeSetting("language", newValue);
2026-04-13 11:23:17 +00:00
};
2026-04-18 11:55:01 +00:00
readonly setTheme = (newValue: string): void => {
this.theme = newValue;
this.storeSetting("theme", newValue);
2026-04-13 11:23:17 +00:00
};
2026-04-01 08:29:39 +00:00
// load
2026-04-18 11:55:01 +00:00
private readonly readSetting = (
pathName: keyof typeof filePaths.settingsModel,
2026-09-23 00:09:09 +02:00
): string | null => {
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 => {
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-18 11:55:01 +00:00
readonly loadUsername = (): void => {
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 => {
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 => {
const content: string | null = this.readSetting("language");
this.language = content ?? SettingsModel.getSystemLanguage();
2026-04-13 11:23:17 +00:00
};
2026-04-18 11:55:01 +00:00
readonly loadTheme = (): void => {
const content: string | null = this.readSetting("theme");
this.theme = content ?? ThemeSettings.System;
2026-04-13 11:23:17 +00:00
};
2026-04-01 08:29:39 +00:00
// init
constructor(public storageModel: StorageModel) {
this.loadId();
this.loadUsername();
this.loadFirstDayofWeek();
this.loadLanguage();
this.loadTheme();
2026-04-01 08:29:39 +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
}
export enum Languages {
English = "en",
German = "de",
Spanish = "es",
}
export enum ThemeSettings {
Dark = "dark",
Light = "light",
System = "system",
Dynamic = "dynamic",
Black = "black",
2026-04-09 18:09:26 +00:00
}