2022-09-24 01:57:51 +00:00
|
|
|
import {Picker} from '@react-native-picker/picker';
|
2022-09-21 01:48:45 +00:00
|
|
|
import {useFocusEffect} from '@react-navigation/native';
|
2022-09-11 03:22:18 +00:00
|
|
|
import React, {useCallback, useContext, 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';
|
2022-09-24 01:57:51 +00:00
|
|
|
import {CustomTheme} from './App';
|
2022-09-25 04:32:49 +00:00
|
|
|
import {darkColors, lightColors} from './colors';
|
2022-07-10 23:26:45 +00:00
|
|
|
import ConfirmDialog from './ConfirmDialog';
|
2022-09-16 09:07:02 +00:00
|
|
|
import {MARGIN} from './constants';
|
2022-09-25 04:32:49 +00:00
|
|
|
import Input from './input';
|
2022-09-11 03:35:20 +00:00
|
|
|
import {SnackbarContext} from './MassiveSnack';
|
2022-09-16 09:07:02 +00:00
|
|
|
import Page from './Page';
|
2022-10-01 03:01:07 +00:00
|
|
|
import {getSettings, updateSettings} from './settings.service';
|
2022-09-26 03:10:13 +00:00
|
|
|
import Switch from './Switch';
|
2022-10-01 03:01:07 +00:00
|
|
|
import {useSettings} from './use-settings';
|
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 [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-10-01 03:01:07 +00:00
|
|
|
const {settings, setSettings} = useSettings();
|
2022-09-24 05:12:58 +00:00
|
|
|
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');
|
2022-10-04 01:35:56 +00:00
|
|
|
const [theme, setTheme] = useState(settings.theme || 'system');
|
2022-10-02 06:07:52 +00:00
|
|
|
const [showDate, setShowDate] = useState(!!settings.showDate);
|
2022-09-24 01:57:51 +00:00
|
|
|
const {color, setColor} = useContext(CustomTheme);
|
2022-08-25 01:01:01 +00:00
|
|
|
const {toast} = useContext(SnackbarContext);
|
2022-07-03 01:50:01 +00:00
|
|
|
|
2022-09-21 01:48:45 +00:00
|
|
|
useFocusEffect(
|
|
|
|
useCallback(() => {
|
|
|
|
NativeModules.AlarmModule.ignoringBattery(setIgnoring);
|
|
|
|
}, []),
|
|
|
|
);
|
2022-07-03 01:50:01 +00:00
|
|
|
|
2022-08-24 00:01:39 +00:00
|
|
|
useEffect(() => {
|
2022-09-16 09:07:02 +00:00
|
|
|
updateSettings({
|
2022-09-04 03:28:21 +00:00
|
|
|
vibrate: +vibrate,
|
|
|
|
alarm: +alarm,
|
|
|
|
sound,
|
|
|
|
notify: +notify,
|
|
|
|
images: +images,
|
2022-09-21 01:48:45 +00:00
|
|
|
showUnit: +showUnit,
|
2022-09-24 01:57:51 +00:00
|
|
|
color,
|
2022-09-24 05:29:52 +00:00
|
|
|
steps: +steps,
|
2022-10-02 05:04:36 +00:00
|
|
|
date,
|
2022-10-02 05:07:43 +00:00
|
|
|
showDate: +showDate,
|
2022-10-04 01:35:56 +00:00
|
|
|
theme,
|
2022-09-04 03:28:21 +00:00
|
|
|
});
|
2022-10-01 03:01:07 +00:00
|
|
|
getSettings().then(setSettings);
|
2022-09-24 05:23:15 +00:00
|
|
|
}, [
|
|
|
|
vibrate,
|
|
|
|
alarm,
|
|
|
|
sound,
|
|
|
|
notify,
|
|
|
|
images,
|
|
|
|
showUnit,
|
|
|
|
color,
|
2022-09-24 05:29:52 +00:00
|
|
|
steps,
|
2022-10-01 03:01:07 +00:00
|
|
|
setSettings,
|
2022-10-02 05:04:36 +00:00
|
|
|
date,
|
2022-10-02 05:07:43 +00:00
|
|
|
showDate,
|
2022-10-04 01:35:56 +00:00
|
|
|
theme,
|
2022-09-24 05:23:15 +00:00
|
|
|
]);
|
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-09-11 03:35:20 +00:00
|
|
|
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);
|
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-20 04:37:59 +00:00
|
|
|
const changeVibrate = useCallback(
|
2022-09-11 03:35:20 +00:00
|
|
|
(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
|
|
|
},
|
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',
|
|
|
|
});
|
2022-09-11 03:35:20 +00:00
|
|
|
if (!fileCopyUri) return;
|
|
|
|
setSound(fileCopyUri);
|
|
|
|
toast('This song will now play after rest timers complete.', 4000);
|
|
|
|
}, [toast]);
|
2022-08-25 08:41:01 +00:00
|
|
|
|
2022-08-27 06:08:23 +00:00
|
|
|
const changeNotify = useCallback(
|
2022-09-11 03:35:20 +00:00
|
|
|
(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);
|
2022-08-27 06:08:23 +00:00
|
|
|
},
|
|
|
|
[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-21 01:48:45 +00:00
|
|
|
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);
|
2022-09-21 01:48:45 +00:00
|
|
|
},
|
|
|
|
[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);
|
2022-09-24 05:23:15 +00:00
|
|
|
},
|
|
|
|
[toast],
|
|
|
|
);
|
|
|
|
|
2022-10-02 05:07:43 +00:00
|
|
|
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],
|
|
|
|
);
|
|
|
|
|
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},
|
2022-09-21 01:48:45 +00:00
|
|
|
{name: 'Show unit', value: showUnit, onChange: changeUnit},
|
2022-09-24 05:29:52 +00:00
|
|
|
{name: 'Show steps', value: steps, onChange: changeSteps},
|
2022-10-02 05:07:43 +00:00
|
|
|
{name: 'Show date', value: showDate, onChange: changeShowDate},
|
2022-08-23 00:04:52 +00:00
|
|
|
];
|
|
|
|
|
2022-08-20 04:37:59 +00:00
|
|
|
return (
|
2022-09-16 09:07:02 +00:00
|
|
|
<Page search={search} setSearch={setSearch}>
|
2022-09-24 01:57:51 +00:00
|
|
|
<ScrollView style={{marginTop: MARGIN}}>
|
2022-09-11 03:22:18 +00:00
|
|
|
{switches
|
2022-09-11 03:41:15 +00:00
|
|
|
.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>
|
2022-08-27 06:08:23 +00:00
|
|
|
))}
|
2022-10-04 01:35:56 +00:00
|
|
|
{'theme'.includes(search.toLowerCase()) && (
|
2022-09-29 01:44:01 +00:00
|
|
|
<Picker
|
2022-10-04 01:35:56 +00:00
|
|
|
style={{color}}
|
2022-09-29 01:44:01 +00:00
|
|
|
dropdownIconColor={color}
|
2022-10-04 01:35:56 +00:00
|
|
|
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>
|
|
|
|
)}
|
2022-10-04 01:35:56 +00:00
|
|
|
{'color'.includes(search.toLowerCase()) && (
|
2022-09-24 01:57:51 +00:00
|
|
|
<Picker
|
2022-09-29 01:52:38 +00:00
|
|
|
style={{color, marginTop: -10}}
|
2022-09-24 01:57:51 +00:00
|
|
|
dropdownIconColor={color}
|
|
|
|
selectedValue={color}
|
|
|
|
onValueChange={value => setColor(value)}>
|
2022-10-04 01:35:56 +00:00
|
|
|
{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}
|
2022-10-04 01:35:56 +00:00
|
|
|
label="Primary color"
|
2022-09-25 04:43:29 +00:00
|
|
|
color={colorOption.hex}
|
2022-09-25 04:32:49 +00:00
|
|
|
/>
|
|
|
|
))}
|
2022-09-24 01:57:51 +00:00
|
|
|
</Picker>
|
|
|
|
)}
|
2022-10-02 05:04:36 +00:00
|
|
|
{'date format'.includes(search.toLowerCase()) && (
|
|
|
|
<Picker
|
2022-10-02 06:07:52 +00:00
|
|
|
style={{color, marginTop: -10}}
|
2022-10-02 05:04:36 +00:00
|
|
|
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)" />
|
2022-10-02 05:04:36 +00:00
|
|
|
</Picker>
|
|
|
|
)}
|
2022-09-25 10:51:05 +00:00
|
|
|
{'alarm sound'.includes(search.toLowerCase()) && (
|
2022-09-29 01:52:38 +00:00
|
|
|
<Button style={{alignSelf: 'flex-start'}} onPress={changeSound}>
|
2022-09-25 10:51:05 +00:00
|
|
|
Alarm sound
|
|
|
|
{sound
|
|
|
|
? ': ' + sound.split('/')[sound.split('/').length - 1]
|
|
|
|
: null}
|
|
|
|
</Button>
|
|
|
|
)}
|
2022-08-27 06:08:23 +00:00
|
|
|
</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-09-16 09:07:02 +00:00
|
|
|
</Page>
|
2022-07-03 01:50:01 +00:00
|
|
|
);
|
|
|
|
}
|