Massive/SettingsPage.tsx

252 lines
8.0 KiB
TypeScript
Raw Normal View History

import {Picker} from '@react-native-picker/picker';
import {useFocusEffect} from '@react-navigation/native';
2022-10-14 04:27:19 +00:00
import React, {useCallback, useEffect, useState} from 'react';
2022-09-26 03:10:13 +00:00
import {NativeModules, ScrollView} from 'react-native';
2022-08-26 01:54:51 +00:00
import DocumentPicker from 'react-native-document-picker';
2022-09-26 03:10:13 +00:00
import {Button} from 'react-native-paper';
import {useColor} from './color';
2022-09-25 04:32:49 +00:00
import {darkColors, lightColors} from './colors';
import ConfirmDialog from './ConfirmDialog';
import {MARGIN} from './constants';
2022-09-25 04:32:49 +00:00
import Input from './input';
2022-10-14 04:27:19 +00:00
import {useSnackbar} from './MassiveSnack';
import Page from './Page';
import {getSettings, updateSettings} from './settings.service';
2022-09-26 03:10:13 +00:00
import Switch from './Switch';
import {useSettings} from './use-settings';
2022-07-08 03:45:24 +00:00
export default function SettingsPage() {
2022-08-20 04:37:59 +00:00
const [battery, setBattery] = useState(false);
2022-07-07 00:45:45 +00:00
const [ignoring, setIgnoring] = useState(false);
2022-08-23 00:04:52 +00:00
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);
2022-09-24 05:29:52 +00:00
const [steps, setSteps] = useState(!!settings.steps);
2022-10-02 06:07:52 +00:00
const [date, setDate] = useState(settings.date || '%Y-%m-%d %H:%M');
const [theme, setTheme] = useState(settings.theme || 'system');
2022-10-02 06:07:52 +00:00
const [showDate, setShowDate] = useState(!!settings.showDate);
const [showSets, setShowSets] = useState(!!settings.showSets);
const {color, setColor} = useColor();
2022-10-14 04:27:19 +00:00
const {toast} = useSnackbar();
2022-07-03 01:50:01 +00:00
useFocusEffect(
useCallback(() => {
NativeModules.AlarmModule.ignoringBattery(setIgnoring);
}, []),
);
2022-07-03 01:50:01 +00:00
useEffect(() => {
updateSettings({
vibrate: +vibrate,
alarm: +alarm,
sound,
notify: +notify,
images: +images,
showUnit: +showUnit,
color,
2022-09-24 05:29:52 +00:00
steps: +steps,
date,
showDate: +showDate,
theme,
showSets: +showSets,
});
getSettings().then(setSettings);
}, [
vibrate,
alarm,
sound,
notify,
images,
showUnit,
color,
2022-09-24 05:29:52 +00:00
steps,
setSettings,
date,
showDate,
theme,
showSets,
]);
const changeAlarmEnabled = useCallback(
(enabled: boolean) => {
2022-08-20 04:37:59 +00:00
setAlarm(enabled);
if (enabled) toast('Timers will now run after each set.', 4000);
else toast('Stopped timers running after each set.', 4000);
2022-08-20 04:37:59 +00:00
if (enabled && !ignoring) setBattery(true);
},
[setBattery, ignoring, toast],
);
2022-08-20 04:37:59 +00:00
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);
2022-08-20 04:37:59 +00:00
},
[setVibrate, toast],
2022-08-20 04:37:59 +00:00
);
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],
);
2022-09-16 09:38:37 +00:00
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);
2022-09-24 05:29:52 +00:00
else toast('Hid unit option for sets.', 4000);
},
[toast],
);
2022-09-24 05:29:52 +00:00
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 changeShowSets = useCallback(
(enabled: boolean) => {
setShowSets(enabled);
if (enabled) toast('Show maximum sets for workouts.', 4000);
else toast('Stopped showing maximum sets for workouts.', 4000);
},
[toast],
);
2022-09-11 03:22:18 +00:00
const switches: Input<boolean>[] = [
{name: 'Rest timers', value: alarm, onChange: changeAlarmEnabled},
{name: 'Vibrate', value: vibrate, onChange: changeVibrate},
{name: 'Record notifications', value: notify, onChange: changeNotify},
2022-09-16 09:38:37 +00:00
{name: 'Show images', value: images, onChange: changeImages},
{name: 'Show unit', value: showUnit, onChange: changeUnit},
2022-09-24 05:29:52 +00:00
{name: 'Show steps', value: steps, onChange: changeSteps},
{name: 'Show date', value: showDate, onChange: changeShowDate},
{name: 'Show sets', value: showSets, onChange: changeShowSets},
2022-08-23 00:04:52 +00:00
];
2022-08-20 04:37:59 +00:00
return (
<Page search={search} setSearch={setSearch}>
<ScrollView style={{marginTop: MARGIN}}>
2022-09-11 03:22:18 +00:00
{switches
.filter(input =>
input.name.toLowerCase().includes(search.toLowerCase()),
)
2022-09-11 03:22:18 +00:00
.map(input => (
2022-09-26 03:10:13 +00:00
<Switch
2022-09-25 05:35:30 +00:00
onPress={() => input.onChange(!input.value)}
2022-09-26 03:10:13 +00:00
key={input.name}
value={input.value}
onValueChange={input.onChange}>
{input.name}
</Switch>
))}
{'theme'.includes(search.toLowerCase()) && (
2022-09-29 01:44:01 +00:00
<Picker
style={{color}}
2022-09-29 01:44:01 +00:00
dropdownIconColor={color}
selectedValue={theme}
onValueChange={value => setTheme(value)}>
<Picker.Item value="system" label="Follow system theme" />
<Picker.Item value="dark" label="Dark theme" />
<Picker.Item value="light" label="Light theme" />
2022-09-29 01:44:01 +00:00
</Picker>
)}
{'color'.includes(search.toLowerCase()) && (
<Picker
style={{color, marginTop: -10}}
dropdownIconColor={color}
selectedValue={color}
onValueChange={value => setColor(value)}>
{lightColors.concat(darkColors).map(colorOption => (
2022-09-25 04:32:49 +00:00
<Picker.Item
2022-09-25 04:43:29 +00:00
key={colorOption.hex}
value={colorOption.hex}
label="Primary color"
2022-09-25 04:43:29 +00:00
color={colorOption.hex}
2022-09-25 04:32:49 +00:00
/>
))}
</Picker>
)}
{'date format'.includes(search.toLowerCase()) && (
<Picker
2022-10-02 06:07:52 +00:00
style={{color, marginTop: -10}}
dropdownIconColor={color}
selectedValue={date}
onValueChange={value => setDate(value)}>
<Picker.Item
value="%Y-%m-%d %H:%M"
label="Format date as 1990-12-24 15:05"
/>
<Picker.Item
value="%Y-%m-%d"
label="Format date as 1990-12-24 (YYYY-MM-dd)"
/>
2022-10-02 06:07:52 +00:00
<Picker.Item value="%d/%m" label="Format date as 24/12 (dd/MM)" />
2022-10-02 06:14:10 +00:00
<Picker.Item value="%H:%M" label="Format date as 15:05 (HH:MM)" />
</Picker>
)}
{'alarm sound'.includes(search.toLowerCase()) && (
<Button style={{alignSelf: 'flex-start'}} onPress={changeSound}>
Alarm sound
{sound
? ': ' + sound.split('/')[sound.split('/').length - 1]
: null}
</Button>
)}
</ScrollView>
<ConfirmDialog
title="Battery optimizations"
2022-08-20 04:37:59 +00:00
show={battery}
setShow={setBattery}
onOk={() => {
NativeModules.AlarmModule.ignoreBattery();
2022-08-20 04:37:59 +00:00
setBattery(false);
}}>
Disable battery optimizations for Massive to use rest timers.
</ConfirmDialog>
</Page>
2022-07-03 01:50:01 +00:00
);
}