mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-03 22:16:54 +00:00
Feat: Settings localStorage state
This commit is contained in:
commit
73a5bab1c7
15 files changed
+120
-122
No files matched your search
+1
-1
@@ -32,7 +32,7 @@
|
||||
"react-popper": "^2.2.3",
|
||||
"react-router-dom": "^5.2.0",
|
||||
"react-scripts": "3.4.3",
|
||||
"recoil": "^0.0.10",
|
||||
"recoil": "^0.1.2",
|
||||
"styled-components": "^5.1.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
||||
+10
-4
@@ -2,11 +2,10 @@ import React from 'react';
|
||||
import './Phone.css';
|
||||
import './i18n';
|
||||
import { Route } from 'react-router-dom';
|
||||
import { useSettings } from './apps/settings/hooks/useSettings';
|
||||
import MessageIcon from '@material-ui/icons/Email';
|
||||
import { CallModal } from './modal/components/CallModal';
|
||||
import { HomeApp } from './apps/home/components/Home';
|
||||
import { ThemeProvider } from '@material-ui/core';
|
||||
import { createMuiTheme, ThemeProvider } from '@material-ui/core';
|
||||
import { useInitKeyboard } from './os/keyboard/hooks/useKeyboard';
|
||||
import { NotificationIcon } from './os/notifications/components/NotificationIcon';
|
||||
import { NotificationBar } from './os/notifications/components/NotificationBar';
|
||||
@@ -25,6 +24,10 @@ 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 { settingsState } from './apps/settings/hooks/useSettings';
|
||||
|
||||
import config from './config/default.json';
|
||||
import { useRecoilState } from 'recoil';
|
||||
|
||||
// Inject mock data when in development env.
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
@@ -245,7 +248,6 @@ function Phone() {
|
||||
useNuiService();
|
||||
usePhoneService();
|
||||
const { visibility } = usePhone();
|
||||
const { settings, currentTheme } = useSettings();
|
||||
const { allApps } = useApps();
|
||||
useSimcardService();
|
||||
useContactsService();
|
||||
@@ -257,10 +259,14 @@ function Phone() {
|
||||
useNotesService();
|
||||
usePhotoService();
|
||||
|
||||
const [settings] = useRecoilState(settingsState);
|
||||
|
||||
if (visibility === false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const currentTheme = () => createMuiTheme(config.themes[settings.theme]);
|
||||
|
||||
const calling = false;
|
||||
|
||||
document.onkeyup = function (data) {
|
||||
@@ -279,7 +285,7 @@ function Phone() {
|
||||
style={{
|
||||
backgroundImage: `url(./media/frames/${settings.frame})`,
|
||||
}}
|
||||
></div>
|
||||
/>
|
||||
<div
|
||||
id='phone'
|
||||
className='PhoneScreen'
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
{
|
||||
"wallpaper": "default.png",
|
||||
"frame": "s10.png",
|
||||
"theme": "taso-dark",
|
||||
"zoom": "100%",
|
||||
"ringtone": "pixel",
|
||||
"ringtoneVol": 100
|
||||
}
|
||||
@@ -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];
|
||||
};
|
||||
@@ -10,6 +10,14 @@
|
||||
"surf.jpg",
|
||||
"beach.jpg"
|
||||
],
|
||||
"defaultSettings": {
|
||||
"wallpaper": "default.png",
|
||||
"frame": "s10.png",
|
||||
"theme": "taso-dark",
|
||||
"zoom": "100%",
|
||||
"ringtone": "pixel",
|
||||
"ringtoneVol": 100
|
||||
},
|
||||
"frames": ["default.png", "s10.png", "tw.png", "samsung.png"],
|
||||
"zoomOptions": ["120%", "100%", "80%", "60%", "40%"],
|
||||
"ringtones": ["marimba", "pixel"],
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import Default from "../default.json";
|
||||
import { atom, useRecoilState } from "recoil";
|
||||
import Default from '../default.json';
|
||||
import { atom, useRecoilState } from 'recoil';
|
||||
|
||||
const configState = atom({
|
||||
key: "config",
|
||||
key: 'config',
|
||||
default: Default,
|
||||
});
|
||||
|
||||
@@ -14,6 +14,7 @@ export const useConfig = (state: any = configState): any => {
|
||||
...oldConfig,
|
||||
[key]: value,
|
||||
}));
|
||||
//setVal(key, value);
|
||||
};
|
||||
|
||||
return [config, setConfig];
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import { atom } from 'recoil';
|
||||
|
||||
export const phoneSettings = {
|
||||
theme: atom({
|
||||
key: 'settingTheme',
|
||||
default: 'default-dark',
|
||||
}),
|
||||
zoom: atom({
|
||||
key: 'settingZoom',
|
||||
default: '100%',
|
||||
}),
|
||||
};
|
||||
@@ -9,4 +9,8 @@ export const phoneState = {
|
||||
key: 'phoneConfig',
|
||||
default: undefined,
|
||||
}),
|
||||
forceUpdateState: atom({
|
||||
key: 'fuckingupdate',
|
||||
default: 0,
|
||||
}),
|
||||
};
|
||||
@@ -1,49 +0,0 @@
|
||||
import { useState } from 'react';
|
||||
|
||||
// TODO: Integrate debug log to fetch when that branch is merged
|
||||
|
||||
/**
|
||||
* A LocalStorage hook designed to behave like the useState hook.
|
||||
* @param {string} key - The unique identifier to be used as a key
|
||||
* @param {string} intialValue - The initialValue to be used
|
||||
* if there is no savedState
|
||||
*/
|
||||
|
||||
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>(() => {
|
||||
try {
|
||||
const data = window.localStorage.getItem(UNIQ_PREFIX + key);
|
||||
// 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)) => {
|
||||
try {
|
||||
// Same API as useState meaning we can accept a function for value
|
||||
const valForStore = value instanceof Function ? value(localVal) : value;
|
||||
// Save that state so we repli useState
|
||||
setLocalVal(valForStore);
|
||||
// Set that localStorage yo
|
||||
window.localStorage.setItem(
|
||||
UNIQ_PREFIX + key,
|
||||
JSON.stringify(valForStore)
|
||||
);
|
||||
} catch (e) {
|
||||
// TODO: Also integrate debug log state error here
|
||||
console.log(e);
|
||||
}
|
||||
};
|
||||
|
||||
return [localVal, setVal];
|
||||
}
|
||||
|
||||
export default useLocalStorage;
|
||||
@@ -1,17 +1,16 @@
|
||||
import React from "react";
|
||||
import { ThemeProvider } from "@material-ui/core";
|
||||
import Paper from "@material-ui/core/Paper";
|
||||
import Snackbar from "@material-ui/core/Snackbar";
|
||||
import Fade from "@material-ui/core/Fade";
|
||||
import { makeStyles } from "@material-ui/core/styles";
|
||||
import { useSettings } from "../../apps/settings/hooks/useSettings";
|
||||
import { usePhone } from "../../os/phone/hooks/usePhone";
|
||||
import React from 'react';
|
||||
import Paper from '@material-ui/core/Paper';
|
||||
import Snackbar from '@material-ui/core/Snackbar';
|
||||
import Fade from '@material-ui/core/Fade';
|
||||
import { makeStyles } from '@material-ui/core/styles';
|
||||
import { useSettings } from '../../apps/settings/hooks/useSettings';
|
||||
import { usePhone } from '../../os/phone/hooks/usePhone';
|
||||
|
||||
const useStyles = makeStyles({
|
||||
paper: {
|
||||
width: "350px",
|
||||
height: "100px",
|
||||
opacity: "0.93",
|
||||
width: '350px',
|
||||
height: '100px',
|
||||
opacity: '0.93',
|
||||
},
|
||||
snackBar: {
|
||||
zIndex: -5, // we want this to appear behind other active NUI events...probably
|
||||
@@ -20,7 +19,7 @@ const useStyles = makeStyles({
|
||||
|
||||
function Notification({ children, handleClose, open }) {
|
||||
const classes = useStyles();
|
||||
const { currentTheme } = useSettings();
|
||||
const [settings] = useSettings();
|
||||
const { config } = usePhone();
|
||||
|
||||
if (!config) return null;
|
||||
@@ -28,19 +27,22 @@ function Notification({ children, handleClose, open }) {
|
||||
const { horizontal, vertical } = config.notificationPosition;
|
||||
|
||||
return (
|
||||
<ThemeProvider theme={currentTheme()}>
|
||||
<Snackbar
|
||||
className={classes.snackBar}
|
||||
anchorOrigin={{ horizontal, vertical }}
|
||||
ClickAwayListenerProps={{ onClickAway: () => setTimeout(() => { handleClose() }, 5000) }}
|
||||
onClose={handleClose}
|
||||
open={open}
|
||||
TransitionComponent={Fade}
|
||||
autoHideDuration={6000}
|
||||
>
|
||||
<Paper className={classes.paper}>{children}</Paper>
|
||||
</Snackbar>
|
||||
</ThemeProvider>
|
||||
<Snackbar
|
||||
className={classes.snackBar}
|
||||
anchorOrigin={{ horizontal, vertical }}
|
||||
ClickAwayListenerProps={{
|
||||
onClickAway: () =>
|
||||
setTimeout(() => {
|
||||
handleClose();
|
||||
}, 5000),
|
||||
}}
|
||||
onClose={handleClose}
|
||||
open={open}
|
||||
TransitionComponent={Fade}
|
||||
autoHideDuration={6000}
|
||||
>
|
||||
<Paper className={classes.paper}>{children}</Paper>
|
||||
</Snackbar>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
+4
-4
@@ -10159,10 +10159,10 @@ recast@~0.11.12:
|
||||
private "~0.1.5"
|
||||
source-map "~0.5.0"
|
||||
|
||||
recoil@^0.0.10:
|
||||
version "0.0.10"
|
||||
resolved "https://registry.yarnpkg.com/recoil/-/recoil-0.0.10.tgz#679ab22306f559f8a63c46fd5ff5241539f9248f"
|
||||
integrity sha512-+9gRqehw3yKETmoZbhSnWu4GO10HDb5xYf1CjLF1oXGK2uT6GX5Lu9mfTXwjxV/jXxEKx8MIRUUbgPxvbJ8SEw==
|
||||
recoil@^0.1.2:
|
||||
version "0.1.2"
|
||||
resolved "https://registry.yarnpkg.com/recoil/-/recoil-0.1.2.tgz#844c612f535826dbe54c977761a67ded16f6d063"
|
||||
integrity sha512-hIRrHlkmW4yITlBFprVYjVPhzPKYrJKoaDrrJtAtbkMeXfXaa/XE5OlyR10n+rNfnKWNToCKb3Z4fo86IGjkzg==
|
||||
|
||||
[email protected]:
|
||||
version "2.2.2"
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
.vscode
|
||||
dist/
|
||||
node_modules/
|
||||
yarn-error.log
|
||||
html
|
||||
@@ -1,8 +0,0 @@
|
||||
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
|
||||
# yarn lockfile v1
|
||||
|
||||
|
||||
[email protected]:
|
||||
version "3.9.7"
|
||||
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.9.7.tgz#98d600a5ebdc38f40cb277522f12dc800e9e25fa"
|
||||
integrity sha512-BLbiRkiBzAwsjut4x/dsibSTB6yWpwT5qWmC2OfuCg3GgVQCSgMs4vEctYPhsaGtd0AeuuHMkjZ2h2WG8MSzRw==
|
||||
Reference in new issue
Block a user