downloadOne method

Future<List<ValidationRule>?> downloadOne(
  1. DataSet dataSet,
  2. num index,
  3. dynamic callback(
    1. RequestProgress,
    2. bool
    ), {
  4. Dio? dioTestClient,
})

Implementation

Future<List<ValidationRule>?> downloadOne(
    DataSet dataSet, num index, Function(RequestProgress, bool) callback,
    {Dio? dioTestClient}) async {
  callback(
      RequestProgress(
          resourceName: this.apiResourceName as String,
          message:
              'Downloading ${this.apiResourceName?.toLowerCase()} for ${dataSet.name} from the server....',
          status: '',
          percentage: 0),
      false);

  final response = await HttpClient.get(
      'validationRules?dataSet=${dataSet.id}&fields=id,name,displayName,created,lastUpdated,description,operator,instruction,displayInstruction,displayFormName,periodOffset,periodType,leftSide,rightSide',
      database: this.database,
      dioTestClient: dioTestClient);

  callback(
      RequestProgress(
          resourceName: this.apiResourceName as String,
          message:
              '${this.apiResourceName?.toLowerCase()} for ${dataSet.name} downloaded successfully',
          status: '',
          percentage: 50),
      false);

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

  List? data =
      response.body != null && response.body[this.apiResourceName] != null
          ? response.body[this.apiResourceName]?.toList()
          : [];

  this.data = (data ?? []).map((dataItem) {
    dataItem['dirty'] = false;
    dataItem['dataSet'] = dataSet.id;
    return ValidationRule.fromJson(dataItem);
  }).toList();
  await this.save();

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

  return this.data;
}