From 0cf5bc2bc7d9fc00c4a452d8c20375460412350e Mon Sep 17 00:00:00 2001 From: Christopher Gjelten Date: Wed, 7 Apr 2021 23:29:18 +0200 Subject: [PATCH] tweak(phone/settings): simplify custom background (#185) Adds the custom background option into wallpaper SettingItem, with an nice DialogForm. --- phone/src/Phone.tsx | 5 +- .../apps/settings/components/SettingItem.tsx | 17 +--- .../apps/settings/components/SettingsApp.tsx | 27 +++--- .../settings/components/WallpaperModal.tsx | 94 ++++++------------- phone/src/apps/settings/hooks/useSettings.ts | 2 - .../apps/settings/utils/isDefaultWallpaper.ts | 8 ++ phone/src/locale/en.json | 9 +- phone/src/locale/no.json | 9 +- phone/src/ui/components/DialogForm.tsx | 69 ++++++++++++++ 9 files changed, 139 insertions(+), 101 deletions(-) create mode 100644 phone/src/apps/settings/utils/isDefaultWallpaper.ts create mode 100644 phone/src/ui/components/DialogForm.tsx diff --git a/phone/src/Phone.tsx b/phone/src/Phone.tsx index a80f7f0..bb799ca 100644 --- a/phone/src/Phone.tsx +++ b/phone/src/Phone.tsx @@ -33,6 +33,7 @@ import WindowSnackbar from './ui/components/WindowSnackbar'; import { usePhone } from './os/phone/hooks/usePhone'; import { useTranslation } from 'react-i18next'; import { useSnackbar } from './ui/hooks/useSnackbar'; +import { isDefaultWallpaper } from './apps/settings/utils/isDefaultWallpaper'; function Phone() { const { t, i18n } = useTranslation(); @@ -104,8 +105,8 @@ function Phone() { id="phone" className="PhoneScreen" style={{ - backgroundImage: settings.customWallpaper - ? `url(${settings.customWallpaper})` + backgroundImage: !isDefaultWallpaper(settings.wallpaper.value) + ? `url(${settings.wallpaper.value})` : `url(media/backgrounds/${settings.wallpaper.value})`, }} > diff --git a/phone/src/apps/settings/components/SettingItem.tsx b/phone/src/apps/settings/components/SettingItem.tsx index 336f393..bf4e167 100644 --- a/phone/src/apps/settings/components/SettingItem.tsx +++ b/phone/src/apps/settings/components/SettingItem.tsx @@ -10,15 +10,12 @@ import { Tooltip, Typography, Switch, - TextField, } from '@material-ui/core'; -import CreateIcon from '@material-ui/icons/Create'; -import { PictureThumbnail } from '../../../ui/components/PictureThumbnail'; interface ISettingItem { options?: any; label: string; - value?: string | number | null; + value?: string | object | number | null; onClick?: any; icon: JSX.Element; } @@ -109,15 +106,3 @@ export const SettingItemIconAction = ({ ); - -export const SettingItemModal = ({ icon, label, secondary, onClick }) => ( - - {icon} - - - - - - - -); diff --git a/phone/src/apps/settings/components/SettingsApp.tsx b/phone/src/apps/settings/components/SettingsApp.tsx index e740a5c..d4fd635 100644 --- a/phone/src/apps/settings/components/SettingsApp.tsx +++ b/phone/src/apps/settings/components/SettingsApp.tsx @@ -1,4 +1,4 @@ -import React, { useState } from 'react'; +import React from 'react'; import { AppWrapper } from '../../../ui/components'; import { AppTitle } from '../../../ui/components/AppTitle'; import { AppContent } from '../../../ui/components/AppContent'; @@ -12,7 +12,6 @@ import { SettingItemIconAction, SettingItemSlider, SettingSwitch, - SettingItemModal, } from './SettingItem'; import { useTranslation } from 'react-i18next'; import VisibilityOffIcon from '@material-ui/icons/VisibilityOff'; @@ -73,9 +72,9 @@ export const SettingsApp = () => { }; const wallpapers = config.wallpapers.map( - MapSettingItem(settings.wallpaper, (val: SettingOption) => - handleSettingChange('wallpaper', val), - ), + MapSettingItem(settings.wallpaper, (val: SettingOption) => { + handleSettingChange('wallpaper', val); + }), ); const frames = config.frames.map( MapSettingItem(settings.frame, (val: SettingOption) => handleSettingChange('frame', val)), @@ -128,6 +127,13 @@ export const SettingsApp = () => { }, ]; + const customWallpaper: IContextMenuOption = { + selected: false, + onClick: () => setCustomWallpaperModal(true), + key: 'CUSTOM_WALLPAPER', + label: t('APPS_SETTINGS_OPTIONS_CUSTOM_WALLPAPER'), + }; + const handleCopyPhoneNumber = () => { setClipboard(simcard.number); addAlert({ @@ -208,18 +214,11 @@ export const SettingsApp = () => { /> } /> - {/* CUSTOM BACKGROUND */} - } - label={t('APPS_SETTINGS_OPTIONS_CUSTOM_WALLPAPER')} - secondary={settings.customWallpaper} - onClick={() => setCustomWallpaperModal(true)} - /> { + const isImageAndUrl = (url) => { return /(http(s?):)([/|.|\w|\s|-])*\.(?:jpg|png|jpeg|gif)/g.test(url); }; - const handleCloseModal = () => { - setCustomWallpaperModal(false); - }; - const handleSettingChange = (key: string | number, value: unknown) => { setSettings({ ...settings, [key]: value }); }; const handleNewWallpaper = () => { - handleSettingChange('customWallpaper', value); - }; - - const handleRemoveWallpaper = () => { - handleSettingChange('customWallpaper', ''); + if (isImageAndUrl(value)) { + handleSettingChange('wallpaper', { + label: 'APPS_SETTINGS_OPTIONS_CUSTOM_WALLPAPER', + value, + }); + setCustomWallpaperModal(false); + } else { + addAlert({ message: t('SETTINGS.OPTIONS.CUSTOM_WALLPAPER.DIALOG_ERROR'), type: 'error' }); + } }; return ( - - - {settings.customWallpaper && ( - - )} - setValue(e.currentTarget.value)} - inputProps={{ - className: classes.inputProps, - }} - placeholder="Image URL" - /> - - {settings.customWallpaper && ( - - {t('GENERIC_DELETE')} - - )} - - + setCustomWallpaperModal(false)} + onSubmit={handleNewWallpaper} + title={t('SETTINGS.OPTIONS.CUSTOM_WALLPAPER.DIALOG_TITLE')} + content={t('SETTINGS.OPTIONS.CUSTOM_WALLPAPER.DIALOG_CONTENT')} + > + setValue(e.currentTarget.value)} + placeholder={t('SETTINGS.OPTIONS.CUSTOM_WALLPAPER.DIALOG_PLACEHOLDER')} + fullWidth + /> + ); } diff --git a/phone/src/apps/settings/hooks/useSettings.ts b/phone/src/apps/settings/hooks/useSettings.ts index dc256e5..9461260 100644 --- a/phone/src/apps/settings/hooks/useSettings.ts +++ b/phone/src/apps/settings/hooks/useSettings.ts @@ -23,7 +23,6 @@ const settingsSchema: Schema = { properties: { language: { $ref: '/SettingOption' }, wallpaper: { $ref: '/SettingOption' }, - customWallpaper: { type: 'string' }, frame: { $ref: '/SettingOption' }, theme: { $ref: '/SettingOption' }, zoom: { $ref: '/SettingOption' }, @@ -64,7 +63,6 @@ export function isSettingsSchemaValid(): boolean { export interface IPhoneSettings { language: SettingOption; wallpaper: SettingOption; - customWallpaper: string; frame: SettingOption; theme: SettingOption; zoom: SettingOption; diff --git a/phone/src/apps/settings/utils/isDefaultWallpaper.ts b/phone/src/apps/settings/utils/isDefaultWallpaper.ts new file mode 100644 index 0000000..097a913 --- /dev/null +++ b/phone/src/apps/settings/utils/isDefaultWallpaper.ts @@ -0,0 +1,8 @@ +import config from '../../../config/default.json'; + +export const isDefaultWallpaper = (value: string) => { + const wallpapers = config.wallpapers; + for (const wallpaper of wallpapers) { + if (wallpaper.value === value) return true; + } +}; diff --git a/phone/src/locale/en.json b/phone/src/locale/en.json index 17c4850..5a5f784 100644 --- a/phone/src/locale/en.json +++ b/phone/src/locale/en.json @@ -18,7 +18,13 @@ "OPTIONS": { "LANGUAGE": "Language", "RESET_SETTINGS": "Reset Settings", - "RESET_SETTINGS_DESC": "This will clear your locally stored data" + "RESET_SETTINGS_DESC": "This will clear your locally stored data", + "CUSTOM_WALLPAPER": { + "DIALOG_TITLE": "Custom Wallpaper", + "DIALOG_CONTENT": "Set a custom wallpaper by entering the URL of the image below!", + "DIALOG_PLACEHOLDER": "Image URL", + "DIALOG_ERROR": "Invalid image or link" + } }, "MESSAGES": { "INVALID_SETTINGS": "Invalid stored settings! Please reset in the Settings App.", @@ -202,7 +208,6 @@ "APPS_SETTINGS_OPTION_NOTIFICATION": "Notification", "APPS_SETTINGS_OPTION_NOTIFICATIONVOL": "Notification Volume", "APPS_SETTING_STREAMER_MODE_DESC": "Hide images before they are shown", - "APPS_SETTINGS_SET_CUSTOM_WALLPAPER": "Set new wallpaper", "APPS_MARKETPLACE_CREATE_LISTING_SUCCESS": "Successfully created a new listing", "APPS_MARKETPLACE_CREATE_LISTING_FAILED": "Failed to create a new listing", diff --git a/phone/src/locale/no.json b/phone/src/locale/no.json index cc8cb00..c4ca458 100644 --- a/phone/src/locale/no.json +++ b/phone/src/locale/no.json @@ -11,7 +11,13 @@ "OPTIONS": { "LANGUAGE": "Språk", "RESET_SETTINGS": "Reset Settings", - "RESET_SETTINGS_DESC": "Dette vil slette all lagret data" + "RESET_SETTINGS_DESC": "Dette vil slette all lagret data", + "CUSTOM_WALLPAPER": { + "DIALOG_TITLE": "Tilpasset bakgrunn", + "DIALOG_CONTENT": "Velg en tilpasset bakgrunn ved å kopiere en lenke av bilde!", + "DIALOG_PLACEHOLDER": "Bilde URL", + "DIALOG_ERROR": "Feil bilde eller link!" + } }, "MESSAGES": { "INVALID_SETTINGS": "Invalid stored settings! Please reset in the Settings App.", @@ -177,6 +183,7 @@ "APPS_SETTINGS_PHONE_NUMBER": "Telefonnummer", "APPS_SETTINGS_OPTION_WALLPAPER": "Bakgrunn", + "APPS_SETTINGS_OPTIONS_CUSTOM_WALLPAPER": "Tilpasset bakgrunn", "APPS_SETTINGS_OPTION_FRAME": "Deksel", "APPS_SETTINGS_OPTION_THEME": "Tema", "APPS_SETTINGS_OPTION_ZOOM": "Forstørrelse", diff --git a/phone/src/ui/components/DialogForm.tsx b/phone/src/ui/components/DialogForm.tsx new file mode 100644 index 0000000..c9a71d3 --- /dev/null +++ b/phone/src/ui/components/DialogForm.tsx @@ -0,0 +1,69 @@ +import React from 'react'; +import { makeStyles } from '@material-ui/core/styles'; +import { Button, DialogContent, DialogContentText, DialogTitle, Paper } from '@material-ui/core'; +import DialogActions from '@material-ui/core/DialogActions'; + +const useStyles = makeStyles({ + root: { + paddingLeft: '10px', + zIndex: 10, + width: '90%', + display: 'flex', + flexFlow: 'column nowrap', + position: 'absolute', + top: '80px', + }, + displayBlock: { + /*Sets modal to center*/ + display: 'flex', + flexFlow: 'column nowrap', + justifyContent: 'center', + alignItems: 'center', + position: 'relative', + }, + displayNone: { + display: 'none', + }, +}); + +interface DialogFormProps { + children: React.ReactNode; + open: boolean; + handleClose: (event: React.MouseEvent) => void; // No idea what those types are + onSubmit: (event: React.MouseEvent) => void; + title: string; + content: string; +} + +export default function DialogForm({ + children, + open, + handleClose, + onSubmit, + title, + content, +}: DialogFormProps) { + const classes = useStyles(); + + const showHideClassName = open ? classes.displayBlock : classes.displayNone; + + return ( +
+ + {title} + + {content} + {children} + + + + + + +
+ ); +}