From 943bf15aab09dd2dc5bac120714a4e44485e4cea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maty=C3=A1=C5=A1=20Caras?= Date: Mon, 29 Jan 2024 20:19:03 +0100 Subject: [PATCH] feat: add basic sorting (#9) also made some cosmetic changes elsewhere, --- CHANGELOG.md | 3 +++ lib/l10n/app_cs.arb | 6 ++++- lib/l10n/app_en.arb | 6 ++++- lib/util/sorting.dart | 58 +++++++++++++++++++++++++++++++++++++++++++ lib/views/home.dart | 17 +++++++------ lib/views/setup.dart | 1 + 6 files changed, 81 insertions(+), 10 deletions(-) create mode 100644 lib/util/sorting.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index 43015f8..acf2aaf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ - Placeholder text is now inserted into the field in setup, instead of showing as label - Show version text in about dialog - Added tessdata license text into about dialog +- Added sorting by oldest +- Moved search into three-dot menu +- Make search case-insensitive # 1.0.0-alpha+5 - Add tests - Add searching through entries to homepage diff --git a/lib/l10n/app_cs.arb b/lib/l10n/app_cs.arb index 8767f04..f7074e0 100644 --- a/lib/l10n/app_cs.arb +++ b/lib/l10n/app_cs.arb @@ -106,5 +106,9 @@ "exportCompleted":"Export dokončen", "importCompleted":"Import dokončen", "setup":"Prvotní nastavení", - "sourceCode":"Zdrojový kód" + "sourceCode":"Zdrojový kód", + "sortNewest":"Nejnovější první", + "sortOldest":"Nejstarší první", + "sort":"Seřadit", + "search":"Prohledat" } \ No newline at end of file diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index c9c9d90..89e1ddb 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -222,5 +222,9 @@ "exportCompleted":"Export completed", "importCompleted":"Import completed", "setup":"Setup", - "sourceCode":"Source code" + "sourceCode":"Source code", + "sortNewest":"Newest first", + "sortOldest":"Oldest first", + "sort":"Sort", + "search":"Search" } \ No newline at end of file diff --git a/lib/util/sorting.dart b/lib/util/sorting.dart new file mode 100644 index 0000000..b1af612 --- /dev/null +++ b/lib/util/sorting.dart @@ -0,0 +1,58 @@ +import 'package:grouped_list/grouped_list.dart'; +import 'package:intl/intl.dart'; + +/// Sorts [GroupedListView]'s group by newest group +int groupSortNewest(String a, String b, String locale) { + // TODO: better sorting algorithm lol + final yearA = RegExp(r'\d+').firstMatch(a); + if (yearA == null) return 0; + final yearB = RegExp(r'\d+').firstMatch(b); + if (yearB == null) return 0; + final compareYears = int.parse(yearB.group(0)!).compareTo( + int.parse(yearA.group(0)!), + ); + if (compareYears != 0) { + return compareYears; + } + final months = List.generate( + 12, + (index) => DateFormat.MMMM(locale).format( + DateTime(2023, index + 1), + ), + ); + final monthA = RegExp('[^0-9 ]+').firstMatch(a); + if (monthA == null) return 0; + final monthB = RegExp('[^0-9 ]+').firstMatch(b); + if (monthB == null) return 0; + return months.indexOf(monthB.group(0)!).compareTo( + months.indexOf(monthA.group(0)!), + ); +} + +/// Sorts [GroupedListView]'s group by oldest group +int groupSortOldest(String a, String b, String locale) { + // TODO: better sorting algorithm lol + final yearA = RegExp(r'\d+').firstMatch(a); + if (yearA == null) return 0; + final yearB = RegExp(r'\d+').firstMatch(b); + if (yearB == null) return 0; + final compareYears = int.parse(yearA.group(0)!).compareTo( + int.parse(yearB.group(0)!), + ); + if (compareYears != 0) { + return compareYears; + } + final months = List.generate( + 12, + (index) => DateFormat.MMMM(locale).format( + DateTime(2023, index + 1), + ), + ); + final monthA = RegExp('[^0-9 ]+').firstMatch(a); + if (monthA == null) return 0; + final monthB = RegExp('[^0-9 ]+').firstMatch(b); + if (monthB == null) return 0; + return months.indexOf(monthA.group(0)!).compareTo( + months.indexOf(monthB.group(0)!), + ); +} diff --git a/lib/views/home.dart b/lib/views/home.dart index 31ccff7..222ac1c 100644 --- a/lib/views/home.dart +++ b/lib/views/home.dart @@ -197,13 +197,6 @@ class _HomeViewState extends State { actions: _searchActive ? [] : [ - IconButton( - onPressed: () { - _searchActive = true; - setState(() {}); - }, - icon: const Icon(Icons.search), - ), PopupMenuButton( tooltip: AppLocalizations.of(context).sort, icon: const Icon(Icons.sort_rounded), @@ -232,6 +225,7 @@ class _HomeViewState extends State { PopupMenuButton( itemBuilder: (context) => [ AppLocalizations.of(context).settings, + AppLocalizations.of(context).search, AppLocalizations.of(context).about, ] .map((e) => PopupMenuItem(value: e, child: Text(e))) @@ -251,6 +245,12 @@ class _HomeViewState extends State { }); } else if (value == AppLocalizations.of(context).about) { showAbout(context); + } else if (value == AppLocalizations.of(context).search) { + _searchActive = !_searchActive; + if (!_searchActive) { + _filter = ""; + } + setState(() {}); } }, ), @@ -384,7 +384,8 @@ class _HomeViewState extends State { elements: selectedWallet!.entries .where( (element) => element.data.name - .contains(_filter), + .toLowerCase() + .contains(_filter.toLowerCase()), ) .toList(), itemComparator: (a, b) => diff --git a/lib/views/setup.dart b/lib/views/setup.dart index 4b6b48c..3410cc4 100644 --- a/lib/views/setup.dart +++ b/lib/views/setup.dart @@ -57,6 +57,7 @@ class _SetupViewState extends State { void didChangeDependencies() { super.didChangeDependencies(); if (categories.isEmpty) { + name = AppLocalizations.of(context).setupNamePlaceholder; categories = [ WalletCategory( name: AppLocalizations.of(context).noCategory,