download method

  1. @override
void download({
  1. String? url,
  2. File? file,
  3. void onReceiveProgress(
    1. int received,
    2. int total,
    3. bool failed
    )?,
  4. void onDone()?,
})
override

Implementation

@override
void download({
  String? url,
  File? file,
  void Function(int received, int total, bool failed)? onReceiveProgress,
  void Function()? onDone,
}) async {
  if (status != UpgradeStatus.available) {
    debugPrint("[UpgradeManager] The update is not available.");
    return;
  }

  final uri = Uri.parse(url ?? fileURL);

  state.updateUpgradeStatus(status: UpgradeStatus.downloading);

  var received = 0;
  var contentLength = 0;

  try {
    final StreamedResponse response = await Client().send(Request('GET', uri));
    contentLength = response.contentLength ?? 1;

    file ??= await defaultFileLocation(uri.pathSegments.last);
    final sink = file.openWrite();

    await response.stream.map((stream) {
      received += stream.length;
      onReceiveProgress?.call(received, contentLength, false);
      return stream;
    }).pipe(sink);

    sink.close();
    filePath = file.absolute.path;
    state.updateUpgradeStatus(status: UpgradeStatus.readyToInstall);
    onReceiveProgress?.call(contentLength, contentLength, false);
    onDone?.call();
  } catch (e) {
    onReceiveProgress?.call(received, contentLength, true);
    state.updateUpgradeStatus(status: UpgradeStatus.error);
    debugPrint("[UpgradeManager] Downloading Error: ${e.toString()}");
  }
}