Massive/StartPlan.tsx

180 lines
5.6 KiB
TypeScript
Raw Normal View History

import {
NavigationProp,
RouteProp,
useFocusEffect,
useNavigation,
useRoute,
} from '@react-navigation/native'
import {useCallback, useMemo, useRef, useState} from 'react'
2022-12-30 00:25:47 +00:00
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'
import {getBestSet, getLast} from './best.service'
2022-12-01 01:18:37 +00:00
import {PADDING} from './constants'
2022-10-31 04:22:08 +00:00
import CountMany from './count-many'
import {AppDataSource} from './data-source'
import {getNow, setRepo, settingsRepo} from './db'
import GymSet from './gym-set'
2022-10-31 04:22:08 +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'
import {toast} from './toast'
import useDark from './use-dark'
2022-10-05 10:38:52 +00:00
export default function StartPlan() {
2022-10-31 04:22:08 +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 dark = useDark()
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)
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]
console.log(`${StartPlan.name}.next:`, {workout})
const last = await getLast(workout.name)
if (!last) return
delete last.id
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(() => {
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))
)
toast("Great work King! That's a new record.")
if (!settings.alarm) return
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
color={dark ? 'white' : 'white'}
onPress={() => navigation.navigate('EditPlan', {plan: params.plan})}
icon="edit"
/>
</StackHeader>
<View style={{padding: PADDING, flex: 1, flexDirection: 'column'}}>
<View style={{flex: 1}}>
2022-12-29 00:57:19 +00:00
<AppInput
label="Reps"
keyboardType="numeric"
value={reps}
onChangeText={setReps}
onSubmitEditing={() => weightRef.current?.focus()}
selection={selection}
onSelectionChange={e => setSelection(e.nativeEvent.selection)}
innerRef={repsRef}
/>
2022-12-29 00:57:19 +00:00
<AppInput
label="Weight"
keyboardType="numeric"
value={weight}
onChangeText={setWeight}
onSubmitEditing={handleSubmit}
innerRef={weightRef}
blurOnSubmit
/>
{settings?.showUnit && (
2022-12-29 00:57:19 +00:00
<AppInput
autoCapitalize="none"
label="Unit"
value={unit}
onChangeText={setUnit}
innerRef={unitRef}
/>
)}
{counts && (
<FlatList
data={counts}
2022-10-30 02:23:22 +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="contained" 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
}