download method

  1. @override
Future<List<AttributeReservedValue>?> download(
  1. dynamic callback(
    1. RequestProgress,
    2. bool
    ), {
  2. Dio? dioTestClient,
})
override

Implementation

@override
Future<List<AttributeReservedValue>?> download(
    Function(RequestProgress, bool) callback,
    {Dio? dioTestClient}) async {
  callback(
      RequestProgress(
          resourceName: this.apiResourceName as String,
          message: 'Retrieving reserved attributes from phone database....',
          status: '',
          percentage: 0),
      false);
  List<ProgramTrackedEntityAttribute> reservedAttributes =
      await ProgramTrackedEntityAttributeQuery(database: database)
          .where(attribute: 'generated', value: true)
          .get();

  callback(
      RequestProgress(
          resourceName: this.apiResourceName as String,
          message:
              '${reservedAttributes.length} reserved attributes retrieved successfully',
          status: '',
          percentage: 50),
      false);

  callback(
      RequestProgress(
          resourceName: this.apiResourceName as String,
          message:
              'Downloading and saving reserved values for ${reservedAttributes.length} reserved attributes...',
          status: '',
          percentage: 51),
      false);

  final queue = Queue(parallel: 50);
  num availableItemCount = 0;

  reservedAttributes.forEach((reservedAttribute) {
    availableItemCount++;
    queue.add(() => downloadReservedValueByAttribute(reservedAttribute,
        dioTestClient: dioTestClient));
  });

  if (availableItemCount == 0) {
    queue.cancel();
  } else {
    await queue.onComplete;
  }

  callback(
      RequestProgress(
          resourceName: this.apiResourceName as String,
          message: 'Reserved values successfully saved into the database',
          status: '',
          percentage: 100),
      true);
  return await AttributeReservedValueQuery(database: database).get();
}