Massive/SettingsPage.tsx

244 lines
6.4 KiB
TypeScript
Raw Normal View History

2022-08-23 00:04:52 +00:00
import React, {
ReactNode,
useCallback,
useContext,
2022-08-23 00:04:52 +00:00
useEffect,
useState,
} from 'react';
import {NativeModules, ScrollView, StyleSheet, Text, View} from 'react-native';
2022-08-26 01:54:51 +00:00
import DocumentPicker from 'react-native-document-picker';
import {Button, Searchbar} from 'react-native-paper';
import {DatabaseContext, SnackbarContext} from './App';
import ConfirmDialog from './ConfirmDialog';
2022-08-26 01:54:51 +00:00
import MassiveInput from './MassiveInput';
2022-07-17 01:45:31 +00:00
import MassiveSwitch from './MassiveSwitch';
import Settings from './settings';
2022-07-08 03:45:24 +00:00
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>('');
const [maxSets, setMaxSets] = 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-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 db = useContext(DatabaseContext);
const {toast} = useContext(SnackbarContext);
2022-07-03 01:50:01 +00:00
const refresh = useCallback(async () => {
const [result] = await db.executeSql(`SELECT * FROM settings LIMIT 1`);
const settings: Settings = result.rows.item(0);
console.log('SettingsPage.refresh:', {settings});
setMinutes(settings.minutes.toString());
setSeconds(settings.seconds.toString());
setAlarm(!!settings.alarm);
setPredict(!!settings.predict);
setMaxSets(settings.sets.toString());
setVibrate(!!settings.vibrate);
setSound(settings.sound);
setNotify(!!settings.notify);
2022-07-19 04:38:58 +00:00
NativeModules.AlarmModule.ignoringBattery(setIgnoring);
}, [db]);
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(() => {
db.executeSql(
`UPDATE settings SET vibrate=?,minutes=?,sets=?,seconds=?,alarm=?,predict=?,sound=?,notify=?`,
[vibrate, minutes, maxSets, seconds, alarm, predict, sound, notify],
);
}, [vibrate, minutes, maxSets, seconds, alarm, predict, sound, notify, db]);
const changeAlarmEnabled = useCallback(
(enabled: boolean) => {
2022-08-20 04:37:59 +00:00
setAlarm(enabled);
toast('Time your rest duration 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);
toast('Predict your next set based on todays plan.', 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(
(value: boolean) => {
setVibrate(value);
toast('When a timer completes, vibrate your phone.', 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) setSound(fileCopyUri);
}, []);
const changeNotify = useCallback(
(value: boolean) => {
setNotify(value);
toast('If a set is a new record, show a notification.', 4000);
},
[toast],
);
2022-08-23 00:04:52 +00:00
const items: {name: string; element: ReactNode}[] = [
{
name: 'Sets per workout',
element: (
2022-08-26 01:54:51 +00:00
<MassiveInput
2022-08-23 00:04:52 +00:00
label="Sets per workout"
value={maxSets}
keyboardType="numeric"
onChangeText={value => {
setMaxSets(value);
}}
/>
),
},
{
name: 'Rest minutes',
2022-08-23 00:04:52 +00:00
element: (
2022-08-26 01:54:51 +00:00
<MassiveInput
label="Rest minutes"
value={minutes}
2022-08-23 00:04:52 +00:00
keyboardType="numeric"
placeholder="3"
onChangeText={text => {
setMinutes(text);
2022-08-23 00:04:52 +00:00
}}
/>
),
},
{
name: 'Rest seconds',
2022-08-23 00:04:52 +00:00
element: (
2022-08-26 01:54:51 +00:00
<MassiveInput
label="Rest seconds"
value={seconds}
2022-08-23 00:04:52 +00:00
keyboardType="numeric"
placeholder="30"
onChangeText={s => {
setSeconds(s);
2022-08-23 00:04:52 +00:00
}}
/>
),
},
{
name: 'Rest timers',
element: (
<>
<Text style={styles.text}>Rest timers</Text>
<MassiveSwitch
style={[styles.text, {alignSelf: 'flex-start'}]}
value={alarm}
onValueChange={changeAlarmEnabled}
/>
</>
),
},
{
name: 'Vibrate',
element: (
<>
<Text style={styles.text}>Vibrate</Text>
<MassiveSwitch
style={[styles.text, {alignSelf: 'flex-start'}]}
value={vibrate}
onValueChange={changeVibrate}
/>
</>
),
},
{
name: 'Predict sets',
element: (
<>
<Text style={styles.text}>Predict sets</Text>
<MassiveSwitch
style={[styles.text, {alignSelf: 'flex-start'}]}
value={predict}
onValueChange={changePredict}
/>
</>
),
},
{
name: 'Record notifications',
2022-08-23 00:04:52 +00:00
element: (
<>
<Text style={styles.text}>Record notifications</Text>
2022-08-23 00:04:52 +00:00
<MassiveSwitch
style={[styles.text, {alignSelf: 'flex-start'}]}
value={notify}
onValueChange={changeNotify}
2022-08-23 00:04:52 +00:00
/>
</>
),
},
{
name: 'Alarm sound',
element: (
<Button onPress={changeSound}>
Alarm sound
{sound ? ': ' + sound.split('/')[sound.split('/').length - 1] : null}
</Button>
),
},
2022-08-23 00:04:52 +00:00
];
2022-08-20 04:37:59 +00:00
return (
<View style={styles.container}>
<Searchbar
style={{marginBottom: 10}}
placeholder="Search"
value={search}
onChangeText={setSearch}
/>
<ScrollView>
{items
.filter(item =>
item.name.toLowerCase().includes(search.toLowerCase()),
)
.map(item => (
<React.Fragment key={item.name}>{item.element}</React.Fragment>
))}
</ScrollView>
<ConfirmDialog
title="Battery optimizations"
2022-08-20 04:37:59 +00:00
show={battery}
setShow={setBattery}
onOk={() => {
2022-07-19 04:38:58 +00:00
NativeModules.AlarmModule.openSettings();
2022-08-20 04:37:59 +00:00
setBattery(false);
}}>
Disable battery optimizations for Massive to use rest timers.
</ConfirmDialog>
2022-07-03 01:50:01 +00:00
</View>
);
}
const styles = StyleSheet.create({
container: {
padding: 10,
2022-07-05 03:33:42 +00:00
flex: 1,
2022-07-03 01:50:01 +00:00
},
text: {
marginBottom: 10,
},
2022-07-03 01:50:01 +00:00
});