diff --git a/lib/view/pages/files/fileElement.dart b/lib/view/pages/files/fileElement.dart index d65f9d1..1db2a57 100644 --- a/lib/view/pages/files/fileElement.dart +++ b/lib/view/pages/files/fileElement.dart @@ -11,6 +11,7 @@ import 'package:path_provider/path_provider.dart'; import '../../../api/marianumcloud/webdav/queries/listFiles/cacheableFile.dart'; import '../../../api/marianumcloud/webdav/webdavApi.dart'; import '../../../widget/confirmDialog.dart'; +import '../../../widget/fileViewer.dart'; import '../../../widget/unimplementedDialog.dart'; import 'files.dart'; @@ -20,8 +21,8 @@ class FileElement extends StatefulWidget { final void Function() refetch; const FileElement(this.file, this.path, this.refetch, {Key? key}) : super(key: key); - static Future download(String remotePath, String name, Function(double) onProgress, Function(OpenResult) onDone) async { - Directory paths = await getApplicationDocumentsDirectory(); + static Future download(BuildContext context, String remotePath, String name, Function(double) onProgress, Function(OpenResult) onDone) async { + Directory paths = await getTemporaryDirectory(); String local = paths.path + Platform.pathSeparator + name; @@ -34,11 +35,12 @@ class FileElement extends StatefulWidget { progress: ProgressImplementation(), deleteOnCancel: true, onDone: () { - Future result = OpenFile.open(local); - - result.then((value) => { - onDone(value) - }); + //Future result = OpenFile.open(local); // TODO legacy - refactor remove + Navigator.of(context).push(MaterialPageRoute(builder: (context) => FileViewer(path: local))); + onDone(OpenResult(message: "File viewer opened", type: ResultType.done)); + // result.then((value) => { + // onDone(value) + // }); }, ); @@ -125,7 +127,7 @@ class _FileElementState extends State { log("Download: ${widget.file.path} to ${widget.file.name}"); - downloadCore = FileElement.download(widget.file.path, widget.file.name, (progress) { + downloadCore = FileElement.download(context, widget.file.path, widget.file.name, (progress) { setState(() => percent = progress); }, (result) { if(result.type != ResultType.done) { diff --git a/lib/view/pages/talk/chatBubble.dart b/lib/view/pages/talk/chatBubble.dart index 918506e..10b355b 100644 --- a/lib/view/pages/talk/chatBubble.dart +++ b/lib/view/pages/talk/chatBubble.dart @@ -235,7 +235,7 @@ class _ChatBubbleState extends State { } downloadProgress = 1; - downloadCore = FileElement.download(message.file!.path!, message.file!.name, (progress) { + downloadCore = FileElement.download(context, message.file!.path!, message.file!.name, (progress) { if(progress > 1) { setState(() { downloadProgress = progress; diff --git a/lib/widget/fileViewer.dart b/lib/widget/fileViewer.dart new file mode 100644 index 0000000..a87e8d8 --- /dev/null +++ b/lib/widget/fileViewer.dart @@ -0,0 +1,66 @@ +import 'dart:io'; + +import 'package:better_open_file/better_open_file.dart'; +import 'package:flutter/material.dart'; +import 'package:photo_view/photo_view.dart'; +import 'package:syncfusion_flutter_pdfviewer/pdfviewer.dart'; + +import 'confirmDialog.dart'; +import 'placeholderView.dart'; + +class FileViewer extends StatelessWidget { + final String path; + final bool openExternal; + const FileViewer({super.key, required this.path, this.openExternal = false}); + + + @override + Widget build(BuildContext context) { + AppBar appbar = AppBar( + title: Text(path.split("/").last), + actions: [ + IconButton(onPressed: () => ConfirmDialog( + title: "Extern öffnen", + content: "Möchtest du die Datei mit dem Systemdialog öffnen?", + onConfirm: () => Navigator.of(context).push(MaterialPageRoute(builder: (context) => FileViewer(path: path, openExternal: true))), + confirmButton: "Öffnen", + ).asDialog(context), icon: const Icon(Icons.open_in_new)) + ], + ); + switch(openExternal ? "x" : path.split(".").last) { + case "png": + case "jpg": + case "jpeg": + return Scaffold( + appBar: appbar, + backgroundColor: Colors.white, + body: PhotoView( + imageProvider: Image.file(File(path)).image, + backgroundDecoration: const BoxDecoration(color: Colors.white60), + ) + ); + + + case "pdf": + return Scaffold( + appBar: appbar, + body: SfPdfViewer.file( + File(path), + ), + ); + + default: + OpenFile.open(path).then((result) { + Navigator.of(context).pop(); + if(result.type != ResultType.done) { + showDialog(context: context, builder: (context) { + return AlertDialog( + content: Text(result.message), + ); + }); + } + }); + return const PlaceholderView(text: "Datei wird extern geöffnet...", icon: Icons.open_in_new); + } + } +}