From 940c6ead8c1bb87b7f520ccb1c6d2523f00c0894 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elias=20M=C3=BCller?= Date: Mon, 8 May 2023 14:02:53 +0200 Subject: [PATCH] Added file sorting --- .idea/libraries/Dart_Packages.xml | 100 ++++++++++++++-------------- .idea/libraries/Flutter_Plugins.xml | 18 ++--- lib/screen/pages/files/files.dart | 67 +++++++++++++++++-- 3 files changed, 117 insertions(+), 68 deletions(-) diff --git a/.idea/libraries/Dart_Packages.xml b/.idea/libraries/Dart_Packages.xml index eb737ec..fa0bb6d 100644 --- a/.idea/libraries/Dart_Packages.xml +++ b/.idea/libraries/Dart_Packages.xml @@ -5,14 +5,14 @@ - - @@ -40,7 +40,7 @@ - @@ -145,7 +145,7 @@ - @@ -180,7 +180,7 @@ - @@ -264,7 +264,7 @@ - @@ -432,14 +432,14 @@ - - @@ -460,7 +460,7 @@ - @@ -530,14 +530,14 @@ - - @@ -614,7 +614,7 @@ - @@ -649,7 +649,7 @@ - @@ -754,14 +754,14 @@ - - @@ -789,7 +789,7 @@ - @@ -831,14 +831,14 @@ - - @@ -859,7 +859,7 @@ - @@ -880,14 +880,14 @@ - - @@ -978,7 +978,7 @@ - @@ -992,7 +992,7 @@ - @@ -1020,7 +1020,7 @@ - @@ -1090,7 +1090,7 @@ - @@ -1098,13 +1098,13 @@ - - - + + + - + @@ -1119,12 +1119,12 @@ - + - + @@ -1136,7 +1136,7 @@ - + @@ -1156,11 +1156,11 @@ - - + + - + @@ -1170,8 +1170,8 @@ - - + + @@ -1186,7 +1186,7 @@ - + @@ -1201,25 +1201,25 @@ - - + + - + - - + + - + - - + + @@ -1232,13 +1232,13 @@ - + - + - + @@ -1248,7 +1248,7 @@ - + diff --git a/.idea/libraries/Flutter_Plugins.xml b/.idea/libraries/Flutter_Plugins.xml index a1271f3..436cfd5 100644 --- a/.idea/libraries/Flutter_Plugins.xml +++ b/.idea/libraries/Flutter_Plugins.xml @@ -9,24 +9,18 @@ - - - - - - - - - - - - + + + + + + diff --git a/lib/screen/pages/files/files.dart b/lib/screen/pages/files/files.dart index ac112cc..34ab24c 100644 --- a/lib/screen/pages/files/files.dart +++ b/lib/screen/pages/files/files.dart @@ -16,10 +16,55 @@ class Files extends StatefulWidget { State 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 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 { FilesProps props = FilesProps(); ListFilesResponse? data; + SortOption currentSort = SortOption.name; + @override void initState() { super.initState(); @@ -36,24 +81,34 @@ class _FilesState extends State { @override Widget build(BuildContext context) { + if(data == null) return SizedBox.shrink(); + List files = data!.files.toList()..sort(SortOptions.getOption(currentSort).compare); + return Scaffold( appBar: AppBar( - title: Text(widget.path.length > 0 ? widget.path.last : "Dateien"), + title: Text(widget.path.isNotEmpty ? widget.path.last : "Dateien"), actions: [ IconButton( icon: const Icon(Icons.search), onPressed: () => {}, ), - IconButton( + PopupMenuButton( icon: const Icon(Icons.sort), - onPressed: () => {}, - ) + itemBuilder: (context) { + return SortOptions.options.keys.map((key) => PopupMenuItem(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: data!.files.toList().length, + itemCount: files.length, itemBuilder: (context, index) { - CacheableFile file = data!.files.toList().skip(index).first; + CacheableFile file = files.toList().skip(index).first; return FileElement(file, widget.path); }, ),