From ce60787ec2c4f97b98b36f9e1ec0f57ea47b4e6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elias=20M=C3=BCller?= Date: Thu, 11 May 2023 21:14:32 +0200 Subject: [PATCH] Make file downloads cancellable --- lib/screen/pages/files/fileElement.dart | 30 ++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/screen/pages/files/fileElement.dart b/lib/screen/pages/files/fileElement.dart index 25ab9c6..ded9f94 100644 --- a/lib/screen/pages/files/fileElement.dart +++ b/lib/screen/pages/files/fileElement.dart @@ -52,6 +52,7 @@ class FileElement extends StatefulWidget { class _FileElementState extends State { double percent = 0; + Future? downloadCore; Widget getSubtitle() { if(widget.file.currentlyDownloading) { @@ -89,13 +90,40 @@ class _FileElementState extends State { }, )); } else { + if(widget.file.currentlyDownloading) { + showDialog(context: context, builder: (context) { + return AlertDialog( + title: const Text("Download abbrechen?"), + content: const Text("Möchtest du den Download abbrechen?"), + actions: [ + TextButton(onPressed: () { + Navigator.of(context).pop(); + }, child: const Text("Nein")), + TextButton(onPressed: () { + downloadCore?.then((value) { + if(!value.isCancelled) value.cancel(); + Navigator.of(context).pop(); + }); + setState(() { + widget.file.currentlyDownloading = false; + percent = 0; + downloadCore = null; + }); + }, child: const Text("Ja, Abbrechen")) + ], + ); + }); + + return; + } + setState(() { widget.file.currentlyDownloading = true; }); log("Download: ${widget.file.path} to ${widget.file.name}"); - FileElement.download(widget.file.path, widget.file.name, (progress) { + downloadCore = FileElement.download(widget.file.path, widget.file.name, (progress) { setState(() => percent = progress); }, (result) { if(result.type != ResultType.done) {