download method

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

Implementation

@override
Future<List<DataStore>?> download(Function(RequestProgress, bool) callback,
    {Dio? dioTestClient}) async {
  if (this.namespace == null || this.key == null) {
    throw HttpException(
        'Provide namespace and key while downloading datastore');
  } else {
    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);

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

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

      await this.save();

      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;
  }
}