download method

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

Implementation

@override
Future<List<TrackedEntityInstance>?> 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);

  this.data = await this.fetchOnline(dioTestClient: dioTestClient);

  if ((data ?? []).isNotEmpty) {
    callback(
        RequestProgress(
            resourceName: this.apiResourceName as String,
            message:
                '${data.length} ${this.apiResourceName?.toLowerCase()} downloaded successfully',
            status: '',
            percentage: 50),
        false);

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

    await this.save();

    List<TrackedEntityInstanceRelationship> relationships = [];
    data.forEach((dataItem) {
      relationships = [...relationships, ...dataItem.relationships];
    });

    if (relationships.length > 0) {
      await downloadRelatedTrackedEntityInstances(
          relationships: relationships, dioTestClient: dioTestClient);
    }

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

    return this.data;
  }
  callback(
      RequestProgress(
          resourceName: this.apiResourceName as String,
          message: 'No ${this.apiResourceName?.toLowerCase()} found',
          status: '',
          percentage: 100),
      true);
  return this.data;
}