Crossed out cancelled hours, implemented file uploading
This commit is contained in:
72
lib/screen/pages/files/fileUpload.dart
Normal file
72
lib/screen/pages/files/fileUpload.dart
Normal file
@ -0,0 +1,72 @@
|
||||
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),
|
||||
);
|
||||
}
|
||||
}
|
@ -5,9 +5,9 @@ import 'dart:io';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:loader_overlay/loader_overlay.dart';
|
||||
import 'package:marianum_mobile/api/marianumcloud/webdav/queries/listFiles/cacheableFile.dart';
|
||||
import 'package:marianum_mobile/screen/pages/files/fileUpload.dart';
|
||||
import 'package:marianum_mobile/widget/errorView.dart';
|
||||
import 'package:nextcloud/nextcloud.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:persistent_bottom_nav_bar/persistent_tab_view.dart';
|
||||
|
||||
import '../../../api/marianumcloud/webdav/queries/listFiles/listFilesCache.dart';
|
||||
import '../../../api/marianumcloud/webdav/queries/listFiles/listFilesResponse.dart';
|
||||
@ -77,7 +77,10 @@ class _FilesState extends State<Files> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_query();
|
||||
}
|
||||
|
||||
void _query() {
|
||||
ListFilesCache(
|
||||
path: widget.path.isEmpty ? "/" : widget.path.join("/"),
|
||||
onUpdate: (ListFilesResponse d) => {
|
||||
@ -192,13 +195,19 @@ class _FilesState extends State<Files> {
|
||||
child: const Icon(Icons.upload),
|
||||
),
|
||||
body: data == null ? const Center(child: CircularProgressIndicator()) : data!.files.isEmpty ? const ErrorView(icon: Icons.folder_off_rounded, text: "Der Ordner ist leer") : LoaderOverlay(
|
||||
child: ListView.builder(
|
||||
itemCount: files.length,
|
||||
itemBuilder: (context, index) {
|
||||
CacheableFile file = files.toList().skip(index).first;
|
||||
return FileElement(file, widget.path);
|
||||
child: RefreshIndicator(
|
||||
onRefresh: () {
|
||||
_query();
|
||||
return Future.delayed(const Duration(seconds: 3));
|
||||
},
|
||||
),
|
||||
child: ListView.builder(
|
||||
itemCount: files.length,
|
||||
itemBuilder: (context, index) {
|
||||
CacheableFile file = files.toList().skip(index).first;
|
||||
return FileElement(file, widget.path);
|
||||
},
|
||||
),
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
@ -209,26 +218,10 @@ class _FilesState extends State<Files> {
|
||||
return;
|
||||
}
|
||||
|
||||
SharedPreferences preferences = await SharedPreferences.getInstance();
|
||||
WebDavClient client = NextcloudClient("https://cloud.marianum-fulda.de/", username: preferences.getString("username"), password: preferences.getString("password"), loginName: preferences.getString("username")).webdav;
|
||||
|
||||
log("UPLOAD STARTING: $path");
|
||||
context.loaderOverlay.show();
|
||||
File file = File(path);
|
||||
var remotePath = "${widget.path.join("/")}/${file.path.split(Platform.pathSeparator).last}";
|
||||
PersistentNavBarNavigator.pushNewScreen(context, screen: FileUpload(localPath: path, remotePath: remotePath), withNavBar: false);
|
||||
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) {
|
||||
return SimpleDialog(
|
||||
children: [
|
||||
Image.memory(file.readAsBytesSync()),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
client.upload(file.readAsBytesSync(), "/${file.path.split(Platform.pathSeparator).last}").then((value) {
|
||||
log("UPLOADED ${value.statusCode}");
|
||||
context.loaderOverlay.hide();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user