Massive/ViewBest.tsx

183 lines
5.7 KiB
TypeScript
Raw Normal View History

2022-07-11 01:00:17 +00:00
import {
RouteProp,
useFocusEffect,
useNavigation,
useRoute,
} from '@react-navigation/native';
2022-07-09 01:48:45 +00:00
import * as shape from 'd3-shape';
2022-08-24 01:23:21 +00:00
import React, {
useCallback,
useContext,
useEffect,
useRef,
useState,
} from 'react';
import {useColorScheme, View} from 'react-native';
2022-08-26 01:54:51 +00:00
import {FileSystem} from 'react-native-file-access';
import {Text, IconButton} from 'react-native-paper';
2022-08-24 01:23:21 +00:00
import Share from 'react-native-share';
2022-07-11 01:00:17 +00:00
import {Grid, LineChart, XAxis, YAxis} from 'react-native-svg-charts';
2022-08-24 01:23:21 +00:00
import ViewShot from 'react-native-view-shot';
import {CombinedDarkTheme, CombinedDefaultTheme} from './App';
2022-07-11 01:00:17 +00:00
import {BestPageParams} from './BestPage';
import {DatabaseContext} from './Routes';
2022-07-11 01:00:17 +00:00
import Set from './set';
import {formatMonth} from './time';
2022-07-08 12:11:10 +00:00
2022-08-24 00:32:57 +00:00
interface Volume {
name: string;
created: string;
value: number;
unit: string;
}
2022-07-11 01:00:17 +00:00
export default function ViewBest() {
const {params} = useRoute<RouteProp<BestPageParams, 'ViewBest'>>();
2022-08-24 00:32:57 +00:00
const [weights, setWeights] = useState<Set[]>([]);
const [volumes, setVolumes] = useState<Volume[]>([]);
2022-07-08 12:11:10 +00:00
const db = useContext(DatabaseContext);
2022-07-11 01:00:17 +00:00
const navigation = useNavigation();
const dark = useColorScheme() === 'dark';
2022-08-24 01:23:21 +00:00
const viewShot = useRef<ViewShot>(null);
2022-07-11 01:00:17 +00:00
useFocusEffect(
useCallback(() => {
2022-07-12 03:54:04 +00:00
console.log(`${ViewBest.name}.useFocusEffect`);
2022-07-11 01:00:17 +00:00
navigation.getParent()?.setOptions({
headerLeft: () => (
<IconButton icon="arrow-back" onPress={() => navigation.goBack()} />
),
2022-08-24 01:23:21 +00:00
headerRight: () => (
<IconButton
onPress={() =>
viewShot.current?.capture?.().then(async uri => {
const base64 = await FileSystem.readFile(uri, 'base64');
const url = `data:image/jpeg;base64,${base64}`;
Share.open({
message: params.best.name,
2022-08-24 01:23:21 +00:00
type: 'image/jpeg',
url,
failOnCancel: false,
});
})
}
icon="share-social-outline"
/>
),
2022-07-11 01:00:17 +00:00
title: params.best.name,
});
2022-08-24 01:23:21 +00:00
}, [navigation, params.best]),
2022-07-11 01:00:17 +00:00
);
2022-07-08 12:11:10 +00:00
useEffect(() => {
2022-07-12 03:54:04 +00:00
console.log(`${ViewBest.name}.useEffect`);
2022-08-24 00:32:57 +00:00
const selectWeights = `
SELECT max(weight) AS weight,
STRFTIME('%Y-%m-%d', created) as created, unit
FROM sets
WHERE name = ? AND NOT hidden
2022-08-24 00:32:57 +00:00
GROUP BY name, STRFTIME('%Y-%m-%d', created)
`;
const selectVolumes = `
SELECT sum(weight * reps) AS value,
STRFTIME('%Y-%m-%d', created) as created, unit
FROM sets
WHERE name = ? AND NOT hidden
GROUP BY name, STRFTIME('%Y-%m-%d', created)
`;
const refresh = async () => {
2022-08-24 00:32:57 +00:00
const [weightsResult] = await db.executeSql(selectWeights, [
params.best.name,
]);
if (weightsResult.rows.length === 0) return;
setWeights(weightsResult.rows.raw());
const [volumesResult] = await db.executeSql(selectVolumes, [
params.best.name,
]);
console.log(volumesResult.rows.raw());
if (volumesResult.rows.length === 0) return;
setVolumes(volumesResult.rows.raw());
};
2022-07-08 12:11:10 +00:00
refresh();
2022-07-11 01:00:17 +00:00
}, [params.best.name, db]);
2022-07-11 01:00:17 +00:00
const axesSvg = {fontSize: 10, fill: 'grey'};
const verticalContentInset = {top: 10, bottom: 10};
const xAxisHeight = 30;
2022-07-12 03:54:04 +00:00
2022-07-08 12:11:10 +00:00
return (
2022-08-24 01:23:21 +00:00
<ViewShot style={{padding: 10}} ref={viewShot}>
2022-07-11 01:00:17 +00:00
<Text>Best weight per day</Text>
<View style={{height: 300, padding: 20, flexDirection: 'row'}}>
2022-07-11 01:00:17 +00:00
<YAxis
2022-08-24 00:32:57 +00:00
data={weights.map(set => set.weight)}
style={{marginBottom: xAxisHeight}}
contentInset={verticalContentInset}
svg={axesSvg}
formatLabel={value => `${value}${weights[0].unit}`}
/>
<View style={{flex: 1, marginLeft: 10}}>
<LineChart
style={{flex: 1}}
data={weights.map(set => set.weight)}
contentInset={verticalContentInset}
curve={shape.curveBasis}
svg={{
stroke: dark
2022-08-26 01:54:51 +00:00
? CombinedDarkTheme.colors.primary
: CombinedDefaultTheme.colors.primary,
2022-08-24 00:32:57 +00:00
}}>
<Grid />
</LineChart>
<XAxis
style={{marginHorizontal: -10, height: xAxisHeight}}
data={weights}
formatLabel={(_value, index) =>
formatMonth(weights[index].created!)
}
contentInset={{left: 10, right: 10}}
svg={axesSvg}
/>
</View>
</View>
<Text>Volume per day</Text>
<View style={{height: 300, padding: 20, flexDirection: 'row'}}>
<YAxis
data={volumes.map(volume => volume.value)}
2022-07-11 01:00:17 +00:00
style={{marginBottom: xAxisHeight}}
contentInset={verticalContentInset}
svg={axesSvg}
2022-08-24 00:32:57 +00:00
formatLabel={(value: number) =>
`${value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',')}${
volumes[0].unit
}`
}
2022-07-11 01:00:17 +00:00
/>
<View style={{flex: 1, marginLeft: 10}}>
<LineChart
style={{flex: 1}}
2022-08-24 00:32:57 +00:00
data={volumes.map(volume => volume.value)}
2022-07-11 01:00:17 +00:00
contentInset={verticalContentInset}
curve={shape.curveBasis}
svg={{
stroke: dark
2022-08-26 01:54:51 +00:00
? CombinedDarkTheme.colors.primary
: CombinedDefaultTheme.colors.primary,
}}>
2022-07-11 01:00:17 +00:00
<Grid />
</LineChart>
<XAxis
style={{marginHorizontal: -10, height: xAxisHeight}}
2022-08-24 00:32:57 +00:00
data={weights}
formatLabel={(_value, index) =>
formatMonth(volumes[index]?.created)
}
2022-07-11 01:00:17 +00:00
contentInset={{left: 10, right: 10}}
svg={axesSvg}
/>
</View>
</View>
2022-08-24 01:23:21 +00:00
</ViewShot>
2022-07-08 12:11:10 +00:00
);
}