fix(watch): handle missing workout and remove conflicting delegate

This commit is contained in:
2026-08-08 12:19:57 +02:00
parent f3b3a8b944
commit 38082afdb9
2 changed files with 25 additions and 10 deletions
-5
View File
@@ -15,11 +15,6 @@ private class SessionDelegate: NSObject, WCSessionDelegate {
onActivationComplete?(activationState) onActivationComplete?(activationState)
} }
func session(_ session: WCSession, didReceiveMessage message: [String: Any]) {
print("[WatchConnectivity] Received message: \(message["type"] ?? "unknown")")
onMessageReceived?(message)
}
func session(_ session: WCSession, didReceiveMessage message: [String: Any], replyHandler: @escaping ([String: Any]) -> Void) { func session(_ session: WCSession, didReceiveMessage message: [String: Any], replyHandler: @escaping ([String: Any]) -> Void) {
print("[WatchConnectivity] Received message with reply: \(message["type"] ?? "unknown")") print("[WatchConnectivity] Received message with reply: \(message["type"] ?? "unknown")")
replyHandler(["status": "received"]) replyHandler(["status": "received"])
+25 -5
View File
@@ -153,17 +153,37 @@ export async function saveWatchCompletedWorkout(data: {
? Math.round(data.laps.reduce((sum, l) => sum + (l.heartRate || 0), 0) / data.laps.length) ? Math.round(data.laps.reduce((sum, l) => sum + (l.heartRate || 0), 0) / data.laps.length)
: 0; : 0;
await db const existing = await db
.update(workouts) .select({ id: workouts.id })
.set({ .from(workouts)
.where(eq(workouts.id, data.workoutId))
.limit(1);
if (existing.length === 0) {
await db.insert(workouts).values({
id: data.workoutId,
name: "Watch Workout",
poolLength: 25,
finishedAt: new Date().toISOString(), finishedAt: new Date().toISOString(),
totalDuration: data.duration, totalDuration: data.duration,
totalDistance: data.totalDistance || 0, totalDistance: data.totalDistance || 0,
calories: data.calories || 0, calories: data.calories || 0,
avgHeartRate, avgHeartRate,
source: "watch", source: "watch",
}) });
.where(eq(workouts.id, data.workoutId)); } else {
await db
.update(workouts)
.set({
finishedAt: new Date().toISOString(),
totalDuration: data.duration,
totalDistance: data.totalDistance || 0,
calories: data.calories || 0,
avgHeartRate,
source: "watch",
})
.where(eq(workouts.id, data.workoutId));
}
for (const lap of data.laps) { for (const lap of data.laps) {
await db.insert(laps).values({ await db.insert(laps).values({