2022-07-11 00:28:30 +00:00
|
|
|
import {
|
|
|
|
RouteProp,
|
|
|
|
useFocusEffect,
|
|
|
|
useNavigation,
|
|
|
|
useRoute,
|
|
|
|
} from '@react-navigation/native';
|
2022-07-20 03:48:48 +00:00
|
|
|
import React, {useCallback, useContext} from 'react';
|
2022-08-28 05:25:31 +00:00
|
|
|
import {NativeModules, View} from 'react-native';
|
2022-07-20 03:48:48 +00:00
|
|
|
import {IconButton} from 'react-native-paper';
|
2022-09-16 09:07:02 +00:00
|
|
|
import {PADDING} from './constants';
|
2022-09-24 06:47:31 +00:00
|
|
|
import {HomePageParams} from './home-page-params';
|
2022-09-11 03:35:20 +00:00
|
|
|
import {SnackbarContext} from './MassiveSnack';
|
2022-07-07 02:18:38 +00:00
|
|
|
import Set from './set';
|
2022-09-16 09:07:02 +00:00
|
|
|
import {addSet, updateSet} from './set.service';
|
2022-07-20 03:48:48 +00:00
|
|
|
import SetForm from './SetForm';
|
2022-10-01 03:01:07 +00:00
|
|
|
import {getSettings, updateSettings} from './settings.service';
|
|
|
|
import {useSettings} from './use-settings';
|
2022-07-03 01:50:01 +00:00
|
|
|
|
2022-07-10 12:04:13 +00:00
|
|
|
export default function EditSet() {
|
2022-07-11 00:28:30 +00:00
|
|
|
const {params} = useRoute<RouteProp<HomePageParams, 'EditSet'>>();
|
2022-10-05 10:38:52 +00:00
|
|
|
const {set} = params;
|
2022-07-10 12:04:13 +00:00
|
|
|
const navigation = useNavigation();
|
2022-08-25 01:01:01 +00:00
|
|
|
const {toast} = useContext(SnackbarContext);
|
2022-10-01 03:01:07 +00:00
|
|
|
const {settings, setSettings} = useSettings();
|
2022-07-10 12:04:13 +00:00
|
|
|
|
2022-07-11 00:28:30 +00:00
|
|
|
useFocusEffect(
|
|
|
|
useCallback(() => {
|
2022-09-24 07:09:22 +00:00
|
|
|
console.log(`${EditSet.name}.focus:`, set);
|
|
|
|
let title = 'Create set';
|
|
|
|
if (typeof set.id === 'number') title = 'Edit set';
|
2022-09-09 03:32:47 +00:00
|
|
|
navigation.getParent()?.setOptions({
|
|
|
|
headerLeft: () => (
|
|
|
|
<IconButton icon="arrow-back" onPress={() => navigation.goBack()} />
|
|
|
|
),
|
|
|
|
headerRight: null,
|
2022-09-24 07:09:22 +00:00
|
|
|
title,
|
2022-07-11 00:28:30 +00:00
|
|
|
});
|
2022-10-05 10:38:52 +00:00
|
|
|
}, [navigation, set]),
|
2022-07-11 00:28:30 +00:00
|
|
|
);
|
|
|
|
|
2022-10-01 03:01:07 +00:00
|
|
|
const startTimer = useCallback(
|
|
|
|
async (value: Set) => {
|
|
|
|
if (!settings.alarm) return;
|
|
|
|
const milliseconds =
|
|
|
|
Number(value.minutes) * 60 * 1000 + Number(value.seconds) * 1000;
|
|
|
|
NativeModules.AlarmModule.timer(
|
|
|
|
milliseconds,
|
|
|
|
!!settings.vibrate,
|
|
|
|
settings.sound,
|
|
|
|
);
|
|
|
|
const next = new Date();
|
|
|
|
next.setTime(next.getTime() + milliseconds);
|
|
|
|
await updateSettings({...settings, nextAlarm: next.toISOString()});
|
|
|
|
setSettings(await getSettings());
|
|
|
|
},
|
|
|
|
[settings, setSettings],
|
|
|
|
);
|
2022-07-10 12:04:13 +00:00
|
|
|
|
2022-07-20 03:48:48 +00:00
|
|
|
const update = useCallback(
|
2022-10-01 00:30:06 +00:00
|
|
|
async (value: Set) => {
|
|
|
|
console.log(`${EditSet.name}.update`, value);
|
|
|
|
await updateSet(value);
|
2022-07-20 03:48:48 +00:00
|
|
|
navigation.goBack();
|
|
|
|
},
|
2022-09-04 02:05:25 +00:00
|
|
|
[navigation],
|
2022-07-20 03:48:48 +00:00
|
|
|
);
|
2022-07-10 12:04:13 +00:00
|
|
|
|
2022-07-20 03:48:48 +00:00
|
|
|
const add = useCallback(
|
2022-10-01 00:30:06 +00:00
|
|
|
async (value: Set) => {
|
|
|
|
console.log(`${EditSet.name}.add`, {set: value});
|
|
|
|
startTimer(value);
|
|
|
|
await addSet(value);
|
2022-09-21 01:48:45 +00:00
|
|
|
if (!settings.notify) return navigation.goBack();
|
2022-08-24 01:04:45 +00:00
|
|
|
if (
|
2022-10-01 00:30:06 +00:00
|
|
|
value.weight > set.weight ||
|
|
|
|
(value.reps > set.reps && value.weight === set.weight)
|
2022-08-24 01:04:45 +00:00
|
|
|
)
|
2022-08-27 06:09:45 +00:00
|
|
|
toast("Great work King, that's a new record!", 3000);
|
2022-07-20 03:48:48 +00:00
|
|
|
navigation.goBack();
|
|
|
|
},
|
2022-10-01 03:01:07 +00:00
|
|
|
[navigation, startTimer, set, toast, settings],
|
2022-07-20 03:48:48 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const save = useCallback(
|
2022-10-01 00:30:06 +00:00
|
|
|
async (value: Set) => {
|
|
|
|
if (typeof set.id === 'number') return update(value);
|
|
|
|
return add(value);
|
2022-07-20 03:48:48 +00:00
|
|
|
},
|
2022-09-24 07:09:22 +00:00
|
|
|
[update, add, set.id],
|
2022-07-20 03:48:48 +00:00
|
|
|
);
|
2022-07-10 12:04:13 +00:00
|
|
|
|
2022-07-03 01:50:01 +00:00
|
|
|
return (
|
2022-10-13 03:32:12 +00:00
|
|
|
<View style={{padding: PADDING, flex: 1}}>
|
2022-10-05 10:38:52 +00:00
|
|
|
<SetForm save={save} set={set} />
|
2022-07-11 00:28:30 +00:00
|
|
|
</View>
|
2022-07-03 01:50:01 +00:00
|
|
|
);
|
|
|
|
}
|