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

HKMetadataKeySyncIdentifier / HKMetadataKeySyncVersion are not updated when passed in Quantity.metadata #46

Open
umangsh opened this issue Jun 22, 2022 · 3 comments

Comments

@umangsh
Copy link

umangsh commented Jun 22, 2022

Is your feature request related to a problem? Please describe.
Is it possible to write HKCumulativeQuantitySample values? For instance, I'd like to write QuantityType.dietaryEnergyConsumed values per day. Sample code:

  final now = DateTime.now();
  final lastMidnight = DateTime(now.year, now.month, now.day);
  const harmonized = QuantityHarmonized(100, 'kcal', null);
  final steps = Quantity(
    '',
    QuantityType.dietaryEnergyConsumed.identifier,
    lastMidnight.millisecondsSinceEpoch,
    now.millisecondsSinceEpoch,
    null,
    SourceRevision(...),
    harmonized,
  );
  await HealthKitReporter.save(steps);

Currently, when data is saved this way, calorie values are split per hour in apple health.
In this example, 100 calories are added at 12 noon, and apple health displays (100/12) calories consumed every hour (midnight - 1, 1-2, ... )

Describe the solution you'd like
Cumulative daily consumption shown as a single data point.

@umangsh
Copy link
Author

umangsh commented Jun 23, 2022

HKMetadataKeySyncIdentifier and HKMetadataKeySyncVersion is relevant, setting them in QuantityHarmonized.metadata is ignored

    QuantityHarmonized(nutrient.amount!, nutrient.unit!, <String, dynamic>{
      'HKMetadataKeySyncIdentifier':
          '${DateFormat(constants.DATE_FORMAT).format(date)}-${nutrient.id}',
      'HKMetadataKeySyncVersion': DateTime.now().millisecondsSinceEpoch,
    }),

@umangsh umangsh changed the title Cumulative Quantity Sample HKMetadataKeySyncIdentifier / HKMetadataKeySyncVersion are not updated when passed in Quantity.metadata Jun 23, 2022
@umangsh
Copy link
Author

umangsh commented Jun 23, 2022

I'm using iphone 13 mini, ios 15.5. Unable to set HKMetadataKeySyncVersion as any type in Quantity.harmonized['metadata']

  1. HKMetadataKeySyncVersion can't be passed in as a String, exception in App Delegate.

QuantityHarmonized(100, 'kcal', <String, String>{
'HKMetadataKeySyncVersion': '${DateTime.now().millisecondsSinceEpoch}',
'HKMetadataKeySyncIdentifier':
// ignore: lines_longer_than_80_chars
'${constants.appTitle}-${DateFormat(constants.DATE_FORMAT).format(date)}-${nutrient.id}',
}),

throws @objc class AppDelegate: FlutterAppDelegate {
Thread 1: "Invalid class __NSCFString for metadata key: HKMetadataKeySyncVersion. Expected NSNumber."

  1. HKMetadataKeySyncVersion is dropped when passed as int / double, the cast from [String: Any] to [String: String] fails silently at https://github.com/VictorKachalov/HealthKitReporter/blob/6bae58ded9a089dd870e6db7480ace35775a2a85/Sources/Model/Payload/Quantity.swift#L190.

QuantityHarmonized(100, 'kcal', <String, dynamic>{
'HKMetadataKeySyncVersion': DateTime.now().millisecondsSinceEpoch,
'HKMetadataKeySyncIdentifier':
// ignore: lines_longer_than_80_chars
'${constants.appTitle}-${DateFormat(constants.DATE_FORMAT).format(date)}-${nutrient.id}',
}),

Debug output in Quantity.swift:

dictionary["metadata"] : Optional({
HKMetadataKeySyncIdentifier = "FAMNOM-2022-06-22-1092";
HKMetadataKeySyncVersion = 1656022275836;
})

metadata: nil

@kvs-coder
Copy link
Owner

Hi @umangsh

Please check new 2.1.0 version (be sure that updated HealthKitReporter to 2.0.0 is used)

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