Make file downloads cancellable

This commit is contained in:
Elias Müller 2023-05-11 21:14:32 +02:00
parent 53799dcc9a
commit ce60787ec2

View File

@ -52,6 +52,7 @@ class FileElement extends StatefulWidget {
class _FileElementState extends State<FileElement> {
double percent = 0;
Future<DownloaderCore>? downloadCore;
Widget getSubtitle() {
if(widget.file.currentlyDownloading) {
@ -89,13 +90,40 @@ class _FileElementState extends State<FileElement> {
},
));
} 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) {