Compare commits

...

3 commits

Author SHA1 Message Date
Matyáš Caras
9f95581b96
Added translation using Weblate (Slovak) 2024-01-29 20:19:17 +01:00
Matyáš Caras
943bf15aab
feat: add basic sorting (#9)
also made some cosmetic changes elsewhere,
2024-01-29 20:19:03 +01:00
Matyáš Caras
2226d37f8f
fix: save the placeholder name into name var 2024-01-29 20:07:56 +01:00
7 changed files with 160 additions and 77 deletions

View file

@ -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

View file

@ -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"
}

View file

@ -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"
}

1
lib/l10n/app_sk.arb Normal file
View file

@ -0,0 +1 @@
{}

58
lib/util/sorting.dart Normal file
View file

@ -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<String>.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<String>.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)!),
);
}

View file

@ -24,6 +24,7 @@ import 'package:prasule/pw/platformbutton.dart';
import 'package:prasule/pw/platformfield.dart';
import 'package:prasule/pw/platformroute.dart';
import 'package:prasule/util/drawer.dart';
import 'package:prasule/util/sorting.dart';
import 'package:prasule/util/text_color.dart';
import 'package:prasule/util/utils.dart';
import 'package:prasule/views/create_entry.dart';
@ -41,13 +42,15 @@ class HomeView extends StatefulWidget {
}
class _HomeViewState extends State<HomeView> {
Wallet? selectedWallet;
List<Wallet> wallets = [];
Wallet? selectedWallet; // current wallet
List<Wallet> wallets = []; // all available wallets
DateTime? prevDate;
late String locale;
var _searchActive = false;
var _filter = "";
late String locale; // user's locale
var _searchActive = false; // whether search field is shown
var _filter = ""; // search filter
final searchFocus = FocusNode();
var sort = SortType.newest;
@override
void didChangeDependencies() {
super.didChangeDependencies();
@ -191,40 +194,67 @@ class _HomeViewState extends State<HomeView> {
},
),
),
actions: [
if (!_searchActive)
IconButton(
onPressed: () {
_searchActive = true;
setState(() {});
},
icon: const Icon(Icons.search),
),
if (!_searchActive)
PopupMenuButton(
itemBuilder: (context) => [
AppLocalizations.of(context).settings,
AppLocalizations.of(context).about,
].map((e) => PopupMenuItem(value: e, child: Text(e))).toList(),
onSelected: (value) {
if (value == AppLocalizations.of(context).settings) {
Navigator.of(context)
.push(
platformRoute(
(context) => const SettingsView(),
),
)
.then((value) async {
wallets = await WalletManager.listWallets();
selectedWallet =
await WalletManager.loadWallet(selectedWallet!.name);
});
} else if (value == AppLocalizations.of(context).about) {
showAbout(context);
}
},
),
],
actions: _searchActive
? []
: [
PopupMenuButton(
tooltip: AppLocalizations.of(context).sort,
icon: const Icon(Icons.sort_rounded),
itemBuilder: (context) => [
AppLocalizations.of(context).sortNewest,
AppLocalizations.of(context).sortOldest,
]
.map(
(e) => PopupMenuItem(
value: e,
child: Text(e),
),
)
.toList(),
onSelected: (value) {
if (value == AppLocalizations.of(context).sortNewest) {
sort = SortType.newest;
setState(() {});
} else if (value ==
AppLocalizations.of(context).sortOldest) {
sort = SortType.oldest;
setState(() {});
}
},
),
PopupMenuButton(
itemBuilder: (context) => [
AppLocalizations.of(context).settings,
AppLocalizations.of(context).search,
AppLocalizations.of(context).about,
]
.map((e) => PopupMenuItem(value: e, child: Text(e)))
.toList(),
onSelected: (value) {
if (value == AppLocalizations.of(context).settings) {
Navigator.of(context)
.push(
platformRoute(
(context) => const SettingsView(),
),
)
.then((value) async {
wallets = await WalletManager.listWallets();
selectedWallet = await WalletManager.loadWallet(
selectedWallet!.name);
});
} else if (value == AppLocalizations.of(context).about) {
showAbout(context);
} else if (value == AppLocalizations.of(context).search) {
_searchActive = !_searchActive;
if (!_searchActive) {
_filter = "";
}
setState(() {});
}
},
),
],
),
body: Center(
child: SizedBox(
@ -354,47 +384,20 @@ class _HomeViewState extends State<HomeView> {
elements: selectedWallet!.entries
.where(
(element) => element.data.name
.contains(_filter),
.toLowerCase()
.contains(_filter.toLowerCase()),
)
.toList(),
itemComparator: (a, b) =>
b.date.compareTo(a.date),
(sort == SortType.newest)
? b.date.compareTo(a.date)
: a.date.compareTo(b.date),
groupBy: (e) =>
DateFormat.yMMMM(locale).format(e.date),
groupComparator: (a, b) {
// 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<String>.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)!),
);
},
groupComparator: (a, b) =>
(sort == SortType.newest)
? groupSortNewest(a, b, locale)
: groupSortOldest(a, b, locale),
itemBuilder: (context, element) => Slidable(
endActionPane: ActionPane(
motion: const ScrollMotion(),
@ -760,3 +763,12 @@ class _HomeViewState extends State<HomeView> {
);
}
}
/// Represents entry sorting type
enum SortType {
/// Sort newest first
newest,
/// Sort oldest first
oldest
}

View file

@ -57,6 +57,7 @@ class _SetupViewState extends State<SetupView> {
void didChangeDependencies() {
super.didChangeDependencies();
if (categories.isEmpty) {
name = AppLocalizations.of(context).setupNamePlaceholder;
categories = [
WalletCategory(
name: AppLocalizations.of(context).noCategory,