diff --git a/BestList.tsx b/BestList.tsx index eef96b8..96671e1 100644 --- a/BestList.tsx +++ b/BestList.tsx @@ -4,25 +4,26 @@ import { useNavigation, } from '@react-navigation/native'; import React, {useCallback, useEffect, useState} from 'react'; -import {FlatList, Image} from 'react-native'; +import {FlatList} from 'react-native'; import {List} from 'react-native-paper'; -import {getBestReps, getBestWeights} from './best.service'; +import {getBestReps, getBestWeights, getMaxWeights} from './best.service'; import {BestPageParams} from './BestPage'; +import Chart from './Chart'; import Page from './Page'; import Set from './set'; -import {useSettings} from './use-settings'; export default function BestList() { const [bests, setBests] = useState(); + const [maxWeights, setMaxWeights] = useState(); const [search, setSearch] = useState(''); const navigation = useNavigation>(); - const {settings} = useSettings(); const refresh = useCallback(async () => { - const weights = await getBestWeights(search); - console.log(`${BestList.name}.refresh:`, {length: weights.length}); + getMaxWeights().then(setMaxWeights); + const bestWeights = await getBestWeights(search); + console.log(`${BestList.name}.refresh:`, {length: bestWeights.length}); let newBest: Set[] = []; - for (const set of weights) { + for (const set of bestWeights) { const reps = await getBestReps(set.name, set.weight); newBest.push(...reps); } @@ -42,18 +43,24 @@ export default function BestList() { refresh(); }, [search, refresh]); + const left = useCallback( + (name: string) => { + const xData = maxWeights?.filter(set => set.name === name) || []; + console.log(`${BestList.name}:`, {xData}); + const yData = xData.map(set => set.weight); + console.log(`${BestList.name}:`, {yData}); + return ; + }, + [maxWeights], + ); + const renderItem = ({item}: {item: Set}) => ( navigation.navigate('ViewBest', {best: item})} - left={() => - (settings.images && item.image && ( - - )) || - null - } + right={() => left(item.name)} /> ); diff --git a/Chart.tsx b/Chart.tsx index 0ee44ec..f1e4488 100644 --- a/Chart.tsx +++ b/Chart.tsx @@ -11,11 +11,13 @@ export default function Chart({ xFormat, xData, yFormat, + height, }: { yData: number[]; xData: Set[]; - xFormat: (value: any, index: number) => string; - yFormat: (value: any) => string; + xFormat?: (value: any, index: number) => string; + yFormat?: (value: any) => string; + height?: number; }) { const {color} = useContext(CustomTheme); const axesSvg = {fontSize: 10, fill: 'grey'}; @@ -24,14 +26,17 @@ export default function Chart({ return ( <> - - + + {yFormat && ( + + )} - + {xFormat && ( + + )} diff --git a/best.service.ts b/best.service.ts index 43eee49..041ea3c 100644 --- a/best.service.ts +++ b/best.service.ts @@ -4,6 +4,15 @@ import Set from './set'; import {defaultSet} from './set.service'; import Volume from './volume'; +export const getMaxWeights = async (): Promise => { + const select = ` + SELECT MAX(weight) AS weight, name, STRFTIME('%Y-%m', created) AS created + FROM sets GROUP BY name, STRFTIME('%Y-%m', created); + `; + const [result] = await db.executeSql(select, []); + return result.rows.raw(); +}; + export const getBestSet = async (name: string): Promise => { const bestWeight = ` SELECT name, reps, unit, MAX(weight) AS weight