diff --git a/phone/src/Phone.tsx b/phone/src/Phone.tsx index 5ae11c6..09a5bb6 100644 --- a/phone/src/Phone.tsx +++ b/phone/src/Phone.tsx @@ -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 (
-
+
<> diff --git a/phone/src/apps/settings/components/SettingsApp.tsx b/phone/src/apps/settings/components/SettingsApp.tsx index f8071ca..ac7eaeb 100644 --- a/phone/src/apps/settings/components/SettingsApp.tsx +++ b/phone/src/apps/settings/components/SettingsApp.tsx @@ -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 }) => ( @@ -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 = () => { /> } @@ -97,28 +105,28 @@ export const SettingsApp = () => { }> } /> } /> } /> } diff --git a/phone/src/apps/settings/hooks/useSettings.ts b/phone/src/apps/settings/hooks/useSettings.ts index 61def22..8ef1312 100644 --- a/phone/src/apps/settings/hooks/useSettings.ts +++ b/phone/src/apps/settings/hooks/useSettings.ts @@ -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 }; }; diff --git a/phone/src/config/hooks/useConfig.ts b/phone/src/config/hooks/useConfig.ts index 87e2fcf..617940e 100644 --- a/phone/src/config/hooks/useConfig.ts +++ b/phone/src/config/hooks/useConfig.ts @@ -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]; diff --git a/phone/src/os/phone/hooks/useLocalStorage.ts b/phone/src/os/phone/hooks/useLocalStorage.ts index ab5a4b0..9c8bd80 100644 --- a/phone/src/os/phone/hooks/useLocalStorage.ts +++ b/phone/src/os/phone/hooks/useLocalStorage.ts @@ -9,7 +9,7 @@ import { useState } from 'react'; * if there is no savedState */ -function useLocalStorage(key: string, initialValue: T) { +function useLocalStorage(key?: string, initialValue?: T) { // Serves as a nice identifying prefix for the key const UNIQ_PREFIX = 'npwd_phone_'; const [localVal, setLocalVal] = useState(() => { @@ -25,8 +25,22 @@ function useLocalStorage(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(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(key: string, initialValue: T) { } }; - return [localVal, setVal]; + return [localVal, setVal, getStorageItem] as const; } export default useLocalStorage;