insertActivity method

ProgressListener? insertActivity(
  1. Activity activity, {
  2. required String fitFilePath,
  3. void onComplete(
    1. GemError error,
    2. int localActivityID
    )?,
})

Stores a new activity locally and queues it for sync.

Stored are the user, the title, the location, the times, the metrics and the sport type, effort, visibility and long description of Activity.record. The first entry of Activity.files, if any, gives the size and location of the recorded track; attach other files with addActivityFile. The store assigns the local id and, when empty, Activity.clientUuid and Activity.shareToken; read them back with getLocalActivity.

Parameters

  • activity: The activity; Activity.userID must be set.
  • fitFilePath: The path of the exported recorded track, which becomes the ActivityFileType.sensors file of the activity. Must not be empty; the file is not read here.
  • onComplete: Called with GemError.success and the local id of the activity, or with GemError.general and -1 when it could not be stored.

Returns

Implementation

ProgressListener? insertActivity(
  Activity activity, {
  required String fitFilePath,
  void Function(GemError error, int localActivityID)? onComplete,
}) {
  final EventDrivenProgressListener progressListener =
      EventDrivenProgressListener();
  GemKitPlatform.instance.registerEventHandler(
    progressListener.id,
    progressListener,
  );

  progressListener.registerOnCompleteWithData((
    int err,
    String hint,
    Map<dynamic, dynamic> json,
  ) {
    GemKitPlatform.instance.unregisterEventHandler(progressListener.id);
    onComplete?.call(GemErrorExtension.fromCode(err), json['result']);
  });

  final OperationResult resultString = objectMethod(
    pointerId,
    'ActivityStore',
    'insertActivity',
    args: <String, dynamic>{
      'activity': activity.toJson(),
      'fitFilePath': fitFilePath,
      'listener': progressListener.id,
    },
  );
  final GemError errorCode = GemErrorExtension.fromCode(
    resultString['result'],
  );

  if (errorCode != GemError.success) {
    onComplete?.call(errorCode, -1);
    return null;
  }

  return progressListener;
}