Make file downloads cancellable
This commit is contained in:
parent
53799dcc9a
commit
ce60787ec2
@ -52,6 +52,7 @@ class FileElement extends StatefulWidget {
|
|||||||
|
|
||||||
class _FileElementState extends State<FileElement> {
|
class _FileElementState extends State<FileElement> {
|
||||||
double percent = 0;
|
double percent = 0;
|
||||||
|
Future<DownloaderCore>? downloadCore;
|
||||||
|
|
||||||
Widget getSubtitle() {
|
Widget getSubtitle() {
|
||||||
if(widget.file.currentlyDownloading) {
|
if(widget.file.currentlyDownloading) {
|
||||||
@ -89,13 +90,40 @@ class _FileElementState extends State<FileElement> {
|
|||||||
},
|
},
|
||||||
));
|
));
|
||||||
} else {
|
} 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(() {
|
setState(() {
|
||||||
widget.file.currentlyDownloading = true;
|
widget.file.currentlyDownloading = true;
|
||||||
});
|
});
|
||||||
|
|
||||||
log("Download: ${widget.file.path} to ${widget.file.name}");
|
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);
|
setState(() => percent = progress);
|
||||||
}, (result) {
|
}, (result) {
|
||||||
if(result.type != ResultType.done) {
|
if(result.type != ResultType.done) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user