downloadContentsZip static method

Future<void> downloadContentsZip(
  1. String appBundle,
  2. bool usb,
  3. dynamic setProgress(
    1. double
    ),
  4. dynamic setError(
    1. String
    ),
  5. dynamic setAllOk(
    1. bool
    ),
)

Implementation

static Future<void> downloadContentsZip(String appBundle, bool usb, Function(double) setProgress,
    Function(String) setError, Function(bool) setAllOk) async {
  double progress = 0.0;
  String error = "";
  // TODO? revisar y optimizar
  try {
    String url = usb ? "http://127.0.0.1" : "http://192.168.2.101";
    Directory? directory = await getExternalStorageDirectory();

    if (directory != null) {
      String zipFilePath = '${directory.path}/gvam.zip';
      await Dio().download("$url:8000/get_files/$appBundle", zipFilePath,
          options: Options(responseType: ResponseType.bytes), onReceiveProgress: (count, total) {
        if (total != -1) {
          progress = count / total;
          setProgress(progress);
        }
      });

      descompressZipByPath(zipFilePath, directory);
      setAllOk(true);
      Dio().post("$url:8000/increment_call_count");
    } else {
      throw Exception('Failed to get storage directory.');
    }
  } catch (e) {
    error = 'Error: $e';
    setError(error);
  }
}