137 lines
4.1 KiB
Dart
137 lines
4.1 KiB
Dart
|
|
import 'package:flutter/material.dart';
|
|
import 'package:marianum_mobile/api/marianumcloud/webdav/queries/listFiles/cacheableFile.dart';
|
|
import 'package:marianum_mobile/widget/errorView.dart';
|
|
|
|
import '../../../api/marianumcloud/webdav/queries/listFiles/listFilesCache.dart';
|
|
import '../../../api/marianumcloud/webdav/queries/listFiles/listFilesResponse.dart';
|
|
import '../../../data/files/filesProps.dart';
|
|
import 'fileElement.dart';
|
|
|
|
class Files extends StatefulWidget {
|
|
List<String> path;
|
|
Files(this.path, {Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<Files> createState() => _FilesState();
|
|
}
|
|
|
|
class BetterSortOption {
|
|
String displayName;
|
|
int Function(CacheableFile, CacheableFile) compare;
|
|
|
|
BetterSortOption({required this.displayName, required this.compare});
|
|
}
|
|
|
|
enum SortOption {
|
|
name,
|
|
date,
|
|
size
|
|
}
|
|
|
|
class SortOptions {
|
|
static Map<SortOption, BetterSortOption> options = {
|
|
SortOption.name: BetterSortOption(
|
|
displayName: "Name",
|
|
compare: (CacheableFile a, CacheableFile b) => a.name.compareTo(b.name)
|
|
),
|
|
SortOption.date: BetterSortOption(
|
|
displayName: "Datum",
|
|
compare: (CacheableFile a, CacheableFile b) => a.modifiedAt!.compareTo(b.modifiedAt!)
|
|
),
|
|
SortOption.size: BetterSortOption(
|
|
displayName: "Größe",
|
|
compare: (CacheableFile a, CacheableFile b) {
|
|
if(a.isDirectory || b.isDirectory) return a.isDirectory ? 1 : 0;
|
|
if(a.size == null) return 0;
|
|
if(b.size == null) return 1;
|
|
return a.size!.compareTo(b.size!);
|
|
}
|
|
)
|
|
};
|
|
|
|
static String getDisplayName(SortOption option) {
|
|
return options[option]?.displayName ?? "";
|
|
}
|
|
|
|
static BetterSortOption getOption(SortOption option) {
|
|
return options[option]!;
|
|
}
|
|
}
|
|
|
|
class _FilesState extends State<Files> {
|
|
FilesProps props = FilesProps();
|
|
ListFilesResponse? data;
|
|
|
|
SortOption currentSort = SortOption.name;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
|
|
ListFilesCache(
|
|
path: widget.path.isEmpty ? "/" : widget.path.join("/"),
|
|
onUpdate: (ListFilesResponse d) => {
|
|
setState(() {
|
|
data = d;
|
|
}),
|
|
}
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
if(data == null) return SizedBox.shrink();
|
|
List<CacheableFile> files = data!.files.toList()..sort(SortOptions.getOption(currentSort).compare);
|
|
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: Text(widget.path.isNotEmpty ? widget.path.last : "Dateien"),
|
|
actions: [
|
|
IconButton(
|
|
icon: const Icon(Icons.search),
|
|
onPressed: () => {},
|
|
),
|
|
PopupMenuButton<SortOption>(
|
|
icon: const Icon(Icons.sort),
|
|
itemBuilder: (context) {
|
|
return SortOptions.options.keys.map((key) => PopupMenuItem<SortOption>(value: key, child: Text(SortOptions.getDisplayName(key)))).toList();
|
|
},
|
|
onSelected: (e) {
|
|
setState(() {
|
|
currentSort = e;
|
|
});
|
|
},
|
|
),
|
|
],
|
|
),
|
|
body: data == null ? const Center(child: CircularProgressIndicator()) : data!.files.isEmpty ? const ErrorView(icon: Icons.folder_off_rounded, text: "Der Ordner ist leer") : ListView.builder(
|
|
itemCount: files.length,
|
|
itemBuilder: (context, index) {
|
|
CacheableFile file = files.toList().skip(index).first;
|
|
return FileElement(file, widget.path);
|
|
},
|
|
),
|
|
// Consumer<FilesProps>(
|
|
// builder: (context, value, child) {
|
|
//
|
|
// if(value.listFilesResponse.files.isEmpty) {
|
|
// return const ErrorView(text: "Der Ordner ist leer", icon: Icons.folder_off_outlined);
|
|
// }
|
|
//
|
|
// List<CacheableFile> files = value.listFilesResponse.files.toList();
|
|
// files.sort((a, b) => a.isDirectory ? -1 : 1);
|
|
//
|
|
// return ListView.builder(
|
|
// itemCount: files.length,
|
|
// itemBuilder: (context, index) {
|
|
// CacheableFile file = files.skip(index).first;
|
|
// return FileElement(file, props);
|
|
// },
|
|
// );
|
|
// }
|
|
// ),
|
|
);
|
|
}
|
|
}
|