Feat: Settings localStorage state

This commit is contained in:
Taso committed 2021-01-12 12:04:46 -05:00
commit 73a5bab1c7
15 files changed
+120 -122

No files matched your search

@@ -5,6 +5,8 @@ import {
Divider,
ListItemIcon,
} from '@material-ui/core';
import { SvgIconProps } from '@material-ui/core';
interface IProps {
options?: any;
label: string;
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useEffect, useState } from 'react';
import { AppWrapper } from '../../../ui/components';
import { AppTitle } from '../../../ui/components/AppTitle';
import { AppContent } from '../../../ui/components/AppContent';
@@ -7,7 +7,6 @@ import {
MapStringOptions,
} from '../../../ui/hooks/useContextMenu';
import { useConfig } from '../../../config/hooks/useConfig';
import { useSettings } from '../hooks/useSettings';
import { List } from '../../../ui/components/List';
import { useSimcard } from '../../../os/simcard/hooks/useSimcard';
import { useApp } from '../../../os/apps/hooks/useApps';
@@ -25,6 +24,8 @@ import {
} from '@material-ui/icons';
import { ListSubheader } from '@material-ui/core';
import { useRecoilState } from 'recoil';
import { settingsState } from '../hooks/useSettings';
const SubHeaderComp = (props: { text: string }) => (
<ListSubheader component='div' disableSticky>
@@ -35,29 +36,39 @@ const SubHeaderComp = (props: { text: string }) => (
export const SettingsApp = () => {
const settingsApp = useApp('SETTINGS');
const [config] = useConfig();
const { setSettings, settings } = useSettings();
const simcard = useSimcard();
const [settings, setSettings] = useRecoilState(settingsState);
const handleSettingChange = (key: string | number, value: unknown) => {
setSettings({ ...settings, [key]: value });
};
const { t } = useTranslation();
const wallpapers = config.wallpapers.map(
MapStringOptions(settings.wallpaper, (val: string) =>
setSettings('wallpaper', val)
handleSettingChange('wallpaper', val)
)
);
const frames = config.frames.map(
MapStringOptions(settings.frame, (val: string) => setSettings('frame', val))
MapStringOptions(settings.frame, (val: string) =>
handleSettingChange('frame', val)
)
);
const themes = Object.keys(config.themes).map(
MapStringOptions(settings.theme, (val: string) => setSettings('theme', val))
MapStringOptions(settings.theme, (val: string) =>
handleSettingChange('theme', val)
)
);
const zoomOptions = config.zoomOptions.map(
MapStringOptions(settings.zoom, (val: string) => setSettings('zoom', val))
MapStringOptions(settings.zoom, (val: string) =>
handleSettingChange('zoom', val)
)
);
// Doesn't actually do anything for the time being
const ringtones = config.ringtones.map(
MapStringOptions(settings.ringtone, (val: string) =>
setSettings('ringtone', val)
handleSettingChange('ringtone', val)
)
);
// * Probably gonna make this a slider component in the future
-8
View File
@@ -1,8 +0,0 @@
{
"wallpaper": "default.png",
"frame": "s10.png",
"theme": "taso-dark",
"zoom": "100%",
"ringtone": "pixel",
"ringtoneVol": 100
}
+24 -12
View File
@@ -1,16 +1,28 @@
import Default from "../default.json";
import { useConfig } from "../../../config/hooks/useConfig";
import { atom } from "recoil";
import { createMuiTheme } from "@material-ui/core";
import { atom, DefaultValue, useRecoilState } from 'recoil';
import config from '../../../config/default.json';
const settingsState = atom({
key: "settings",
default: Default,
const localStorageEffect = (key) => ({ setSelf, onSet }) => {
const savedVal = localStorage.getItem(key);
if (savedVal != null) {
setSelf(JSON.parse(savedVal));
}
onSet((newValue) => {
if (newValue instanceof DefaultValue) {
localStorage.removeItem(key);
} else {
localStorage.setItem(key, JSON.stringify(newValue));
}
});
};
export const settingsState = atom({
key: 'settings',
default: config.defaultSettings,
effects_UNSTABLE: [localStorageEffect('settings')],
});
export const useSettings = (): any => {
const [settings, setSettings] = useConfig(settingsState);
const [config] = useConfig();
const currentTheme = () => createMuiTheme(config.themes[settings.theme]);
return { settings, setSettings, currentTheme };
export const useSettings = () => {
const [settings, setSettings] = useRecoilState(settingsState);
return [settings, setSettings];
};