Add new record notification to plan

This commit is contained in:
Brandon Presley 2022-10-16 14:38:01 +13:00
parent 4d35d617e8
commit 2fd9635e40
1 changed files with 21 additions and 11 deletions

View File

@ -15,6 +15,7 @@ import CountMany from './count-many';
import MassiveInput from './MassiveInput'; import MassiveInput from './MassiveInput';
import {useSnackbar} from './MassiveSnack'; import {useSnackbar} from './MassiveSnack';
import {PlanPageParams} from './plan-page-params'; import {PlanPageParams} from './plan-page-params';
import Set from './set';
import {addSet, countManyToday} from './set.service'; import {addSet, countManyToday} from './set.service';
import SetForm from './SetForm'; import SetForm from './SetForm';
import {useSettings} from './use-settings'; import {useSettings} from './use-settings';
@ -29,6 +30,7 @@ export default function StartPlan() {
const {toast} = useSnackbar(); const {toast} = useSnackbar();
const [minutes, setMinutes] = useState(set.minutes); const [minutes, setMinutes] = useState(set.minutes);
const [seconds, setSeconds] = useState(set.seconds); const [seconds, setSeconds] = useState(set.seconds);
const [best, setBest] = useState<Set>();
const [selected, setSelected] = useState(0); const [selected, setSelected] = useState(0);
const {settings} = useSettings(); const {settings} = useSettings();
const [counts, setCounts] = useState<CountMany[]>(); const [counts, setCounts] = useState<CountMany[]>();
@ -54,11 +56,12 @@ export default function StartPlan() {
title: params.plan.days.replace(/,/g, ', '), title: params.plan.days.replace(/,/g, ', '),
}); });
countManyToday().then(setCounts); countManyToday().then(setCounts);
}, [navigation, params]), getBestSet(workouts[0]).then(setBest);
}, [navigation, params, workouts]),
); );
const handleSubmit = async () => { const handleSubmit = async () => {
console.log(`${SetForm.name}.handleSubmit:`, {reps, weight, unit}); console.log(`${SetForm.name}.handleSubmit:`, {reps, weight, unit, best});
await addSet({ await addSet({
name, name,
weight: +weight, weight: +weight,
@ -70,7 +73,14 @@ export default function StartPlan() {
unit, unit,
}); });
countManyToday().then(setCounts); countManyToday().then(setCounts);
toast('Added set', 3000); if (!best) toast('Added set', 3000);
else if (
settings.notify &&
(+weight > best.weight || (+reps > best.reps && +weight === best.weight))
)
toast("Great work King! That's a new record.", 3000);
else if (settings.alarm) toast('Resting...', 3000);
else toast('Added set', 3000);
if (!settings.alarm) return; if (!settings.alarm) return;
const milliseconds = Number(minutes) * 60 * 1000 + Number(seconds) * 1000; const milliseconds = Number(minutes) * 60 * 1000 + Number(seconds) * 1000;
const args = [milliseconds, !!settings.vibrate, settings.sound]; const args = [milliseconds, !!settings.vibrate, settings.sound];
@ -92,14 +102,14 @@ export default function StartPlan() {
console.log(`${StartPlan.name}.next:`, {name, workouts}); console.log(`${StartPlan.name}.next:`, {name, workouts});
const workout = workouts[index]; const workout = workouts[index];
console.log(`${StartPlan.name}.next:`, {workout}); console.log(`${StartPlan.name}.next:`, {workout});
const best = await getBestSet(workout); const newBest = await getBestSet(workout);
console.log(`${StartPlan.name}.next:`, {best}); setMinutes(newBest.minutes);
setMinutes(best.minutes); setSeconds(newBest.seconds);
setSeconds(best.seconds); setName(newBest.name);
setName(best.name); setReps(newBest.reps.toString());
setReps(best.reps.toString()); setWeight(newBest.weight.toString());
setWeight(best.weight.toString()); setUnit(newBest.unit);
setUnit(best.unit); setBest(newBest);
}, },
[name, workouts], [name, workouts],
); );