Revert "Revert "Optimize query in StartPlan""
This reverts commit 129b00dc5d
.
This commit is contained in:
parent
a804d9ef05
commit
eba33c2599
|
@ -11,7 +11,7 @@ import MassiveInput from './MassiveInput';
|
||||||
import {useSnackbar} from './MassiveSnack';
|
import {useSnackbar} from './MassiveSnack';
|
||||||
import {PlanPageParams} from './plan-page-params';
|
import {PlanPageParams} from './plan-page-params';
|
||||||
import Set from './set';
|
import Set from './set';
|
||||||
import {addSet, countManyToday, getDistinctSets} from './set.service';
|
import {addSet, countMany} from './set.service';
|
||||||
import SetForm from './SetForm';
|
import SetForm from './SetForm';
|
||||||
import StackHeader from './StackHeader';
|
import StackHeader from './StackHeader';
|
||||||
import {useSettings} from './use-settings';
|
import {useSettings} from './use-settings';
|
||||||
|
@ -30,7 +30,6 @@ export default function StartPlan() {
|
||||||
const [selected, setSelected] = useState(0);
|
const [selected, setSelected] = useState(0);
|
||||||
const {settings} = useSettings();
|
const {settings} = useSettings();
|
||||||
const [counts, setCounts] = useState<CountMany[]>();
|
const [counts, setCounts] = useState<CountMany[]>();
|
||||||
const [distinctSets, setDistinctSets] = useState<Set[]>();
|
|
||||||
const weightRef = useRef<TextInput>(null);
|
const weightRef = useRef<TextInput>(null);
|
||||||
const repsRef = useRef<TextInput>(null);
|
const repsRef = useRef<TextInput>(null);
|
||||||
const unitRef = useRef<TextInput>(null);
|
const unitRef = useRef<TextInput>(null);
|
||||||
|
@ -44,15 +43,11 @@ export default function StartPlan() {
|
||||||
|
|
||||||
useFocusEffect(
|
useFocusEffect(
|
||||||
useCallback(() => {
|
useCallback(() => {
|
||||||
countManyToday().then(newCounts => {
|
countMany(workouts).then(newCounts => {
|
||||||
setCounts(newCounts);
|
setCounts(newCounts);
|
||||||
console.log(`${StartPlan.name}.focus:`, {newCounts});
|
console.log(`${StartPlan.name}.focus:`, {newCounts});
|
||||||
});
|
});
|
||||||
getDistinctSets({limit: 100, offset: 0, term: '%'}).then(newDistinct => {
|
}, [workouts]),
|
||||||
setDistinctSets(newDistinct);
|
|
||||||
console.log(`${StartPlan.name}.focus:`, {newDistinct});
|
|
||||||
});
|
|
||||||
}, []),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
const handleSubmit = async () => {
|
const handleSubmit = async () => {
|
||||||
|
@ -67,7 +62,7 @@ export default function StartPlan() {
|
||||||
image: set.image,
|
image: set.image,
|
||||||
unit,
|
unit,
|
||||||
});
|
});
|
||||||
countManyToday().then(setCounts);
|
countMany(workouts).then(setCounts);
|
||||||
if (
|
if (
|
||||||
settings.notify &&
|
settings.notify &&
|
||||||
(+weight > best.weight || (+reps > best.reps && +weight === best.weight))
|
(+weight > best.weight || (+reps > best.reps && +weight === best.weight))
|
||||||
|
@ -110,20 +105,6 @@ export default function StartPlan() {
|
||||||
[name, workouts],
|
[name, workouts],
|
||||||
);
|
);
|
||||||
|
|
||||||
const getDescription = useCallback(
|
|
||||||
(countName: string) => {
|
|
||||||
const count = counts?.find(c => c.name === countName);
|
|
||||||
console.log(`${StartPlan.name}:`, {count, countName});
|
|
||||||
if (!distinctSets) return;
|
|
||||||
const distinct = distinctSets.find(d => d.name === countName);
|
|
||||||
console.log(`${StartPlan.name}:`, {distinct});
|
|
||||||
if (settings.showSets)
|
|
||||||
return `${count?.total || 0} / ${distinct?.sets || 3}`;
|
|
||||||
return count?.total || '0';
|
|
||||||
},
|
|
||||||
[counts, distinctSets, settings.showSets],
|
|
||||||
);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<StackHeader title={params.plan.days.replace(/,/g, ', ')} />
|
<StackHeader title={params.plan.days.replace(/,/g, ', ')} />
|
||||||
|
@ -157,13 +138,17 @@ export default function StartPlan() {
|
||||||
innerRef={unitRef}
|
innerRef={unitRef}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
{counts && distinctSets && (
|
{counts && (
|
||||||
<FlatList
|
<FlatList
|
||||||
data={workouts}
|
data={counts}
|
||||||
renderItem={({item, index}) => (
|
renderItem={({item, index}) => (
|
||||||
<List.Item
|
<List.Item
|
||||||
title={item}
|
title={item.name}
|
||||||
description={getDescription(item)}
|
description={
|
||||||
|
settings.showSets
|
||||||
|
? `${item.total} / ${item.sets ?? 3}`
|
||||||
|
: item.total.toString()
|
||||||
|
}
|
||||||
onPress={() => select(index)}
|
onPress={() => select(index)}
|
||||||
left={() => (
|
left={() => (
|
||||||
<View
|
<View
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
export default interface CountMany {
|
export default interface CountMany {
|
||||||
name: string;
|
name: string;
|
||||||
total: number;
|
total: number;
|
||||||
|
sets?: number;
|
||||||
}
|
}
|
||||||
|
|
|
@ -159,14 +159,21 @@ export const countToday = async (name: string): Promise<number> => {
|
||||||
return Number(result.rows.item(0)?.total);
|
return Number(result.rows.item(0)?.total);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const countManyToday = async (): Promise<CountMany[]> => {
|
export const countMany = async (names: string[]): Promise<CountMany[]> => {
|
||||||
|
const questions = names.map(_ => '?').join(',');
|
||||||
|
console.log({questions, names});
|
||||||
const select = `
|
const select = `
|
||||||
SELECT COUNT(*) as total, name FROM sets
|
SELECT workouts.name, COUNT(sets.id) as total
|
||||||
WHERE created LIKE strftime('%Y-%m-%d%%', 'now', 'localtime')
|
FROM (
|
||||||
AND NOT hidden
|
SELECT distinct name FROM sets
|
||||||
GROUP BY name
|
WHERE name IN (${questions})
|
||||||
|
) workouts
|
||||||
|
LEFT JOIN sets ON sets.name = workouts.name
|
||||||
|
AND sets.created LIKE STRFTIME('%Y-%m-%d%%', 'now', 'localtime')
|
||||||
|
AND NOT sets.hidden
|
||||||
|
GROUP BY workouts.name;
|
||||||
`;
|
`;
|
||||||
const [result] = await db.executeSql(select);
|
const [result] = await db.executeSql(select, names);
|
||||||
return result.rows.raw();
|
return result.rows.raw();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user