download method

dynamic download({
  1. required dynamic callback(
    1. List<RequestProgress>?,
    2. bool,
    3. dynamic
    ),
  2. Database? database,
})

Implementation

download(
    {required Function(List<RequestProgress>?, bool, dynamic) callback,
    Database? database}) async {
  if (!this.metadataSyncInProgress) {
    this.metadataSyncInProgress = true;

    try {
      List<QueryResource> queryResources = QueryResource.getQueryResources(
          currentUser: null, database: database);

      List<RequestProgress> requestProgresses =
          queryResources.map((QueryResource queryResource) {
        return RequestProgress(
            resourceName: queryResource.name,
            message:
                'Downloading ${queryResource.name.toUpperCase()} from the server',
            status: '',
            percentage: 0);
      }).toList();

      final queue = Queue(parallel: 2);

      queryResources.forEach((queryResource) {
        queue.add(() => HttpClient.get(queryResource.dhisUrl));
      });

      await queue.onComplete;

      callback(requestProgresses, false, null);
    } catch (e) {
      callback(null, false, e);
    }
  }
}