2022-07-11 01:00:17 +00:00
|
|
|
import {
|
|
|
|
RouteProp,
|
|
|
|
useFocusEffect,
|
|
|
|
useNavigation,
|
|
|
|
useRoute,
|
|
|
|
} from '@react-navigation/native';
|
2022-09-04 02:05:25 +00:00
|
|
|
import React, {useCallback, useEffect, useRef, useState} from 'react';
|
2022-08-26 01:54:51 +00:00
|
|
|
import {FileSystem} from 'react-native-file-access';
|
2022-09-02 00:24:28 +00:00
|
|
|
import {IconButton} from 'react-native-paper';
|
|
|
|
import RNPickerSelect from 'react-native-picker-select';
|
2022-08-24 01:23:21 +00:00
|
|
|
import Share from 'react-native-share';
|
|
|
|
import ViewShot from 'react-native-view-shot';
|
2022-09-04 04:56:46 +00:00
|
|
|
import {getVolumes, getWeightsBy} from './best.service';
|
2022-07-11 01:00:17 +00:00
|
|
|
import {BestPageParams} from './BestPage';
|
2022-09-02 00:24:28 +00:00
|
|
|
import Chart from './Chart';
|
2022-09-16 09:07:02 +00:00
|
|
|
import {PADDING} from './constants';
|
2022-09-04 03:28:21 +00:00
|
|
|
import {Metrics} from './metrics';
|
|
|
|
import {Periods} from './periods';
|
2022-07-11 01:00:17 +00:00
|
|
|
import Set from './set';
|
|
|
|
import {formatMonth} from './time';
|
2022-09-04 03:28:21 +00:00
|
|
|
import Volume from './volume';
|
2022-09-02 00:24:28 +00:00
|
|
|
|
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-09-02 00:24:28 +00:00
|
|
|
const [metric, setMetric] = useState(Metrics.Weight);
|
|
|
|
const [period, setPeriod] = useState(Periods.Monthly);
|
2022-07-11 01:00:17 +00:00
|
|
|
const navigation = useNavigation();
|
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({
|
2022-08-27 04:43:25 +00:00
|
|
|
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
|
|
|
|
2022-09-02 00:24:28 +00:00
|
|
|
useEffect(() => {
|
2022-09-04 03:28:21 +00:00
|
|
|
if (metric === Metrics.Weight)
|
2022-09-04 04:56:46 +00:00
|
|
|
getWeightsBy(params.best.name, period).then(setWeights);
|
2022-09-04 03:28:21 +00:00
|
|
|
else if (metric === Metrics.Volume)
|
|
|
|
getVolumes(params.best.name, period).then(setVolumes);
|
|
|
|
|
2022-09-02 00:24:28 +00:00
|
|
|
console.log(`${ViewBest.name}.useEffect`, {metric, period});
|
2022-09-04 03:28:21 +00:00
|
|
|
}, [params.best.name, metric, period]);
|
2022-07-12 03:54:04 +00:00
|
|
|
|
2022-07-08 12:11:10 +00:00
|
|
|
return (
|
2022-09-16 09:07:02 +00:00
|
|
|
<ViewShot style={{padding: PADDING}} ref={viewShot}>
|
2022-09-02 00:24:28 +00:00
|
|
|
<RNPickerSelect
|
|
|
|
onValueChange={setMetric}
|
|
|
|
items={[
|
|
|
|
{label: Metrics.Weight, value: Metrics.Weight},
|
|
|
|
{label: Metrics.Volume, value: Metrics.Volume},
|
|
|
|
]}
|
|
|
|
value={metric}
|
|
|
|
/>
|
|
|
|
<RNPickerSelect
|
|
|
|
onValueChange={setPeriod}
|
|
|
|
items={[
|
|
|
|
{label: Periods.Weekly, value: Periods.Weekly},
|
|
|
|
{label: Periods.Monthly, value: Periods.Monthly},
|
|
|
|
{label: Periods.Yearly, value: Periods.Yearly},
|
|
|
|
]}
|
|
|
|
value={period}
|
|
|
|
/>
|
|
|
|
{metric === Metrics.Volume && (
|
|
|
|
<Chart
|
|
|
|
yData={volumes.map(v => v.value)}
|
|
|
|
yFormat={(value: number) =>
|
2022-08-24 00:32:57 +00:00
|
|
|
`${value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',')}${
|
|
|
|
volumes[0].unit
|
|
|
|
}`
|
|
|
|
}
|
2022-09-02 00:24:28 +00:00
|
|
|
xData={weights}
|
|
|
|
xFormat={(_value, index) => formatMonth(weights[index].created!)}
|
2022-07-11 01:00:17 +00:00
|
|
|
/>
|
2022-09-02 00:24:28 +00:00
|
|
|
)}
|
|
|
|
{metric === Metrics.Weight && (
|
|
|
|
<Chart
|
|
|
|
yData={weights.map(set => set.weight)}
|
|
|
|
yFormat={value => `${value}${weights[0].unit}`}
|
|
|
|
xData={weights}
|
|
|
|
xFormat={(_value, index) => formatMonth(weights[index].created!)}
|
|
|
|
/>
|
|
|
|
)}
|
2022-08-24 01:23:21 +00:00
|
|
|
</ViewShot>
|
2022-07-08 12:11:10 +00:00
|
|
|
);
|
|
|
|
}
|