2022-09-20 00:28:15 +00:00
|
|
|
import {Picker} from '@react-native-picker/picker';
|
2022-07-11 01:00:17 +00:00
|
|
|
import {
|
|
|
|
RouteProp,
|
|
|
|
useFocusEffect,
|
|
|
|
useNavigation,
|
|
|
|
useRoute,
|
|
|
|
} from '@react-navigation/native';
|
2022-09-18 08:24:05 +00:00
|
|
|
import React, {useCallback, useEffect, useState} from 'react';
|
2022-09-20 00:28:15 +00:00
|
|
|
import {useColorScheme, View} from 'react-native';
|
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';
|
2022-08-24 01:23:21 +00:00
|
|
|
import Share from 'react-native-share';
|
2022-09-18 08:24:05 +00:00
|
|
|
import {captureScreen} 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-09-20 00:28:15 +00:00
|
|
|
const dark = useColorScheme() === 'dark';
|
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();
|
|
|
|
|
|
|
|
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={() =>
|
2022-09-18 08:24:05 +00:00
|
|
|
captureScreen().then(async uri => {
|
2022-08-24 01:23:21 +00:00
|
|
|
const base64 = await FileSystem.readFile(uri, 'base64');
|
|
|
|
const url = `data:image/jpeg;base64,${base64}`;
|
|
|
|
Share.open({
|
|
|
|
type: 'image/jpeg',
|
|
|
|
url,
|
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
|
|
|
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-18 08:24:05 +00:00
|
|
|
<View style={{padding: PADDING}}>
|
2022-09-20 00:28:15 +00:00
|
|
|
<Picker
|
|
|
|
style={{color: dark ? 'white' : 'black'}}
|
|
|
|
dropdownIconColor={dark ? 'white' : 'black'}
|
|
|
|
selectedValue={metric}
|
|
|
|
onValueChange={value => setMetric(value)}>
|
|
|
|
<Picker.Item value={Metrics.Volume} label={Metrics.Volume} />
|
|
|
|
<Picker.Item value={Metrics.Weight} label={Metrics.Weight} />
|
|
|
|
</Picker>
|
|
|
|
<Picker
|
|
|
|
style={{color: dark ? 'white' : 'black'}}
|
|
|
|
dropdownIconColor={dark ? 'white' : 'black'}
|
|
|
|
selectedValue={period}
|
|
|
|
onValueChange={value => setPeriod(value)}>
|
|
|
|
<Picker.Item value={Periods.Weekly} label={Periods.Weekly} />
|
|
|
|
<Picker.Item value={Periods.Monthly} label={Periods.Monthly} />
|
|
|
|
<Picker.Item value={Periods.Yearly} label={Periods.Yearly} />
|
|
|
|
</Picker>
|
2022-09-02 00:24:28 +00:00
|
|
|
{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-09-18 08:24:05 +00:00
|
|
|
</View>
|
2022-07-08 12:11:10 +00:00
|
|
|
);
|
|
|
|
}
|