import { useEffect, useState } from "react"; import { View, Text, Pressable, ActivityIndicator } 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 { RootStackParamList } from "@navigation/types"; import type { RouteProp } from "@react-navigation/native"; import { getWorkoutWithSets } from "@db/operations"; import { useSyncWorkout } from "../../watch-connectivity/hooks/useSyncWorkout"; import { useWatchConnection } from "../../watch-connectivity/hooks/useWatchConnection"; import { formatDistance } from "@utils/formatDistance"; import { STROKE_COLORS } from "@constants/strokes"; import type { WorkoutSet } from "@/types/workout"; type WorkoutWithSets = { id: string; name: string | null; poolLength: number; totalDistance: number | null; sets: WorkoutSet[]; }; export function PreSwimScreen() { const { t } = useTranslation("active-workout"); const { t: tc } = useTranslation("common"); const navigation = useNavigation>(); const route = useRoute>(); const [workout, setWorkout] = useState(null); const [syncResult, setSyncResult] = useState<"sent" | "error" | null>(null); const { isSyncing, sendWorkoutToWatch } = useSyncWorkout(); const { status, isPaired, isInstalled, isReady } = useWatchConnection(); useEffect(() => { loadWorkout(); }, []); const loadWorkout = async () => { const data = await getWorkoutWithSets(route.params.workoutId); setWorkout(data as WorkoutWithSets | null); }; const handleStartSwim = async () => { if (!workout || !isReady) return; const result = await sendWorkoutToWatch( workout.id, workout.name, workout.poolLength, workout.sets ); if ("error" in result) { setSyncResult("error"); return; } setSyncResult("sent"); }; if (!workout) { return ( ); } if (syncResult === "sent") { return ( ⌚ {t("workoutSentToWatch")} {t("openWatchApp")} navigation.popToTop()} className="bg-slate-900 rounded-2xl py-4 items-center w-full" > {tc("done")} ); } if (syncResult === "error") { return ( ⚠️ {t("failedToSend")} {t("couldNotSend")} setSyncResult(null)} className="bg-sky-500 rounded-2xl py-4 items-center w-full" > {tc("retry")} ); } return ( {workout.name || tc("workout")} {formatDistance(workout.totalDistance ?? 0)} · {tc("pool", { length: workout.poolLength })} {tc("sets")} {workout.sets.map((set, i) => ( {tc(`stroke.${set.stroke.toLowerCase()}`)} {set.repCount}×{set.distancePerRep}m ))} {t("rest")} {t("betweenSets", { duration: workout.sets[0]?.restDuration ?? 0 })} {isReady ? t("watchStatus.ready") : isPaired ? isInstalled ? t("watchStatus.pairedNotInRange") : t("watchStatus.pairedNotInstalled") : t("watchStatus.noWatch")} {isSyncing ? ( ) : ( {t("sendToWatchAndStart")} )} {!isReady && ( {t("watchRequired")} )} ); }