import {Picker} from '@react-native-picker/picker'; import {useFocusEffect} from '@react-navigation/native'; import React, {useCallback, useEffect, useState} from 'react'; import {NativeModules, ScrollView} from 'react-native'; import DocumentPicker from 'react-native-document-picker'; import {Button} from 'react-native-paper'; import {useColor} from './color'; import {darkColors, lightColors} from './colors'; import ConfirmDialog from './ConfirmDialog'; import {MARGIN} from './constants'; import Input from './input'; import {useSnackbar} from './MassiveSnack'; import Page from './Page'; import {getSettings, updateSettings} from './settings.service'; import Switch from './Switch'; import {useSettings} from './use-settings'; export default function SettingsPage() { const [battery, setBattery] = useState(false); const [ignoring, setIgnoring] = useState(false); const [search, setSearch] = useState(''); const {settings, setSettings} = useSettings(); const [vibrate, setVibrate] = useState(!!settings.vibrate); const [alarm, setAlarm] = useState(!!settings.alarm); const [sound, setSound] = useState(settings.sound); const [notify, setNotify] = useState(!!settings.notify); const [images, setImages] = useState(!!settings.images); const [showUnit, setShowUnit] = useState(!!settings.showUnit); const [steps, setSteps] = useState(!!settings.steps); const [date, setDate] = useState(settings.date || '%Y-%m-%d %H:%M'); const [theme, setTheme] = useState(settings.theme || 'system'); const [showDate, setShowDate] = useState(!!settings.showDate); const {color, setColor} = useColor(); const {toast} = useSnackbar(); useFocusEffect( useCallback(() => { NativeModules.AlarmModule.ignoringBattery(setIgnoring); }, []), ); useEffect(() => { updateSettings({ vibrate: +vibrate, alarm: +alarm, sound, notify: +notify, images: +images, showUnit: +showUnit, color, steps: +steps, date, showDate: +showDate, theme, }); getSettings().then(setSettings); }, [ vibrate, alarm, sound, notify, images, showUnit, color, steps, setSettings, date, showDate, theme, ]); const changeAlarmEnabled = useCallback( (enabled: boolean) => { setAlarm(enabled); if (enabled) toast('Timers will now run after each set.', 4000); else toast('Stopped timers running after each set.', 4000); if (enabled && !ignoring) setBattery(true); }, [setBattery, ignoring, toast], ); const changeVibrate = useCallback( (enabled: boolean) => { setVibrate(enabled); if (enabled) toast('When a timer completes, vibrate your phone.', 4000); else toast('Stop vibrating at the end of timers.', 4000); }, [setVibrate, toast], ); const changeSound = useCallback(async () => { const {fileCopyUri} = await DocumentPicker.pickSingle({ type: 'audio/*', copyTo: 'documentDirectory', }); if (!fileCopyUri) return; setSound(fileCopyUri); toast('This song will now play after rest timers complete.', 4000); }, [toast]); const changeNotify = useCallback( (enabled: boolean) => { setNotify(enabled); if (enabled) toast('Show when a set is a new record.', 4000); else toast('Stopped showing notifications for new records.', 4000); }, [toast], ); const changeImages = useCallback( (enabled: boolean) => { setImages(enabled); if (enabled) toast('Show images for sets.', 4000); else toast('Stopped showing images for sets.', 4000); }, [toast], ); const changeUnit = useCallback( (enabled: boolean) => { setShowUnit(enabled); if (enabled) toast('Show option to select unit for sets.', 4000); else toast('Hid unit option for sets.', 4000); }, [toast], ); const changeSteps = useCallback( (enabled: boolean) => { setSteps(enabled); if (enabled) toast('Show steps for a workout.', 4000); else toast('Stopped showing steps for workouts.', 4000); }, [toast], ); const changeShowDate = useCallback( (enabled: boolean) => { setShowDate(enabled); if (enabled) toast('Show date for sets by default.', 4000); else toast('Stopped showing date for sets by default.', 4000); }, [toast], ); const switches: Input[] = [ {name: 'Rest timers', value: alarm, onChange: changeAlarmEnabled}, {name: 'Vibrate', value: vibrate, onChange: changeVibrate}, {name: 'Record notifications', value: notify, onChange: changeNotify}, {name: 'Show images', value: images, onChange: changeImages}, {name: 'Show unit', value: showUnit, onChange: changeUnit}, {name: 'Show steps', value: steps, onChange: changeSteps}, {name: 'Show date', value: showDate, onChange: changeShowDate}, ]; return ( {switches .filter(input => input.name.toLowerCase().includes(search.toLowerCase()), ) .map(input => ( input.onChange(!input.value)} key={input.name} value={input.value} onValueChange={input.onChange}> {input.name} ))} {'theme'.includes(search.toLowerCase()) && ( setTheme(value)}> )} {'color'.includes(search.toLowerCase()) && ( setColor(value)}> {lightColors.concat(darkColors).map(colorOption => ( ))} )} {'date format'.includes(search.toLowerCase()) && ( setDate(value)}> )} {'alarm sound'.includes(search.toLowerCase()) && ( )} { NativeModules.AlarmModule.ignoreBattery(); setBattery(false); }}> Disable battery optimizations for Massive to use rest timers. ); }