From b95024abe0faf7faaa37cb121dfa91cd420e8285 Mon Sep 17 00:00:00 2001 From: Brandon Presley Date: Fri, 21 Oct 2022 18:39:06 +1300 Subject: [PATCH] Fix rest timers for newly edited Workouts Previously if you were to add a new workout, then add a set for that workout immediately afterwards, the rest timers would be the default 3:30. Now, they are the actual value set when creating the workout. --- EditSet.tsx | 19 +++++++------------ set.service.ts | 11 +++++++++++ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/EditSet.tsx b/EditSet.tsx index 9e9edb8..ce31341 100644 --- a/EditSet.tsx +++ b/EditSet.tsx @@ -11,9 +11,8 @@ import {PADDING} from './constants'; import {HomePageParams} from './home-page-params'; import {useSnackbar} from './MassiveSnack'; import Set from './set'; -import {addSet, updateSet} from './set.service'; +import {addSet, getSet, updateSet} from './set.service'; import SetForm from './SetForm'; -import {getSettings, updateSettings} from './settings.service'; import {useSettings} from './use-settings'; export default function EditSet() { @@ -21,7 +20,7 @@ export default function EditSet() { const {set} = params; const navigation = useNavigation(); const {toast} = useSnackbar(); - const {settings, setSettings} = useSettings(); + const {settings} = useSettings(); useFocusEffect( useCallback(() => { @@ -39,21 +38,17 @@ export default function EditSet() { ); const startTimer = useCallback( - async (value: Set) => { + async (name: string) => { if (!settings.alarm) return; - const milliseconds = - Number(value.minutes) * 60 * 1000 + Number(value.seconds) * 1000; + const {minutes, seconds} = await getSet(name); + const milliseconds = (minutes ?? 3) * 60 * 1000 + (seconds ?? 0) * 1000; NativeModules.AlarmModule.timer( milliseconds, !!settings.vibrate, settings.sound, ); - const next = new Date(); - next.setTime(next.getTime() + milliseconds); - await updateSettings({...settings, nextAlarm: next.toISOString()}); - setSettings(await getSettings()); }, - [settings, setSettings], + [settings], ); const update = useCallback( @@ -68,7 +63,7 @@ export default function EditSet() { const add = useCallback( async (value: Set) => { console.log(`${EditSet.name}.add`, {set: value}); - startTimer(value); + startTimer(value.name); await addSet(value); if (!settings.notify) return navigation.goBack(); if ( diff --git a/set.service.ts b/set.service.ts index 2114e08..bdd5d66 100644 --- a/set.service.ts +++ b/set.service.ts @@ -63,6 +63,17 @@ interface PageParams { format?: string; } +export const getSet = async (name: string): Promise => { + const select = ` + SELECT * + FROM sets + WHERE name = ? + LIMIT 1 + `; + const [result] = await db.executeSql(select, [name]); + return result.rows.item(0); +}; + export const getSets = async ({ search, limit,