Massive/SettingsPage.tsx

189 lines
6.0 KiB
TypeScript
Raw Normal View History

2022-09-11 03:22:18 +00:00
import React, {useCallback, useContext, useEffect, useState} from 'react';
2022-09-16 09:38:37 +00:00
import {NativeModules, ScrollView} from 'react-native';
2022-08-26 01:54:51 +00:00
import DocumentPicker from 'react-native-document-picker';
import {Button, Text} from 'react-native-paper';
import ConfirmDialog from './ConfirmDialog';
import {MARGIN} from './constants';
2022-08-26 01:54:51 +00:00
import MassiveInput from './MassiveInput';
import {SnackbarContext} from './MassiveSnack';
2022-07-17 01:45:31 +00:00
import MassiveSwitch from './MassiveSwitch';
import Page from './Page';
import {getSettings, updateSettings} from './settings.service';
2022-07-08 03:45:24 +00:00
2022-09-11 03:22:18 +00:00
interface Input<T> {
name: string;
value?: T;
onChange: (value: T) => void;
}
export default function SettingsPage() {
2022-08-20 04:37:59 +00:00
const [vibrate, setVibrate] = useState(true);
2022-07-03 01:50:01 +00:00
const [minutes, setMinutes] = useState<string>('');
2022-09-11 03:22:18 +00:00
const [sets, setSets] = useState<string>('3');
2022-07-03 01:50:01 +00:00
const [seconds, setSeconds] = useState<string>('');
const [alarm, setAlarm] = useState(false);
const [predict, setPredict] = useState(false);
const [sound, setSound] = useState<string>('');
const [notify, setNotify] = useState(false);
2022-09-01 01:06:49 +00:00
const [images, setImages] = useState(false);
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 {toast} = useContext(SnackbarContext);
2022-07-03 01:50:01 +00:00
const refresh = useCallback(async () => {
const settings = await getSettings();
console.log('SettingsPage.refresh:', {settings});
setMinutes(settings.minutes.toString());
setSeconds(settings.seconds.toString());
setAlarm(!!settings.alarm);
setPredict(!!settings.predict);
2022-09-11 03:22:18 +00:00
setSets(settings.sets.toString());
setVibrate(!!settings.vibrate);
setSound(settings.sound);
setNotify(!!settings.notify);
2022-09-01 01:06:49 +00:00
setImages(!!settings.images);
2022-07-19 04:38:58 +00:00
NativeModules.AlarmModule.ignoringBattery(setIgnoring);
}, []);
2022-07-07 00:45:45 +00:00
useEffect(() => {
2022-07-07 00:45:45 +00:00
refresh();
}, [refresh]);
2022-07-03 01:50:01 +00:00
useEffect(() => {
updateSettings({
vibrate: +vibrate,
minutes: +minutes,
seconds: +seconds,
alarm: +alarm,
predict: +predict,
sound,
notify: +notify,
images: +images,
sets: +sets,
});
}, [vibrate, minutes, sets, seconds, alarm, predict, sound, notify, images]);
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],
);
const changePredict = useCallback(
2022-07-10 05:53:38 +00:00
(enabled: boolean) => {
setPredict(enabled);
if (enabled) toast('Predict your next set based on todays plan.', 4000);
else toast('New sets will always be empty.', 4000);
2022-07-10 05:53:38 +00:00
},
[setPredict, toast],
2022-07-10 05:53:38 +00:00
);
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],
);
2022-09-11 03:22:18 +00:00
const inputs: Input<string>[] = [
{name: 'Sets per workout', value: sets, onChange: setSets},
{name: 'Rest minutes', value: minutes, onChange: setMinutes},
{name: 'Rest seconds', value: seconds, onChange: setSeconds},
];
const switches: Input<boolean>[] = [
{name: 'Rest timers', value: alarm, onChange: changeAlarmEnabled},
{name: 'Vibrate', value: vibrate, onChange: changeVibrate},
{name: 'Predict sets', value: predict, onChange: changePredict},
{name: 'Record notifications', value: notify, onChange: changeNotify},
2022-09-16 09:38:37 +00:00
{name: 'Show images', value: images, onChange: changeImages},
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
{inputs
.filter(input =>
input.name.toLowerCase().includes(search.toLowerCase()),
)
2022-09-11 03:22:18 +00:00
.map(input => (
<MassiveInput
key={input.name}
label={input.name}
value={input.value}
keyboardType="numeric"
onChangeText={input.onChange}
/>
))}
{switches
.filter(input =>
input.name.toLowerCase().includes(search.toLowerCase()),
)
2022-09-11 03:22:18 +00:00
.map(input => (
<React.Fragment key={input.name}>
2022-09-16 09:38:37 +00:00
<Text style={{marginBottom: MARGIN}}>{input.name}</Text>
2022-09-11 03:22:18 +00:00
<MassiveSwitch
2022-09-16 09:38:37 +00:00
style={{alignSelf: 'flex-start', marginBottom: MARGIN}}
2022-09-11 03:22:18 +00:00
value={input.value}
onValueChange={input.onChange}
/>
</React.Fragment>
))}
{'alarm sound'.includes(search.toLowerCase()) && (
2022-09-11 03:22:18 +00:00
<Button 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
);
}