fix(watch): handle missing workout and remove conflicting delegate
This commit is contained in:
@@ -15,11 +15,6 @@ private class SessionDelegate: NSObject, WCSessionDelegate {
|
||||
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) {
|
||||
print("[WatchConnectivity] Received message with reply: \(message["type"] ?? "unknown")")
|
||||
replyHandler(["status": "received"])
|
||||
|
||||
@@ -153,6 +153,25 @@ export async function saveWatchCompletedWorkout(data: {
|
||||
? Math.round(data.laps.reduce((sum, l) => sum + (l.heartRate || 0), 0) / data.laps.length)
|
||||
: 0;
|
||||
|
||||
const existing = await db
|
||||
.select({ id: workouts.id })
|
||||
.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(),
|
||||
totalDuration: data.duration,
|
||||
totalDistance: data.totalDistance || 0,
|
||||
calories: data.calories || 0,
|
||||
avgHeartRate,
|
||||
source: "watch",
|
||||
});
|
||||
} else {
|
||||
await db
|
||||
.update(workouts)
|
||||
.set({
|
||||
@@ -164,6 +183,7 @@ export async function saveWatchCompletedWorkout(data: {
|
||||
source: "watch",
|
||||
})
|
||||
.where(eq(workouts.id, data.workoutId));
|
||||
}
|
||||
|
||||
for (const lap of data.laps) {
|
||||
await db.insert(laps).values({
|
||||
|
||||
Reference in New Issue
Block a user