From 1f9d6ca6bf90d7bc6f9ef6ec9021a79000e63c87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elias=20M=C3=BCller?= Date: Mon, 8 May 2023 20:00:28 +0200 Subject: [PATCH] Added ascending and descending sort option in files --- lib/screen/pages/files/files.dart | 79 ++++++++++++++++++------------- 1 file changed, 46 insertions(+), 33 deletions(-) diff --git a/lib/screen/pages/files/files.dart b/lib/screen/pages/files/files.dart index 34ab24c..24b6331 100644 --- a/lib/screen/pages/files/files.dart +++ b/lib/screen/pages/files/files.dart @@ -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 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 { ListFilesResponse? data; SortOption currentSort = SortOption.name; + bool currentSortDirection = true; @override void initState() { @@ -81,8 +82,8 @@ 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); + List 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 { icon: const Icon(Icons.search), onPressed: () => {}, ), + PopupMenuButton( + icon: Icon(currentSortDirection ? Icons.text_rotate_up : Icons.text_rotation_down), + itemBuilder: (context) { + return [true, false].map((e) => PopupMenuItem( + 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( icon: const Icon(Icons.sort), itemBuilder: (context) { - return SortOptions.options.keys.map((key) => PopupMenuItem(value: key, child: Text(SortOptions.getDisplayName(key)))).toList(); + return SortOptions.options.keys.map((key) => PopupMenuItem( + 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 { ], ), 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( - // builder: (context, value, child) { - // - // if(value.listFilesResponse.files.isEmpty) { - // return const ErrorView(text: "Der Ordner ist leer", icon: Icons.folder_off_outlined); - // } - // - // List 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); + }, + ), ); } }