2022-11-16 04:05:49 +00:00
|
|
|
import {RouteProp, useFocusEffect, useRoute} from '@react-navigation/native'
|
2022-12-03 09:13:35 +00:00
|
|
|
import {useCallback, useMemo, useRef, useState} from 'react'
|
2022-12-30 00:25:47 +00:00
|
|
|
import {FlatList, NativeModules, TextInput, View} from 'react-native'
|
2022-12-03 09:29:45 +00:00
|
|
|
import {Button, ProgressBar} from 'react-native-paper'
|
2023-01-01 02:20:56 +00:00
|
|
|
import AppInput from './AppInput'
|
2022-10-31 04:22:08 +00:00
|
|
|
import {getBestSet} 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'
|
2022-11-01 03:50:03 +00:00
|
|
|
import {getNow, setRepo, settingsRepo} from './db'
|
2022-12-17 03:44:11 +00:00
|
|
|
import GymSet from './gym-set'
|
2022-10-31 04:22:08 +00:00
|
|
|
import {PlanPageParams} from './plan-page-params'
|
2022-11-01 03:50:03 +00:00
|
|
|
import Settings from './settings'
|
2022-10-31 04:22:08 +00:00
|
|
|
import StackHeader from './StackHeader'
|
|
|
|
import StartPlanItem from './StartPlanItem'
|
2022-11-01 02:55:37 +00:00
|
|
|
import {toast} from './toast'
|
2022-10-05 10:38:52 +00:00
|
|
|
|
2022-10-12 01:07:48 +00:00
|
|
|
export default function StartPlan() {
|
2022-10-31 04:22:08 +00:00
|
|
|
const {params} = useRoute<RouteProp<PlanPageParams, 'StartPlan'>>()
|
2022-12-03 09:13:35 +00:00
|
|
|
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)
|
2022-11-01 03:50:03 +00:00
|
|
|
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])
|
2022-10-05 10:38:52 +00:00
|
|
|
|
2022-10-12 01:51:30 +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
|
|
|
})
|
2022-10-12 01:51:30 +00:00
|
|
|
|
2022-11-10 02:15:27 +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 = `
|
2022-11-12 05:19:39 +00:00
|
|
|
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
|
2022-10-31 00:20:36 +00:00
|
|
|
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
|
|
|
`
|
2022-11-10 02:15:27 +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 newBest = await getBestSet(workout.name)
|
2022-12-17 03:44:11 +00:00
|
|
|
if (!newBest) return
|
2022-11-02 01:41:30 +00:00
|
|
|
delete newBest.id
|
2022-10-31 04:22:08 +00:00
|
|
|
console.log(`${StartPlan.name}.next:`, {newBest})
|
|
|
|
setReps(newBest.reps.toString())
|
|
|
|
setWeight(newBest.weight.toString())
|
|
|
|
setUnit(newBest.unit)
|
2022-10-31 04:05:31 +00:00
|
|
|
},
|
2022-12-17 03:44:11 +00:00
|
|
|
[counts],
|
2022-10-31 04:22:08 +00:00
|
|
|
)
|
2022-10-31 04:05:31 +00:00
|
|
|
|
2022-11-16 04:05:49 +00:00
|
|
|
useFocusEffect(
|
|
|
|
useCallback(() => {
|
|
|
|
settingsRepo.findOne({where: {}}).then(setSettings)
|
2022-12-03 09:13:35 +00:00
|
|
|
refresh()
|
|
|
|
}, [refresh]),
|
2022-11-16 04:05:49 +00:00
|
|
|
)
|
|
|
|
|
2022-10-05 10:38:52 +00:00
|
|
|
const handleSubmit = async () => {
|
2023-01-04 00:24:49 +00:00
|
|
|
const now = await getNow()
|
2022-12-17 03:44:11 +00:00
|
|
|
const workout = counts[selected]
|
|
|
|
const best = await getBestSet(workout.name)
|
|
|
|
delete best.id
|
2022-12-03 09:13:35 +00:00
|
|
|
const newSet: GymSet = {
|
2022-11-02 01:41:30 +00:00
|
|
|
...best,
|
2022-10-05 10:38:52 +00:00
|
|
|
weight: +weight,
|
|
|
|
reps: +reps,
|
|
|
|
unit,
|
2022-10-31 00:20:36 +00:00
|
|
|
created: now,
|
2022-10-31 04:05:31 +00:00
|
|
|
hidden: false,
|
2022-12-03 09:13:35 +00:00
|
|
|
}
|
|
|
|
await setRepo.save(newSet)
|
2022-10-31 04:22:08 +00:00
|
|
|
await refresh()
|
2022-10-16 03:08:38 +00:00
|
|
|
if (
|
2022-10-16 01:38:01 +00:00
|
|
|
settings.notify &&
|
|
|
|
(+weight > best.weight || (+reps > best.reps && +weight === best.weight))
|
|
|
|
)
|
2022-11-01 02:55:37 +00:00
|
|
|
toast("Great work King! That's a new record.")
|
2022-11-12 05:22:58 +00:00
|
|
|
if (!settings.alarm) return
|
2022-11-02 01:41:30 +00:00
|
|
|
const milliseconds =
|
|
|
|
Number(best.minutes) * 60 * 1000 + Number(best.seconds) * 1000
|
2023-01-02 05:54:35 +00:00
|
|
|
NativeModules.AlarmModule.timer(milliseconds)
|
2022-10-31 04:22:08 +00:00
|
|
|
}
|
2022-10-05 10:38:52 +00:00
|
|
|
|
|
|
|
return (
|
2022-10-23 06:13:58 +00:00
|
|
|
<>
|
|
|
|
<StackHeader title={params.plan.days.replace(/,/g, ', ')} />
|
|
|
|
<View style={{padding: PADDING, flex: 1, flexDirection: 'column'}}>
|
|
|
|
<View style={{flex: 1}}>
|
2022-12-29 00:57:19 +00:00
|
|
|
<AppInput
|
2022-10-23 06:13:58 +00:00
|
|
|
label="Reps"
|
|
|
|
keyboardType="numeric"
|
|
|
|
value={reps}
|
|
|
|
onChangeText={setReps}
|
|
|
|
onSubmitEditing={() => weightRef.current?.focus()}
|
|
|
|
selection={selection}
|
|
|
|
onSelectionChange={e => setSelection(e.nativeEvent.selection)}
|
|
|
|
innerRef={repsRef}
|
2022-10-12 01:07:48 +00:00
|
|
|
/>
|
2022-12-29 00:57:19 +00:00
|
|
|
<AppInput
|
2022-10-23 06:13:58 +00:00
|
|
|
label="Weight"
|
|
|
|
keyboardType="numeric"
|
|
|
|
value={weight}
|
|
|
|
onChangeText={setWeight}
|
|
|
|
onSubmitEditing={handleSubmit}
|
|
|
|
innerRef={weightRef}
|
|
|
|
blurOnSubmit
|
2022-10-16 04:20:13 +00:00
|
|
|
/>
|
2022-11-01 03:50:03 +00:00
|
|
|
{settings?.showUnit && (
|
2022-12-29 00:57:19 +00:00
|
|
|
<AppInput
|
2022-10-23 06:13:58 +00:00
|
|
|
autoCapitalize="none"
|
|
|
|
label="Unit"
|
|
|
|
value={unit}
|
2022-12-08 01:50:10 +00:00
|
|
|
onChangeText={setUnit}
|
2022-10-23 06:13:58 +00:00
|
|
|
innerRef={unitRef}
|
|
|
|
/>
|
|
|
|
)}
|
2022-10-30 01:27:22 +00:00
|
|
|
{counts && (
|
2022-10-23 06:13:58 +00:00
|
|
|
<FlatList
|
2022-10-30 01:27:22 +00:00
|
|
|
data={counts}
|
2022-10-30 02:23:22 +00:00
|
|
|
renderItem={props => (
|
2022-12-03 09:29:45 +00:00
|
|
|
<View>
|
|
|
|
<StartPlanItem
|
|
|
|
{...props}
|
|
|
|
onUndo={refresh}
|
|
|
|
onSelect={select}
|
|
|
|
selected={selected}
|
|
|
|
/>
|
|
|
|
<ProgressBar
|
|
|
|
progress={(props.item.total || 0) / (props.item.sets || 3)}
|
|
|
|
/>
|
|
|
|
</View>
|
2022-10-23 06:13:58 +00:00
|
|
|
)}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</View>
|
2022-11-30 05:14:34 +00:00
|
|
|
<Button mode="contained" icon="save" onPress={handleSubmit}>
|
2022-10-23 06:13:58 +00:00
|
|
|
Save
|
|
|
|
</Button>
|
2022-10-05 10:38:52 +00:00
|
|
|
</View>
|
2022-10-23 06:13:58 +00:00
|
|
|
</>
|
2022-10-31 04:22:08 +00:00
|
|
|
)
|
2022-10-05 10:38:52 +00:00
|
|
|
}
|