Implemented simple and basic file downloading
This commit is contained in:
89
lib/screen/pages/files/fileDownload.dart
Normal file
89
lib/screen/pages/files/fileDownload.dart
Normal file
@ -0,0 +1,89 @@
|
||||
import 'dart:developer';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flowder/flowder.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:marianum_mobile/api/marianumcloud/webdav/webdavApi.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
|
||||
class FileDownload extends StatefulWidget {
|
||||
String source;
|
||||
String name;
|
||||
FileDownload(this.source, this.name, {Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<FileDownload> createState() => _FileDownloadState();
|
||||
}
|
||||
|
||||
class _FileDownloadState extends State<FileDownload> {
|
||||
late DownloaderCore core;
|
||||
|
||||
void download(String remotePath, String name) async {
|
||||
List<Directory>? paths = await getExternalStorageDirectories(type: StorageDirectory.downloads);
|
||||
|
||||
String local = paths!.first.path + Platform.pathSeparator + name;
|
||||
|
||||
DownloaderUtils options = DownloaderUtils(
|
||||
progressCallback: (current, total) {
|
||||
final progress = (current / total) * 100;
|
||||
setState(() => {
|
||||
percent = progress,
|
||||
});
|
||||
},
|
||||
file: File(local),
|
||||
progress: ProgressImplementation(),
|
||||
deleteOnCancel: true,
|
||||
onDone: () {
|
||||
// OpenFile.open(local).then((value) => () {
|
||||
// log("Result: ${value.toString()}");
|
||||
// });
|
||||
Navigator.of(context).pop();
|
||||
showDialog(context: context, builder: (context) {
|
||||
return AlertDialog(
|
||||
icon: const Icon(Icons.link),
|
||||
title: const Text("Dateipfad"),
|
||||
content: Text(local),
|
||||
);
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
log(local);
|
||||
|
||||
core = await Flowder.download(
|
||||
"${await WebdavApi.webdavConnectString}$remotePath",
|
||||
options,
|
||||
);
|
||||
}
|
||||
|
||||
double percent = 0;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
log("Downloading file: Source(${widget.source}), Name(${widget.name})");
|
||||
download(widget.source, widget.name);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return AlertDialog(
|
||||
title: const Text("Download"),
|
||||
content: ListTile(
|
||||
leading: const Icon(Icons.download),
|
||||
title: LinearProgressIndicator(value: percent/100),
|
||||
trailing: Text("${percent.round()}%"),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
child: const Text("Abbrechen"),
|
||||
onPressed: () => () {
|
||||
if(!core.isCancelled) core.cancel();
|
||||
Navigator.of(context, rootNavigator: true).pop();
|
||||
},
|
||||
)
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
@ -1,7 +1,10 @@
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:filesize/filesize.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:jiffy/jiffy.dart';
|
||||
import 'package:marianum_mobile/api/marianumcloud/webdav/queries/listFiles/cacheableFile.dart';
|
||||
import 'package:marianum_mobile/screen/pages/files/fileDownload.dart';
|
||||
import 'package:marianum_mobile/widget/errorView.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
@ -66,11 +69,10 @@ class _FilesState extends State<Files> {
|
||||
props.enterFolder(file.name);
|
||||
} else {
|
||||
//TODO implement file download / view
|
||||
log(file.path);
|
||||
|
||||
showDialog(context: context, builder: (context) {
|
||||
return const AlertDialog(
|
||||
title: Text("Datei öffnen"),
|
||||
content: Text("Das öffnen von Dateien ist noch nicht implementiert!"),
|
||||
);
|
||||
return FileDownload(file.path, file.name);
|
||||
});
|
||||
}
|
||||
},
|
||||
|
Reference in New Issue
Block a user