save current theme to localStorage

This commit is contained in:
Halit Aksoy 2022-06-04 19:28:57 +03:00
parent 0e48fe9228
commit c5f1741d4b
2 changed files with 25 additions and 5 deletions

View File

@ -2,7 +2,6 @@ import { Texts, TrTr } from "./const/texts";
import { RTMT } from "./rtmt/rtmt"; import { RTMT } from "./rtmt/rtmt";
import { RTMTWS } from "./rtmt/rtmt_websocket"; import { RTMTWS } from "./rtmt/rtmt_websocket";
import { UserKeyStore, UserKeyStoreImpl } from "./store/key_store"; import { UserKeyStore, UserKeyStoreImpl } from "./store/key_store";
import defaultTheme from "./theme/DefaultTheme";
import ThemeManager from "./theme/ThemeManager"; import ThemeManager from "./theme/ThemeManager";
export type Context = { export type Context = {
@ -16,7 +15,7 @@ export const initContext = () => {
const rtmt = new RTMTWS(); const rtmt = new RTMTWS();
const userKeyStore = new UserKeyStoreImpl(); const userKeyStore = new UserKeyStoreImpl();
const texts = TrTr; const texts = TrTr;
const themeManager = new ThemeManager(defaultTheme); const themeManager = new ThemeManager();
return { return {
rtmt: rtmt, rtmt: rtmt,
userKeyStore: userKeyStore, userKeyStore: userKeyStore,

View File

@ -1,18 +1,39 @@
import defaultTheme from "./DefaultTheme";
import oldTheme from "./OldTheme";
import { Theme } from "./Theme"; import { Theme } from "./Theme";
export const themes = [defaultTheme, oldTheme];
const THEME_ID = "theme_id";
export default class ThemeManager { export default class ThemeManager {
_theme: Theme; _theme: Theme;
onThemeChange: (theme: Theme) => void; onThemeChange: (theme: Theme) => void;
constructor(theme: Theme) { constructor() {
this._theme = theme; this._theme = this.readFromLocalStorage() || defaultTheme;
} }
public get theme() { public get theme() {
return this._theme; return this._theme;
} }
public set theme(value) { public set theme(value: Theme) {
this._theme = value; this._theme = value;
this.onThemeChange?.(value); this.onThemeChange?.(value);
this.writetToLocalStorage(value);
}
private writetToLocalStorage(value: Theme) {
localStorage.setItem(THEME_ID, value.id);
}
private readFromLocalStorage(): Theme {
const themeID = localStorage.getItem(THEME_ID);
const theme = themes.find((eachTheme: Theme) => themeID === eachTheme.id);
return theme;
}
public get themes(): Theme[] {
return themes;
} }
} }