Skip to content

Add detailed workout

Marco Boschi edited this page Mar 9, 2017 · 10 revisions

In order to add support to details to a new workout type follow these steps:

  1. In Main.swift add additional HKObjectTypes to healthReadData as appropriate;
  2. If you added at least one type in step 1. increment authRequired to automatically display the prompt to request access to new types;
  3. If currently supported details are not enough for the new workout add a new detail as a static constant in the WorkoutDetail class to express which data to display and how both inside the app and in the CSV file when exporting;
  4. Create a custom subclass of Workout for the new workout type and override init(_, delegate: _), after invoking super.init(_, delegate: _):
  5. Call self.setLengthPrefixFor(distance: _, speed: _, pace: _) if the workout needs different units from kilometers for distance, speed and pace;
  6. Call self.addDetails(_) passing an array of the desired details and order, time will be automatically added as the first;
  7. For each additional data required by the workout call self.addRequest(for: _, withUnit: _, andTimeType: _, searchingBy: _) specifying which data to load and its unit, whether to treat the data as single point in time, i.e. heart rate, or a value for a certain duration, i.e. distance, and how to query for the data, by time of the workout or only data explicitly linked to the workout. Note: when loading distance data (.distanceWalkingRunning and .distanceSwimming) specify .meter() as unit and use self.setLengthPrefixFor() to specify desired prefixes;
  8. Update the factory method Workout.workoutFor(raw: _, delegate: _) with a case for the new workout, note that a single class can be used to handle multiple workout types;
  9. Enjoy!
Clone this wiki locally