2022-09-28 23:28:03 +00:00
|
|
|
import {
|
|
|
|
NavigationProp,
|
|
|
|
useFocusEffect,
|
|
|
|
useNavigation,
|
|
|
|
} from '@react-navigation/native';
|
|
|
|
import {default as React, useCallback, useEffect, useState} from 'react';
|
|
|
|
import {FlatList, Image} from 'react-native';
|
|
|
|
import {List} from 'react-native-paper';
|
|
|
|
import {getBestReps, getBestWeights} from './best.service';
|
2022-09-24 06:47:31 +00:00
|
|
|
import {DrawerParamList} from './drawer-param-list';
|
2022-09-28 23:28:03 +00:00
|
|
|
import Page from './Page';
|
2022-09-04 03:28:21 +00:00
|
|
|
import Set from './set';
|
2022-09-28 23:28:03 +00:00
|
|
|
import {settings} from './settings.service';
|
2022-07-04 04:03:48 +00:00
|
|
|
|
2022-07-11 01:00:17 +00:00
|
|
|
export default function BestPage() {
|
2022-09-28 23:28:03 +00:00
|
|
|
const [bests, setBests] = useState<Set[]>([]);
|
|
|
|
const [search, setSearch] = useState('');
|
|
|
|
const navigation = useNavigation<NavigationProp<DrawerParamList>>();
|
|
|
|
|
|
|
|
const refresh = useCallback(async () => {
|
|
|
|
const weights = await getBestWeights(search);
|
|
|
|
console.log(`${BestPage.name}.refresh:`, {length: weights.length});
|
|
|
|
let newBest: Set[] = [];
|
|
|
|
for (const set of weights) {
|
|
|
|
const reps = await getBestReps(set.name, set.weight);
|
|
|
|
newBest.push(...reps);
|
|
|
|
}
|
|
|
|
setBests(newBest);
|
|
|
|
}, [search]);
|
|
|
|
|
|
|
|
useFocusEffect(
|
|
|
|
useCallback(() => {
|
|
|
|
refresh();
|
|
|
|
navigation.getParent()?.setOptions({
|
|
|
|
headerRight: () => null,
|
|
|
|
});
|
|
|
|
}, [refresh, navigation]),
|
|
|
|
);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
refresh();
|
|
|
|
}, [search, refresh]);
|
|
|
|
|
|
|
|
const renderItem = ({item}: {item: Set}) => (
|
|
|
|
<List.Item
|
|
|
|
key={item.name}
|
|
|
|
title={item.name}
|
|
|
|
description={`${item.reps} x ${item.weight}${item.unit || 'kg'}`}
|
|
|
|
onPress={() => navigation.navigate('View best', {best: item})}
|
|
|
|
left={() =>
|
|
|
|
(settings.images && item.image && (
|
|
|
|
<Image source={{uri: item.image}} style={{height: 75, width: 75}} />
|
|
|
|
)) ||
|
|
|
|
null
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
);
|
2022-07-04 04:03:48 +00:00
|
|
|
|
2022-07-03 01:50:01 +00:00
|
|
|
return (
|
2022-09-28 23:28:03 +00:00
|
|
|
<Page search={search} setSearch={setSearch}>
|
|
|
|
<FlatList
|
|
|
|
style={{height: '99%'}}
|
|
|
|
ListEmptyComponent={
|
|
|
|
<List.Item
|
|
|
|
title="No exercises yet"
|
|
|
|
description="Once sets have been added, this will highlight your personal bests."
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
renderItem={renderItem}
|
|
|
|
data={bests}
|
2022-07-06 04:27:36 +00:00
|
|
|
/>
|
2022-09-28 23:28:03 +00:00
|
|
|
</Page>
|
2022-07-03 01:50:01 +00:00
|
|
|
);
|
|
|
|
}
|