Added in-app fileviewer for images and pdf files

This commit is contained in:
Elias Müller 2023-06-18 18:15:49 +02:00
parent 6fda2f1078
commit 3d6e0cec0e
3 changed files with 77 additions and 9 deletions

View File

@ -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<DownloaderCore> download(String remotePath, String name, Function(double) onProgress, Function(OpenResult) onDone) async {
Directory paths = await getApplicationDocumentsDirectory();
static Future<DownloaderCore> 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<OpenResult> result = OpenFile.open(local);
result.then((value) => {
onDone(value)
});
//Future<OpenResult> 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<FileElement> {
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) {

View File

@ -235,7 +235,7 @@ class _ChatBubbleState extends State<ChatBubble> {
}
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;

View File

@ -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);
}
}
}