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.
This commit is contained in:
Brandon Presley 2022-10-21 18:39:06 +13:00
parent 4ba86be8af
commit b95024abe0
2 changed files with 18 additions and 12 deletions

View File

@ -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 (

View File

@ -63,6 +63,17 @@ interface PageParams {
format?: string;
}
export const getSet = async (name: string): Promise<Set> => {
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,