Skip to content
This repository has been archived by the owner on Dec 4, 2023. It is now read-only.

Commit

Permalink
Support for "nutrition.calories" and "nutrition.carbohydrates" in `qu…
Browse files Browse the repository at this point in the history
…eryAggregated()` [dariosalvi78#17]
  • Loading branch information
sgilroy committed Oct 21, 2016
1 parent d0029e8 commit 8cfd54e
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/android/HealthPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,8 @@ private void queryAggregated(final JSONArray args, final CallbackContext callbac
builder.aggregate(DataType.TYPE_BASAL_METABOLIC_RATE, DataType.AGGREGATE_BASAL_METABOLIC_RATE_SUMMARY);
} else if (datatype.equalsIgnoreCase("activity")) {
builder.aggregate(DataType.TYPE_ACTIVITY_SEGMENT, DataType.AGGREGATE_ACTIVITY_SUMMARY);
} else if (nutritiondatatypes.get(datatype)) {
builder.aggregate(DataType.TYPE_NUTRITION, DataType.AGGREGATE_NUTRITION_SUMMARY);
} else {
callbackContext.error("Datatype " + datatype + " not supported");
return;
Expand Down Expand Up @@ -740,6 +742,9 @@ private void queryAggregated(final JSONArray args, final CallbackContext callbac
} else if (datatype.equalsIgnoreCase("activity")) {
retBucket.put("value", new JSONObject());
retBucket.put("unit", "activitySummary");
} else if (nutritiondatatypes.get(datatype)) {
// TODO: set the correct unit for each nutrition type
retBucket.put("unit", "kcal");
}
}
}
Expand All @@ -764,6 +769,24 @@ private void queryAggregated(final JSONArray args, final CallbackContext callbac
float ncal = datapoint.getValue(Field.FIELD_AVERAGE).asFloat();
double ocal = retBucket.getDouble("value");
retBucket.put("value", ocal + ncal);
} else if (nutritiondatatypes.get(datatype)) {
Value nutrients = datapoint.getValue(Field.FIELD_NUTRIENTS);
String field = null, unit = null;
switch (datatype) {
case "nutrition.calories":
field = Field.NUTRIENT_CALORIES;
unit = "kcal";
break;
case "nutrition.carbohydrates":
field = Field.NUTRIENT_TOTAL_CARBS;
unit = "g";
break;
}
if (field != null) {
float value = nutrients.getKeyValue(field);
double total = retBucket.getDouble("value");
retBucket.put("value", total + value);
}
} else if (datatype.equalsIgnoreCase("activity")) {
String activity = datapoint.getValue(Field.FIELD_ACTIVITY).asActivity();
JSONObject actobj = retBucket.getJSONObject("value");
Expand Down

0 comments on commit 8cfd54e

Please sign in to comment.