95 lines
2.6 KiB
TypeScript
95 lines
2.6 KiB
TypeScript
import { useEffect, useState } from "react";
|
|
import { View, Text } from "react-native";
|
|
import { useTranslation } from "react-i18next";
|
|
import { useNavigation, useRoute } from "@react-navigation/native";
|
|
import type { NativeStackNavigationProp } from "@react-navigation/native-stack";
|
|
import type { RouteProp } from "@react-navigation/native";
|
|
import type { RootStackParamList } from "@navigation/types";
|
|
import { getWorkoutWithSets, getWorkoutLaps } from "@db/operations";
|
|
import { WorkoutSummary } from "../components/WorkoutSummary";
|
|
|
|
type Set = {
|
|
stroke: string;
|
|
distancePerRep: number;
|
|
repCount: number;
|
|
position: number;
|
|
};
|
|
|
|
type Lap = {
|
|
setPosition: number;
|
|
repNumber: number;
|
|
distance: number;
|
|
duration: number;
|
|
heartRate: number | null;
|
|
swolf: number | null;
|
|
};
|
|
|
|
export function PostSwimScreen() {
|
|
const { t } = useTranslation("active-workout");
|
|
const navigation = useNavigation<NativeStackNavigationProp<RootStackParamList>>();
|
|
const route = useRoute<RouteProp<RootStackParamList, "PostSwim">>();
|
|
const [data, setData] = useState<{
|
|
name: string | null;
|
|
poolLength: number;
|
|
totalDistance: number;
|
|
totalDuration: number;
|
|
calories: number | null;
|
|
avgHeartRate: number | null;
|
|
sets: Set[];
|
|
laps: Lap[];
|
|
avgSwolf: number | null;
|
|
} | null>(null);
|
|
|
|
useEffect(() => {
|
|
loadSummary();
|
|
}, []);
|
|
|
|
const loadSummary = async () => {
|
|
const workout = await getWorkoutWithSets(route.params.workoutId);
|
|
const laps = await getWorkoutLaps(route.params.workoutId);
|
|
|
|
if (!workout) return;
|
|
|
|
const totalDuration = workout.laps?.reduce((acc: number, l: any) => acc + l.duration, 0) ?? 0;
|
|
const avgSwolf =
|
|
laps.length > 0
|
|
? Math.round(laps.reduce((acc, l) => acc + (l.swolf ?? 0), 0) / laps.length)
|
|
: null;
|
|
|
|
setData({
|
|
name: workout.name,
|
|
poolLength: workout.poolLength,
|
|
totalDistance: workout.totalDistance ?? 0,
|
|
totalDuration,
|
|
calories: workout.calories,
|
|
avgHeartRate: workout.avgHeartRate,
|
|
sets: workout.sets ?? [],
|
|
laps,
|
|
avgSwolf,
|
|
});
|
|
};
|
|
|
|
if (!data) {
|
|
return (
|
|
<View className="flex-1 bg-white items-center justify-center">
|
|
<Text className="text-slate-400">{t("loadingSummary")}</Text>
|
|
</View>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<WorkoutSummary
|
|
name={data.name}
|
|
totalDistance={data.totalDistance}
|
|
totalDuration={data.totalDuration}
|
|
calories={data.calories}
|
|
avgHeartRate={data.avgHeartRate}
|
|
poolLength={data.poolLength}
|
|
sets={data.sets}
|
|
laps={data.laps}
|
|
avgSwolf={data.avgSwolf}
|
|
onDone={() => navigation.popToTop()}
|
|
/>
|
|
);
|
|
}
|