package com.iamthefij.otbeta.api; import org.json.JSONArray; import org.json.JSONObject; import java.util.ArrayList; import java.util.List; /** * A single workout with all metrics and details */ public class Workout { private HeartRateZones mHrm; private final Integer mCalories; private final Integer mDuration; private final Integer mId; private final Integer mPoints; private final Integer mStartTimeUtc; private Metric mDistance; private Metric mHeartRate; private Metric mResistance; private Metric mSpeed; private Metric mVertical; private final String mDeviceType; private final String mNotes; private final String mPointsLabel; private final String mSource; private final String mSourceLogo; private final String mStartTimeLocal; private final String mTimezone; public HeartRateZones getHrm() { return mHrm; } public Integer getCalories() { return mCalories; } public Integer getDuration() { return mDuration; } public Integer getId() { return mId; } public Integer getPoints() { return mPoints; } public Integer getStartTimeUtc() { return mStartTimeUtc; } public Metric getDistance() { return mDistance; } public Metric getHeartRate() { return mHeartRate; } public Metric getResistance() { return mResistance; } public Metric getSpeed() { return mSpeed; } public Metric getVertical() { return mVertical; } public String getDeviceType() { return mDeviceType; } public String getNotes() { return mNotes; } public String getPointsLabel() { return mPointsLabel; } public String getSource() { return mSource; } public String getSourceLogo() { return mSourceLogo; } public String getStartTimeLocal() { return mStartTimeLocal; } public String getTimezone() { return mTimezone; } public Workout(JSONObject workout) { mCalories = workout.optInt("calories"); mDuration = workout.optInt("duration"); mId = workout.optInt("id"); mPoints = workout.optInt("points"); mStartTimeUtc = workout.optInt("startTimeUtc"); mDeviceType = workout.optString("deviceType"); mNotes = workout.optString("notes"); mPointsLabel = workout.optString("pointsLabel"); mSource = workout.optString("source"); mSourceLogo = workout.optString("sourceLogo"); mStartTimeLocal = workout.optString("startTimeLocal"); mTimezone = workout.optString("timezone"); JSONObject distance = workout.optJSONObject("distance"); if (distance != null) { mDistance = new Metric(distance); } JSONObject heartRate = workout.optJSONObject("heartRate"); if (heartRate != null) { mHeartRate = new Metric(heartRate); } JSONObject resistance = workout.optJSONObject("resistance"); if (resistance != null) { mResistance = new Metric(resistance); } JSONObject speed = workout.optJSONObject("speed"); if (speed != null) { mSpeed = new Metric(speed); } JSONObject vertical = workout.optJSONObject("vertical"); if (vertical != null) { mVertical = new Metric(vertical); } JSONObject hrm = workout.optJSONObject("hrm"); if (hrm != null) { mHrm = new HeartRateZones(hrm); } } public class Metric { private final Double mAverage; private final Integer mInterval; private final String mUnits; private List mValues; private final Double mValue; public Double getAverage() { return mAverage; } public Integer getInterval() { return mInterval; } public String getUnits() { return mUnits; } public List getValues() { return mValues; } public Double getValue() { return mValue; } public Metric(JSONObject metric) { mAverage = metric.optDouble("average"); mInterval = metric.optInt("interval"); mUnits = metric.optString("units"); mValue = metric.optDouble("value"); JSONArray values = metric.optJSONArray("values"); if (values != null) { mValues = new ArrayList<>(values.length()); for (int i = 0; i < values.length(); i++) { mValues.add(values.optDouble(i)); } } } } public class HeartRateZones { private final Integer mBlueZoneSeconds; private final Integer mGreenZoneSeconds; private final Integer mGreyZoneSeconds; private final Integer mOrangeZoneSeconds; private final Integer mRedZoneSeconds; private final Integer mTargetHeartRate; public Integer getBlueZoneSeconds() { return mBlueZoneSeconds; } public Integer getGreenZoneSeconds() { return mGreenZoneSeconds; } public Integer getGreyZoneSeconds() { return mGreyZoneSeconds; } public Integer getOrangeZoneSeconds() { return mOrangeZoneSeconds; } public Integer getRedZoneSeconds() { return mRedZoneSeconds; } public Integer getTargetHeartRate() { return mTargetHeartRate; } public HeartRateZones(JSONObject zones) { mBlueZoneSeconds = zones.optInt("blueZoneSeconds"); mGreenZoneSeconds = zones.optInt("greenZoneSeconds"); mGreyZoneSeconds = zones.optInt("greyZoneSeconds"); mOrangeZoneSeconds = zones.optInt("orangeZoneSeconds"); mRedZoneSeconds = zones.optInt("redZoneSeconds"); mTargetHeartRate = zones.optInt("targetHeartRate"); } } }