diff --git a/phone/public/media/notifications/99kr.ogg b/phone/public/media/notifications/99kr.ogg new file mode 100644 index 0000000..f157c2c Binary files /dev/null and b/phone/public/media/notifications/99kr.ogg differ diff --git a/phone/public/media/notifications/online.ogg b/phone/public/media/notifications/online.ogg new file mode 100644 index 0000000..7b103b3 Binary files /dev/null and b/phone/public/media/notifications/online.ogg differ diff --git a/phone/src/PhoneContainer.tsx b/phone/src/PhoneContainer.tsx index 4c974dd..935d5fa 100644 --- a/phone/src/PhoneContainer.tsx +++ b/phone/src/PhoneContainer.tsx @@ -4,16 +4,19 @@ import { NotificationsProvider } from './os/notifications/providers/Notification import { usePhoneTheme } from './os/phone/hooks/usePhoneTheme'; import SnackbarProvider from './ui/providers/SnackbarProvider'; import Phone from './Phone'; +import { SoundProvider } from './os/sound/providers/SoundProvider'; export const PhoneContainer = () => { const currentTheme = usePhoneTheme(); return ( - - - - - + + + + + + + ); }; diff --git a/phone/src/apps/example/components/ExampleApp.tsx b/phone/src/apps/example/components/ExampleApp.tsx index 2f5fc69..07d96e0 100644 --- a/phone/src/apps/example/components/ExampleApp.tsx +++ b/phone/src/apps/example/components/ExampleApp.tsx @@ -9,7 +9,7 @@ import { AppTitle } from '../../../ui/components/AppTitle'; import PlayIcon from '@material-ui/icons/PlayArrow'; import StopIcon from '@material-ui/icons/Stop'; import { Button, IconButton } from '@material-ui/core'; -import useSound from '../../../os/phone/hooks/useSound'; +import useSound from '../../../os/sound/hooks/useSound'; export const ExampleApp = () => { // calling the example hook, and we assign the value to a variable diff --git a/phone/src/apps/settings/components/SettingItem.tsx b/phone/src/apps/settings/components/SettingItem.tsx index 808670b..177931e 100644 --- a/phone/src/apps/settings/components/SettingItem.tsx +++ b/phone/src/apps/settings/components/SettingItem.tsx @@ -1,10 +1,5 @@ import React from 'react'; -import { - ListItem, - ListItemText, - Divider, - ListItemIcon, -} from '@material-ui/core'; +import { ListItem, ListItemText, ListItemIcon } from '@material-ui/core'; interface IProps { options?: any; @@ -19,26 +14,12 @@ export const SettingItem = ({ label, value, onClick, - icon + icon, }: IProps) => { - if (!onClick) - return ( - <> - - {icon} - - - - - ); - return ( - <> - onClick(options)} button> - {icon} - - - - + onClick?.(options)} button> + {icon} + + ); }; diff --git a/phone/src/apps/settings/components/SettingsApp.tsx b/phone/src/apps/settings/components/SettingsApp.tsx index 7cef04b..235984d 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, { useEffect, useState } from 'react'; import { AppWrapper } from '../../../ui/components'; import { AppTitle } from '../../../ui/components/AppTitle'; import { AppContent } from '../../../ui/components/AppContent'; @@ -23,12 +23,20 @@ import { VolumeUp, } from '@material-ui/icons'; -import { ListSubheader } from '@material-ui/core'; +import { + Box, + ListItem, + ListItemIcon, + ListItemSecondaryAction, + ListItemText, + ListSubheader, + Slider, +} from '@material-ui/core'; import { useRecoilState } from 'recoil'; import { settingsState } from '../hooks/useSettings'; const SubHeaderComp = (props: { text: string }) => ( - + {props.text} ); @@ -43,6 +51,33 @@ export const SettingsApp = () => { 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( @@ -65,18 +100,22 @@ export const SettingsApp = () => { handleSettingChange('zoom', val) ) ); - // Doesn't actually do anything for the time being const ringtones = config.ringtones.map( MapStringOptions(settings.ringtone, (val: string) => handleSettingChange('ringtone', val) ) ); - // * Probably gonna make this a slider component in the future - // const ringtoneVols = config.ringtoneVols.map( - // MapStringOptions(settings.ringtoneVol, (val: number) => setSettings('ringtoneVol', val)) - // ) + const notifications = config.notifications.map( + MapStringOptions(settings.notification, (val: string) => + handleSettingChange('notification', val) + ) + ); - // TODO: These new settings all work + const twitterNotifications = config.notifications.map( + MapStringOptions(settings.notification, (val: string) => + handleSettingChange('TWITTER_notification', val) + ) + ); const [openMenu, closeMenu, ContextMenu, isMenuOpen] = useContextMenu(); return ( @@ -96,17 +135,58 @@ export const SettingsApp = () => { onClick={openMenu} icon={} /> - { - // * NOTE: This component is most likely temporary - // * Probably want to make it a slider in the future. Ignore hardcoded for the meantime - } + + + + + + + + + handleSettingChange('ringtoneVol', sliders[0]) + } + /> + + , + + } + icon={} /> + + + + + + + + + handleSettingChange('notificationVol', sliders[1]) + } + /> + + + }> { icon={} /> + } + > + } + /> + + + + + + + + + + handleSettingChange('TWITTER_notificationVol', sliders[2]) + } + /> + + + + diff --git a/phone/src/apps/settings/hooks/useSettings.ts b/phone/src/apps/settings/hooks/useSettings.ts index 86eae75..c2dc1c6 100644 --- a/phone/src/apps/settings/hooks/useSettings.ts +++ b/phone/src/apps/settings/hooks/useSettings.ts @@ -22,13 +22,18 @@ export const settingsState = atom({ effects_UNSTABLE: [localStorageEffect('settings')], }); -interface IPhoneSettings { +export interface IPhoneSettings { wallpaper: string; frame: string; theme: string; zoom: number; ringtone: string; ringtoneVol: number; + notification: string; + notificationVol: number; + TWITTER_notification: string; + TWITTER_notificationVol: number; + TWITTER_notifyNewFeedTweet: boolean; } export const useSettings = () => { diff --git a/phone/src/apps/settings/utils/getRingtonePath.ts b/phone/src/apps/settings/utils/getRingtonePath.ts new file mode 100644 index 0000000..2bf991c --- /dev/null +++ b/phone/src/apps/settings/utils/getRingtonePath.ts @@ -0,0 +1,5 @@ +export const getRingtonePath = (name?: string) => + `./media/ringtones/${name || 'pixel'}.ogg`; + +export const getNotificationPath = (name?: string) => + `./media/notifications/${name || 'online'}.ogg`; diff --git a/phone/src/apps/twitter/components/TwitterApp.tsx b/phone/src/apps/twitter/components/TwitterApp.tsx index d86a8cd..583a89a 100644 --- a/phone/src/apps/twitter/components/TwitterApp.tsx +++ b/phone/src/apps/twitter/components/TwitterApp.tsx @@ -190,5 +190,5 @@ InjectDebugData( }, }, ], - 2000 + 3000 ); diff --git a/phone/src/apps/twitter/components/tweet/Tweet.tsx b/phone/src/apps/twitter/components/tweet/Tweet.tsx index d382a36..cf1255d 100644 --- a/phone/src/apps/twitter/components/tweet/Tweet.tsx +++ b/phone/src/apps/twitter/components/tweet/Tweet.tsx @@ -48,6 +48,7 @@ const useStyles = makeStyles(() => ({ }, message: { fontSize: '18px', + wordBreak: 'break-all', }, buttonContainer: { display: 'flex', diff --git a/phone/src/apps/twitter/hooks/useTwitterNotifications.ts b/phone/src/apps/twitter/hooks/useTwitterNotifications.ts index d6cb625..977b350 100644 --- a/phone/src/apps/twitter/hooks/useTwitterNotifications.ts +++ b/phone/src/apps/twitter/hooks/useTwitterNotifications.ts @@ -1,17 +1,23 @@ -import { useCallback } from "react"; -import { useTranslation } from "react-i18next"; -import { useHistory } from "react-router-dom"; -import { useRecoilState } from "recoil"; -import { useApp } from "../../../os/apps/hooks/useApps"; -import { useNotifications } from "../../../os/notifications/hooks/useNotifications"; -import { twitterState } from "./state"; +import { useCallback } from 'react'; +import { useTranslation } from 'react-i18next'; +import { useHistory } from 'react-router-dom'; +import { useRecoilState } from 'recoil'; +import { useApp } from '../../../os/apps/hooks/useApps'; +import { useNotifications } from '../../../os/notifications/hooks/useNotifications'; +import { twitterState } from './state'; export const useTwitterNotifications = () => { const { t } = useTranslation(); const history = useHistory(); + const { addNotificationAlert } = useNotifications(); + const { icon, notificationIcon } = useApp('TWITTER'); - const [unreadCount, setUnreadCount] = useRecoilState(twitterState.unreadTweetsCount); + + const [unreadCount, setUnreadCount] = useRecoilState( + twitterState.unreadTweetsCount + ); + const setNotification = useCallback( ({ profile_name, message }) => { setUnreadCount((curr) => { @@ -19,6 +25,7 @@ export const useTwitterNotifications = () => { addNotificationAlert( { app: 'TWITTER', + sound: true, title: t('APPS_TWITTER_NEW_BROADCAST', { profile_name }), onClick: () => history.push('/twitter'), content: message, @@ -39,5 +46,5 @@ export const useTwitterNotifications = () => { [setUnreadCount, addNotificationAlert, t, icon, notificationIcon, history] ); - return { setNotification, setUnreadCount, unreadCount } + return { setNotification, setUnreadCount, unreadCount }; }; diff --git a/phone/src/common/utils/usePreviousState.ts b/phone/src/common/utils/usePreviousState.ts new file mode 100644 index 0000000..11584dd --- /dev/null +++ b/phone/src/common/utils/usePreviousState.ts @@ -0,0 +1,13 @@ +import { useEffect, useRef } from 'react'; + +export function usePreviousState(value) { + // The ref object is a generic container whose current property is mutable ... + // ... and can hold any value, similar to an instance property on a class + const ref = useRef(); + // Store current value in ref + useEffect(() => { + ref.current = value; + }, [value]); // Only re-run if value changes + // Return previous value (happens before update in useEffect above) + return ref.current; +} diff --git a/phone/src/config/default.json b/phone/src/config/default.json index 17eb8bb..7dd1144 100644 --- a/phone/src/config/default.json +++ b/phone/src/config/default.json @@ -1,14 +1,25 @@ { "cellProvider": "AT&T", "language": "en", - "wallpapers": ["beach.jpg", "minimal.jpg", "surf.jpg", "waves.jpg", "projecterror.jpg"], + "wallpapers": [ + "beach.jpg", + "minimal.jpg", + "surf.jpg", + "waves.jpg", + "projecterror.jpg" + ], "defaultSettings": { "wallpaper": "projecterror.jpg", "frame": "default.png", "theme": "taso-dark", "zoom": 1, "ringtone": "pixel", - "ringtoneVol": 100 + "ringtoneVol": 50, + "notification": "online", + "notificationVol": 50, + "TWITTER_notification": "online", + "TWITTER_notificationVol": 100, + "TWITTER_notifyNewFeedTweet": true }, "debug": { "printDebugLogs": true, @@ -25,6 +36,7 @@ ], "zoomOptions": [1, 0.9, 0.8], "ringtones": ["marimba", "pixel"], + "notifications": ["online", "99kr"], "themes": { "default-dark": { "palette": { @@ -44,7 +56,7 @@ "palette": { "type": "dark", "primary": { - "main": "#a6d4fa" + "main": "#a6d4fa" }, "background": { "paper": "#1c1c1c", diff --git a/phone/src/locale/en.json b/phone/src/locale/en.json index b1872dc..aaa5a68 100644 --- a/phone/src/locale/en.json +++ b/phone/src/locale/en.json @@ -103,9 +103,6 @@ "APPS_CALCULATOR_COPIED": "Result was copied to clipboard!", - "APPS_MARKETPLACE_CREATE_LISTING_SUCCESS": "Successfully created a new listing", - "APPS_MARKETPALCE_CREATE_LISTING_FAILED": "Failed to create a new listing", - "APPS_SETTINGS_PHONE_NUMBER": "Phone Number", "APPS_SETTINGS_OPTION_WALLPAPER": "Wallpaper", "APPS_SETTINGS_OPTION_FRAME": "Frame", @@ -113,10 +110,12 @@ "APPS_SETTINGS_OPTION_ZOOM": "Zoom", "APPS_SETTINGS_OPTION_RINGTONE": "Ringtone", "APPS_SETTINGS_OPTION_RINGTONEVOL": "Ringtone Volume", - "APPS_SETTINGS_OPTION_NOTIFICATION": "Notification", "APPS_SETTINGS_OPTION_NOTIFICATIONVOL": "Notification Volume", + "APPS_MARKETPLACE_CREATE_LISTING_SUCCESS": "Successfully created a new listing", + "APPS_MARKETPALCE_CREATE_LISTING_FAILED": "Failed to create a new listing", + "GENERIC_SAVE": "Save", "GENERIC_UPDATE": "Update", "GENERIC_DELETE": "Delete", diff --git a/phone/src/os/call/components/CallTimer.tsx b/phone/src/os/call/components/CallTimer.tsx index 2b6414b..4a65284 100644 --- a/phone/src/os/call/components/CallTimer.tsx +++ b/phone/src/os/call/components/CallTimer.tsx @@ -38,7 +38,7 @@ function CallTimer({ isAccepted }) { return () => clearInterval(timer.current) }, [isAccepted, setDuration]); - if (!duration) return null; + if (!duration || !isAccepted) return null; return (
{ + const { t } = useTranslation(); + const history = useHistory(); + const { addNotificationAlert, removeId } = useNotifications(); + + const { play, stop, playing } = useRingtoneSound(); + + const { icon, notificationIcon } = useApp('DIALER'); + + const callNotificationBase = { + app: 'CALL', + id: NOTIFICATION_ID, + cantClose: true, + icon, + onClick: () => history.push('/call'), + notificationIcon, + }; + + const { call } = useCall(); + + const setNotification = (call: CallProps) => { + if (!call || !call.active) { + stop(); + removeId(NOTIFICATION_ID); + return; + } + if (call.accepted) { + stop(); + removeId(NOTIFICATION_ID); + addNotificationAlert( + { + ...callNotificationBase, + keepWhenPhoneClosed: true, + content: ( + + {t('APPS_DIALER_CURRENT_CALL_WITH', { + transmitter: call.transmitter, + })} + + ), + title: t('APPS_DIALER_CURRENT_CALL_TITLE'), + }, + true + ); + } + if (!call.isTransmitter && !call.accepted) { + if (!playing()) { + play(); + } + removeId(NOTIFICATION_ID); + addNotificationAlert( + { + ...callNotificationBase, + title: t('APPS_DIALER_INCOMING_CALL_TITLE', { + transmitter: call.transmitter, + }), + keepWhenPhoneClosed: true, + content: ( + + {t('APPS_DIALER_TRANSMITTER_IS_CALLING', { + transmitter: call.transmitter, + })} + + ), + }, + true + ); + } + }; + + const resetCallNotification = () => setNotification(call); + + return { setNotification, resetCallNotification }; +}; diff --git a/phone/src/os/call/hooks/useCallService.tsx b/phone/src/os/call/hooks/useCallService.tsx index cc39dc8..13e9fc9 100644 --- a/phone/src/os/call/hooks/useCallService.tsx +++ b/phone/src/os/call/hooks/useCallService.tsx @@ -1,18 +1,14 @@ -import React, { useEffect, useState } from 'react'; +import { useEffect, useState } from 'react'; import { useNuiEvent } from '../../nui-events/hooks/useNuiEvent'; import { useSetRecoilState } from 'recoil'; -import { callerState } from './state'; -import { useNotifications } from '../../notifications/hooks/useNotifications'; -import { useTranslation } from 'react-i18next'; -import { CallNotification } from '../components/CallNotification'; import { useCall } from './useCall'; import { CallProps } from '../../../common/typings/call'; -import { useApp } from '../../apps/hooks/useApps'; import InjectDebugData from '../../debug/InjectDebugData'; import { useCallModal } from './useCallModal'; import { useHistory, useLocation } from 'react-router-dom'; - -const NOTIFICATION_ID = 'call:current'; +import { callerState } from './state'; +import { useCallNotifications } from './useCallNotifications'; +import { useDuration } from './useDuration'; InjectDebugData([ /* { @@ -22,7 +18,7 @@ InjectDebugData([ accepted: false, isTransmitter: false, transmitter: 'Chip', - receiver: 'Taso', + receiver: 'Taso', active: true, }, }, @@ -34,33 +30,19 @@ InjectDebugData([ ]); export const useCallService = () => { - const { t } = useTranslation(); const { modal } = useCallModal(); const history = useHistory(); const { pathname } = useLocation(); - const { - addNotificationAlert, - removeId, - addNotification, - } = useNotifications(); const { call, setCall } = useCall(); + const { resetDuration } = useDuration(); - const { icon, notificationIcon } = useApp('DIALER'); + const { setNotification } = useCallNotifications(); const setModal = useSetRecoilState(callerState.callModal); const [modalHasBeenOpenedThisCall, setModalOpened] = useState(false); - const callNotificationBase = { - app: 'CALL', - id: NOTIFICATION_ID, - cantClose: true, - icon, - onClick: () => history.push('/call'), - notificationIcon, - }; - useEffect(() => { setModalOpened(!!modal); }, [modal]); @@ -75,47 +57,10 @@ export const useCallService = () => { }, [history, modal, pathname, modalHasBeenOpenedThisCall]); const _setCall = (_call: CallProps) => { + resetDuration(); setCall(_call); - if (!_call || !_call.active) { - removeId(NOTIFICATION_ID); - return; - } - if (_call.accepted) { - removeId(NOTIFICATION_ID); - addNotification({ - ...callNotificationBase, - content: ( - - {t('APPS_DIALER_CURRENT_CALL_WITH', { - transmitter: _call.transmitter, - })} - - ), - title: t('APPS_DIALER_CURRENT_CALL_TITLE'), - }); - } - if (!_call.isTransmitter && !_call.accepted) { - removeId(NOTIFICATION_ID); - addNotificationAlert( - { - ...callNotificationBase, - title: t('APPS_DIALER_INCOMING_CALL_TITLE', { - transmitter: _call.transmitter, - }), - keepWhenPhoneClosed: true, - content: ( - - {t('APPS_DIALER_TRANSMITTER_IS_CALLING', { - transmitter: _call.transmitter, - })} - - ), - }, - true - ); - } + setNotification(_call); }; - useNuiEvent('CALL', 'setCaller', _setCall, call); useNuiEvent('CALL', 'callModal', setModal); }; diff --git a/phone/src/os/call/hooks/useDuration.ts b/phone/src/os/call/hooks/useDuration.ts index 67a143a..52220e5 100644 --- a/phone/src/os/call/hooks/useDuration.ts +++ b/phone/src/os/call/hooks/useDuration.ts @@ -1,7 +1,14 @@ +import { useCallback } from 'react'; import { useRecoilState } from 'recoil'; import { callerState } from './state'; export const useDuration = () => { const [duration, setDuration] = useRecoilState(callerState.callDuration); - return { duration, setDuration} -} \ No newline at end of file + + const resetDuration = useCallback( + () => setDuration({ ms: 0, s: 0, m: 0, h: 0 }), + [setDuration] + ); + + return { duration, setDuration, resetDuration }; +}; diff --git a/phone/src/os/notifications/components/NotificationAlert.tsx b/phone/src/os/notifications/components/NotificationAlert.tsx index c7d3044..c5e8f5b 100644 --- a/phone/src/os/notifications/components/NotificationAlert.tsx +++ b/phone/src/os/notifications/components/NotificationAlert.tsx @@ -29,6 +29,7 @@ const useStyles = makeStyles((theme) => ({ }, }, alertContent: { + wordBreak: 'break-all', display: '-webkit-box', maxWidth: '360px', height: '60px', diff --git a/phone/src/os/notifications/providers/NotificationsProvider.tsx b/phone/src/os/notifications/providers/NotificationsProvider.tsx index 05ee812..43d69d7 100644 --- a/phone/src/os/notifications/providers/NotificationsProvider.tsx +++ b/phone/src/os/notifications/providers/NotificationsProvider.tsx @@ -5,9 +5,13 @@ import React, { useMemo, useRef, useEffect, + useContext, } from 'react'; import { useRecoilValue } from 'recoil'; +import { useSettings } from '../../../apps/settings/hooks/useSettings'; import { phoneState } from '../../phone/hooks/state'; +import { soundContext } from '../../sound/providers/SoundProvider'; +import { getSoundSettings } from '../../sound/utils/getSoundSettings'; import { DEFAULT_ALERT_HIDE_TIME } from '../notifications.constants'; export interface INotification { @@ -17,6 +21,7 @@ export interface INotification { content?: React.ReactNode; icon?: JSX.Element; notificationIcon?: JSX.Element; + sound?: boolean; cantClose?: boolean; keepWhenPhoneClosed?: boolean; onClose?: (notification: INotification) => void; @@ -59,13 +64,17 @@ export const NotificationsContext = createContext<{ export function NotificationsProvider({ children }) { const isPhoneOpen = useRecoilValue(phoneState.visibility); + const [settings] = useSettings(); + const [barUncollapsed, setBarUncollapsed] = useState(false); const [notifications, setNotifications] = useState([]); + const { mount, play } = useContext(soundContext); + const alertTimeout = useRef(); const [alerts, setAlerts] = useState< - Array<[INotification, boolean, Partial]> + Array<[INotification, boolean, Partial, string | undefined]> >([]); const [currentAlert, setCurrentAlert] = useState(); @@ -122,7 +131,12 @@ export function NotificationsProvider({ children }) { ); const _showAlert = useCallback( - (n: INotification, persist: boolean, update: Partial) => { + ( + n: INotification, + persist: boolean, + update: Partial, + soundUrl: string + ) => { return new Promise((res) => { const resolve = () => { if (persist) { @@ -144,6 +158,10 @@ export function NotificationsProvider({ children }) { onCloseAlert: onExit(n.onClose), }); + if (n.sound && soundUrl) { + play(soundUrl); + } + if (n.keepWhenPhoneClosed) { return; } @@ -155,7 +173,7 @@ export function NotificationsProvider({ children }) { }, DEFAULT_ALERT_HIDE_TIME + 300); }); }, - [addOrUpdateNotification] + [addOrUpdateNotification, play] ); const addNotificationAlert = ( @@ -163,7 +181,18 @@ export function NotificationsProvider({ children }) { persist?: boolean, update?: Partial ) => { - setAlerts((curr) => [...curr, [n, persist, update]]); + if (n.sound) { + const { sound, volume } = getSoundSettings( + 'notification', + settings, + n.app + ); + mount(sound, volume, false).then(({ url }) => + setAlerts((curr) => [...curr, [n, persist, update, url]]) + ); + return; + } + setAlerts((curr) => [...curr, [n, persist, update, undefined]]); }; const removeId = (id: string) => { diff --git a/phone/src/os/phone/hooks/usePhone.ts b/phone/src/os/phone/hooks/usePhone.ts index b511a69..acdde8d 100644 --- a/phone/src/os/phone/hooks/usePhone.ts +++ b/phone/src/os/phone/hooks/usePhone.ts @@ -1,6 +1,7 @@ import { useRecoilValue } from 'recoil'; import { IServerConfig } from '../../../common/typings/config'; import ServerConfig from '../../../config.autogen.json'; +import { useCallNotifications } from '../../call/hooks/useCallNotifications'; import { useNotifications } from '../../notifications/hooks/useNotifications'; import Nui from '../../nui-events/utils/Nui'; import { phoneState } from './state'; @@ -16,9 +17,11 @@ export const usePhone = (): IUsePhone => { const isPhoneOpen = useRecoilValue(phoneState.visibility); const { removeAlerts } = useNotifications(); + const { resetCallNotification } = useCallNotifications(); const closePhone = () => { removeAlerts(); + resetCallNotification(); Nui.send('phone:close'); }; const openPhone = () => { diff --git a/phone/src/os/phone/hooks/usePhoneVisibility.ts b/phone/src/os/phone/hooks/usePhoneVisibility.ts index e279dfa..6f27bab 100644 --- a/phone/src/os/phone/hooks/usePhoneVisibility.ts +++ b/phone/src/os/phone/hooks/usePhoneVisibility.ts @@ -38,7 +38,7 @@ export const usePhoneVisibility = () => { const bottom = useMemo(() => { if (!visibility) { - return `calc(-70% * ${zoom})`; + return `calc(-728px * ${zoom})`; } return 0; }, [visibility, zoom]); diff --git a/phone/src/os/sound/hooks/useRingtoneSound.ts b/phone/src/os/sound/hooks/useRingtoneSound.ts new file mode 100644 index 0000000..3792ccf --- /dev/null +++ b/phone/src/os/sound/hooks/useRingtoneSound.ts @@ -0,0 +1,47 @@ +import { useCallback, useContext, useEffect, useMemo } from 'react'; +import { useSettings } from '../../../apps/settings/hooks/useSettings'; +import { soundContext } from '../providers/SoundProvider'; +import { getSoundSettings } from '../utils/getSoundSettings'; + +export const useRingtoneSound = () => { + const [settings] = useSettings(); + + const context = useContext(soundContext); + + if (!context) { + throw new Error('useRingtoneSound must be wrapped in SoundProvider'); + } + + const options = useMemo(() => getSoundSettings('ringtone', settings), [ + settings, + ]); + + useEffect(() => { + if (!context.isMounted(options.sound)) { + context.mount(options.sound, options.volume, true); + return; + } + context.volume(options.sound, options.volume); + }, [context, options.sound, options.volume]); + + const play = useCallback(() => { + context.play(options.sound, options.volume, true); + }, [context, options.sound, options.volume]); + + const stop = useCallback(() => { + context.stop(options.sound); + }, [context, options.sound]); + + const playing = useCallback(() => { + return context.playing(options.sound); + }, [context, options.sound]); + + return useMemo( + () => ({ + play, + stop, + playing, + }), + [play, playing, stop] + ); +}; diff --git a/phone/src/os/phone/hooks/useSound.ts b/phone/src/os/sound/hooks/useSound.ts similarity index 100% rename from phone/src/os/phone/hooks/useSound.ts rename to phone/src/os/sound/hooks/useSound.ts diff --git a/phone/src/os/sound/providers/SoundProvider.tsx b/phone/src/os/sound/providers/SoundProvider.tsx new file mode 100644 index 0000000..f620851 --- /dev/null +++ b/phone/src/os/sound/providers/SoundProvider.tsx @@ -0,0 +1,154 @@ +import { Howl } from 'howler'; +import React, { createContext, useCallback, useEffect, useRef } from 'react'; + +interface IMountResponse { + howl: Howl; + url: string; +} + +interface ISoundContext { + mount( + url: string, + volume?: number, + loop?: boolean, + autoplay?: boolean + ): Promise; + play(url?: string, volume?: number, loop?: boolean): void; + stop(url: string): void; + remove(url: string): boolean; + volume(url: string, volume?: number): number; + playing(url: string): boolean; + isMounted(url: string): boolean; +} + +export const soundContext = createContext(null); + +export const SoundProvider = ({ children }: { children: React.ReactNode }) => { + const soundRefs = useRef>(); + + useEffect(() => { + soundRefs.current = new Map(); + }, []); + + const mount = ( + url: string, + volume: number = 1, + loop: boolean = false, + autoplay: boolean = false + ) => { + return new Promise((res) => { + if (!soundRefs.current) { + soundRefs.current = new Map(); + } + + const find = soundRefs.current.get(url); + + if (find) { + find.howl.volume(volume); + find.howl.loop(loop); + if (autoplay) { + find.howl.play(); + } + return res({ howl: find.howl, url }); + } + + const instance = new Howl({ + src: url, + loop, + volume, + autoplay, + }); + + soundRefs.current.set(url, { howl: instance, volume }); + + instance.once('load', () => { + console.log('ready', instance); + res({ howl: instance, url }); + }); + + const onError = (_id, e) => { + res(null); + console.error('Howler Error \n', e, '\nid: ', _id, '\nsrc: ', url); + }; + + instance.once('loaderror', onError); + + instance.once('playerror', onError); + }); + }; + + const play = useCallback((url: string, volume: number, loop: boolean) => { + const sound = soundRefs.current.get(url); + let fadedSounds = []; + if (sound) { + soundRefs.current.forEach((ref, key) => { + const isCurrent = key === url; + const isPlaying = ref.howl.playing(); + const isLoop = ref.howl.loop(); + if (isCurrent || !isLoop || !isPlaying) { + return; + } + fadedSounds.push(ref); + const currVol = ref.howl.volume(); + ref.howl.fade(currVol, currVol * 0.3, 50); + }); + if (volume !== undefined) { + sound.howl.volume(volume); + } + if (loop !== undefined) { + sound.howl.loop(loop); + } + sound.howl.play(); + + sound.howl.once('end', () => { + fadedSounds.forEach((s) => { + s.howl.fade(s.howl.volume(), s.volume, 50); + }); + fadedSounds = undefined; + }); + return; + } + mount(url, volume, loop, true); + }, []); + + const volume = (url: string, volume: number = undefined): number => { + const sound = soundRefs.current.get(url); + return sound?.howl.volume(volume) as number; + }; + + const stop = (url: string) => { + const sound = soundRefs.current.get(url); + sound?.howl.stop(); + }; + + const remove = (url: string): boolean => { + const sound = soundRefs.current.get(url); + if (sound) { + sound.howl.stop(); + sound.howl.unload(); + soundRefs.current.delete(url); + return true; + } + return false; + }; + + const isMounted = (url: string): boolean => { + if (!soundRefs.current) { + return false; + } + return !!soundRefs.current.get(url); + }; + + const playing = (url: string) => { + const sound = soundRefs.current.get(url); + return sound?.howl.playing() || false; + }; + + return ( + + {children} + + ); +}; diff --git a/phone/src/os/sound/utils/getSoundSettings.ts b/phone/src/os/sound/utils/getSoundSettings.ts new file mode 100644 index 0000000..05cd918 --- /dev/null +++ b/phone/src/os/sound/utils/getSoundSettings.ts @@ -0,0 +1,34 @@ +import { IPhoneSettings } from '../../../apps/settings/hooks/useSettings'; +import { + getNotificationPath, + getRingtonePath, +} from '../../../apps/settings/utils/getRingtonePath'; + +const getPath = { + ringtone: getRingtonePath, + notification: getNotificationPath, +}; + +/** + * Gets the configuration for SoundProvider based on settings and apps + */ +export const getSoundSettings = ( + type: 'ringtone' | 'notification', + settings: IPhoneSettings, + app?: string +) => { + try { + return app + ? { + sound: getPath[type](settings[`${app}_${type}`]), + volume: settings[`${app}_${type}Vol`] / 100, + } + : { + sound: getPath[type](settings[type]), + volume: settings[`${type}Vol`] / 100, + }; + } catch (e) { + console.error('getSoundSettings.ts error', e, 'Using default sounds'); + return { sound: getPath[type](), volume: 1 }; + } +}; diff --git a/phone/src/ui/components/AppIcon.tsx b/phone/src/ui/components/AppIcon.tsx index 1bfd94d..99c5dde 100644 --- a/phone/src/ui/components/AppIcon.tsx +++ b/phone/src/ui/components/AppIcon.tsx @@ -1,5 +1,5 @@ import React from 'react'; -import { fade, makeStyles, Theme } from '@material-ui/core/styles'; +import { darken, makeStyles, Theme } from '@material-ui/core/styles'; import Avatar from '@material-ui/core/Avatar'; import { green } from '@material-ui/core/colors'; import { Badge, Button, Tooltip, Zoom } from '@material-ui/core'; @@ -13,7 +13,7 @@ const useStyles = makeStyles( }, avatar: { '&:hover': { - backgroundColor: ({ backgroundColor }) => fade(backgroundColor, 0.6), + backgroundColor: ({ backgroundColor }) => darken(backgroundColor, 0.1), }, backgroundColor: ({ backgroundColor }) => backgroundColor, color: ({ color }) => color, diff --git a/phone/src/ui/hooks/useContextMenu.tsx b/phone/src/ui/hooks/useContextMenu.tsx index a75f810..c95c45d 100644 --- a/phone/src/ui/hooks/useContextMenu.tsx +++ b/phone/src/ui/hooks/useContextMenu.tsx @@ -12,9 +12,9 @@ export const MapStringOptions = (current, onClick) => (string) => { }; }; -export const useContextMenu = (_options = []): UseContextMenu => { +export const useContextMenu = (_options?): UseContextMenu => { const [open, setOpen] = useState(false); - const [options, setOptions] = useState(_options); + const [options, setOptions] = useState(_options || []); const onClose = () => setOpen(false);