import 'dart:developer'; import 'dart:io'; import 'package:better_open_file/better_open_file.dart'; import 'package:filesize/filesize.dart'; import 'package:flowder/flowder.dart'; import 'package:flutter/cupertino.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/files.dart'; import 'package:path_provider/path_provider.dart'; import '../../../api/marianumcloud/webdav/webdavApi.dart'; class FileElement extends StatefulWidget { final CacheableFile file; final List path; const FileElement(this.file, this.path, {Key? key}) : super(key: key); static Future download(String remotePath, String name, Function(double) onProgress, Function(OpenResult) onDone) async { Directory paths = await getApplicationDocumentsDirectory(); String local = paths.path + Platform.pathSeparator + name; DownloaderUtils options = DownloaderUtils( progressCallback: (current, total) { final progress = (current / total) * 100; onProgress(progress); }, file: File(local), progress: ProgressImplementation(), deleteOnCancel: true, onDone: () { Future result = OpenFile.open(local); result.then((value) => { onDone(value) }); }, ); return await Flowder.download( "${await WebdavApi.webdavConnectString}$remotePath", options, ); } @override State createState() => _FileElementState(); } class _FileElementState extends State { double percent = 0; Future? downloadCore; Widget getSubtitle() { if(widget.file.currentlyDownloading) { return Row( children: [ Container( margin: const EdgeInsets.only(right: 10), child: const Text("Download:"), ), Expanded( child: LinearProgressIndicator(value: percent/100), ), Container( margin: const EdgeInsets.only(left: 10), child: Text("${percent.round()}%"), ), ], ); } return widget.file.isDirectory ? Text("geändert ${Jiffy(widget.file.modifiedAt).fromNow()}") : Text("${filesize(widget.file.size)}, ${Jiffy(widget.file.modifiedAt).fromNow()}"); } @override Widget build(BuildContext context) { return ListTile( leading: Icon(widget.file.isDirectory ? Icons.folder : Icons.file_open_outlined), title: Text(widget.file.name), subtitle: getSubtitle(), trailing: Icon(widget.file.isDirectory ? Icons.arrow_right : Icons.open_in_browser), onTap: () { if(widget.file.isDirectory) { Navigator.of(context).push(MaterialPageRoute( builder: (context) { return Files(widget.path.toList()..add(widget.file.name)); }, )); } 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}"); downloadCore = FileElement.download(widget.file.path, widget.file.name, (progress) { setState(() => percent = progress); }, (result) { if(result.type != ResultType.done) { showDialog(context: context, builder: (context) { return AlertDialog( title: const Text("Download"), content: Text(result.message), ); }); } setState(() { widget.file.currentlyDownloading = false; percent = 0; }); }); } }, onLongPress: () { showModalBottomSheet( context: context, builder: (context) { return ListView( children: [ ListTile( leading: const Icon(Icons.delete), title: Text("'${widget.file.name}' Löschen"), ), const ListTile( leading: Icon(Icons.share), title: Text("Teilen"), ) ], ); }, ); }, ); } }