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