Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support queries for nutrition (dietary/food) data types in Google Fit #17

Closed
3 tasks done
sgilroy opened this issue Oct 20, 2016 · 6 comments
Closed
3 tasks done

Comments

@sgilroy
Copy link

sgilroy commented Oct 20, 2016

We are currently using the Telerik HealthKit plugin to get nutrition data types from HealthKit. The cordova-plugin-health plugin does not (yet) support this.

To get nutrition data from Google Fit, query for DataType.TYPE_NUTRITION. For each data point, nutrients can be retrieved via datapoint.getValue(Field.FIELD_NUTRIENTS) and then specifiec nutrients can be retrieved via nutrients.getKeyValue(Field.NUTRIENT_TOTAL_CARBS).

Documentation on Field.FIELD_NUTRIENTS:
https://developers.google.com/android/reference/com/google/android/gms/fitness/data/Field.html#FIELD_NUTRIENTS

List of nutrients (constants prefixed with NUTRIENT_):
https://developers.google.com/android/reference/com/google/android/gms/fitness/data/Field#constant-summary

Tasks:

  • include all nutrient types in nutritiondatatypes
  • support retrieving samples for each nutrient type in query() with appropriate unit value for each type
  • support aggregated queries for each nutrient type in queryAggregated()

Eventually, support should also be added for mapping and querying the equivalent data types in HealthKit (see this list for reference). However, I don't personally need HealthKit support at the moment, so I will start with just the Google Fit side.

sgilroy added a commit to TwineHealth/cordova-plugin-health that referenced this issue Oct 20, 2016
sgilroy added a commit to TwineHealth/cordova-plugin-health that referenced this issue Oct 21, 2016
sgilroy added a commit to TwineHealth/cordova-plugin-health that referenced this issue Oct 21, 2016
sgilroy added a commit to TwineHealth/cordova-plugin-health that referenced this issue Oct 21, 2016
@ghost ghost closed this as completed in #18 Oct 21, 2016
@ghost
Copy link

ghost commented Oct 21, 2016

Thanks very much for working on this.

I have a question about Telerik's plugin:
I merged their repo with mine on Sept. 27 2016. It may already work with the nutrition quantity samples, or am I missing something?

In the future, it would be nice to have a "nutrition" datatype that returns all the known nutrients of a given sample (or aggregated) in an object (eg { fat.total: 50, cholesterol: 0.03 }), but that would probably be more difficult to implement with HealthKit.

@dariosalvi78
Copy link
Owner

Is this also working on the iOS part?
In that case I can prepare a release.

@sgilroy
Copy link
Author

sgilroy commented Nov 2, 2016

@dariosalviwork I suspect that the nutrition samples could be queried via the separate HealthKit API (we are doing this using the Telerik plugin itself) but not via the health plugin API using the platform-agnostic type identifiers.

Telerik healthkit plugin:

window.plugins.healthkit.querySampleType(
  {
    'startDate' : new Date(new Date().getTime()-2*24*60*60*1000), // two days ago
    'endDate'   : new Date(), // now
    'sampleType': 'HKQuantityTypeIdentifierDietarySugar',
    'unit'      : 'g'
  },
  onSuccess,
  onError
);

cordova-plugin-health

window.navigator.health.query(
  {
    'startDate' : new Date(new Date().getTime()-2*24*60*60*1000), // two days ago
    'endDate'   : new Date(), // now
    'dataType': 'nutrition.sugar',
    'unit'      : 'g'
  },
  onSuccess,
  onError
);

So there are multiple outstanding issues:

  1. translate from the nutrition data types, such as "nutrition.sugar" to the HealthKit sample types, such as "HKQuantityTypeIdentifierDietarySugar"
  2. correlation queries, including support for a "nutrion" correlation

@dariosalvi78
Copy link
Owner

the health plugin uses the teleriks' plugin. AFAIK they are completely in sync, so there should be no missing features.

1: that's easy enough, I can do that
2: not sure why we would need this. As far as I have seen, the Android part only queries for specific substances

what about aggregated values, have you tried with Telerik's plugin?

@sgilroy
Copy link
Author

sgilroy commented Nov 2, 2016

Yes, we are using the Telerik plugin for aggregated values (by day) via window.plugins.healthkit.querySampleTypeAggregated().

As for the correlation queries, Google Fit does not actually allow individual nutrients (substances) to be queried directly. It only supports queries for DataType.TYPE_NUTRITION and returns data points with all of the available nutrient fields. I implemented the query in the plugin to pull out the appropriate field (such as Field.NUTRIENT_SUGAR) and return just that one value. A correlation query should be fairly straight forward to implement in Google Fit, and can be done in a way that matches up closely to the window.plugins.healthkit.queryCorrelationType() from the Telerik plugin.

@ghost
Copy link

ghost commented Nov 22, 2016

I have extended your work, now nutrition is supported with the same interface on both Fit and HK. I have also added the data type "nutrition" that computes a summary of all known nutrients.

Still to be done: store() of nutrients.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants