Use stack navigation for Best
This commit is contained in:
parent
0f540cc778
commit
ad907a83c3
95
BestList.tsx
Normal file
95
BestList.tsx
Normal file
|
@ -0,0 +1,95 @@
|
||||||
|
import {
|
||||||
|
NavigationProp,
|
||||||
|
useFocusEffect,
|
||||||
|
useNavigation,
|
||||||
|
} from '@react-navigation/native';
|
||||||
|
import React, {useCallback, useContext, useEffect, useState} from 'react';
|
||||||
|
import {FlatList, StyleSheet, View} from 'react-native';
|
||||||
|
import {List, Searchbar} from 'react-native-paper';
|
||||||
|
import {DatabaseContext} from './App';
|
||||||
|
import Best from './best';
|
||||||
|
import {BestPageParams} from './BestPage';
|
||||||
|
|
||||||
|
export default function BestList() {
|
||||||
|
const [bests, setBests] = useState<Best[]>([]);
|
||||||
|
const [search, setSearch] = useState('');
|
||||||
|
const [refreshing, setRefresing] = useState(false);
|
||||||
|
const db = useContext(DatabaseContext);
|
||||||
|
const navigation = useNavigation<NavigationProp<BestPageParams>>();
|
||||||
|
|
||||||
|
const refresh = useCallback(async () => {
|
||||||
|
const bestWeight = `
|
||||||
|
SELECT name, reps, unit, MAX(weight) AS weight
|
||||||
|
FROM sets
|
||||||
|
WHERE name LIKE ?
|
||||||
|
GROUP BY name;
|
||||||
|
`;
|
||||||
|
const bestReps = `
|
||||||
|
SELECT name, MAX(reps) as reps, unit, weight
|
||||||
|
FROM sets
|
||||||
|
WHERE name = ?
|
||||||
|
AND weight = ?
|
||||||
|
GROUP BY name;
|
||||||
|
`;
|
||||||
|
const [weight] = await db.executeSql(bestWeight, [`%${search}%`]);
|
||||||
|
if (!weight) return setBests([]);
|
||||||
|
let newBest: Best[] = [];
|
||||||
|
for (let i = 0; i < weight.rows.length; i++) {
|
||||||
|
const [reps] = await db.executeSql(bestReps, [
|
||||||
|
weight.rows.item(i).name,
|
||||||
|
weight.rows.item(i).weight,
|
||||||
|
]);
|
||||||
|
newBest = newBest.concat(reps.rows.raw());
|
||||||
|
}
|
||||||
|
setBests(newBest);
|
||||||
|
}, [search, db]);
|
||||||
|
|
||||||
|
useFocusEffect(
|
||||||
|
useCallback(() => {
|
||||||
|
refresh();
|
||||||
|
}, [refresh]),
|
||||||
|
);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
refresh();
|
||||||
|
}, [search, refresh]);
|
||||||
|
|
||||||
|
const renderItem = ({item}: {item: Best}) => (
|
||||||
|
<List.Item
|
||||||
|
key={item.name}
|
||||||
|
title={item.name}
|
||||||
|
description={`${item.reps} x ${item.weight}${item.unit}`}
|
||||||
|
onPress={() => navigation.navigate('ViewBest', {best: item})}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<View style={styles.container}>
|
||||||
|
<Searchbar placeholder="Search" value={search} onChangeText={setSearch} />
|
||||||
|
<FlatList
|
||||||
|
ListEmptyComponent={
|
||||||
|
<List.Item
|
||||||
|
title="No exercises yet"
|
||||||
|
description="Once sets have been added, Exercises list your personal bests."
|
||||||
|
/>
|
||||||
|
}
|
||||||
|
refreshing={refreshing}
|
||||||
|
onRefresh={async () => {
|
||||||
|
setRefresing(true);
|
||||||
|
await refresh();
|
||||||
|
setRefresing(false);
|
||||||
|
}}
|
||||||
|
renderItem={renderItem}
|
||||||
|
data={bests}
|
||||||
|
/>
|
||||||
|
</View>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const styles = StyleSheet.create({
|
||||||
|
container: {
|
||||||
|
padding: 10,
|
||||||
|
flexGrow: 1,
|
||||||
|
paddingBottom: '10%',
|
||||||
|
},
|
||||||
|
});
|
108
BestPage.tsx
108
BestPage.tsx
|
@ -1,86 +1,42 @@
|
||||||
import React, {useCallback, useContext, useEffect, useState} from 'react';
|
import {DrawerNavigationProp} from '@react-navigation/drawer';
|
||||||
import {FlatList, StyleSheet, View} from 'react-native';
|
import {useNavigation} from '@react-navigation/native';
|
||||||
import {List, Searchbar} from 'react-native-paper';
|
import {createStackNavigator} from '@react-navigation/stack';
|
||||||
import {DatabaseContext} from './App';
|
import React from 'react';
|
||||||
|
import {IconButton} from 'react-native-paper';
|
||||||
|
import {DrawerParamList} from './App';
|
||||||
import Best from './best';
|
import Best from './best';
|
||||||
|
import BestList from './BestList';
|
||||||
import ViewBest from './ViewBest';
|
import ViewBest from './ViewBest';
|
||||||
|
|
||||||
|
const Stack = createStackNavigator<BestPageParams>();
|
||||||
|
export type BestPageParams = {
|
||||||
|
BestList: {};
|
||||||
|
ViewBest: {
|
||||||
|
best: Best;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
export default function BestPage() {
|
export default function BestPage() {
|
||||||
const [bests, setBests] = useState<Best[]>([]);
|
const navigation = useNavigation<DrawerNavigationProp<DrawerParamList>>();
|
||||||
const [search, setSearch] = useState('');
|
|
||||||
const [refreshing, setRefresing] = useState(false);
|
|
||||||
const [best, setBest] = useState<Best>();
|
|
||||||
const db = useContext(DatabaseContext);
|
|
||||||
|
|
||||||
const refresh = useCallback(async () => {
|
|
||||||
const bestWeight = `
|
|
||||||
SELECT name, reps, unit, MAX(weight) AS weight
|
|
||||||
FROM sets
|
|
||||||
WHERE name LIKE ?
|
|
||||||
GROUP BY name;
|
|
||||||
`;
|
|
||||||
const bestReps = `
|
|
||||||
SELECT name, MAX(reps) as reps, unit, weight
|
|
||||||
FROM sets
|
|
||||||
WHERE name = ?
|
|
||||||
AND weight = ?
|
|
||||||
GROUP BY name;
|
|
||||||
`;
|
|
||||||
const [weight] = await db.executeSql(bestWeight, [`%${search}%`]);
|
|
||||||
if (!weight) return setBests([]);
|
|
||||||
let newBest: Best[] = [];
|
|
||||||
for (let i = 0; i < weight.rows.length; i++) {
|
|
||||||
const [reps] = await db.executeSql(bestReps, [
|
|
||||||
weight.rows.item(i).name,
|
|
||||||
weight.rows.item(i).weight,
|
|
||||||
]);
|
|
||||||
newBest = newBest.concat(reps.rows.raw());
|
|
||||||
}
|
|
||||||
setBests(newBest);
|
|
||||||
}, [search, db]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
refresh();
|
|
||||||
}, [search, refresh]);
|
|
||||||
|
|
||||||
const renderItem = ({item}: {item: Best}) => (
|
|
||||||
<List.Item
|
|
||||||
key={item.name}
|
|
||||||
title={item.name}
|
|
||||||
description={`${item.reps} x ${item.weight}${item.unit}`}
|
|
||||||
onPress={() => setBest(item)}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<View style={styles.container}>
|
<Stack.Navigator
|
||||||
<Searchbar placeholder="Search" value={search} onChangeText={setSearch} />
|
screenOptions={{headerShown: false, animationEnabled: false}}>
|
||||||
<FlatList
|
<Stack.Screen name="BestList" component={BestList} />
|
||||||
ListEmptyComponent={
|
<Stack.Screen
|
||||||
<List.Item
|
name="ViewBest"
|
||||||
title="No exercises yet"
|
component={ViewBest}
|
||||||
description="Once sets have been added, Exercises list your personal bests."
|
listeners={{
|
||||||
/>
|
beforeRemove: () => {
|
||||||
}
|
navigation.setOptions({
|
||||||
refreshing={refreshing}
|
headerLeft: () => (
|
||||||
onRefresh={async () => {
|
<IconButton icon="menu" onPress={navigation.openDrawer} />
|
||||||
setRefresing(true);
|
),
|
||||||
await refresh();
|
title: 'Home',
|
||||||
setRefresing(false);
|
});
|
||||||
|
},
|
||||||
}}
|
}}
|
||||||
renderItem={renderItem}
|
|
||||||
data={bests}
|
|
||||||
/>
|
/>
|
||||||
|
</Stack.Navigator>
|
||||||
<ViewBest setBest={setBest} best={best} />
|
|
||||||
</View>
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const styles = StyleSheet.create({
|
|
||||||
container: {
|
|
||||||
padding: 10,
|
|
||||||
flexGrow: 1,
|
|
||||||
paddingBottom: '10%',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
|
@ -37,7 +37,6 @@ export default function PlanList() {
|
||||||
);
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!search) return;
|
|
||||||
refresh();
|
refresh();
|
||||||
}, [search, refresh]);
|
}, [search, refresh]);
|
||||||
|
|
||||||
|
|
|
@ -51,7 +51,6 @@ export default function SetList() {
|
||||||
}, [setRefreshing, refresh]);
|
}, [setRefreshing, refresh]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!search) return;
|
|
||||||
refresh();
|
refresh();
|
||||||
}, [search, refresh]);
|
}, [search, refresh]);
|
||||||
|
|
||||||
|
|
104
ViewBest.tsx
104
ViewBest.tsx
|
@ -1,21 +1,35 @@
|
||||||
|
import {
|
||||||
|
RouteProp,
|
||||||
|
useFocusEffect,
|
||||||
|
useNavigation,
|
||||||
|
useRoute,
|
||||||
|
} from '@react-navigation/native';
|
||||||
import * as shape from 'd3-shape';
|
import * as shape from 'd3-shape';
|
||||||
import React, {useContext, useEffect, useState} from 'react';
|
import React, {useCallback, useContext, useEffect, useState} from 'react';
|
||||||
import {View} from 'react-native';
|
import {Text, View} from 'react-native';
|
||||||
import {Button, Dialog, Portal} from 'react-native-paper';
|
import {IconButton} from 'react-native-paper';
|
||||||
import {Grid, LineChart, YAxis} from 'react-native-svg-charts';
|
import {Grid, LineChart, XAxis, YAxis} from 'react-native-svg-charts';
|
||||||
import {DatabaseContext} from './App';
|
import {DatabaseContext} from './App';
|
||||||
import Best from './best';
|
import {BestPageParams} from './BestPage';
|
||||||
|
import Set from './set';
|
||||||
|
import {formatMonth} from './time';
|
||||||
|
|
||||||
export default function ViewBest({
|
export default function ViewBest() {
|
||||||
best,
|
const {params} = useRoute<RouteProp<BestPageParams, 'ViewBest'>>();
|
||||||
setBest,
|
const [sets, setSets] = useState<Set[]>([]);
|
||||||
}: {
|
|
||||||
best?: Best;
|
|
||||||
setBest: (best?: Best) => void;
|
|
||||||
}) {
|
|
||||||
const [data, setData] = useState<number[]>([]);
|
|
||||||
const [unit, setUnit] = useState<string>();
|
|
||||||
const db = useContext(DatabaseContext);
|
const db = useContext(DatabaseContext);
|
||||||
|
const navigation = useNavigation();
|
||||||
|
|
||||||
|
useFocusEffect(
|
||||||
|
useCallback(() => {
|
||||||
|
navigation.getParent()?.setOptions({
|
||||||
|
headerLeft: () => (
|
||||||
|
<IconButton icon="arrow-back" onPress={() => navigation.goBack()} />
|
||||||
|
),
|
||||||
|
title: params.best.name,
|
||||||
|
});
|
||||||
|
}, [navigation, params.best.name]),
|
||||||
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const selectBest = `
|
const selectBest = `
|
||||||
|
@ -25,52 +39,46 @@ export default function ViewBest({
|
||||||
GROUP BY name, STRFTIME('%Y-%m-%d', created)
|
GROUP BY name, STRFTIME('%Y-%m-%d', created)
|
||||||
`;
|
`;
|
||||||
const refresh = async () => {
|
const refresh = async () => {
|
||||||
const [result] = await db.executeSql(selectBest, [best?.name]);
|
const [result] = await db.executeSql(selectBest, [params.best.name]);
|
||||||
if (result.rows.length === 0) return;
|
if (result.rows.length === 0) return;
|
||||||
console.log(`${ViewBest.name}.${refresh.name}:`, result.rows.raw());
|
console.log(`${ViewBest.name}.${refresh.name}:`, result.rows.raw());
|
||||||
setData(result.rows.raw().map(row => row.weight));
|
setSets(result.rows.raw());
|
||||||
setUnit(result.rows.item(0).unit);
|
|
||||||
};
|
};
|
||||||
refresh();
|
refresh();
|
||||||
}, [best, db]);
|
}, [params.best.name, db]);
|
||||||
|
|
||||||
const contentInset = {top: 20, bottom: 20};
|
|
||||||
|
|
||||||
|
const axesSvg = {fontSize: 10, fill: 'grey'};
|
||||||
|
const verticalContentInset = {top: 10, bottom: 10};
|
||||||
|
const xAxisHeight = 30;
|
||||||
return (
|
return (
|
||||||
<Portal>
|
<View style={{padding: 10}}>
|
||||||
<Dialog visible={!!best} onDismiss={() => setBest(undefined)}>
|
<Text>Best weight per day</Text>
|
||||||
<Dialog.Title>{best?.name}</Dialog.Title>
|
<View style={{height: 200, padding: 20, flexDirection: 'row'}}>
|
||||||
<Dialog.Content>
|
|
||||||
<View style={{height: 200, flexDirection: 'row'}}>
|
|
||||||
<YAxis
|
<YAxis
|
||||||
data={data}
|
data={sets.map(set => set.weight)}
|
||||||
contentInset={contentInset}
|
style={{marginBottom: xAxisHeight}}
|
||||||
svg={{
|
contentInset={verticalContentInset}
|
||||||
fill: 'grey',
|
svg={axesSvg}
|
||||||
fontSize: 10,
|
formatLabel={value => `${value}${sets[0].unit}`}
|
||||||
}}
|
|
||||||
numberOfTicks={10}
|
|
||||||
formatLabel={value => `${value}${unit}`}
|
|
||||||
/>
|
/>
|
||||||
|
<View style={{flex: 1, marginLeft: 10}}>
|
||||||
<LineChart
|
<LineChart
|
||||||
style={{flex: 1, marginLeft: 16}}
|
style={{flex: 1}}
|
||||||
data={data}
|
data={sets.map(set => set.weight)}
|
||||||
svg={{stroke: 'rgb(134, 65, 244)'}}
|
contentInset={verticalContentInset}
|
||||||
curve={shape.curveNatural}
|
curve={shape.curveNatural}
|
||||||
contentInset={contentInset}>
|
svg={{stroke: 'rgb(134, 65, 244)'}}>
|
||||||
<Grid />
|
<Grid />
|
||||||
</LineChart>
|
</LineChart>
|
||||||
|
<XAxis
|
||||||
|
style={{marginHorizontal: -10, height: xAxisHeight}}
|
||||||
|
data={sets}
|
||||||
|
formatLabel={(_value, index) => formatMonth(sets[index].created)}
|
||||||
|
contentInset={{left: 10, right: 10}}
|
||||||
|
svg={axesSvg}
|
||||||
|
/>
|
||||||
|
</View>
|
||||||
|
</View>
|
||||||
</View>
|
</View>
|
||||||
</Dialog.Content>
|
|
||||||
<Dialog.Actions>
|
|
||||||
<Button
|
|
||||||
mode="contained"
|
|
||||||
icon="close"
|
|
||||||
onPress={() => setBest(undefined)}>
|
|
||||||
Close
|
|
||||||
</Button>
|
|
||||||
</Dialog.Actions>
|
|
||||||
</Dialog>
|
|
||||||
</Portal>
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
7
time.ts
7
time.ts
|
@ -37,3 +37,10 @@ export function format(date: Date) {
|
||||||
].join(':') + (isPM ? ' pm' : 'am');
|
].join(':') + (isPM ? ' pm' : 'am');
|
||||||
return `${day} ${dd} ${mm}, ${time}`;
|
return `${day} ${dd} ${mm}, ${time}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function formatMonth(iso: string) {
|
||||||
|
const date = new Date(iso);
|
||||||
|
const dd = date.getDate().toString();
|
||||||
|
const mm = (date.getMonth() + 1).toString();
|
||||||
|
return `${dd}/${mm}`;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user