download method

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

Implementation

@override
Future<List<DataValueSet>?> download(Function(RequestProgress, bool) callback,
    {Dio? dioTestClient}) async {
  callback(
      RequestProgress(
          resourceName: this.apiResourceName as String,
          message:
              'Downloading ${this.apiResourceName?.toLowerCase()} from the server....',
          status: '',
          percentage: 0),
      false);

  final dhisUrl = await this.dhisUrl();
  final response = await HttpClient.get(dhisUrl,
      database: this.database, dioTestClient: dioTestClient);

  final data = response.body;

  if (data != null && data['status'] != null && data['status'] == 'ERROR') {
    return [];
  }

  callback(
      RequestProgress(
          resourceName: this.apiResourceName as String,
          message:
              '${this.apiResourceName?.toLowerCase()}(${this.dataSet}-${this.orgUnit}-${this.period}) downloaded successfully',
          status: '',
          percentage: 50),
      false);
  data['dirty'] = false;
  data['synced'] = true;
  this.data = DataValueSet.fromJson(data);

  callback(
      RequestProgress(
          resourceName: this.apiResourceName as String,
          message:
              'Saving ${this.apiResourceName?.toLowerCase()}(${this.dataSet}-${this.orgUnit}-${this.period}) into phone database...',
          status: '',
          percentage: 51),
      false);

  await this.save();

  callback(
      RequestProgress(
          resourceName: this.apiResourceName as String,
          message:
              '${this.apiResourceName?.toLowerCase()}(${this.dataSet}-${this.orgUnit}-${this.period}) successfully saved into the database',
          status: '',
          percentage: 100),
      true);

  return [this.data];
}