mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 22:16:40 +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 { usePhotoService } from './apps/camera/hooks/usePhotoService';
|
||||||
import Nui from './os/nui-events/utils/Nui';
|
import Nui from './os/nui-events/utils/Nui';
|
||||||
import { usePhone } from './os/phone/hooks/usePhone';
|
import { usePhone } from './os/phone/hooks/usePhone';
|
||||||
|
import useLocalStorage from './os/phone/hooks/useLocalStorage';
|
||||||
|
|
||||||
// Inject mock data when in development env.
|
// Inject mock data when in development env.
|
||||||
if (process.env.NODE_ENV === 'development') {
|
if (process.env.NODE_ENV === 'development') {
|
||||||
@@ -245,6 +246,7 @@ function Phone() {
|
|||||||
useNuiService();
|
useNuiService();
|
||||||
usePhoneService();
|
usePhoneService();
|
||||||
const { visibility } = usePhone();
|
const { visibility } = usePhone();
|
||||||
|
const [, , getStorageItem] = useLocalStorage();
|
||||||
const { settings, currentTheme } = useSettings();
|
const { settings, currentTheme } = useSettings();
|
||||||
const { allApps } = useApps();
|
const { allApps } = useApps();
|
||||||
useSimcardService();
|
useSimcardService();
|
||||||
@@ -272,19 +274,23 @@ function Phone() {
|
|||||||
return (
|
return (
|
||||||
<ThemeProvider theme={currentTheme()}>
|
<ThemeProvider theme={currentTheme()}>
|
||||||
<div className='PhoneWrapper'>
|
<div className='PhoneWrapper'>
|
||||||
<div style={{ zoom: settings.zoom }}>
|
<div style={{ zoom: getStorageItem('zoom') }}>
|
||||||
<div className='Phone'>
|
<div className='Phone'>
|
||||||
<div
|
<div
|
||||||
className='PhoneFrame'
|
className='PhoneFrame'
|
||||||
style={{
|
style={{
|
||||||
backgroundImage: `url(./media/frames/${settings.frame})`,
|
backgroundImage: `url(./media/frames/${getStorageItem(
|
||||||
|
'frame'
|
||||||
|
)})`,
|
||||||
}}
|
}}
|
||||||
></div>
|
></div>
|
||||||
<div
|
<div
|
||||||
id='phone'
|
id='phone'
|
||||||
className='PhoneScreen'
|
className='PhoneScreen'
|
||||||
style={{
|
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 { AppWrapper } from '../../../ui/components';
|
||||||
import { AppTitle } from '../../../ui/components/AppTitle';
|
import { AppTitle } from '../../../ui/components/AppTitle';
|
||||||
import { AppContent } from '../../../ui/components/AppContent';
|
import { AppContent } from '../../../ui/components/AppContent';
|
||||||
@@ -24,6 +24,7 @@ import {
|
|||||||
} from '@material-ui/icons';
|
} from '@material-ui/icons';
|
||||||
|
|
||||||
import { ListSubheader } from '@material-ui/core';
|
import { ListSubheader } from '@material-ui/core';
|
||||||
|
import useLocalStorage from '../../../os/phone/hooks/useLocalStorage';
|
||||||
|
|
||||||
const SubHeaderComp = (props: { text: string }) => (
|
const SubHeaderComp = (props: { text: string }) => (
|
||||||
<ListSubheader component='div' disableSticky>
|
<ListSubheader component='div' disableSticky>
|
||||||
@@ -35,26 +36,33 @@ export const SettingsApp = () => {
|
|||||||
const settingsApp = useApp('SETTINGS');
|
const settingsApp = useApp('SETTINGS');
|
||||||
const [config] = useConfig();
|
const [config] = useConfig();
|
||||||
const { setSettings, settings } = useSettings();
|
const { setSettings, settings } = useSettings();
|
||||||
|
const [, setVal, getStorageItem] = useLocalStorage();
|
||||||
const simcard = useSimcard();
|
const simcard = useSimcard();
|
||||||
|
|
||||||
const wallpapers = config.wallpapers.map(
|
const wallpapers = config.wallpapers.map(
|
||||||
MapStringOptions(settings.wallpaper, (val: string) =>
|
MapStringOptions(getStorageItem('wallpaper'), (val: string) =>
|
||||||
setSettings('wallpaper', val)
|
setVal('wallpaper', val)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
const frames = config.frames.map(
|
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(
|
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(
|
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
|
// Doesn't actually do anything for the time being
|
||||||
const ringtones = config.ringtones.map(
|
const ringtones = config.ringtones.map(
|
||||||
MapStringOptions(settings.ringtone, (val: string) =>
|
MapStringOptions(getStorageItem('ringtone'), (val: string) =>
|
||||||
setSettings('ringtone', val)
|
setVal('ringtone', val)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
// * Probably gonna make this a slider component in the future
|
// * Probably gonna make this a slider component in the future
|
||||||
@@ -77,7 +85,7 @@ export const SettingsApp = () => {
|
|||||||
/>
|
/>
|
||||||
<SettingItem
|
<SettingItem
|
||||||
label='Ringtone'
|
label='Ringtone'
|
||||||
value={settings.ringtone}
|
value={getStorageItem('ringtone')}
|
||||||
options={ringtones}
|
options={ringtones}
|
||||||
onClick={openMenu}
|
onClick={openMenu}
|
||||||
icon={<LibraryMusic />}
|
icon={<LibraryMusic />}
|
||||||
@@ -97,28 +105,28 @@ export const SettingsApp = () => {
|
|||||||
<List disablePadding subheader={<SubHeaderComp text='Appearance' />}>
|
<List disablePadding subheader={<SubHeaderComp text='Appearance' />}>
|
||||||
<SettingItem
|
<SettingItem
|
||||||
label='Theme'
|
label='Theme'
|
||||||
value={settings.theme}
|
value={getStorageItem('theme')}
|
||||||
options={themes}
|
options={themes}
|
||||||
onClick={openMenu}
|
onClick={openMenu}
|
||||||
icon={<Brush />}
|
icon={<Brush />}
|
||||||
/>
|
/>
|
||||||
<SettingItem
|
<SettingItem
|
||||||
label='Wallpaper'
|
label='Wallpaper'
|
||||||
value={settings.wallpaper}
|
value={getStorageItem('wallpaper')}
|
||||||
options={wallpapers}
|
options={wallpapers}
|
||||||
onClick={openMenu}
|
onClick={openMenu}
|
||||||
icon={<Wallpaper />}
|
icon={<Wallpaper />}
|
||||||
/>
|
/>
|
||||||
<SettingItem
|
<SettingItem
|
||||||
label='Frame'
|
label='Frame'
|
||||||
value={settings.frame}
|
value={getStorageItem('frame')}
|
||||||
options={frames}
|
options={frames}
|
||||||
onClick={openMenu}
|
onClick={openMenu}
|
||||||
icon={<Smartphone />}
|
icon={<Smartphone />}
|
||||||
/>
|
/>
|
||||||
<SettingItem
|
<SettingItem
|
||||||
label='Zoom'
|
label='Zoom'
|
||||||
value={settings.zoom}
|
value={getStorageItem('zoom')}
|
||||||
options={zoomOptions}
|
options={zoomOptions}
|
||||||
onClick={openMenu}
|
onClick={openMenu}
|
||||||
icon={<ZoomIn />}
|
icon={<ZoomIn />}
|
||||||
|
|||||||
@@ -1,16 +1,19 @@
|
|||||||
import Default from "../default.json";
|
import Default from '../default.json';
|
||||||
import { useConfig } from "../../../config/hooks/useConfig";
|
import { useConfig } from '../../../config/hooks/useConfig';
|
||||||
import { atom } from "recoil";
|
import { atom } from 'recoil';
|
||||||
import { createMuiTheme } from "@material-ui/core";
|
import { createMuiTheme } from '@material-ui/core';
|
||||||
|
import useLocalStorage from '../../../os/phone/hooks/useLocalStorage';
|
||||||
|
|
||||||
const settingsState = atom({
|
const settingsState = atom({
|
||||||
key: "settings",
|
key: 'settings',
|
||||||
default: Default,
|
default: Default,
|
||||||
});
|
});
|
||||||
|
|
||||||
export const useSettings = (): any => {
|
export const useSettings = (): any => {
|
||||||
const [settings, setSettings] = useConfig(settingsState);
|
const [, , getStorageItem] = useLocalStorage('theme', 'taso-dark');
|
||||||
|
const [settings, setSettings] = useLocalStorage();
|
||||||
const [config] = useConfig();
|
const [config] = useConfig();
|
||||||
const currentTheme = () => createMuiTheme(config.themes[settings.theme]);
|
const currentTheme = () =>
|
||||||
|
createMuiTheme(config.themes[getStorageItem('theme')]);
|
||||||
return { settings, setSettings, currentTheme };
|
return { settings, setSettings, currentTheme };
|
||||||
};
|
};
|
||||||
@@ -1,19 +1,19 @@
|
|||||||
import Default from "../default.json";
|
import Default from '../default.json';
|
||||||
import { atom, useRecoilState } from "recoil";
|
import { atom, useRecoilState } from 'recoil';
|
||||||
|
import useLocalStorage from '../../os/phone/hooks/useLocalStorage';
|
||||||
|
|
||||||
const configState = atom({
|
const configState = atom({
|
||||||
key: "config",
|
key: 'config',
|
||||||
default: Default,
|
default: Default,
|
||||||
});
|
});
|
||||||
|
|
||||||
export const useConfig = (state: any = configState): any => {
|
export const useConfig = (state: any = configState): any => {
|
||||||
|
const [, setVal] = useLocalStorage();
|
||||||
const [config, _setConfig] = useRecoilState(state);
|
const [config, _setConfig] = useRecoilState(state);
|
||||||
|
|
||||||
const setConfig = (key, value) => {
|
const setConfig = (key, value) => {
|
||||||
_setConfig((oldConfig) => ({
|
console.log(`setConfig: ${key} ${value}`);
|
||||||
...oldConfig,
|
setVal(key, value);
|
||||||
[key]: value,
|
|
||||||
}));
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return [config, setConfig];
|
return [config, setConfig];
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import { useState } from 'react';
|
|||||||
* if there is no savedState
|
* 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
|
// Serves as a nice identifying prefix for the key
|
||||||
const UNIQ_PREFIX = 'npwd_phone_';
|
const UNIQ_PREFIX = 'npwd_phone_';
|
||||||
const [localVal, setLocalVal] = useState<T>(() => {
|
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
|
// 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 {
|
try {
|
||||||
// Same API as useState meaning we can accept a function for value
|
// Same API as useState meaning we can accept a function for value
|
||||||
const valForStore = value instanceof Function ? value(localVal) : value;
|
const valForStore = value instanceof Function ? value(localVal) : value;
|
||||||
@@ -34,7 +48,7 @@ function useLocalStorage<T>(key: string, initialValue: T) {
|
|||||||
setLocalVal(valForStore);
|
setLocalVal(valForStore);
|
||||||
// Set that localStorage yo
|
// Set that localStorage yo
|
||||||
window.localStorage.setItem(
|
window.localStorage.setItem(
|
||||||
UNIQ_PREFIX + key,
|
UNIQ_PREFIX + localKey,
|
||||||
JSON.stringify(valForStore)
|
JSON.stringify(valForStore)
|
||||||
);
|
);
|
||||||
} catch (e) {
|
} 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;
|
export default useLocalStorage;
|
||||||
Reference in new issue
Block a user