2022-11-01 03:50:03 +00:00
|
|
|
import {
|
|
|
|
RouteProp,
|
|
|
|
useFocusEffect,
|
|
|
|
useNavigation,
|
|
|
|
useRoute,
|
|
|
|
} from '@react-navigation/native'
|
|
|
|
import {useCallback, useState} from 'react'
|
2022-10-31 04:22:08 +00:00
|
|
|
import {NativeModules, View} from 'react-native'
|
|
|
|
import {PADDING} from './constants'
|
2022-11-01 03:50:03 +00:00
|
|
|
import {setRepo, settingsRepo} from './db'
|
2022-10-31 04:22:08 +00:00
|
|
|
import GymSet from './gym-set'
|
|
|
|
import {HomePageParams} from './home-page-params'
|
|
|
|
import SetForm from './SetForm'
|
2022-11-01 03:50:03 +00:00
|
|
|
import Settings from './settings'
|
2022-10-31 04:22:08 +00:00
|
|
|
import StackHeader from './StackHeader'
|
2022-11-01 02:55:37 +00:00
|
|
|
import {toast} from './toast'
|
2022-07-03 01:50:01 +00:00
|
|
|
|
2022-07-10 12:04:13 +00:00
|
|
|
export default function EditSet() {
|
2022-10-31 04:22:08 +00:00
|
|
|
const {params} = useRoute<RouteProp<HomePageParams, 'EditSet'>>()
|
|
|
|
const {set} = params
|
|
|
|
const navigation = useNavigation()
|
2022-11-01 03:50:03 +00:00
|
|
|
const [settings, setSettings] = useState<Settings>()
|
|
|
|
|
|
|
|
useFocusEffect(
|
|
|
|
useCallback(() => {
|
|
|
|
settingsRepo.findOne({where: {}}).then(setSettings)
|
|
|
|
}, []),
|
|
|
|
)
|
2022-07-10 12:04:13 +00:00
|
|
|
|
2022-10-01 03:01:07 +00:00
|
|
|
const startTimer = useCallback(
|
2022-10-21 05:39:06 +00:00
|
|
|
async (name: string) => {
|
2022-10-31 04:22:08 +00:00
|
|
|
if (!settings.alarm) return
|
|
|
|
const {minutes, seconds} = await setRepo.findOne({where: {name}})
|
|
|
|
const milliseconds = (minutes ?? 3) * 60 * 1000 + (seconds ?? 0) * 1000
|
2022-10-01 03:01:07 +00:00
|
|
|
NativeModules.AlarmModule.timer(
|
|
|
|
milliseconds,
|
2022-11-01 03:50:03 +00:00
|
|
|
settings.vibrate,
|
2022-10-01 03:01:07 +00:00
|
|
|
settings.sound,
|
2022-11-01 03:50:03 +00:00
|
|
|
settings.noSound,
|
2022-10-31 04:22:08 +00:00
|
|
|
)
|
2022-10-01 03:01:07 +00:00
|
|
|
},
|
2022-10-21 05:39:06 +00:00
|
|
|
[settings],
|
2022-10-31 04:22:08 +00:00
|
|
|
)
|
2022-07-10 12:04:13 +00:00
|
|
|
|
2022-07-20 03:48:48 +00:00
|
|
|
const add = useCallback(
|
2022-10-31 00:20:36 +00:00
|
|
|
async (value: GymSet) => {
|
2022-10-31 04:22:08 +00:00
|
|
|
startTimer(value.name)
|
|
|
|
console.log(`${EditSet.name}.add`, {set: value})
|
|
|
|
const result = await setRepo.save(value)
|
|
|
|
console.log({result})
|
|
|
|
if (!settings.notify) return
|
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-11-01 02:55:37 +00:00
|
|
|
toast("Great work King! That's a new record.")
|
2022-07-20 03:48:48 +00:00
|
|
|
},
|
2022-11-01 02:55:37 +00:00
|
|
|
[startTimer, set, settings],
|
2022-10-31 04:22:08 +00:00
|
|
|
)
|
2022-07-20 03:48:48 +00:00
|
|
|
|
|
|
|
const save = useCallback(
|
2022-10-31 00:20:36 +00:00
|
|
|
async (value: GymSet) => {
|
2022-10-31 04:22:08 +00:00
|
|
|
if (typeof set.id === 'number') await setRepo.save(value)
|
|
|
|
else await add(value)
|
|
|
|
navigation.goBack()
|
2022-07-20 03:48:48 +00:00
|
|
|
},
|
2022-10-31 00:20:36 +00:00
|
|
|
[add, set.id, navigation],
|
2022-10-31 04:22:08 +00:00
|
|
|
)
|
2022-07-10 12:04:13 +00:00
|
|
|
|
2022-07-03 01:50:01 +00:00
|
|
|
return (
|
2022-10-23 06:13:58 +00:00
|
|
|
<>
|
|
|
|
<StackHeader title="Edit set" />
|
|
|
|
<View style={{padding: PADDING, flex: 1}}>
|
2022-11-01 03:50:03 +00:00
|
|
|
{settings && <SetForm settings={settings} save={save} set={set} />}
|
2022-10-23 06:13:58 +00:00
|
|
|
</View>
|
|
|
|
</>
|
2022-10-31 04:22:08 +00:00
|
|
|
)
|
2022-07-03 01:50:01 +00:00
|
|
|
}
|