refactor: LocalStorage state integration

This commit is contained in:
Taso committed 2021-01-12 11:28:20 -05:00
1 parent 5af6e4936d
commit 8912650f0e
10 files changed
+103 -154

No files matched your search

@@ -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';
@@ -24,7 +23,8 @@ import {
} from '@material-ui/icons';
import { ListSubheader } from '@material-ui/core';
import useLocalStorage from '../../../os/phone/hooks/useLocalStorage';
import { useRecoilState } from 'recoil';
import { settingsState } from '../hooks/useSettings';
const SubHeaderComp = (props: { text: string }) => (
<ListSubheader component='div' disableSticky>
@@ -36,31 +36,37 @@ export const SettingsApp = () => {
const settingsApp = useApp('SETTINGS');
const [config] = useConfig();
const simcard = useSimcard();
const [settings, setSettings] = useRecoilState(settingsState);
const [wallpaper, setWallpaper] = useLocalStorage<any>(
'wallpaper',
'surf.jpg'
);
const [frame, setFrame] = useLocalStorage<any>('frame', 's10.png');
const [theme, setTheme] = useLocalStorage<any>('theme', 'default-dark');
const [zoom, setZoom] = useLocalStorage<any>('zoom', '100%');
const [ringtone, setRingtone] = useLocalStorage<any>('ringtone', '');
const handleSettingChange = (key: string | number, value: unknown) => {
setSettings({ ...settings, [key]: value });
};
const wallpapers = config.wallpapers.map(
MapStringOptions(wallpaper, (val: string) => setWallpaper(val))
MapStringOptions(settings.wallpaper, (val: string) =>
handleSettingChange('wallpaper', val)
)
);
const frames = config.frames.map(
MapStringOptions(frame, (val: string) => setFrame(val))
MapStringOptions(settings.frame, (val: string) =>
handleSettingChange('frame', val)
)
);
const themes = Object.keys(config.themes).map(
MapStringOptions(theme, (val: string) => setTheme(val))
MapStringOptions(settings.theme, (val: string) =>
handleSettingChange('theme', val)
)
);
const zoomOptions = config.zoomOptions.map(
MapStringOptions(zoom, (val: string) => setZoom(val))
MapStringOptions(settings.zoom, (val: string) =>
handleSettingChange('zoom', val)
)
);
// Doesn't actually do anything for the time being
const ringtones = config.ringtones.map(
MapStringOptions(ringtone, (val: string) => setRingtone(val))
MapStringOptions(settings.ringtone, (val: string) =>
handleSettingChange('ringtone', val)
)
);
// * Probably gonna make this a slider component in the future
// const ringtoneVols = config.ringtoneVols.map(
@@ -69,8 +75,6 @@ export const SettingsApp = () => {
// TODO: These new settings all work
useEffect(() => {}, [theme]);
const [openMenu, closeMenu, ContextMenu, isMenuOpen] = useContextMenu();
return (
<AppWrapper>
@@ -84,7 +88,7 @@ export const SettingsApp = () => {
/>
<SettingItem
label='Ringtone'
value={ringtone}
value={settings.ringtone}
options={ringtones}
onClick={openMenu}
icon={<LibraryMusic />}
@@ -104,28 +108,28 @@ export const SettingsApp = () => {
<List disablePadding subheader={<SubHeaderComp text='Appearance' />}>
<SettingItem
label='Theme'
value={theme}
value={settings.theme}
options={themes}
onClick={openMenu}
icon={<Brush />}
/>
<SettingItem
label='Wallpaper'
value={wallpaper}
value={settings.wallpaper}
options={wallpapers}
onClick={openMenu}
icon={<Wallpaper />}
/>
<SettingItem
label='Frame'
value={frame}
value={settings.frame}
options={frames}
onClick={openMenu}
icon={<Smartphone />}
/>
<SettingItem
label='Zoom'
value={zoom}
value={settings.zoom}
options={zoomOptions}
onClick={openMenu}
icon={<ZoomIn />}
-8
View File
@@ -1,8 +0,0 @@
{
"wallpaper": "default.png",
"frame": "s10.png",
"theme": "taso-dark",
"zoom": "100%",
"ringtone": "pixel",
"ringtoneVol": 100
}
+23 -14
View File
@@ -1,19 +1,28 @@
import Default from '../default.json';
import { useConfig } from '../../../config/hooks/useConfig';
import { atom } from 'recoil';
import { createMuiTheme } from '@material-ui/core';
import useLocalStorage, {
getStorage,
} from '../../../os/phone/hooks/useLocalStorage';
import { atom, DefaultValue, useRecoilState } from 'recoil';
import config from '../../../config/default.json';
const settingsState = atom({
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: Default,
default: config.defaultSettings,
effects_UNSTABLE: [localStorageEffect('settings')],
});
export const useSettings = (): any => {
const theme = getStorage('theme');
const [config] = useConfig();
const currentTheme = () => createMuiTheme(config.themes[theme]);
return { currentTheme };
export const useSettings = () => {
const [settings, setSettings] = useRecoilState(settingsState);
return [settings, setSettings];
};