// // 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 var healthKitWorkoutData: HealthKitWorkoutData? @State var difficulty: Float = 0 @State var notes: String = "" @State var isUploading: Bool = false @State 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 { if isUploading { ProgressView("Uploading") } VStack { WorkoutInfoView(workout: workout) Divider() HStack { if let calsBurned = healthKitWorkoutData?.caloriesBurned { CaloriesBurnedView(healthKitWorkoutData: $healthKitWorkoutData, calsBurned: calsBurned) } } RateWorkoutView(difficulty: $difficulty) .frame(maxHeight: 88) Divider() TextField("Notes", text: $notes) .frame(height: 55) .textFieldStyle(PlainTextFieldStyle()) .padding([.horizontal], 4) .overlay(RoundedRectangle(cornerRadius: 16).stroke(Color(uiColor: .clear))).background(Color(uiColor: .init(red: 200/255, green: 200/255, blue: 200/255, alpha: 0.2))) .cornerRadius(8) if gettingHealthKitData { ProgressView("Getting HealthKit data") .padding() } Spacer() Button("Upload", action: { upload(postBody: postData) }) .frame(maxWidth: .infinity, alignment: .center) .frame(height: 44) .foregroundColor(.blue) .background(.yellow) .cornerRadius(Constants.buttonRadius) .padding() .frame(maxWidth: .infinity) .disabled(isUploading || gettingHealthKitData) } .padding([.leading, .trailing]) } .alert("Upload Failed", isPresented: $hasError) { Button("OK", role: .cancel) {} } message: { Text(errorMessage) } // .onChange(of: bridgeModule.healthKitUUID, perform: { healthKitUUID in // if let healthKitUUID = healthKitUUID { // gettingHealthKitData = true // healthKitHelper.getDetails(forHealthKitUUID: healthKitUUID, // completion: { healthKitWorkoutData in // guard let healthStore = healthKitWorkoutData else { // return // } // self.healthKitWorkoutData = healthKitWorkoutData // gettingHealthKitData = false // }) // } // }) } 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 }) } }