download method

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

Implementation

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

  List data =
      response.body != null && response.body[this.apiResourceName] != null
          ? response.body[this.apiResourceName]?.toList()
          : [];
  if (response.body == null || response.body[this.apiResourceName] == null) {
    callback(
        RequestProgress(
            resourceName: this.apiResourceName as String,
            message:
                'Downloading ${this.apiResourceName} failed with status code ${response.statusCode}',
            status: '',
            percentage: 0),
        false);
  }

  if (data.isEmpty) {
    callback(
        RequestProgress(
            resourceName: this.apiResourceName as String,
            message: 'No ${this.apiResourceName} found.',
            status: '',
            percentage: 0),
        true);
  }

  if (data.isNotEmpty) {
    this.data = data.map((dataItem) {
      dataItem['dirty'] = false;
      dataItem['synced'] = true;
      return User.fromApi(dataItem);
    }).toList();

    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();

    final List<UserGroupUser> userGroupsList = [];

    for (var user in data) {
      for (var userGroup in user['userGroups']) {
        final group = {
          'id': '${user['id']}_${userGroup['id']}',
          'userId': user['id'],
          'groupId': userGroup['id']
        };

        userGroupsList.add(UserGroupUser.fromJson(group));
      }
    }

    await UserGroupUserQuery(database: database)
        .setData(userGroupsList)
        .save();

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

  return this.data;
}