feat(watch): add lap detection via accelerometer and workout sync to phone
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
import { useEffect, useRef } from "react";
|
||||
import WatchConnectivity from "../../../modules/watch-connectivity";
|
||||
import { saveWatchCompletedWorkout } from "@db/operations";
|
||||
|
||||
export function useWatchWorkoutSync() {
|
||||
const pollingRef = useRef<ReturnType<typeof setInterval> | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
pollingRef.current = setInterval(async () => {
|
||||
try {
|
||||
const events = (await WatchConnectivity.pollEvents()) as any[];
|
||||
for (const event of events) {
|
||||
if (event.type === "workoutCompleted") {
|
||||
console.log("[useWatchWorkoutSync] Received workoutCompleted:", event.workoutId);
|
||||
await saveWatchCompletedWorkout({
|
||||
workoutId: event.workoutId,
|
||||
duration: event.duration ?? 0,
|
||||
totalDistance: event.totalDistance ?? 0,
|
||||
calories: event.calories ?? 0,
|
||||
laps: event.laps ?? [],
|
||||
});
|
||||
console.log("[useWatchWorkoutSync] Saved completed workout to DB");
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
// Silently ignore polling errors
|
||||
}
|
||||
}, 2000);
|
||||
|
||||
return () => {
|
||||
if (pollingRef.current) {
|
||||
clearInterval(pollingRef.current);
|
||||
}
|
||||
};
|
||||
}, []);
|
||||
}
|
||||
Reference in New Issue
Block a user