cph-cachet / flutter-plugins

A collection of Flutter plugins developed by CACHET
551 stars 681 forks source link

[health: ^9.0.1] No bucket aggregation support #902

Open TechHelper opened 9 months ago

TechHelper commented 9 months ago

I want to fetch health data of suppose last 7 days. But it should be bucket aggregated data per day. Currently when I fetch the calories data, it does not group that data by the date, instead it fetches some data points which has date range something like this: dateFrom as 15th feb 2024 20:00 and dateTo as 16th feb 2024 10:00. I think native SDK supports that.

erisanolasheni commented 9 months ago

To have this will be really great.

YatsenkoDev commented 9 months ago

I want to fetch health data of suppose last 7 days. But it should be bucket aggregated data per day. Currently when I fetch the calories data, it does not group that data by the date, instead it fetches some data points which has date range something like this: dateFrom as 15th feb 2024 20:00 and dateTo as 16th feb 2024 10:00. I think native SDK supports that.

Unfortunately, native Android and iOS libraries don't have this functionality, and because this Flutter library is a plugin to access native libraries, we can only use what we have. For your task, the only solution can be seven different requests of data at intervals for each date.

Example for steps:

    final stepsDay1 = await health.getTotalStepsInInterval(
        DateTime(2024,1,1), DateTime(2024,1,1,23,59,59));
    final stepsDay2 = await health.getTotalStepsInInterval(
        DateTime(2024,1,2), DateTime(2024,1,2,23,59,59));
...
    final stepsDay7 = await health.getTotalStepsInInterval(
        DateTime(2024,1,7), DateTime(2024,1,7,23,59,59));

It will show exactly the same numbers as Apple Health or Google Fit app. For Calories and Distance methods you can use my forked repository until it is merged here:

health:
    git:
      url: https://github.com/YatsenkoDev/flutter-plugins.git
      path: packages/health
TechHelper commented 9 months ago

I want to fetch health data of suppose last 7 days. But it should be bucket aggregated data per day. Currently when I fetch the calories data, it does not group that data by the date, instead it fetches some data points which has date range something like this: dateFrom as 15th feb 2024 20:00 and dateTo as 16th feb 2024 10:00. I think native SDK supports that.

Unfortunately, native Android and iOS libraries don't have this functionality, and because this Flutter library is a plugin to access native libraries, we can only use what we have. For your task, the only solution can be seven different requests of data at intervals for each date.

Example for steps:

    final stepsDay1 = await health.getTotalStepsInInterval(
        DateTime(2024,1,1), DateTime(2024,1,1,23,59,59));
    final stepsDay2 = await health.getTotalStepsInInterval(
        DateTime(2024,1,2), DateTime(2024,1,2,23,59,59));
...
    final stepsDay7 = await health.getTotalStepsInInterval(
        DateTime(2024,1,7), DateTime(2024,1,7,23,59,59));

It will show exactly the same numbers as Apple Health or Google Fit app. For Calories and Distance methods you can use my forked repository until it is merged here:

health:
    git:
      url: https://github.com/YatsenkoDev/flutter-plugins.git
      path: packages/health

I am going to do this only if I don't get any solution. BTW, I saw your pull request for the same. It can become little easier. Thank you.

AngryDuckFTW commented 9 months ago

I want to fetch health data of suppose last 7 days. But it should be bucket aggregated data per day. Currently when I fetch the calories data, it does not group that data by the date, instead it fetches some data points which has date range something like this: dateFrom as 15th feb 2024 20:00 and dateTo as 16th feb 2024 10:00. I think native SDK supports that.

Unfortunately, native Android and iOS libraries don't have this functionality, and because this Flutter library is a plugin to access native libraries, we can only use what we have. For your task, the only solution can be seven different requests of data at intervals for each date.

Example for steps:

    final stepsDay1 = await health.getTotalStepsInInterval(
        DateTime(2024,1,1), DateTime(2024,1,1,23,59,59));
    final stepsDay2 = await health.getTotalStepsInInterval(
        DateTime(2024,1,2), DateTime(2024,1,2,23,59,59));
...
    final stepsDay7 = await health.getTotalStepsInInterval(
        DateTime(2024,1,7), DateTime(2024,1,7,23,59,59));

It will show exactly the same numbers as Apple Health or Google Fit app. For Calories and Distance methods you can use my forked repository until it is merged here:

health:
    git:
      url: https://github.com/YatsenkoDev/flutter-plugins.git
      path: packages/health

This is wrong BTW, i forked this library myself to achive this because it was annoying that youd hit the rate limit on health connect almost instantly when doing 24 requests for hours of the day or 28-31 requests for days in months etc. You can do it using the 'healthConnectClient.aggregateGroupByPeriod' and 'healthConnectClient.aggregateGroupByDuration'. I have done this successfully for steps for android health connect but i am currently having trouble getting the same to work for Hydration. since it seems to return a Volume object which the agrregate doesnt seem to want to handle