diff --git a/src/Phone.js b/src/Phone.js index ea46555..6db6172 100644 --- a/src/Phone.js +++ b/src/Phone.js @@ -13,6 +13,7 @@ import { ContactsApp } from "./apps/contacts/components/ContactsApp"; import { SettingsApp } from "./apps/settings/components/SettingsApp"; import { PhoneApp } from './apps/phone/components/PhoneApp'; import { ThemeProvider } from "@material-ui/core"; +import { CalculatorApp } from "./apps/calculator/components/CalculatorApp"; function Phone() { const { settings, currentTheme } = useSettings(); @@ -42,6 +43,7 @@ function Phone() { + diff --git a/src/apps/appmarket/hooks/useApps.js b/src/apps/appmarket/hooks/useApps.js index 630e52a..02e2321 100644 --- a/src/apps/appmarket/hooks/useApps.js +++ b/src/apps/appmarket/hooks/useApps.js @@ -3,51 +3,62 @@ import { useConfig } from "../../../config/hooks/useConfig"; import { blue, grey, green } from "@material-ui/core/colors"; import { useTranslation } from "react-i18next"; import ContactsIcon from "@material-ui/icons/Contacts"; -import SettingsIcon from '@material-ui/icons/Settings' -import PhoneIcon from '@material-ui/icons/Phone'; +import SettingsIcon from "@material-ui/icons/Settings"; +import PhoneIcon from "@material-ui/icons/Phone"; import { atom } from "recoil"; +import { useSettings } from "../../settings/hooks/useSettings"; +import { CalculatorIcon } from "../../calculator/components/CalculatorIcon"; const appsState = atom({ - key: 'apps', - default: { preinstalled: [] } + key: "apps", + default: { preinstalled: [] }, }); export const useApps = () => { const [apps, setApps] = useConfig(appsState); + const { currentTheme } = useSettings(); const { t } = useTranslation(); useEffect(() => { setApps("preinstalled", [ { - id: 'phone', - name: t('APPS_PHONE'), + id: "phone", + name: t("APPS_PHONE"), icon: , backgroundColor: green[400], color: green[50], - path: '/phone' + path: "/phone", }, { - id: 'contacts', - name: t('APPS_CONTACTS'), + id: "contacts", + name: t("APPS_CONTACTS"), icon: , backgroundColor: blue[500], color: blue[50], - path: '/contacts' + path: "/contacts", }, { - id: 'settings', - name: t('APPS_SETTINGS'), + id: "calculator", + name: t("APPS_SETTINGS"), + icon: , + backgroundColor: currentTheme().palette.primary.light, + color: currentTheme().palette.primary.contrastText, + path: "/calculator", + }, + { + id: "settings", + name: t("APPS_SETTINGS"), icon: , backgroundColor: grey[700], color: grey[50], - path: '/settings' - } + path: "/settings", + }, ]); // eslint-disable-next-line }, []); const allApps = [...apps.preinstalled]; - const getApp = id => allApps.find(a => a.id === id) || {}; + const getApp = (id) => allApps.find((a) => a.id === id) || {}; return { apps, allApps, setApps, getApp }; }; diff --git a/src/apps/calculator/components/Calculator.js b/src/apps/calculator/components/Calculator.js new file mode 100644 index 0000000..8818e7d --- /dev/null +++ b/src/apps/calculator/components/Calculator.js @@ -0,0 +1,68 @@ +import React from "react"; +import { useCalculator, CalcButton } from "../hooks/useCalculator"; +import { Grid, makeStyles, Box, Paper } from "@material-ui/core"; + +const useStyles = makeStyles((theme) => ({ + button: { + fontSize: theme.typography.h5.fontSize, + padding: theme.spacing(2), + }, + result: { + fontSize: theme.typography.h2.fontSize, + textAlign: "right", + }, +})); + +export const Calculator = ({ ...props }) => { + const { + result, + equals, + clear, + clearAll, + divider, + multiplier, + substractor, + adder, + dot, + zero, + one, + two, + three, + four, + five, + six, + seven, + eight, + nine, + } = useCalculator(); + const classes = useStyles(); + return ( + + + {result()} + + + + + + + + + + + + + + + + + + + + + + + + + ); +}; diff --git a/src/apps/calculator/components/CalculatorApp.js b/src/apps/calculator/components/CalculatorApp.js new file mode 100644 index 0000000..c6e2464 --- /dev/null +++ b/src/apps/calculator/components/CalculatorApp.js @@ -0,0 +1,22 @@ +import React from "react"; +import { AppWrapper } from "../../../ui/components"; +import { AppTitle } from "../../../ui/components/AppTitle"; +import { AppContent } from "../../../ui/components/AppContent"; +import { useTranslation } from "react-i18next"; +import { useApps } from "../../appmarket/hooks/useApps"; +import { Calculator } from "./Calculator"; + +export const CalculatorApp = () => { + const { t } = useTranslation(); + const { getApp } = useApps(); + return ( + + + {t("APPS_CALCULATOR")} + + + + + + ); +}; diff --git a/src/apps/calculator/components/CalculatorIcon.js b/src/apps/calculator/components/CalculatorIcon.js new file mode 100644 index 0000000..398c66c --- /dev/null +++ b/src/apps/calculator/components/CalculatorIcon.js @@ -0,0 +1,43 @@ +import React from "react"; +import { SvgIcon } from "@material-ui/core"; + +export const CalculatorIcon = ({ ...props }) => { + return ( + + + + + + + + + + + + + + ); +}; diff --git a/src/apps/calculator/hooks/useCalculator.js b/src/apps/calculator/hooks/useCalculator.js new file mode 100644 index 0000000..251c7f7 --- /dev/null +++ b/src/apps/calculator/hooks/useCalculator.js @@ -0,0 +1,168 @@ +import React, { useReducer } from "react"; +import { Grid, Button } from "@material-ui/core"; + +const initialState = { + value: 0, + op: "", + num1: 0, + num2: "", + num3: "", +}; + +const Reducer = (state, action) => { + switch (action.type) { + case "2": + if (!state.num1) { + return { ...state, num1: state.num2, op: action.payload, num2: "" }; + } + if (!state.num2) { + return { ...state, op: action.payload }; + } + if (state.num1 && state.op) { + return { + ...state, + value: state.op(state.value, state.num2), + op: action.payload, + }; + } + return { + ...state, + value: state.op(state.num1, state.num2), + num2: "", + op: action.payload, + num1: state.op(state.num1, state.num2), + }; + + case "1": + if (state.num2 === "" && action.payload === ".") { + return { ...state, num2: "0" + action.payload }; + } + if (state.num2 === "") { + return { ...state, num2: action.payload }; + } + if (action.payload === "." && state.num2.includes(".")) { + return state; + } else { + return { ...state, num2: state.num2 + action.payload }; + } + + case "3": + if (state.op && state.num2) { + return { + ...state, + value: state.op(state.num1, state.num2), + num2: "", + num1: state.op(state.num1, state.num2), + num3: state.num2, + }; + } + if (state.op && state.value) { + return { ...state, value: state.op(state.value, state.num3) }; + } + return state; + + case "c": + if (state.num2.length === 2 && state.num2.includes("0.")) { + return { ...state, num2: "" }; + } + if (state.num2.length > 1) { + return { ...state, num2: state.num2.slice(0, -1) }; + } + return { ...state, num2: "" }; + + case "ac": + return { + value: 0, + op: "", + num1: 0, + num2: "", + num3: "", + }; + + default: + return state; + } +}; + +export const CalcButton = ({ button, className, ...props }) => ( + + + +); + +export const useCalculator = () => { + const [state, dispatch] = useReducer(Reducer, initialState); + + const getPayload = (label, type) => { + if (type !== "2") { + return `${label}`; + } + switch (label) { + case "/": + return (a, b) => Number(a) / Number(b); + case "*": + return (a, b) => Number(a) * Number(b); + case "+": + return (a, b) => Number(a) + Number(b); + case "-": + return (a, b) => Number(a) - Number(b); + default: + break; + } + }; + + const buttonProps = (label, type) => ({ + label, + onClick: () => + dispatch({ + type, + payload: getPayload(label, type), + }), + }); + + const number = (n) => buttonProps(n, "1"); + const operator = (sym) => buttonProps(sym, "2"); + const clear = buttonProps("C", "c"); + const clearAll = buttonProps("AC", "ac"); + const equals = buttonProps("=", "3"); + const zero = number(0); + const dot = number("."); + const divider = operator("/"); + const multiplier = operator("*"); + const adder = operator("+"); + const substractor = operator("-"); + const one = number(1); + const two = number(2); + const three = number(3); + const four = number(4); + const five = number(5); + const six = number(6); + const seven = number(7); + const eight = number(8); + const nine = number(9); + const result = () => (!state.num2 ? state.value : state.num2); + + return { + result, + equals, + clear, + clearAll, + divider, + multiplier, + substractor, + adder, + dot, + zero, + one, + two, + three, + four, + five, + six, + seven, + eight, + nine, + }; +}; diff --git a/src/locale/en.json b/src/locale/en.json index 69da9f8..995877c 100644 --- a/src/locale/en.json +++ b/src/locale/en.json @@ -4,6 +4,7 @@ "APPS_PHONE": "Phone", "APPS_CONTACTS": "Contacts", "APPS_MESSAGES": "Messages", + "APPS_CALCULATOR": "Calculator", "APPS_SETTINGS": "Settings", "APPS_SETTINGS_OPTIONS_WALLPAPER": "Wallpaper", "APPS_SETTINGS_OPTIONS_FRAME": "Frame"