73 lines
2.0 KiB
Dart
73 lines
2.0 KiB
Dart
import 'dart:developer';
|
|
import 'dart:io';
|
|
|
|
import 'package:async/async.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:marianum_mobile/api/marianumcloud/webdav/webdavApi.dart';
|
|
|
|
class FileUpload extends StatefulWidget {
|
|
final String localPath;
|
|
final String remotePath;
|
|
const FileUpload({Key? key, required this.localPath, required this.remotePath}) : super(key: key);
|
|
|
|
@override
|
|
State<FileUpload> createState() => _FileUploadState();
|
|
}
|
|
|
|
class _FileUploadState extends State<FileUpload> {
|
|
CancelableOperation? cancelableOperation;
|
|
late File localFile;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
localFile = File(widget.localPath);
|
|
}
|
|
|
|
bool isRunning() {
|
|
return cancelableOperation != null && !(cancelableOperation!.isCompleted);
|
|
}
|
|
|
|
bool isComplete() {
|
|
return cancelableOperation?.isCompleted ?? false;
|
|
}
|
|
|
|
void start() async {
|
|
cancelableOperation = CancelableOperation.fromFuture(
|
|
(await WebdavApi.webdav).upload(localFile.readAsBytesSync(), widget.remotePath).then((value) {
|
|
log("Upload done!");
|
|
}),
|
|
onCancel: () => log("Upload cancelled"),
|
|
);
|
|
|
|
cancelableOperation!.then((e) {
|
|
setState(() {});
|
|
});
|
|
setState(() {});
|
|
}
|
|
|
|
void stop() {
|
|
cancelableOperation?.cancel();
|
|
setState(() {});
|
|
Navigator.of(context).pop();
|
|
}
|
|
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
List<Widget> actions = List<Widget>.empty(growable: true);
|
|
if(!isRunning() && !isComplete()) actions.add(TextButton(onPressed: start, child: const Text("Upload starten")));
|
|
if(isRunning()) actions.add(TextButton(onPressed: stop, child: const Text("Upload Abbrechen")));
|
|
if(isComplete()) actions.add(TextButton(onPressed: Navigator.of(context).pop, child: const Text("Fertig")));
|
|
|
|
return AlertDialog(
|
|
title: const Text("Hochladen"),
|
|
content: Center(
|
|
child: isRunning() ? const CircularProgressIndicator() : const Text("Datei hochladen"),
|
|
),
|
|
actions: actions,
|
|
icon: const Icon(Icons.upload),
|
|
);
|
|
}
|
|
}
|