private void MakeRouteWorkoutsSlothy(HKWorkout workout, HKWorkoutRoute route) { // Get all of the locations this.LoadRouteLocations(route, (locations) => { // Slothify route var newLocations = this.SlothifyRouteLocations(locations); this.UpdateWorkoutLocationsRoute(workout, route, newLocations); }); }
private void UpdateWorkoutLocationsRoute(HKWorkout workout, HKWorkoutRoute route, List <CLLocation> newLocations) { // create a workout route builder var workoutRouteBuilder = new HKWorkoutRouteBuilder(this.healthStore, null); // insert updated route locations workoutRouteBuilder.InsertRouteData(newLocations.ToArray(), (success, error) => { if (success) { var syncIdentifier = route.Metadata?.SyncIdentifier; if (!string.IsNullOrEmpty(syncIdentifier)) { // new metadata with the same sync identifier and a higher version var objects = new NSObject[] { new NSString(syncIdentifier), NSNumber.FromInt32(2) }; var keys = new NSString[] { HKMetadataKey.SyncIdentifier, HKMetadataKey.SyncVersion }; var dictionary = NSDictionary.FromObjectsAndKeys(objects, keys); var metadata = new HKMetadata(dictionary); // finish the route with updated metadata workoutRouteBuilder.FinishRoute(workout, metadata, (workoutRoute, routeRrror) => { if (workoutRoute != null) { Console.WriteLine($"Workout route updated: ({route})"); } else { Console.WriteLine($"An error occurred while finishing the new route: ({error?.LocalizedDescription ?? "Unknown"})"); } }); } else { throw new ArgumentNullException(nameof(syncIdentifier), "Missing expected sync identifier on route"); } } else { Console.WriteLine($"An error occurred while inserting route data ({error?.LocalizedDescription ?? "Unknown"})"); } }); }
private void LoadRouteLocations(HKWorkoutRoute route, Action <List <CLLocation> > completion) { var locations = new List <CLLocation>(); var locationQuery = new HKWorkoutRouteQuery(route, (query, routeData, done, error) => { if (routeData != null) { locations.AddRange(routeData); if (done) { completion(locations); } } else { Console.WriteLine($"Error occurred wile querying for locations: ({error?.LocalizedDescription ?? ""})"); } }); this.healthStore.ExecuteQuery(locationQuery); }