From 64c4e0ec16aafa178187192273c02738c82a7dd4 Mon Sep 17 00:00:00 2001 From: Taso Date: Wed, 3 Mar 2021 16:42:38 -0500 Subject: [PATCH] enhance(phone/settings): Added labels to settings and misc cleanup This change alters the default localStorage schema, please clear your localStorage cache for testing --- phone/src/Phone.tsx | 6 +- .../apps/settings/components/SettingItem.tsx | 51 ++++- .../apps/settings/components/SettingsApp.tsx | 164 +++++---------- phone/src/apps/settings/hooks/useSettings.ts | 21 +- phone/src/config/default.json | 189 ++++++++++++------ phone/src/config/themes.json | 38 ++++ .../providers/NotificationsProvider.tsx | 2 +- phone/src/os/phone/hooks/usePhoneTheme.ts | 5 +- phone/src/os/sound/utils/getSoundSettings.ts | 8 +- phone/src/ui/components/Notification.tsx | 2 +- phone/src/ui/hooks/useContextMenu.tsx | 14 ++ 11 files changed, 290 insertions(+), 210 deletions(-) create mode 100644 phone/src/config/themes.json diff --git a/phone/src/Phone.tsx b/phone/src/Phone.tsx index 0534715..d7abdfc 100644 --- a/phone/src/Phone.tsx +++ b/phone/src/Phone.tsx @@ -59,21 +59,21 @@ function Phone() { className="Phone" style={{ transformOrigin: 'right bottom', - transform: `scale(${settings.zoom}`, + transform: `scale(${settings.zoom.value}`, bottom, }} >
<> diff --git a/phone/src/apps/settings/components/SettingItem.tsx b/phone/src/apps/settings/components/SettingItem.tsx index 4302702..92068e0 100644 --- a/phone/src/apps/settings/components/SettingItem.tsx +++ b/phone/src/apps/settings/components/SettingItem.tsx @@ -1,7 +1,14 @@ import React from 'react'; -import { ListItem, ListItemText, ListItemIcon } from '@material-ui/core'; +import { + ListItem, + ListItemText, + ListItemIcon, + ListItemSecondaryAction, + Box, + Slider, +} from '@material-ui/core'; -interface IProps { +interface ISettingItem { options?: any; label: string; value?: string | number | null; @@ -9,11 +16,35 @@ interface IProps { icon: JSX.Element; } -export const SettingItem = ({ options, label, value, onClick, icon }: IProps) => { - return ( - onClick?.(options)} button> - {icon} - - - ); -}; +export const SettingItem = ({ options, label, value, onClick, icon }: ISettingItem) => ( + onClick?.(options)} button> + {icon} + + +); + +interface ISettingSlider { + label: string; + icon: JSX.Element; + value: number; + onCommit: (event: React.ChangeEvent<{}>, value: number | number[]) => void; +} + +export const SettingItemSlider = ({ icon, label, value, onCommit }: ISettingSlider) => ( + + {icon} + + + + + + + +); diff --git a/phone/src/apps/settings/components/SettingsApp.tsx b/phone/src/apps/settings/components/SettingsApp.tsx index effb4d4..eb11a61 100644 --- a/phone/src/apps/settings/components/SettingsApp.tsx +++ b/phone/src/apps/settings/components/SettingsApp.tsx @@ -1,13 +1,13 @@ -import React, { useEffect, useState } from 'react'; +import React from 'react'; import { AppWrapper } from '../../../ui/components'; import { AppTitle } from '../../../ui/components/AppTitle'; import { AppContent } from '../../../ui/components/AppContent'; -import { useContextMenu, MapStringOptions } from '../../../ui/hooks/useContextMenu'; +import { useContextMenu, MapSettingItem, SettingOption } from '../../../ui/hooks/useContextMenu'; import { useConfig } from '../../../config/hooks/useConfig'; import { List } from '../../../ui/components/List'; import { useSimcard } from '../../../os/simcard/hooks/useSimcard'; import { useApp } from '../../../os/apps/hooks/useApps'; -import { SettingItem } from './SettingItem'; +import { SettingItem, SettingItemSlider } from './SettingItem'; import { useTranslation } from 'react-i18next'; import { @@ -20,17 +20,8 @@ import { VolumeUp, } from '@material-ui/icons'; -import { - Box, - ListItem, - ListItemIcon, - ListItemSecondaryAction, - ListItemText, - ListSubheader, - Slider, -} from '@material-ui/core'; -import { useRecoilState } from 'recoil'; -import { settingsState } from '../hooks/useSettings'; +import { ListSubheader } from '@material-ui/core'; +import { useSettings } from '../hooks/useSettings'; const SubHeaderComp = (props: { text: string }) => ( @@ -42,57 +33,39 @@ export const SettingsApp = () => { const settingsApp = useApp('SETTINGS'); const [config] = useConfig(); const simcard = useSimcard(); - const [settings, setSettings] = useRecoilState(settingsState); + const [settings, setSettings] = useSettings(); + const { t } = useTranslation(); const handleSettingChange = (key: string | number, value: unknown) => { setSettings({ ...settings, [key]: value }); }; - const [sliders, setSliders] = useState([]); - - useEffect(() => { - setSliders((curr) => { - if (!curr.length) { - return [settings.ringtoneVol, settings.notificationVol, settings.TWITTER_notificationVol]; - } - return curr; - }); - }, [settings.notificationVol, settings.ringtoneVol, settings.TWITTER_notificationVol]); - - const handleSliderChange = (idx) => (_e: any, value: number | number[]) => { - setSliders((curr) => { - const newValues = [...curr]; - newValues[idx] = value; - return newValues; - }); - }; - - const { t } = useTranslation(); - const wallpapers = config.wallpapers.map( - MapStringOptions(settings.wallpaper, (val: string) => handleSettingChange('wallpaper', val)), + MapSettingItem(settings.wallpaper, (val: SettingOption) => + handleSettingChange('wallpaper', val), + ), ); const frames = config.frames.map( - MapStringOptions(settings.frame, (val: string) => handleSettingChange('frame', val)), + MapSettingItem(settings.frame, (val: SettingOption) => handleSettingChange('frame', val)), ); - const themes = Object.keys(config.themes).map( - MapStringOptions(settings.theme, (val: string) => handleSettingChange('theme', val)), + const themes = config.themes.map( + MapSettingItem(settings.theme, (val: SettingOption) => handleSettingChange('theme', val)), ); const zoomOptions = config.zoomOptions.map( - MapStringOptions(settings.zoom, (val: string) => handleSettingChange('zoom', val)), + MapSettingItem(settings.zoom, (val: SettingOption) => handleSettingChange('zoom', val)), ); const ringtones = config.ringtones.map( - MapStringOptions(settings.ringtone, (val: string) => handleSettingChange('ringtone', val)), + MapSettingItem(settings.ringtone, (val: SettingOption) => handleSettingChange('ringtone', val)), ); - const notifications = config.notifications.map( - MapStringOptions(settings.notification, (val: string) => - handleSettingChange('notification', val), + const notifications = config.notiSounds.map( + MapSettingItem(settings.notiSound, (val: SettingOption) => + handleSettingChange('notiSound', val), ), ); - const twitterNotifications = config.notifications.map( - MapStringOptions(settings.notification, (val: string) => - handleSettingChange('TWITTER_notification', val), + const twitterNotifications = config.notiSounds.map( + MapSettingItem(settings.notiSound, (item: SettingOption) => + handleSettingChange('TWITTER_notiSound', item), ), ); @@ -109,84 +82,56 @@ export const SettingsApp = () => { /> } /> - - - - - - - - handleSettingChange('ringtoneVol', sliders[0])} - /> - - - + } + value={settings.ringtoneVol} + onCommit={(e, val) => handleSettingChange('ringtoneVol', val)} + /> } /> - - - - - - - - handleSettingChange('notificationVol', sliders[1])} - /> - - - + } + value={settings.notiSoundVol} + onCommit={(e, val) => handleSettingChange('notiSoundVol', val)} + /> }> } /> } /> } /> } @@ -195,34 +140,17 @@ export const SettingsApp = () => { }> } /> - - - - - - - - - - handleSettingChange('TWITTER_notificationVol', sliders[2]) - } - /> - - - + handleSettingChange('TWITTER_notiSoundVol', val)} + icon={} + /> diff --git a/phone/src/apps/settings/hooks/useSettings.ts b/phone/src/apps/settings/hooks/useSettings.ts index c2dc1c6..7de72b7 100644 --- a/phone/src/apps/settings/hooks/useSettings.ts +++ b/phone/src/apps/settings/hooks/useSettings.ts @@ -1,5 +1,6 @@ import { atom, DefaultValue, useRecoilState } from 'recoil'; import config from '../../../config/default.json'; +import { SettingOption } from '../../../ui/hooks/useContextMenu'; const localStorageEffect = (key) => ({ setSelf, onSet }) => { const savedVal = localStorage.getItem(key); @@ -23,19 +24,19 @@ export const settingsState = atom({ }); export interface IPhoneSettings { - wallpaper: string; - frame: string; - theme: string; - zoom: number; - ringtone: string; + wallpaper: SettingOption; + frame: SettingOption; + theme: SettingOption; + zoom: SettingOption; + ringtone: SettingOption; ringtoneVol: number; - notification: string; - notificationVol: number; - TWITTER_notification: string; - TWITTER_notificationVol: number; + notiSound: SettingOption; + notiSoundVol: number; + TWITTER_notiSound: SettingOption; + TWITTER_notiSoundVol: number; TWITTER_notifyNewFeedTweet: boolean; } export const useSettings = () => { - return useRecoilState(settingsState); + return useRecoilState(settingsState); }; diff --git a/phone/src/config/default.json b/phone/src/config/default.json index 46f4a48..f08642d 100644 --- a/phone/src/config/default.json +++ b/phone/src/config/default.json @@ -1,73 +1,142 @@ { "cellProvider": "AT&T", "language": "en", - "wallpapers": ["beach.jpg", "minimal.jpg", "surf.jpg", "waves.jpg", "projecterror.jpg"], + "wallpapers": [ + { + "label": "Minimal", + "value": "minimal.jpg" + }, + { + "label": "Surf", + "value": "surf.jpg" + }, + { + "label": "Waves", + "value": "waves.jpg" + }, + { + "label": "Project Error", + "value": "projecterror.jpg" + } + ], + "frames": [ + { + "label": "Default", + "value": "default.png" + }, + { + "label": "Blue", + "value": "blue.png" + }, + { + "label": "Gold", + "value": "gold.png" + }, + { + "label": "Kawaii", + "value": "kawaii.png" + }, + { + "label": "Minimal", + "value": "minimal.png" + }, + { + "label": "Pink", + "value": "pink.png" + }, + { + "label": "White", + "value": "white.png" + } + ], + "zoomOptions": [ + { + "label": "100%", + "value": 1 + }, + { + "label": "90%", + "value": 0.9 + }, + { + "label": "80%", + "value": 0.8 + }, + { + "label": "70%", + "value": 0.7 + } + ], + "ringtones": [ + { + "label": "Marimba", + "value": "marimba" + }, + { + "label": "Pixel", + "value": "pixel" + } + ], + "notiSounds": [ + { + "label": "Online", + "value": "online" + }, + { + "label": "99kr", + "value": "99kr" + }], "defaultSettings": { - "wallpaper": "projecterror.jpg", - "frame": "default.png", - "theme": "taso-dark", - "zoom": 1, - "ringtone": "pixel", + "wallpaper": { + "label": "Project Error", + "value":"projecterror.jpg" + }, + "frame": { + "label": "Default", + "value":"default.png" + }, + "theme": { + "label": "taso-dark", + "value":"taso-dark" + }, + "zoom": { + "label": "100%", + "value": 1 + }, + "ringtone": { + "label": "Pixel", + "value": "pixel" + }, + "notiSound": { + "label": "Online", + "value": "online" + }, "ringtoneVol": 50, - "notification": "online", - "notificationVol": 50, - "TWITTER_notification": "online", - "TWITTER_notificationVol": 100, + "notiSoundVol": 50, + "TWITTER_notiSound": { + "label": "Online", + "value": "online" + }, + "TWITTER_notiSoundVol": 100, "TWITTER_notifyNewFeedTweet": true }, + "themes": [ + { + "label": "Taso Dark", + "value": "taso-dark" + }, + { + "label": "Default Dark", + "value": "default-dark" + }, + { + "label": "Default Light", + "value": "default-light" + } + ], "debug": { "printDebugLogs": true, "logLevel": 1 }, - "frames": [ - "default.png", - "blue.png", - "gold.png", - "kawaii.png", - "minimal.png", - "pink.png", - "white.png" - ], - "zoomOptions": [1, 0.9, 0.8], - "ringtones": ["marimba", "pixel"], - "notifications": ["online", "99kr"], - "themes": { - "default-dark": { - "palette": { - "type": "dark", - "primary": { - "main": "#a6d4fa" - } - } - }, - "default-light": { - "palette": { - "type": "light" - } - }, - "taso-dark": { - "common": {}, - "palette": { - "type": "dark", - "primary": { - "main": "#a6d4fa" - }, - "background": { - "paper": "#1c1c1c", - "default": "#121212", - "dark": "#000000", - "light": "#383838" - }, - "text": { - "primary": "#fff", - "secondary": "rgba(255, 255, 255, 0.7)", - "disabled": "rgba(255, 255, 255, 0.5)", - "hint": "rgba(255, 255, 255, 0.5)", - "icon": "rgba(255, 255, 255, 0.5)" - } - }, - "divider": "rgba(255, 255, 255, 0.12)" - } - }, "SentryErrorMetrics": true } diff --git a/phone/src/config/themes.json b/phone/src/config/themes.json new file mode 100644 index 0000000..c8029c9 --- /dev/null +++ b/phone/src/config/themes.json @@ -0,0 +1,38 @@ +{ + "default-dark": { + "palette": { + "type": "dark", + "primary": { + "main": "#a6d4fa" + } + } + }, + "default-light": { + "palette": { + "type": "light" + } + }, + "taso-dark": { + "common": {}, + "palette": { + "type": "dark", + "primary": { + "main": "#a6d4fa" + }, + "background": { + "paper": "#1c1c1c", + "default": "#121212", + "dark": "#000000", + "light": "#383838" + }, + "text": { + "primary": "#fff", + "secondary": "rgba(255, 255, 255, 0.7)", + "disabled": "rgba(255, 255, 255, 0.5)", + "hint": "rgba(255, 255, 255, 0.5)", + "icon": "rgba(255, 255, 255, 0.5)" + } + }, + "divider": "rgba(255, 255, 255, 0.12)" + } +} \ No newline at end of file diff --git a/phone/src/os/notifications/providers/NotificationsProvider.tsx b/phone/src/os/notifications/providers/NotificationsProvider.tsx index 94ad7bf..7041863 100644 --- a/phone/src/os/notifications/providers/NotificationsProvider.tsx +++ b/phone/src/os/notifications/providers/NotificationsProvider.tsx @@ -162,7 +162,7 @@ export function NotificationsProvider({ children }) { update?: Partial, ) => { if (n.sound) { - const { sound, volume } = getSoundSettings('notification', settings, n.app); + const { sound, volume } = getSoundSettings('notiSound', settings, n.app); mount(sound, volume, false).then(({ url }) => setAlerts((curr) => [...curr, [n, persist, update, url]]), ); diff --git a/phone/src/os/phone/hooks/usePhoneTheme.ts b/phone/src/os/phone/hooks/usePhoneTheme.ts index 5a37202..a14d775 100644 --- a/phone/src/os/phone/hooks/usePhoneTheme.ts +++ b/phone/src/os/phone/hooks/usePhoneTheme.ts @@ -1,10 +1,9 @@ import { useMemo } from 'react'; import { createMuiTheme } from '@material-ui/core'; import { useSettings } from '../../../apps/settings/hooks/useSettings'; -import * as config from '../../../config/default.json'; +import themes from '../../../config/themes.json'; export const usePhoneTheme = () => { const [settings] = useSettings(); - - return useMemo(() => createMuiTheme(config.themes[settings.theme]), [settings.theme]); + return useMemo(() => createMuiTheme(themes[settings.theme.value]), [settings.theme]); }; diff --git a/phone/src/os/sound/utils/getSoundSettings.ts b/phone/src/os/sound/utils/getSoundSettings.ts index f43ebc7..824016d 100644 --- a/phone/src/os/sound/utils/getSoundSettings.ts +++ b/phone/src/os/sound/utils/getSoundSettings.ts @@ -3,25 +3,25 @@ import { getNotificationPath, getRingtonePath } from '../../../apps/settings/uti const getPath = { ringtone: getRingtonePath, - notification: getNotificationPath, + notiSound: getNotificationPath, }; /** * Gets the configuration for SoundProvider based on settings and apps */ export const getSoundSettings = ( - type: 'ringtone' | 'notification', + type: 'ringtone' | 'notiSound', settings: IPhoneSettings, app?: string, ) => { try { return app ? { - sound: getPath[type](settings[`${app}_${type}`] || settings[type]), + sound: getPath[type](settings[`${app}_${type}`].value || settings[type].value), volume: (settings[`${app}_${type}Vol`] || settings[`${type}Vol`]) / 100, } : { - sound: getPath[type](settings[type]), + sound: getPath[type](settings[type as string].value), volume: settings[`${type}Vol`] / 100, }; } catch (e) { diff --git a/phone/src/ui/components/Notification.tsx b/phone/src/ui/components/Notification.tsx index d097f47..fa964d5 100644 --- a/phone/src/ui/components/Notification.tsx +++ b/phone/src/ui/components/Notification.tsx @@ -30,7 +30,7 @@ function Notification({ children, handleClose, open }) { if (!config) return null; const { horizontal, vertical } = config.notificationPosition; - const currentTheme = () => createMuiTheme(themeConfig.themes[settings.theme]); + const currentTheme = () => createMuiTheme(themeConfig.themes[settings.theme.value]); return ( diff --git a/phone/src/ui/hooks/useContextMenu.tsx b/phone/src/ui/hooks/useContextMenu.tsx index c95c45d..46d6ede 100644 --- a/phone/src/ui/hooks/useContextMenu.tsx +++ b/phone/src/ui/hooks/useContextMenu.tsx @@ -12,6 +12,20 @@ export const MapStringOptions = (current, onClick) => (string) => { }; }; +export interface SettingOption { + label: string; + value: string | number; +} + +export const MapSettingItem = (current: SettingOption, onClick: Function) => ( + item: SettingOption, +) => ({ + selected: current.value === item.value, + onClick: () => onClick(item), + key: item.value, + label: item.label, +}); + export const useContextMenu = (_options?): UseContextMenu => { const [open, setOpen] = useState(false); const [options, setOptions] = useState(_options || []);