Apple Fitness workout fixer + Strava uploader

fixed load workouts logic

mbenz12 6e8ca515 fa79687e

+19 -19
+6
WorkoutEditor.xcodeproj/xcuserdata/lynx1.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist
···
··· 1 + <?xml version="1.0" encoding="UTF-8"?> 2 + <Bucket 3 + uuid = "EB215442-ED55-4CF8-86BF-C6B2D0D621EC" 4 + type = "1" 5 + version = "2.0"> 6 + </Bucket>
+8 -4
WorkoutEditor/HealthKitManager.swift
··· 8 import Foundation 9 import HealthKit 10 11 - class HealthKitManager { 12 static let shared = HealthKitManager() 13 14 private let healthStore = HKHealthStore() 15 16 // Request authorization for specific health data types ··· 35 } 36 } 37 38 - func loadWorkouts(completion: @escaping ([HKWorkout]?, Error?) -> Void) { 39 let workoutType = HKObjectType.workoutType() 40 let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: false) 41 let query = HKSampleQuery(sampleType: workoutType, predicate: nil, limit: 100, sortDescriptors: [sortDescriptor]) { (query, samples, error) in 42 guard let workouts = samples as? [HKWorkout] else { 43 - completion(nil, error) 44 return 45 } 46 - completion(workouts, error) 47 } 48 49 self.healthStore.execute(query)
··· 8 import Foundation 9 import HealthKit 10 11 + class HealthKitManager: ObservableObject { 12 static let shared = HealthKitManager() 13 14 + @Published var loadedWorkouts: [HKWorkout]? 15 private let healthStore = HKHealthStore() 16 17 // Request authorization for specific health data types ··· 36 } 37 } 38 39 + func loadWorkouts() { 40 let workoutType = HKObjectType.workoutType() 41 let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: false) 42 let query = HKSampleQuery(sampleType: workoutType, predicate: nil, limit: 100, sortDescriptors: [sortDescriptor]) { (query, samples, error) in 43 guard let workouts = samples as? [HKWorkout] else { 44 return 45 } 46 + 47 + // Store the loaded workouts and trigger an update 48 + DispatchQueue.main.async { 49 + self.loadedWorkouts = workouts 50 + } 51 } 52 53 self.healthStore.execute(query)
+1
WorkoutEditor/WorkoutEditorApp.swift
··· 17 if success { 18 // Proceed with HealthKit-related functionality 19 print("Authorization requested successfully") 20 } else { 21 // Handle authorization error 22 print("Error requesting HealthKit authorization: \(error?.localizedDescription ?? "Unknown error")")
··· 17 if success { 18 // Proceed with HealthKit-related functionality 19 print("Authorization requested successfully") 20 + HealthKitManager.shared.loadWorkouts() 21 } else { 22 // Handle authorization error 23 print("Error requesting HealthKit authorization: \(error?.localizedDescription ?? "Unknown error")")
+4 -15
WorkoutEditor/WorkoutListView.swift
··· 9 import HealthKit 10 11 struct WorkoutListView: View { 12 - @State private var workouts: [HKWorkout] = [] 13 var body: some View { 14 NavigationView { 15 - List(workouts, id: \.uuid) { workout in 16 NavigationLink(destination: WorkoutEditView(workout: workout)) { 17 // Display date in "DD-MM-YYYY" format along with activity type 18 Text("\(formattedDate(workout.startDate)) - \(workout.workoutActivityType.activityTypeDescription)") 19 } 20 } 21 - .onAppear { 22 - loadWorkouts() 23 } 24 .navigationTitle("Workouts") 25 .navigationBarItems(trailing: NavigationLink(destination: WorkoutAddView()) { 26 Image(systemName: "plus") 27 }) 28 - } 29 - } 30 - 31 - private func loadWorkouts() { 32 - HealthKitManager.shared.loadWorkouts { (loadedWorkouts, error) in 33 - if let error = error { 34 - // Handle error 35 - print("Error loading workouts: \(error.localizedDescription)") 36 - } else if let loadedWorkouts = loadedWorkouts { 37 - self.workouts = loadedWorkouts 38 - } 39 } 40 } 41
··· 9 import HealthKit 10 11 struct WorkoutListView: View { 12 + @StateObject private var healthKitManager = HealthKitManager.shared 13 var body: some View { 14 NavigationView { 15 + List(healthKitManager.loadedWorkouts ?? [], id: \.uuid) { workout in 16 NavigationLink(destination: WorkoutEditView(workout: workout)) { 17 // Display date in "DD-MM-YYYY" format along with activity type 18 Text("\(formattedDate(workout.startDate)) - \(workout.workoutActivityType.activityTypeDescription)") 19 } 20 } 21 + .onAppear() { 22 + healthKitManager.loadWorkouts() 23 } 24 .navigationTitle("Workouts") 25 .navigationBarItems(trailing: NavigationLink(destination: WorkoutAddView()) { 26 Image(systemName: "plus") 27 }) 28 } 29 } 30