Use stack navigation for Best

This commit is contained in:
Brandon Presley 2022-07-11 13:00:17 +12:00
parent 0f540cc778
commit ad907a83c3
6 changed files with 197 additions and 133 deletions

95
BestList.tsx Normal file
View 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%',
},
});

View File

@ -1,86 +1,42 @@
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 {DrawerNavigationProp} from '@react-navigation/drawer';
import {useNavigation} from '@react-navigation/native';
import {createStackNavigator} from '@react-navigation/stack';
import React from 'react';
import {IconButton} from 'react-native-paper';
import {DrawerParamList} from './App';
import Best from './best';
import BestList from './BestList';
import ViewBest from './ViewBest';
const Stack = createStackNavigator<BestPageParams>();
export type BestPageParams = {
BestList: {};
ViewBest: {
best: Best;
};
};
export default function BestPage() {
const [bests, setBests] = useState<Best[]>([]);
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)}
/>
);
const navigation = useNavigation<DrawerNavigationProp<DrawerParamList>>();
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);
<Stack.Navigator
screenOptions={{headerShown: false, animationEnabled: false}}>
<Stack.Screen name="BestList" component={BestList} />
<Stack.Screen
name="ViewBest"
component={ViewBest}
listeners={{
beforeRemove: () => {
navigation.setOptions({
headerLeft: () => (
<IconButton icon="menu" onPress={navigation.openDrawer} />
),
title: 'Home',
});
},
}}
renderItem={renderItem}
data={bests}
/>
<ViewBest setBest={setBest} best={best} />
</View>
</Stack.Navigator>
);
}
const styles = StyleSheet.create({
container: {
padding: 10,
flexGrow: 1,
paddingBottom: '10%',
},
});

View File

@ -37,7 +37,6 @@ export default function PlanList() {
);
useEffect(() => {
if (!search) return;
refresh();
}, [search, refresh]);

View File

@ -51,7 +51,6 @@ export default function SetList() {
}, [setRefreshing, refresh]);
useEffect(() => {
if (!search) return;
refresh();
}, [search, refresh]);

View File

@ -1,21 +1,35 @@
import {
RouteProp,
useFocusEffect,
useNavigation,
useRoute,
} from '@react-navigation/native';
import * as shape from 'd3-shape';
import React, {useContext, useEffect, useState} from 'react';
import {View} from 'react-native';
import {Button, Dialog, Portal} from 'react-native-paper';
import {Grid, LineChart, YAxis} from 'react-native-svg-charts';
import React, {useCallback, useContext, useEffect, useState} from 'react';
import {Text, View} from 'react-native';
import {IconButton} from 'react-native-paper';
import {Grid, LineChart, XAxis, YAxis} from 'react-native-svg-charts';
import {DatabaseContext} from './App';
import Best from './best';
import {BestPageParams} from './BestPage';
import Set from './set';
import {formatMonth} from './time';
export default function ViewBest({
best,
setBest,
}: {
best?: Best;
setBest: (best?: Best) => void;
}) {
const [data, setData] = useState<number[]>([]);
const [unit, setUnit] = useState<string>();
export default function ViewBest() {
const {params} = useRoute<RouteProp<BestPageParams, 'ViewBest'>>();
const [sets, setSets] = useState<Set[]>([]);
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(() => {
const selectBest = `
@ -25,52 +39,46 @@ export default function ViewBest({
GROUP BY name, STRFTIME('%Y-%m-%d', created)
`;
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;
console.log(`${ViewBest.name}.${refresh.name}:`, result.rows.raw());
setData(result.rows.raw().map(row => row.weight));
setUnit(result.rows.item(0).unit);
setSets(result.rows.raw());
};
refresh();
}, [best, db]);
const contentInset = {top: 20, bottom: 20};
}, [params.best.name, db]);
const axesSvg = {fontSize: 10, fill: 'grey'};
const verticalContentInset = {top: 10, bottom: 10};
const xAxisHeight = 30;
return (
<Portal>
<Dialog visible={!!best} onDismiss={() => setBest(undefined)}>
<Dialog.Title>{best?.name}</Dialog.Title>
<Dialog.Content>
<View style={{height: 200, flexDirection: 'row'}}>
<YAxis
data={data}
contentInset={contentInset}
svg={{
fill: 'grey',
fontSize: 10,
}}
numberOfTicks={10}
formatLabel={value => `${value}${unit}`}
/>
<LineChart
style={{flex: 1, marginLeft: 16}}
data={data}
svg={{stroke: 'rgb(134, 65, 244)'}}
curve={shape.curveNatural}
contentInset={contentInset}>
<Grid />
</LineChart>
</View>
</Dialog.Content>
<Dialog.Actions>
<Button
mode="contained"
icon="close"
onPress={() => setBest(undefined)}>
Close
</Button>
</Dialog.Actions>
</Dialog>
</Portal>
<View style={{padding: 10}}>
<Text>Best weight per day</Text>
<View style={{height: 200, padding: 20, flexDirection: 'row'}}>
<YAxis
data={sets.map(set => set.weight)}
style={{marginBottom: xAxisHeight}}
contentInset={verticalContentInset}
svg={axesSvg}
formatLabel={value => `${value}${sets[0].unit}`}
/>
<View style={{flex: 1, marginLeft: 10}}>
<LineChart
style={{flex: 1}}
data={sets.map(set => set.weight)}
contentInset={verticalContentInset}
curve={shape.curveNatural}
svg={{stroke: 'rgb(134, 65, 244)'}}>
<Grid />
</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 File

@ -37,3 +37,10 @@ export function format(date: Date) {
].join(':') + (isPM ? ' pm' : 'am');
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}`;
}