Massive/StartPlan.tsx

178 lines
5.6 KiB
TypeScript
Raw Normal View History

import {
NavigationProp,
RouteProp,
useFocusEffect,
useNavigation,
useRoute,
} from '@react-navigation/native'
2023-06-27 03:16:59 +00:00
import { useCallback, useMemo, useRef, useState } from 'react'
import { FlatList, NativeModules, TextInput, View } from 'react-native'
import { Button, IconButton, ProgressBar } from 'react-native-paper'
2023-01-01 02:20:56 +00:00
import AppInput from './AppInput'
2023-06-27 03:16:59 +00:00
import { getBestSet, getLast } from './best.service'
import { PADDING } from './constants'
2022-10-31 04:22:08 +00:00
import CountMany from './count-many'
2023-06-27 03:16:59 +00:00
import { AppDataSource } from './data-source'
import { getNow, setRepo, settingsRepo } from './db'
import GymSet from './gym-set'
2023-06-27 03:16:59 +00:00
import { PlanPageParams } from './plan-page-params'
import Settings from './settings'
2022-10-31 04:22:08 +00:00
import StackHeader from './StackHeader'
import StartPlanItem from './StartPlanItem'
2023-06-27 03:16:59 +00:00
import { toast } from './toast'
2022-10-05 10:38:52 +00:00
export default function StartPlan() {
2023-06-27 03:16:59 +00:00
const { params } = useRoute<RouteProp<PlanPageParams, 'StartPlan'>>()
const [reps, setReps] = useState(params.first?.reps.toString() || '0')
const [weight, setWeight] = useState(params.first?.weight.toString() || '0')
const [unit, setUnit] = useState<string>(params.first?.unit || 'kg')
2022-10-31 04:22:08 +00:00
const [selected, setSelected] = useState(0)
const [settings, setSettings] = useState<Settings>()
2022-10-31 04:22:08 +00:00
const [counts, setCounts] = useState<CountMany[]>()
const weightRef = useRef<TextInput>(null)
const repsRef = useRef<TextInput>(null)
const unitRef = useRef<TextInput>(null)
const workouts = useMemo(() => params.plan.workouts.split(','), [params])
const navigation = useNavigation<NavigationProp<PlanPageParams>>()
2022-10-05 10:38:52 +00:00
const [selection, setSelection] = useState({
start: 0,
2022-10-31 04:05:31 +00:00
end: 0,
2022-10-31 04:22:08 +00:00
})
const refresh = useCallback(async () => {
2022-10-31 04:05:31 +00:00
const questions = workouts
.map((workout, index) => `('${workout}',${index})`)
2022-10-31 04:22:08 +00:00
.join(',')
2022-10-31 04:05:31 +00:00
const select = `
SELECT workouts.name, COUNT(sets.id) as total, sets.sets
2022-10-31 04:05:31 +00:00
FROM (select 0 as name, 0 as sequence union values ${questions}) as workouts
LEFT JOIN sets ON sets.name = workouts.name
AND sets.created LIKE STRFTIME('%Y-%m-%d%%', 'now', 'localtime')
AND NOT sets.hidden
2022-10-31 04:05:31 +00:00
GROUP BY workouts.name
ORDER BY workouts.sequence
LIMIT -1
OFFSET 1
2022-10-31 04:22:08 +00:00
`
const newCounts = await AppDataSource.manager.query(select)
2023-06-27 03:16:59 +00:00
console.log(`${StartPlan.name}.focus:`, { newCounts })
setCounts(newCounts)
2022-10-31 04:22:08 +00:00
}, [workouts])
2022-10-30 02:34:17 +00:00
2022-10-31 04:05:31 +00:00
const select = useCallback(
async (index: number, newCounts?: CountMany[]) => {
2022-10-31 04:22:08 +00:00
setSelected(index)
if (!counts && !newCounts) return
const workout = counts ? counts[index] : newCounts[index]
2023-06-27 03:16:59 +00:00
console.log(`${StartPlan.name}.next:`, { workout })
const last = await getLast(workout.name)
if (!last) return
delete last.id
2023-06-27 03:16:59 +00:00
console.log(`${StartPlan.name}.select:`, { last })
setReps(last.reps.toString())
setWeight(last.weight.toString())
setUnit(last.unit)
2022-10-31 04:05:31 +00:00
},
[counts],
2022-10-31 04:22:08 +00:00
)
2022-10-31 04:05:31 +00:00
useFocusEffect(
useCallback(() => {
2023-06-27 03:16:59 +00:00
settingsRepo.findOne({ where: {} }).then(setSettings)
refresh()
}, [refresh]),
)
2022-10-05 10:38:52 +00:00
const handleSubmit = async () => {
2023-01-04 00:24:49 +00:00
const now = await getNow()
const workout = counts[selected]
const best = await getBestSet(workout.name)
delete best.id
const newSet: GymSet = {
...best,
2022-10-05 10:38:52 +00:00
weight: +weight,
reps: +reps,
unit,
created: now,
2022-10-31 04:05:31 +00:00
hidden: false,
}
await setRepo.save(newSet)
2022-10-31 04:22:08 +00:00
await refresh()
if (
2022-10-16 01:38:01 +00:00
settings.notify &&
(+weight > best.weight || (+reps > best.reps && +weight === best.weight))
2023-06-27 03:16:59 +00:00
) {
toast('Great work King! That\'s a new record.')
}
if (!settings.alarm) return
2023-06-27 03:16:59 +00:00
const milliseconds = Number(best.minutes) * 60 * 1000 +
Number(best.seconds) * 1000
NativeModules.AlarmModule.timer(milliseconds)
2022-10-31 04:22:08 +00:00
}
2022-10-05 10:38:52 +00:00
return (
<>
<StackHeader title={params.plan.days.replace(/,/g, ', ')}>
<IconButton
2023-06-27 03:16:59 +00:00
onPress={() => navigation.navigate('EditPlan', { plan: params.plan })}
icon='edit'
/>
</StackHeader>
2023-06-27 03:16:59 +00:00
<View style={{ padding: PADDING, flex: 1, flexDirection: 'column' }}>
<View style={{ flex: 1 }}>
2022-12-29 00:57:19 +00:00
<AppInput
2023-06-27 03:16:59 +00:00
label='Reps'
keyboardType='numeric'
value={reps}
onChangeText={setReps}
onSubmitEditing={() => weightRef.current?.focus()}
selection={selection}
2023-06-27 03:16:59 +00:00
onSelectionChange={(e) => setSelection(e.nativeEvent.selection)}
innerRef={repsRef}
/>
2022-12-29 00:57:19 +00:00
<AppInput
2023-06-27 03:16:59 +00:00
label='Weight'
keyboardType='numeric'
value={weight}
onChangeText={setWeight}
onSubmitEditing={handleSubmit}
innerRef={weightRef}
blurOnSubmit
/>
{settings?.showUnit && (
2022-12-29 00:57:19 +00:00
<AppInput
2023-06-27 03:16:59 +00:00
autoCapitalize='none'
label='Unit'
value={unit}
onChangeText={setUnit}
innerRef={unitRef}
/>
)}
{counts && (
<FlatList
data={counts}
2023-06-27 03:16:59 +00:00
renderItem={(props) => (
<View>
<StartPlanItem
{...props}
onUndo={refresh}
onSelect={select}
selected={selected}
/>
<ProgressBar
progress={(props.item.total || 0) / (props.item.sets || 3)}
/>
</View>
)}
/>
)}
</View>
<Button mode='outlined' icon='save' onPress={handleSubmit}>
Save
</Button>
2022-10-05 10:38:52 +00:00
</View>
</>
2022-10-31 04:22:08 +00:00
)
2022-10-05 10:38:52 +00:00
}