Added ascending and descending sort option in files

This commit is contained in:
Elias Müller 2023-05-08 20:00:28 +02:00
parent 9a7813a9fc
commit 1f9d6ca6bf

@ -19,8 +19,9 @@ class Files extends StatefulWidget {
class BetterSortOption {
String displayName;
int Function(CacheableFile, CacheableFile) compare;
IconData icon;
BetterSortOption({required this.displayName, required this.compare});
BetterSortOption({required this.displayName, required this.icon, required this.compare});
}
enum SortOption {
@ -33,14 +34,17 @@ class SortOptions {
static Map<SortOption, BetterSortOption> options = {
SortOption.name: BetterSortOption(
displayName: "Name",
icon: Icons.sort_by_alpha,
compare: (CacheableFile a, CacheableFile b) => a.name.compareTo(b.name)
),
SortOption.date: BetterSortOption(
displayName: "Datum",
icon: Icons.more_time_outlined,
compare: (CacheableFile a, CacheableFile b) => a.modifiedAt!.compareTo(b.modifiedAt!)
),
SortOption.size: BetterSortOption(
displayName: "Größe",
icon: Icons.monitor_weight,
compare: (CacheableFile a, CacheableFile b) {
if(a.isDirectory || b.isDirectory) return a.isDirectory ? 1 : 0;
if(a.size == null) return 0;
@ -50,10 +54,6 @@ class SortOptions {
)
};
static String getDisplayName(SortOption option) {
return options[option]?.displayName ?? "";
}
static BetterSortOption getOption(SortOption option) {
return options[option]!;
}
@ -64,6 +64,7 @@ class _FilesState extends State<Files> {
ListFilesResponse? data;
SortOption currentSort = SortOption.name;
bool currentSortDirection = true;
@override
void initState() {
@ -81,8 +82,8 @@ class _FilesState extends State<Files> {
@override
Widget build(BuildContext context) {
if(data == null) return SizedBox.shrink();
List<CacheableFile> files = data!.files.toList()..sort(SortOptions.getOption(currentSort).compare);
List<CacheableFile> files = (data?.files.toList() ?? List.empty())..sort(SortOptions.getOption(currentSort).compare);
if(currentSortDirection) files = files.reversed.toList();
return Scaffold(
appBar: AppBar(
@ -92,10 +93,41 @@ class _FilesState extends State<Files> {
icon: const Icon(Icons.search),
onPressed: () => {},
),
PopupMenuButton<bool>(
icon: Icon(currentSortDirection ? Icons.text_rotate_up : Icons.text_rotation_down),
itemBuilder: (context) {
return [true, false].map((e) => PopupMenuItem<bool>(
value: e,
enabled: e != currentSortDirection,
child: Row(
children: [
Icon(e ? Icons.text_rotate_up : Icons.text_rotation_down, color: Colors.black),
const SizedBox(width: 15),
Text(e ? "Aufsteigend" : "Absteigend")
],
)
)).toList();
},
onSelected: (e) {
setState(() {
currentSortDirection = e;
});
},
),
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();
return SortOptions.options.keys.map((key) => PopupMenuItem<SortOption>(
value: key,
enabled: key != currentSort,
child: Row(
children: [
Icon(SortOptions.getOption(key).icon, color: Colors.black),
const SizedBox(width: 15),
Text(SortOptions.getOption(key).displayName)
],
)
)).toList();
},
onSelected: (e) {
setState(() {
@ -106,31 +138,12 @@ class _FilesState extends State<Files> {
],
),
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);
// },
// );
// }
// ),
itemCount: files.length,
itemBuilder: (context, index) {
CacheableFile file = files.toList().skip(index).first;
return FileElement(file, widget.path);
},
),
);
}
}