update method

ProgressListener? update(
  1. bool allowChargeNetwork, {
  2. void onStatusUpdated(
    1. ContentUpdaterStatus status
    )?,
  3. void onProgressUpdated(
    1. int progress
    )?,
  4. void onComplete(
    1. GemError error
    )?,
  5. DataSavePolicy dataSavePolicy = DataSavePolicy.useDefault,
})

Starts or resumes the content update process.

Initiates downloading of available updates for the updater's contentType. The download runs in background and reports status and progress via optional callbacks. If the operation cannot be started the provided onComplete callback will be invoked with the corresponding GemError code and the method returns null.

Parameters

Returns

Also see:

  • apply: Apply a downloaded update when ready.

Implementation

ProgressListener? update(
  bool allowChargeNetwork, {
  void Function(ContentUpdaterStatus status)? onStatusUpdated,
  void Function(int progress)? onProgressUpdated,
  void Function(GemError error)? onComplete,
  DataSavePolicy dataSavePolicy = DataSavePolicy.useDefault,
}) {
  final EventDrivenProgressListener progressListener =
      EventDrivenProgressListener();

  if (onStatusUpdated != null) {
    progressListener.registerOnNotifyStatusChanged(
      (int status) =>
          onStatusUpdated(ContentUpdaterStatusExtension.fromId(status)),
    );
  }

  if (onProgressUpdated != null) {
    progressListener.registerOnProgress(
      (int progress) => onProgressUpdated(progress),
    );
  }

  if (onComplete != null) {
    progressListener.registerOnCompleteWithData(
      (int err, String hint, Map<dynamic, dynamic> json) =>
          onComplete(GemErrorExtension.fromCode(err)),
    );
  }

  GemKitPlatform.instance.registerEventHandler(
    progressListener.id,
    progressListener,
  );

  final OperationResult resultString = objectMethod(
    pointerId,
    'ContentUpdater',
    'update',
    args: <String, dynamic>{
      'allowChargeNetwork': allowChargeNetwork,
      'listener': progressListener.id,
      'savePolicy': dataSavePolicy.id,
    },
  );

  final GemError errorCode = GemErrorExtension.fromCode(
    resultString['result'],
  );
  if (errorCode != GemError.success) {
    onComplete?.call(errorCode);
    return null;
  }

  return progressListener;
}