Massive/SettingsPage.tsx

171 lines
5.5 KiB
TypeScript
Raw Normal View History

import {useFocusEffect} from '@react-navigation/native';
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';
import {SnackbarContext} from './MassiveSnack';
2022-07-17 01:45:31 +00:00
import MassiveSwitch from './MassiveSwitch';
import Page from './Page';
import {getSettings, settings, 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);
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 [showUnit, setShowUnit] = useState(true);
const {toast} = useContext(SnackbarContext);
2022-07-03 01:50:01 +00:00
useFocusEffect(
useCallback(() => {
console.log('SettingsPage.refresh:', {settings});
setAlarm(!!settings.alarm);
setPredict(!!settings.predict);
setVibrate(!!settings.vibrate);
setSound(settings.sound);
setNotify(!!settings.notify);
setImages(!!settings.images);
NativeModules.AlarmModule.ignoringBattery(setIgnoring);
}, []),
);
2022-07-03 01:50:01 +00:00
useEffect(() => {
updateSettings({
vibrate: +vibrate,
alarm: +alarm,
predict: +predict,
sound,
notify: +notify,
images: +images,
showUnit: +showUnit,
});
getSettings();
}, [vibrate, alarm, predict, sound, notify, images, showUnit]);
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],
);
const changeUnit = useCallback(
(enabled: boolean) => {
setShowUnit(enabled);
if (enabled) toast('Show option to select unit for sets.', 4000);
else toast('Hid the unit option when adding/editing sets.', 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: '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},
{name: 'Show unit', value: showUnit, onChange: changeUnit},
2022-08-23 00:04:52 +00:00
];
2022-08-20 04:37:59 +00:00
return (
<Page search={search} setSearch={setSearch}>
<ScrollView style={{margin: 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 => (
<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()) && (
<Button style={{alignSelf: 'flex-start'}} onPress={changeSound}>
2022-09-11 03:22:18 +00:00
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
);
}