2022-08-23 00:04:52 +00:00
|
|
|
import React, {
|
|
|
|
ReactNode,
|
|
|
|
useCallback,
|
2022-08-24 00:01:39 +00:00
|
|
|
useContext,
|
2022-08-23 00:04:52 +00:00
|
|
|
useEffect,
|
|
|
|
useState,
|
|
|
|
} from 'react';
|
2022-09-01 11:43:53 +00:00
|
|
|
import {NativeModules, ScrollView, StyleSheet, View} from 'react-native';
|
2022-08-26 01:54:51 +00:00
|
|
|
import DocumentPicker from 'react-native-document-picker';
|
2022-09-01 11:43:53 +00:00
|
|
|
import {Button, Searchbar, Text} from 'react-native-paper';
|
2022-08-30 11:21:25 +00:00
|
|
|
import {SnackbarContext} from './App';
|
2022-07-10 23:26:45 +00:00
|
|
|
import ConfirmDialog from './ConfirmDialog';
|
2022-09-04 03:28:21 +00:00
|
|
|
import {getSettings, setSettings} from './db';
|
2022-08-26 01:54:51 +00:00
|
|
|
import MassiveInput from './MassiveInput';
|
2022-07-17 01:45:31 +00:00
|
|
|
import MassiveSwitch from './MassiveSwitch';
|
2022-07-08 03:45:24 +00:00
|
|
|
|
2022-07-08 03:20:03 +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>('');
|
2022-09-04 03:28:21 +00:00
|
|
|
const [sets, setMaxSets] = useState<string>('3');
|
2022-07-03 01:50:01 +00:00
|
|
|
const [seconds, setSeconds] = useState<string>('');
|
2022-08-27 06:08:23 +00:00
|
|
|
const [alarm, setAlarm] = useState(false);
|
|
|
|
const [predict, setPredict] = useState(false);
|
2022-08-25 08:41:01 +00:00
|
|
|
const [sound, setSound] = useState<string>('');
|
2022-08-27 06:08:23 +00:00
|
|
|
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('');
|
2022-08-25 01:01:01 +00:00
|
|
|
const {toast} = useContext(SnackbarContext);
|
2022-07-03 01:50:01 +00:00
|
|
|
|
2022-07-09 07:39:11 +00:00
|
|
|
const refresh = useCallback(async () => {
|
2022-09-04 03:28:21 +00:00
|
|
|
const settings = await getSettings();
|
2022-08-24 00:01:39 +00:00
|
|
|
console.log('SettingsPage.refresh:', {settings});
|
|
|
|
setMinutes(settings.minutes.toString());
|
|
|
|
setSeconds(settings.seconds.toString());
|
|
|
|
setAlarm(!!settings.alarm);
|
2022-08-27 06:08:23 +00:00
|
|
|
setPredict(!!settings.predict);
|
2022-08-24 00:01:39 +00:00
|
|
|
setMaxSets(settings.sets.toString());
|
|
|
|
setVibrate(!!settings.vibrate);
|
2022-08-25 08:41:01 +00:00
|
|
|
setSound(settings.sound);
|
2022-08-28 07:10:01 +00:00
|
|
|
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-09-04 02:05:25 +00:00
|
|
|
}, []);
|
2022-07-07 00:45:45 +00:00
|
|
|
|
2022-07-07 03:13:44 +00:00
|
|
|
useEffect(() => {
|
2022-07-07 00:45:45 +00:00
|
|
|
refresh();
|
2022-07-09 07:39:11 +00:00
|
|
|
}, [refresh]);
|
2022-07-03 01:50:01 +00:00
|
|
|
|
2022-08-24 00:01:39 +00:00
|
|
|
useEffect(() => {
|
2022-09-04 03:28:21 +00:00
|
|
|
setSettings({
|
|
|
|
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]);
|
2022-08-24 00:01:39 +00:00
|
|
|
|
2022-07-09 07:39:11 +00:00
|
|
|
const changeAlarmEnabled = useCallback(
|
|
|
|
(enabled: boolean) => {
|
2022-08-20 04:37:59 +00:00
|
|
|
setAlarm(enabled);
|
2022-08-27 06:08:23 +00:00
|
|
|
toast('Time your rest duration after each set.', 4000);
|
2022-08-20 04:37:59 +00:00
|
|
|
if (enabled && !ignoring) setBattery(true);
|
2022-07-09 07:39:11 +00:00
|
|
|
},
|
2022-08-27 06:08:23 +00:00
|
|
|
[setBattery, ignoring, toast],
|
2022-07-09 07:39:11 +00:00
|
|
|
);
|
2022-07-06 09:03:56 +00:00
|
|
|
|
2022-08-27 06:08:23 +00:00
|
|
|
const changePredict = useCallback(
|
2022-07-10 05:53:38 +00:00
|
|
|
(enabled: boolean) => {
|
2022-08-27 06:08:23 +00:00
|
|
|
setPredict(enabled);
|
|
|
|
toast('Predict your next set based on todays plan.', 4000);
|
2022-07-10 05:53:38 +00:00
|
|
|
},
|
2022-08-27 06:08:23 +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);
|
2022-08-27 06:08:23 +00:00
|
|
|
toast('When a timer completes, vibrate your phone.', 4000);
|
2022-08-20 04:37:59 +00:00
|
|
|
},
|
2022-08-27 06:08:23 +00:00
|
|
|
[setVibrate, toast],
|
2022-08-20 04:37:59 +00:00
|
|
|
);
|
2022-07-10 07:03:07 +00:00
|
|
|
|
2022-08-25 08:41:01 +00:00
|
|
|
const changeSound = useCallback(async () => {
|
|
|
|
const {fileCopyUri} = await DocumentPicker.pickSingle({
|
|
|
|
type: 'audio/*',
|
|
|
|
copyTo: 'documentDirectory',
|
|
|
|
});
|
|
|
|
if (fileCopyUri) setSound(fileCopyUri);
|
|
|
|
}, []);
|
|
|
|
|
2022-08-27 06:08:23 +00:00
|
|
|
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"
|
2022-09-04 03:28:21 +00:00
|
|
|
value={sets}
|
2022-08-23 00:04:52 +00:00
|
|
|
keyboardType="numeric"
|
|
|
|
onChangeText={value => {
|
|
|
|
setMaxSets(value);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
2022-09-02 02:01:25 +00:00
|
|
|
name: 'Rest minutes Rest seconds',
|
2022-08-23 00:04:52 +00:00
|
|
|
element: (
|
2022-09-02 02:01:25 +00:00
|
|
|
<View style={{flexDirection: 'row', marginBottom: 10}}>
|
|
|
|
<MassiveInput
|
|
|
|
style={{width: 125, marginRight: 10}}
|
|
|
|
label="Rest minutes"
|
|
|
|
value={minutes}
|
|
|
|
keyboardType="numeric"
|
|
|
|
placeholder="3"
|
|
|
|
onChangeText={text => {
|
|
|
|
setMinutes(text);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<MassiveInput
|
|
|
|
style={{width: 125}}
|
|
|
|
label="Rest seconds"
|
|
|
|
value={seconds}
|
|
|
|
keyboardType="numeric"
|
|
|
|
placeholder="30"
|
|
|
|
onChangeText={s => {
|
|
|
|
setSeconds(s);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</View>
|
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}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
2022-08-27 06:08:23 +00:00
|
|
|
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: (
|
|
|
|
<>
|
2022-08-27 06:08:23 +00:00
|
|
|
<Text style={styles.text}>Record notifications</Text>
|
2022-08-23 00:04:52 +00:00
|
|
|
<MassiveSwitch
|
|
|
|
style={[styles.text, {alignSelf: 'flex-start'}]}
|
2022-08-27 06:08:23 +00:00
|
|
|
value={notify}
|
|
|
|
onValueChange={changeNotify}
|
2022-08-23 00:04:52 +00:00
|
|
|
/>
|
|
|
|
</>
|
|
|
|
),
|
|
|
|
},
|
2022-09-01 01:06:49 +00:00
|
|
|
{
|
|
|
|
name: 'Show images',
|
|
|
|
element: (
|
|
|
|
<>
|
|
|
|
<Text style={styles.text}>Show images</Text>
|
|
|
|
<MassiveSwitch
|
|
|
|
style={[styles.text, {alignSelf: 'flex-start'}]}
|
|
|
|
value={images}
|
|
|
|
onValueChange={setImages}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
),
|
|
|
|
},
|
2022-08-25 08:41:01 +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}>
|
2022-08-24 00:01:39 +00:00
|
|
|
<Searchbar
|
|
|
|
style={{marginBottom: 10}}
|
|
|
|
placeholder="Search"
|
|
|
|
value={search}
|
|
|
|
onChangeText={setSearch}
|
|
|
|
/>
|
2022-08-27 06:08:23 +00:00
|
|
|
<ScrollView>
|
|
|
|
{items
|
|
|
|
.filter(item =>
|
|
|
|
item.name.toLowerCase().includes(search.toLowerCase()),
|
|
|
|
)
|
|
|
|
.map(item => (
|
|
|
|
<React.Fragment key={item.name}>{item.element}</React.Fragment>
|
|
|
|
))}
|
|
|
|
</ScrollView>
|
2022-07-10 23:26:45 +00:00
|
|
|
<ConfirmDialog
|
|
|
|
title="Battery optimizations"
|
2022-08-20 04:37:59 +00:00
|
|
|
show={battery}
|
|
|
|
setShow={setBattery}
|
2022-07-10 23:26:45 +00:00
|
|
|
onOk={() => {
|
2022-08-30 11:06:09 +00:00
|
|
|
NativeModules.AlarmModule.ignoreBattery();
|
2022-08-20 04:37:59 +00:00
|
|
|
setBattery(false);
|
2022-07-10 23:26:45 +00:00
|
|
|
}}>
|
|
|
|
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
|
|
|
},
|
2022-07-04 04:17:29 +00:00
|
|
|
text: {
|
|
|
|
marginBottom: 10,
|
|
|
|
},
|
2022-07-03 01:50:01 +00:00
|
|
|
});
|