mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 14:38:45 +00:00
feat(settings): using local storage to save settings
Need to ensure that when a new setting is set, we re-render the phone.
This commit is contained in:
1 parent
08e21f4129
commit
3e03993d5b
5 files changed
+65
-34
No files matched your search
+9
-3
@@ -25,6 +25,7 @@ import { useNotesService } from './apps/notes/hooks/useNotesService';
|
||||
import { usePhotoService } from './apps/camera/hooks/usePhotoService';
|
||||
import Nui from './os/nui-events/utils/Nui';
|
||||
import { usePhone } from './os/phone/hooks/usePhone';
|
||||
import useLocalStorage from './os/phone/hooks/useLocalStorage';
|
||||
|
||||
// Inject mock data when in development env.
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
@@ -245,6 +246,7 @@ function Phone() {
|
||||
useNuiService();
|
||||
usePhoneService();
|
||||
const { visibility } = usePhone();
|
||||
const [, , getStorageItem] = useLocalStorage();
|
||||
const { settings, currentTheme } = useSettings();
|
||||
const { allApps } = useApps();
|
||||
useSimcardService();
|
||||
@@ -272,19 +274,23 @@ function Phone() {
|
||||
return (
|
||||
<ThemeProvider theme={currentTheme()}>
|
||||
<div className='PhoneWrapper'>
|
||||
<div style={{ zoom: settings.zoom }}>
|
||||
<div style={{ zoom: getStorageItem('zoom') }}>
|
||||
<div className='Phone'>
|
||||
<div
|
||||
className='PhoneFrame'
|
||||
style={{
|
||||
backgroundImage: `url(./media/frames/${settings.frame})`,
|
||||
backgroundImage: `url(./media/frames/${getStorageItem(
|
||||
'frame'
|
||||
)})`,
|
||||
}}
|
||||
></div>
|
||||
<div
|
||||
id='phone'
|
||||
className='PhoneScreen'
|
||||
style={{
|
||||
backgroundImage: `url(./media/backgrounds/${settings.wallpaper})`,
|
||||
backgroundImage: `url(./media/backgrounds/${getStorageItem(
|
||||
'wallpaper'
|
||||
)})`,
|
||||
}}
|
||||
>
|
||||
<>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import React from 'react';
|
||||
import React, { useState } from 'react';
|
||||
import { AppWrapper } from '../../../ui/components';
|
||||
import { AppTitle } from '../../../ui/components/AppTitle';
|
||||
import { AppContent } from '../../../ui/components/AppContent';
|
||||
@@ -24,6 +24,7 @@ import {
|
||||
} from '@material-ui/icons';
|
||||
|
||||
import { ListSubheader } from '@material-ui/core';
|
||||
import useLocalStorage from '../../../os/phone/hooks/useLocalStorage';
|
||||
|
||||
const SubHeaderComp = (props: { text: string }) => (
|
||||
<ListSubheader component='div' disableSticky>
|
||||
@@ -35,26 +36,33 @@ export const SettingsApp = () => {
|
||||
const settingsApp = useApp('SETTINGS');
|
||||
const [config] = useConfig();
|
||||
const { setSettings, settings } = useSettings();
|
||||
const [, setVal, getStorageItem] = useLocalStorage();
|
||||
const simcard = useSimcard();
|
||||
|
||||
const wallpapers = config.wallpapers.map(
|
||||
MapStringOptions(settings.wallpaper, (val: string) =>
|
||||
setSettings('wallpaper', val)
|
||||
MapStringOptions(getStorageItem('wallpaper'), (val: string) =>
|
||||
setVal('wallpaper', val)
|
||||
)
|
||||
);
|
||||
const frames = config.frames.map(
|
||||
MapStringOptions(settings.frame, (val: string) => setSettings('frame', val))
|
||||
MapStringOptions(getStorageItem('frame'), (val: string) =>
|
||||
setVal('frame', val)
|
||||
)
|
||||
);
|
||||
const themes = Object.keys(config.themes).map(
|
||||
MapStringOptions(settings.theme, (val: string) => setSettings('theme', val))
|
||||
MapStringOptions(getStorageItem('theme'), (val: string) =>
|
||||
setVal('theme', val)
|
||||
)
|
||||
);
|
||||
const zoomOptions = config.zoomOptions.map(
|
||||
MapStringOptions(settings.zoom, (val: string) => setSettings('zoom', val))
|
||||
MapStringOptions(getStorageItem('zoom'), (val: string) =>
|
||||
setVal('zoom', val)
|
||||
)
|
||||
);
|
||||
// Doesn't actually do anything for the time being
|
||||
const ringtones = config.ringtones.map(
|
||||
MapStringOptions(settings.ringtone, (val: string) =>
|
||||
setSettings('ringtone', val)
|
||||
MapStringOptions(getStorageItem('ringtone'), (val: string) =>
|
||||
setVal('ringtone', val)
|
||||
)
|
||||
);
|
||||
// * Probably gonna make this a slider component in the future
|
||||
@@ -77,7 +85,7 @@ export const SettingsApp = () => {
|
||||
/>
|
||||
<SettingItem
|
||||
label='Ringtone'
|
||||
value={settings.ringtone}
|
||||
value={getStorageItem('ringtone')}
|
||||
options={ringtones}
|
||||
onClick={openMenu}
|
||||
icon={<LibraryMusic />}
|
||||
@@ -97,28 +105,28 @@ export const SettingsApp = () => {
|
||||
<List disablePadding subheader={<SubHeaderComp text='Appearance' />}>
|
||||
<SettingItem
|
||||
label='Theme'
|
||||
value={settings.theme}
|
||||
value={getStorageItem('theme')}
|
||||
options={themes}
|
||||
onClick={openMenu}
|
||||
icon={<Brush />}
|
||||
/>
|
||||
<SettingItem
|
||||
label='Wallpaper'
|
||||
value={settings.wallpaper}
|
||||
value={getStorageItem('wallpaper')}
|
||||
options={wallpapers}
|
||||
onClick={openMenu}
|
||||
icon={<Wallpaper />}
|
||||
/>
|
||||
<SettingItem
|
||||
label='Frame'
|
||||
value={settings.frame}
|
||||
value={getStorageItem('frame')}
|
||||
options={frames}
|
||||
onClick={openMenu}
|
||||
icon={<Smartphone />}
|
||||
/>
|
||||
<SettingItem
|
||||
label='Zoom'
|
||||
value={settings.zoom}
|
||||
value={getStorageItem('zoom')}
|
||||
options={zoomOptions}
|
||||
onClick={openMenu}
|
||||
icon={<ZoomIn />}
|
||||
|
||||
@@ -1,16 +1,19 @@
|
||||
import Default from "../default.json";
|
||||
import { useConfig } from "../../../config/hooks/useConfig";
|
||||
import { atom } from "recoil";
|
||||
import { createMuiTheme } from "@material-ui/core";
|
||||
import Default from '../default.json';
|
||||
import { useConfig } from '../../../config/hooks/useConfig';
|
||||
import { atom } from 'recoil';
|
||||
import { createMuiTheme } from '@material-ui/core';
|
||||
import useLocalStorage from '../../../os/phone/hooks/useLocalStorage';
|
||||
|
||||
const settingsState = atom({
|
||||
key: "settings",
|
||||
key: 'settings',
|
||||
default: Default,
|
||||
});
|
||||
|
||||
export const useSettings = (): any => {
|
||||
const [settings, setSettings] = useConfig(settingsState);
|
||||
const [, , getStorageItem] = useLocalStorage('theme', 'taso-dark');
|
||||
const [settings, setSettings] = useLocalStorage();
|
||||
const [config] = useConfig();
|
||||
const currentTheme = () => createMuiTheme(config.themes[settings.theme]);
|
||||
const currentTheme = () =>
|
||||
createMuiTheme(config.themes[getStorageItem('theme')]);
|
||||
return { settings, setSettings, currentTheme };
|
||||
};
|
||||
@@ -1,19 +1,19 @@
|
||||
import Default from "../default.json";
|
||||
import { atom, useRecoilState } from "recoil";
|
||||
import Default from '../default.json';
|
||||
import { atom, useRecoilState } from 'recoil';
|
||||
import useLocalStorage from '../../os/phone/hooks/useLocalStorage';
|
||||
|
||||
const configState = atom({
|
||||
key: "config",
|
||||
key: 'config',
|
||||
default: Default,
|
||||
});
|
||||
|
||||
export const useConfig = (state: any = configState): any => {
|
||||
const [, setVal] = useLocalStorage();
|
||||
const [config, _setConfig] = useRecoilState(state);
|
||||
|
||||
const setConfig = (key, value) => {
|
||||
_setConfig((oldConfig) => ({
|
||||
...oldConfig,
|
||||
[key]: value,
|
||||
}));
|
||||
console.log(`setConfig: ${key} ${value}`);
|
||||
setVal(key, value);
|
||||
};
|
||||
|
||||
return [config, setConfig];
|
||||
|
||||
@@ -9,7 +9,7 @@ import { useState } from 'react';
|
||||
* if there is no savedState
|
||||
*/
|
||||
|
||||
function useLocalStorage<T>(key: string, initialValue: T) {
|
||||
function useLocalStorage<T>(key?: string, initialValue?: T) {
|
||||
// Serves as a nice identifying prefix for the key
|
||||
const UNIQ_PREFIX = 'npwd_phone_';
|
||||
const [localVal, setLocalVal] = useState<T>(() => {
|
||||
@@ -25,8 +25,22 @@ function useLocalStorage<T>(key: string, initialValue: T) {
|
||||
}
|
||||
});
|
||||
|
||||
const getStorageItem = (localKey: string) => {
|
||||
try {
|
||||
const data = window.localStorage.getItem(UNIQ_PREFIX + localKey);
|
||||
// return stored or initValue if none stored
|
||||
return data ? JSON.parse(data) : initialValue;
|
||||
} catch (e) {
|
||||
// TODO: Integrate debug error state here
|
||||
console.log(e);
|
||||
// We still return initVal if error is thrown
|
||||
return initialValue;
|
||||
}
|
||||
};
|
||||
|
||||
// Trying to rep useState setter pretty close just with localStorage
|
||||
const setVal = (value: T | ((val: T) => T)) => {
|
||||
const setVal = (localKey, value: T | ((val: T) => T)) => {
|
||||
console.log(localKey, value);
|
||||
try {
|
||||
// Same API as useState meaning we can accept a function for value
|
||||
const valForStore = value instanceof Function ? value(localVal) : value;
|
||||
@@ -34,7 +48,7 @@ function useLocalStorage<T>(key: string, initialValue: T) {
|
||||
setLocalVal(valForStore);
|
||||
// Set that localStorage yo
|
||||
window.localStorage.setItem(
|
||||
UNIQ_PREFIX + key,
|
||||
UNIQ_PREFIX + localKey,
|
||||
JSON.stringify(valForStore)
|
||||
);
|
||||
} catch (e) {
|
||||
@@ -43,7 +57,7 @@ function useLocalStorage<T>(key: string, initialValue: T) {
|
||||
}
|
||||
};
|
||||
|
||||
return [localVal, setVal];
|
||||
return [localVal, setVal, getStorageItem] as const;
|
||||
}
|
||||
|
||||
export default useLocalStorage;
|
||||
Reference in new issue
Block a user