cph-cachet / flutter-plugins

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

Health (9.0.1) - Allow individual nutrition values to be written #925

Closed shubhamsinghmutualmobile closed 7 months ago

shubhamsinghmutualmobile commented 8 months ago

Device / Emulator and OS

Describe the bug

After merging https://github.com/cph-cachet/flutter-plugins/pull/924, I realised that if I want to just write the caffeine value to the health store, I would still need to pass the following values as null:

  1. caloriesConsumed
  2. carbohydrates
  3. protein
  4. fatTotal

i.e. my code would look something like this:

final wasWriteSuccessful = await health.writeMeal(
  time.subtract(const Duration(seconds: 1)),
  time,
  null,
  null,
  null,
  null,
  'Caffeine',
  caffeineVolumeGram / 1000,
  MealType.SNACK,
);

which in itself is not a problem, but in order to make this write operation successful, I would need to ask the user to provide write permissions for all data types mentioned above which is not ideal.

Ideally, if I just want to write the caffeine value, I should only need to ask for its specific permission from the user.

I would expect the API to behave in either of the following two ways:

  1. The existing writeMeal method should only consider writing values to the health store for which the user has actually granted permission(s). For example, if the user has only granted permission for caffeine, then no matter what values are being sent for caloriesConsumed, carbohydrates, protein, or fatTotal, the method writeMeal would make attempt to write the caffeine ONLY.

  2. Along with having a method that clubs all nutrition values together (like writeMeal), we could also have separate methods for individual writing (e.g. writeCaffeine, writeProtein, etc).

To Reproduce

The description should suffice

Expected behavior

Expected that the library would allow writing single nutrition values.

Actual behavior

The library clubs all nutrition values together and forces the user to grant permissions for even the values that he does NOT want to write.

Screenshots

N/A

Flutter doctor

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.19.3, on macOS 13.6.4 22G513 darwin-arm64, locale en-IN)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 15.2)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2023.2)
[✓] Connected device (4 available)
    ! Error: Browsing on the local area network for iPhone. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac.
      The device must be opted into Developer Mode to connect wirelessly. (code -27)
[✓] Network resources

• No issues found!

Additional information

N/A

shubhamsinghmutualmobile commented 8 months ago

@bardram I have added a very quick (1-liner) fix for this issue here

Could you please review it?

Apologies that I have to bother you like this. This should be the last time I do so.

shubhamsinghmutualmobile commented 7 months ago

This is fixed with version 10.1.0 👍