Add steps to workouts

Closes #15
This commit is contained in:
Brandon Presley 2022-09-18 18:08:09 +12:00
parent 8499795c97
commit cb446be7b0
7 changed files with 83 additions and 38 deletions

View File

@ -18,7 +18,7 @@ export default function BestList() {
const refresh = useCallback(async () => {
const weights = await getBestWeights(search);
console.log(BestList.name, {weights});
console.log(`${BestList.name}.refresh:`, {length: weights.length});
let newBest: Set[] = [];
for (const set of weights) {
const reps = await getBestReps(set.name, set.weight);

View File

@ -5,7 +5,7 @@ import {
useRoute,
} from '@react-navigation/native';
import React, {useCallback, useState} from 'react';
import {Pressable, ScrollView} from 'react-native';
import {Pressable, ScrollView, View} from 'react-native';
import DocumentPicker from 'react-native-document-picker';
import {Button, Card, IconButton} from 'react-native-paper';
import {MARGIN, PADDING} from './constants';
@ -13,10 +13,13 @@ import MassiveInput from './MassiveInput';
import {updateWorkouts} from './plan.service';
import Set from './set';
import {addSet, getSets, updateSetImage, updateSetName} from './set.service';
import Workout from './workout';
import {addWorkout, getWorkout, updateSteps} from './workout.service';
import {WorkoutsPageParams} from './WorkoutsPage';
export default function EditWorkout() {
const [name, setName] = useState('');
const [steps, setSteps] = useState('');
const [uri, setUri] = useState<string>();
const {params} = useRoute<RouteProp<WorkoutsPageParams, 'EditWorkout'>>();
const navigation = useNavigation();
@ -30,10 +33,11 @@ export default function EditWorkout() {
headerRight: null,
title: params.value.name ? params.value.name : 'New workout',
});
if (params.value.name)
getSets({search: params.value.name, limit: 1, offset: 0}).then(sets =>
setUri(sets[0]?.image),
);
if (!params.value.name) return;
getSets({search: params.value.name, limit: 1, offset: 0}).then(sets =>
setUri(sets[0]?.image),
);
getWorkout(params.value.name).then(workout => setSteps(workout.steps));
}, [navigation, params.value.name]),
);
@ -42,19 +46,22 @@ export default function EditWorkout() {
params: params.value.name,
name,
uri,
steps,
});
if (name) {
await updateSetName(params.value.name, name);
await updateWorkouts(params.value.name, name);
}
if (uri) await updateSetImage(params.value.name, uri);
if (steps) await updateSteps(params.value.name, steps);
navigation.goBack();
}, [navigation, params.value.name, name, uri]);
}, [navigation, params.value.name, name, uri, steps]);
const add = useCallback(async () => {
await addSet({name, reps: 0, weight: 0, hidden: true} as Set);
await addSet({name, reps: 0, weight: 0, hidden: true, image: uri} as Set);
await addWorkout({name, steps} as Workout);
navigation.goBack();
}, [navigation, name]);
}, [navigation, name, steps, uri]);
const save = useCallback(async () => {
if (params.value.name) return update();
@ -70,31 +77,36 @@ export default function EditWorkout() {
}, []);
return (
<ScrollView style={{padding: PADDING}}>
<MassiveInput
label={params.value.name}
value={name}
onChangeText={setName}
/>
{uri ? (
<Pressable style={{marginBottom: MARGIN}} onPress={changeImage}>
<Card.Cover source={{uri}} />
</Pressable>
) : (
<Button
style={{marginBottom: MARGIN}}
onPress={changeImage}
icon="image">
Image
</Button>
)}
<Button
disabled={!name && !!params.value.name && !uri}
mode="contained"
icon="save"
onPress={save}>
<View style={{padding: PADDING}}>
<ScrollView style={{height: '90%'}}>
{uri ? (
<Pressable style={{marginBottom: MARGIN}} onPress={changeImage}>
<Card.Cover source={{uri}} />
</Pressable>
) : (
<Button
style={{marginBottom: MARGIN}}
onPress={changeImage}
icon="image">
Image
</Button>
)}
<MassiveInput
label={params.value.name || 'Name'}
value={name}
onChangeText={setName}
/>
<MassiveInput
selectTextOnFocus={false}
value={steps}
onChangeText={setSteps}
label="Steps"
multiline
/>
</ScrollView>
<Button mode="contained" icon="save" onPress={save}>
Save
</Button>
</ScrollView>
</View>
);
}

View File

@ -7,7 +7,7 @@ import React, {useCallback, useEffect, useState} from 'react';
import {FlatList} from 'react-native';
import {List} from 'react-native-paper';
import Page from './Page';
import {getWorkouts} from './set.service';
import {getDistinctSets} from './set.service';
import SetList from './SetList';
import Workout from './workout';
import WorkoutItem from './WorkoutItem';
@ -23,7 +23,7 @@ export default function WorkoutList() {
const navigation = useNavigation<NavigationProp<WorkoutsPageParams>>();
const refresh = useCallback(async () => {
const newWorkouts = await getWorkouts({
const newWorkouts = await getDistinctSets({
search: `%${search}%`,
limit,
offset: 0,
@ -59,7 +59,7 @@ export default function WorkoutList() {
newOffset,
search,
});
const newWorkouts = await getWorkouts({
const newWorkouts = await getDistinctSets({
search: `%${search}%`,
limit,
offset: newOffset,
@ -73,7 +73,7 @@ export default function WorkoutList() {
const onAdd = useCallback(async () => {
navigation.navigate('EditWorkout', {
value: {name: '', sets: 3, image: ''},
value: {name: '', sets: 3, image: '', steps: ''},
});
}, [navigation]);

5
db.ts
View File

@ -71,6 +71,10 @@ const insertSettings = `
INSERT INTO settings(minutes) VALUES(3);
`;
const addSteps = `
ALTER TABLE workouts ADD COLUMN steps TEXT NULL;
`;
export let db: SQLiteDatabase;
export const migrations = async () => {
@ -84,6 +88,7 @@ export const migrations = async () => {
await db.executeSql(addNotify).catch(() => null);
await db.executeSql(addImage).catch(() => null);
await db.executeSql(addImages).catch(() => null);
await db.executeSql(addSteps).catch(() => null);
const [result] = await db.executeSql(selectSettings);
if (result.rows.length === 0) await db.executeSql(insertSettings);
};

View File

@ -106,7 +106,7 @@ export const getNames = async (): Promise<string[]> => {
return values.map(value => value.name);
};
export const getWorkouts = async ({
export const getDistinctSets = async ({
search,
limit,
offset,

27
workout.service.ts Normal file
View File

@ -0,0 +1,27 @@
import {db} from './db';
import Workout from './workout';
export const getWorkout = async (name: string): Promise<Workout> => {
const select = `
SELECT * FROM workouts
WHERE workouts.name = ?
LIMIT 1
`;
const [result] = await db.executeSql(select, [name]);
return result.rows.raw()[0];
};
export const updateSteps = (name: string, steps: string): Promise<unknown> => {
const select = `
UPDATE workouts SET steps = ? WHERE name = ?
`;
return db.executeSql(select, [steps, name]);
};
export const addWorkout = (value: Workout) => {
const insert = `
INSERT INTO workouts(name, steps)
VALUES (?, ?)
`;
return db.executeSql(insert, [value.name, value.steps]);
};

View File

@ -2,4 +2,5 @@ export default interface Workout {
name: string;
sets: number;
image: string;
steps: string;
}