kvs-coder / health_kit_reporter

A Flutter wrapper for the HealthKitReporter library
https://pub.dev/packages/health_kit_reporter
MIT License
37 stars 35 forks source link

Save WorkoutRouteHarmonized #40

Open bazrafkan opened 2 years ago

bazrafkan commented 2 years ago

I try to save a workout route harmonized but I git error

I try to save like this:

try {
      final canWrite = await HealthKitReporter.isAuthorizedToWrite(
        SeriesType.workoutRoute.identifier,
      );
      if (canWrite) {
        final now = DateTime.now();
        const duration = 60;
        final minuteAgo = now.add(Duration(seconds: -duration));
        final harmonized = WorkoutRouteHarmonized(
          10,
          [
            WorkoutRouteBatch([
              WorkoutRouteLocation(
                43.584231,
                -84.787381,
                281.000,
                10.0,
                null,
                null,
                20.0,
                2.0,
                null,
                minuteAgo.microsecondsSinceEpoch,
                10.0,
              ),
              WorkoutRouteLocation(
                43.582840,
                -84.787370,
                281.000,
                10.0,
                null,
                null,
                20.0,
                2.0,
                null,
                now.millisecondsSinceEpoch,
                10.0,
              ),
            ], true),
          ],
          null,
        );

        final workout = WorkoutRoute(
          'testWorkoutUUID',
          'bicycle',
          minuteAgo.millisecondsSinceEpoch,
          now.millisecondsSinceEpoch,
          device,
          sourceRevision,
          harmonized,
        );
        print('try to save: ${workout.map}');
        final saved = await HealthKitReporter.save(workout);
        print('workoutSaved: $saved');
      } else {
        print('error canWrite workout: $canWrite');
      }
    } catch (e) {
      print(e);
    }

Error

flutter: isRequested auth: true
flutter: try to save: {uuid: testWorkoutUUID, identifier: bicycle, startTimestamp: 1650733338915, endTimestamp: 1650733398915, device: {name: FlutterTracker, manufacturer: kvs, model: T-800, hardwareVersion: 3, firmwareVersion: 3.0, softwareVersion: 1.1.1, localIdentifier: kvs.sample.app, udiDeviceIdentifier: 444-888-555}, sourceRevision: {source: {name: myApp, bundleIdentifier: com.kvs.health_kit_reporter_example}, version: 5, productType: fit, systemVersion: 4, operatingSystem: {majorVersion: 1, minorVersion: 2, patchVersion: 3}}, harmonized: {count: 10, routes: [{locations: [{latitude: 43.584231, longitude: -84.787381, altitude: 281.0, course: 10.0, courseAccuracy: null, floor: null, horizontalAccuracy: 20.0, speed: 2.0, speedAccuracy: null, timestamp: 1650733338915182, verticalAccuracy: 10.0}, {latitude: 43.58284, longitude: -84.78737, altitude: 281.0, course: 10.0, courseAccuracy: null, floor: null, horizontalAccuracy: 20.0, speed: 2.0, speedAccuracy: null, timestamp: 1650733398915, verticalAccuracy: 10.0}], done: true}], metadata: null}}
Unsupported value: HealthKitReporter.HealthKitError.invalidValue("Invalid arguments: [:]") of type __SwiftNativeNSError
*** Assertion failure in -[FlutterStandardWriter writeValue:], FlutterStandardCodec.mm:338
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unsupported value for standard codec'
*** First throw call stack:
(0x180f5370c 0x19868ef04 0x1826f4b00 0x103ded08c 0x103ded7ec 0x103dea7dc 0x10119ead8 0x1011b1e20 0x1011b0868 0x1011a5fbc 0x1011b0910 0x103dea744 0x103923c78 0x103cb1b3c 0x103bd9148 0x103bdceac 0x180f67c58 0x180ef14e8 0x180eec0a0 0x180ecbd94 0x180edec30 0x1a18f9988 0x1836d9c50 0x1834733d0 0x100d170dc 0x1010343d0)
libc++abi: terminating with uncaught exception of type NSException
* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
    frame #0: 0x00000001bb6a1bbc libsystem_kernel.dylib`__pthread_kill + 8
libsystem_kernel.dylib`__pthread_kill:
->  0x1bb6a1bbc <+8>:  b.lo   0x1bb6a1bd8               ; <+36>
    0x1bb6a1bc0 <+12>: stp    x29, x30, [sp, #-0x10]!
    0x1bb6a1bc4 <+16>: mov    x29, sp
    0x1bb6a1bc8 <+20>: bl     0x1bb69d60c               ; cerror_nocancel
Target 0: (Runner) stopped.

Thanks.

kvs-coder commented 2 years ago

@bazrafkan

Hi, currently there is a lack to save workout routes. It will be a separate method in new versions, because on the native side it should be implemented using HKWorkoutRouteBuilder. Please refer to https://developer.apple.com/documentation/healthkit/workouts_and_activity_rings/creating_a_workout_route