Some checks failed
Apple Platform CI / smoke-and-tests (push) Has been cancelled
Deep audit (issues 2-14): - Add missing WCSession handlers for applicationContext and userInfo - Fix BoundedFIFOQueue race condition with serial dispatch queue - Fix timer race condition with main thread guarantee - Fix watch pause state divergence — phone is now source of truth - Fix wrong notification posted on logout (createdNewWorkout → userLoggedOut) - Fix POST status check to accept any 2xx (was exact match) - Fix @StateObject → @ObservedObject for injected viewModel - Add pull-to-refresh to CompletedWorkoutsView - Fix typos: RefreshUserInfoFetcable, defualtPackageModle - Replace string concatenation with interpolation - Replace 6 @StateObject with @ObservedObject for BridgeModule.shared - Replace 7 hardcoded AVPlayer URLs with BaseURLs.currentBaseURL UI audit (issues 1-15): - Fix GeometryReader eating VStack space — replaced with .overlay - Fix refreshable continuation resuming before fetch completes - Remove duplicate @State workouts — derive from DataStore - Decouple leaf views from BridgeModule (pass discrete values) - Convert selectedIds from Array to Set for O(1) lookups - Extract .sorted() from var body into computed properties - Move search filter out of ForEach render loop - Replace import SwiftUI with import Combine in non-UI classes - Mark all @State properties private - Extract L/R exercise auto-add logic to WorkoutViewModel - Use enumerated() instead of .indices in ForEach - Make AddSupersetView frame flexible instead of fixed 300pt - Hoist Set construction out of per-exercise filter loop - Move ViewModel network fetch from init to load() Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
159 lines
5.5 KiB
Swift
159 lines
5.5 KiB
Swift
//
|
|
// CompletedWorkoutView.swift
|
|
// Werkout_ios
|
|
//
|
|
// Created by Trey Tartt on 6/22/23.
|
|
//
|
|
|
|
import SwiftUI
|
|
import HealthKit
|
|
import SharedCore
|
|
|
|
struct CompletedWorkoutView: View {
|
|
@ObservedObject var bridgeModule = BridgeModule.shared
|
|
@State private var healthKitWorkoutData: HealthKitWorkoutData?
|
|
@State private var difficulty: Float = 0
|
|
@State private var notes: String = ""
|
|
@State private var isUploading: Bool = false
|
|
@State private var gettingHealthKitData: Bool = false
|
|
@State private var hasError = false
|
|
@State private var errorMessage = ""
|
|
private let runtimeReporter = RuntimeReporter.shared
|
|
|
|
var postData: [String: Any]
|
|
let healthKitHelper = HealthKitHelper()
|
|
let workout: Workout
|
|
let completedWorkoutDismissed: ((Bool) -> Void)?
|
|
|
|
@Environment(\.dismiss) var dismiss
|
|
|
|
var body: some View {
|
|
ZStack {
|
|
WerkoutTheme.background
|
|
.ignoresSafeArea()
|
|
|
|
if isUploading {
|
|
ProgressView("Uploading")
|
|
.foregroundStyle(WerkoutTheme.textPrimary)
|
|
.progressViewStyle(CircularProgressViewStyle(tint: WerkoutTheme.accent))
|
|
}
|
|
VStack {
|
|
WorkoutInfoView(workout: workout)
|
|
|
|
Divider()
|
|
.overlay(WerkoutTheme.divider)
|
|
|
|
HStack {
|
|
if let calsBurned = healthKitWorkoutData?.caloriesBurned {
|
|
CaloriesBurnedView(healthKitWorkoutData: $healthKitWorkoutData,
|
|
calsBurned: calsBurned)
|
|
}
|
|
}
|
|
|
|
RateWorkoutView(difficulty: $difficulty)
|
|
.frame(maxHeight: 88)
|
|
|
|
Divider()
|
|
.overlay(WerkoutTheme.divider)
|
|
|
|
TextField("Notes", text: $notes)
|
|
.font(WerkoutTheme.bodyText)
|
|
.foregroundStyle(WerkoutTheme.textPrimary)
|
|
.frame(height: 55)
|
|
.textFieldStyle(PlainTextFieldStyle())
|
|
.padding([.horizontal], 4)
|
|
.background(WerkoutTheme.surfaceElevated)
|
|
.overlay(
|
|
RoundedRectangle(cornerRadius: WerkoutTheme.buttonRadius, style: .continuous)
|
|
.strokeBorder(WerkoutTheme.divider, lineWidth: 1)
|
|
)
|
|
.clipShape(RoundedRectangle(cornerRadius: WerkoutTheme.buttonRadius, style: .continuous))
|
|
|
|
if gettingHealthKitData {
|
|
ProgressView("Getting HealthKit data")
|
|
.foregroundStyle(WerkoutTheme.textPrimary)
|
|
.progressViewStyle(CircularProgressViewStyle(tint: WerkoutTheme.accent))
|
|
.padding()
|
|
}
|
|
|
|
Spacer()
|
|
|
|
Button("Upload", action: {
|
|
upload(postBody: postData)
|
|
})
|
|
.font(.system(size: 16, weight: .bold))
|
|
.foregroundStyle(WerkoutTheme.textPrimary)
|
|
.frame(maxWidth: .infinity, alignment: .center)
|
|
.frame(height: 44)
|
|
.glassEffect(.regular.interactive())
|
|
.tint(WerkoutTheme.success)
|
|
.clipShape(RoundedRectangle(cornerRadius: WerkoutTheme.buttonRadius, style: .continuous))
|
|
.padding()
|
|
.frame(maxWidth: .infinity)
|
|
.disabled(isUploading || gettingHealthKitData)
|
|
}
|
|
.padding([.leading, .trailing])
|
|
}
|
|
.alert("Upload Failed", isPresented: $hasError) {
|
|
Button("OK", role: .cancel) {}
|
|
} message: {
|
|
Text(errorMessage)
|
|
}
|
|
}
|
|
|
|
func upload(postBody: [String: Any]) {
|
|
guard isUploading == false else {
|
|
return
|
|
}
|
|
isUploading = true
|
|
|
|
var _postBody = postBody
|
|
_postBody["difficulty"] = difficulty
|
|
_postBody["notes"] = notes
|
|
if let healthKitUUID = bridgeModule.healthKitUUID {
|
|
_postBody["health_kit_workout_uuid"] = healthKitUUID.uuidString
|
|
}
|
|
|
|
CompleteWorkoutFetchable(postData: _postBody).fetch(completion: { result in
|
|
switch result {
|
|
case .success(_):
|
|
DispatchQueue.main.async {
|
|
self.isUploading = false
|
|
bridgeModule.resetCurrentWorkout()
|
|
dismiss()
|
|
completedWorkoutDismissed?(true)
|
|
}
|
|
case .failure(let failure):
|
|
DispatchQueue.main.async {
|
|
self.isUploading = false
|
|
self.errorMessage = failure.localizedDescription
|
|
self.hasError = true
|
|
}
|
|
runtimeReporter.recordError(
|
|
"Completed workout upload failed",
|
|
metadata: ["error": failure.localizedDescription]
|
|
)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
struct CompletedWorkoutView_Previews: PreviewProvider {
|
|
static let postBody = [
|
|
"difficulty": 1,
|
|
"workout_start_time": Date().timeFormatForUpload,
|
|
"workout": 1,
|
|
"total_time": 140,
|
|
"total_calories": Float(120.0),
|
|
"heart_rates": [65,65,4,54,232,12]
|
|
] as [String : Any]
|
|
|
|
static let workout = PreviewData.workout()
|
|
|
|
static var previews: some View {
|
|
CompletedWorkoutView(postData: CompletedWorkoutView_Previews.postBody,
|
|
workout: workout,
|
|
completedWorkoutDismissed: { _ in })
|
|
}
|
|
}
|