From 559e1226f8628fdd18d93a63d88a617c8f3b8e0b Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis Date: Sat, 28 Oct 2023 13:47:58 +0200 Subject: [PATCH] refactor: use named records for the sortbox Signed-off-by: Nikolas Rimikis --- .../lib/src/sort_box/sort_box_builder.dart | 4 ++-- packages/neon/neon_files/lib/sort/files.dart | 4 ++-- .../neon_files/lib/widgets/browser_view.dart | 2 +- .../neon/neon_news/lib/sort/articles.dart | 4 ++-- packages/neon/neon_news/lib/sort/feeds.dart | 4 ++-- packages/neon/neon_news/lib/sort/folders.dart | 10 +++++----- .../neon_news/lib/widgets/folders_view.dart | 4 ++-- .../neon/neon_notes/lib/sort/categories.dart | 2 +- packages/neon/neon_notes/lib/sort/notes.dart | 4 ++-- .../neon_notes/lib/widgets/notes_view.dart | 2 +- packages/sort_box/lib/sort_box.dart | 20 +++++++++++-------- packages/sort_box/test/sort_box_test.dart | 20 +++++++++---------- 12 files changed, 42 insertions(+), 38 deletions(-) diff --git a/packages/neon/neon/lib/src/sort_box/sort_box_builder.dart b/packages/neon/neon/lib/src/sort_box/sort_box_builder.dart index 2a6e323fc67..0e10259b028 100644 --- a/packages/neon/neon/lib/src/sort_box/sort_box_builder.dart +++ b/packages/neon/neon/lib/src/sort_box/sort_box_builder.dart @@ -38,7 +38,7 @@ class SortBoxBuilder extends StatelessWidget { final SortBoxWidgetBuilder builder; /// Pre sorts input. - final Set<(T property, SortBoxOrder order)>? presort; + final Set>? presort; @override Widget build(final BuildContext context) { @@ -52,7 +52,7 @@ class SortBoxBuilder extends StatelessWidget { builder: (final context, final property, final _) => ValueListenableBuilder( valueListenable: sortBoxOrder, builder: (final context, final order, final _) { - final box = (property, order); + final box = (property: property, order: order); return builder(context, sortBox.sort(input, box, presort)); }, diff --git a/packages/neon/neon_files/lib/sort/files.dart b/packages/neon/neon_files/lib/sort/files.dart index b46bc14a868..408691fc77e 100644 --- a/packages/neon/neon_files/lib/sort/files.dart +++ b/packages/neon/neon_files/lib/sort/files.dart @@ -9,10 +9,10 @@ final filesSortBox = SortBox( }, { FilesSortProperty.modifiedDate: { - (FilesSortProperty.name, SortBoxOrder.ascending), + (property: FilesSortProperty.name, order: SortBoxOrder.ascending), }, FilesSortProperty.size: { - (FilesSortProperty.name, SortBoxOrder.ascending), + (property: FilesSortProperty.name, order: SortBoxOrder.ascending), }, }, ); diff --git a/packages/neon/neon_files/lib/widgets/browser_view.dart b/packages/neon/neon_files/lib/widgets/browser_view.dart index 98d3ed90962..e8f138611d8 100644 --- a/packages/neon/neon_files/lib/widgets/browser_view.dart +++ b/packages/neon/neon_files/lib/widgets/browser_view.dart @@ -80,7 +80,7 @@ class _FilesBrowserViewState extends State { sortProperty: widget.bloc.options.filesSortPropertyOption, sortBoxOrder: widget.bloc.options.filesSortBoxOrderOption, presort: const { - (FilesSortProperty.isFolder, SortBoxOrder.ascending), + (property: FilesSortProperty.isFolder, order: SortBoxOrder.ascending), }, input: files, builder: (final context, final sorted) { diff --git a/packages/neon/neon_news/lib/sort/articles.dart b/packages/neon/neon_news/lib/sort/articles.dart index 1909d98da85..f7ef3dd0937 100644 --- a/packages/neon/neon_news/lib/sort/articles.dart +++ b/packages/neon/neon_news/lib/sort/articles.dart @@ -8,10 +8,10 @@ final articlesSortBox = SortBox( }, { ArticlesSortProperty.alphabetical: { - (ArticlesSortProperty.publishDate, SortBoxOrder.descending), + (property: ArticlesSortProperty.publishDate, order: SortBoxOrder.descending), }, ArticlesSortProperty.byFeed: { - (ArticlesSortProperty.alphabetical, SortBoxOrder.ascending), + (property: ArticlesSortProperty.alphabetical, order: SortBoxOrder.ascending), }, }, ); diff --git a/packages/neon/neon_news/lib/sort/feeds.dart b/packages/neon/neon_news/lib/sort/feeds.dart index a873f87c00d..3fa890bd6a5 100644 --- a/packages/neon/neon_news/lib/sort/feeds.dart +++ b/packages/neon/neon_news/lib/sort/feeds.dart @@ -7,10 +7,10 @@ final feedsSortBox = SortBox( }, { FeedsSortProperty.alphabetical: { - (FeedsSortProperty.unreadCount, SortBoxOrder.descending), + (property: FeedsSortProperty.unreadCount, order: SortBoxOrder.descending), }, FeedsSortProperty.unreadCount: { - (FeedsSortProperty.alphabetical, SortBoxOrder.ascending), + (property: FeedsSortProperty.alphabetical, order: SortBoxOrder.ascending), }, }, ); diff --git a/packages/neon/neon_news/lib/sort/folders.dart b/packages/neon/neon_news/lib/sort/folders.dart index 9cc93215798..19f06bd8cc3 100644 --- a/packages/neon/neon_news/lib/sort/folders.dart +++ b/packages/neon/neon_news/lib/sort/folders.dart @@ -2,17 +2,17 @@ part of '../neon_news.dart'; final foldersSortBox = SortBox( { - FoldersSortProperty.alphabetical: (final folderFeedsWrapper) => folderFeedsWrapper.$1.name.toLowerCase(), - FoldersSortProperty.unreadCount: (final folderFeedsWrapper) => folderFeedsWrapper.$3, + FoldersSortProperty.alphabetical: (final folderFeedsWrapper) => folderFeedsWrapper.folder.name.toLowerCase(), + FoldersSortProperty.unreadCount: (final folderFeedsWrapper) => folderFeedsWrapper.unreadCount, }, { FoldersSortProperty.alphabetical: { - (FoldersSortProperty.unreadCount, SortBoxOrder.descending), + (property: FoldersSortProperty.unreadCount, order: SortBoxOrder.descending), }, FoldersSortProperty.unreadCount: { - (FoldersSortProperty.alphabetical, SortBoxOrder.ascending), + (property: FoldersSortProperty.alphabetical, order: SortBoxOrder.ascending), }, }, ); -typedef FolderFeedsWrapper = (news.Folder folder, int feedCount, int unreadCount); +typedef FolderFeedsWrapper = ({news.Folder folder, int feedCount, int unreadCount}); diff --git a/packages/neon/neon_news/lib/widgets/folders_view.dart b/packages/neon/neon_news/lib/widgets/folders_view.dart index 1e7a1a92495..c23425e274d 100644 --- a/packages/neon/neon_news/lib/widgets/folders_view.dart +++ b/packages/neon/neon_news/lib/widgets/folders_view.dart @@ -23,7 +23,7 @@ class NewsFoldersView extends StatelessWidget { final feedCount = feedsInFolder.length; final unreadCount = feedsInFolder.fold(0, (final a, final b) => a + b.unreadCount!); - return (folder, feedCount, unreadCount); + return (folder: folder, feedCount: feedCount, unreadCount: unreadCount); }).toList() : null, builder: (final context, final sorted) => NeonListView( @@ -45,7 +45,7 @@ class NewsFoldersView extends StatelessWidget { final BuildContext context, final FolderFeedsWrapper folderFeedsWrapper, ) { - final (folder, feedCount, unreadCount) = folderFeedsWrapper; + final (folder: folder, feedCount: feedCount, unreadCount: unreadCount) = folderFeedsWrapper; return ListTile( title: Text( folder.name, diff --git a/packages/neon/neon_notes/lib/sort/categories.dart b/packages/neon/neon_notes/lib/sort/categories.dart index 32c16df80d5..d61eb68019d 100644 --- a/packages/neon/neon_notes/lib/sort/categories.dart +++ b/packages/neon/neon_notes/lib/sort/categories.dart @@ -7,7 +7,7 @@ final categoriesSortBox = SortBox( }, { CategoriesSortProperty.notesCount: { - (CategoriesSortProperty.alphabetical, SortBoxOrder.ascending), + (property: CategoriesSortProperty.alphabetical, order: SortBoxOrder.ascending), }, }, ); diff --git a/packages/neon/neon_notes/lib/sort/notes.dart b/packages/neon/neon_notes/lib/sort/notes.dart index b40e3c49b15..bec3c67abe1 100644 --- a/packages/neon/neon_notes/lib/sort/notes.dart +++ b/packages/neon/neon_notes/lib/sort/notes.dart @@ -8,10 +8,10 @@ final notesSortBox = SortBox( }, { NotesSortProperty.alphabetical: { - (NotesSortProperty.lastModified, SortBoxOrder.descending), + (property: NotesSortProperty.lastModified, order: SortBoxOrder.descending), }, NotesSortProperty.lastModified: { - (NotesSortProperty.alphabetical, SortBoxOrder.ascending), + (property: NotesSortProperty.alphabetical, order: SortBoxOrder.ascending), }, }, ); diff --git a/packages/neon/neon_notes/lib/widgets/notes_view.dart b/packages/neon/neon_notes/lib/widgets/notes_view.dart index f73cd8efc12..7225ed1771f 100644 --- a/packages/neon/neon_notes/lib/widgets/notes_view.dart +++ b/packages/neon/neon_notes/lib/widgets/notes_view.dart @@ -16,7 +16,7 @@ class NotesView extends StatelessWidget { builder: (final context, final notesList) => SortBoxBuilder( sortBox: notesSortBox, presort: const { - (NotesSortProperty.favorite, SortBoxOrder.ascending), + (property: NotesSortProperty.favorite, order: SortBoxOrder.ascending), }, sortProperty: bloc.options.notesSortPropertyOption, sortBoxOrder: bloc.options.notesSortBoxOrderOption, diff --git a/packages/sort_box/lib/sort_box.dart b/packages/sort_box/lib/sort_box.dart index 55a12586963..7da6c27ffd9 100644 --- a/packages/sort_box/lib/sort_box.dart +++ b/packages/sort_box/lib/sort_box.dart @@ -1,6 +1,11 @@ /// Signature of a function returning a [Comparable]. typedef ComparableGetter = Comparable Function(T); +/// The box to sort by. +/// +/// A box contains a property and a corresponding order. +typedef Box = ({T property, SortBoxOrder order}); + /// Sorting Box to sort [List]s on multiple properties. class SortBox { /// Constructs a new SortBox. @@ -17,7 +22,7 @@ class SortBox { /// A mapping of values [T] to their *Boxes*. /// /// The Boxes are applied if two elements are considered equal regarding their property [T]. - final Map> _boxes; + final Map>> _boxes; /// Sorts the [input] list according to their [box]. /// @@ -28,8 +33,8 @@ class SortBox { /// This function sorts the input in place and a reference to it mutating the provided list. List sort( final List input, - final (T property, SortBoxOrder order) box, [ - final Set<(T property, SortBoxOrder order)>? presort, + final Box box, [ + final Set>? presort, ]) { if (input.length <= 1) { return input; @@ -38,7 +43,7 @@ class SortBox { final boxes = { ...?presort, box, - ...?_boxes[box.$1], + ...?_boxes[box.property], }; final sorted = input..sort((final item1, final item2) => _compare(item1, item2, boxes.iterator..moveNext())); @@ -49,16 +54,15 @@ class SortBox { int _compare( final R item1, final R item2, - final Iterator<(T property, SortBoxOrder order)> iterator, + final Iterator> iterator, ) { final box = iterator.current; - final (property, sortBoxOrder) = box; - final comparableGetter = _properties[property]!; + final comparableGetter = _properties[box.property]!; final comparable1 = comparableGetter(item1); final comparable2 = comparableGetter(item2); - final order = switch (sortBoxOrder) { + final order = switch (box.order) { SortBoxOrder.ascending => comparable1.compareTo(comparable2), SortBoxOrder.descending => comparable2.compareTo(comparable1), }; diff --git a/packages/sort_box/test/sort_box_test.dart b/packages/sort_box/test/sort_box_test.dart index 01fa842b3e6..d563fe89aba 100644 --- a/packages/sort_box/test/sort_box_test.dart +++ b/packages/sort_box/test/sort_box_test.dart @@ -31,14 +31,14 @@ void main() { }, { FruitSort.alphabetical: { - (FruitSort.count, SortBoxOrder.ascending), + (property: FruitSort.count, order: SortBoxOrder.ascending), }, FruitSort.count: { - (FruitSort.alphabetical, SortBoxOrder.ascending), + (property: FruitSort.alphabetical, order: SortBoxOrder.ascending), }, FruitSort.price: { - (FruitSort.alphabetical, SortBoxOrder.descending), - (FruitSort.count, SortBoxOrder.ascending), + (property: FruitSort.alphabetical, order: SortBoxOrder.descending), + (property: FruitSort.count, order: SortBoxOrder.ascending), }, }, ); @@ -52,7 +52,7 @@ void main() { const Fruit('Banana', 4), const Fruit('Apple', 5), ]; - final sorted = sortBox.sort(fruits, (FruitSort.alphabetical, SortBoxOrder.ascending)); + final sorted = sortBox.sort(fruits, (property: FruitSort.alphabetical, order: SortBoxOrder.ascending)); for (var i = 0; i < 3; i++) { expect(sorted[i].name, 'Apple'); @@ -70,7 +70,7 @@ void main() { const Fruit('Banana', 2), const Fruit('Apple', 3), ]; - final sorted = sortBox.sort(fruits, (FruitSort.count, SortBoxOrder.ascending)); + final sorted = sortBox.sort(fruits, (property: FruitSort.count, order: SortBoxOrder.ascending)); final names = ['Apple', 'Banana', 'Apple', 'Apple', 'Banana']; for (var i = 0; i < 5; i++) { @@ -91,7 +91,7 @@ void main() { const Fruit('Banana', 1), const Fruit('Apple', 2), ]; - final sorted = sortBox.sort(fruits, (FruitSort.count, SortBoxOrder.ascending)); + final sorted = sortBox.sort(fruits, (property: FruitSort.count, order: SortBoxOrder.ascending)); final names = ['Apple', 'Banana', 'Apple', 'Apple', 'Banana']; for (var i = 0; i < 5; i++) { @@ -112,7 +112,7 @@ void main() { const Fruit('Banana', 2), const Fruit('Apple', 5), ]; - final sorted = sortBox.sort(fruits, (FruitSort.alphabetical, SortBoxOrder.ascending)); + final sorted = sortBox.sort(fruits, (property: FruitSort.alphabetical, order: SortBoxOrder.ascending)); for (var i = 0; i < 3; i++) { expect(sorted[i].name, 'Apple'); @@ -134,7 +134,7 @@ void main() { const Fruit('Elderberry', 1), const Fruit('Damson', 1), ]; - final sorted = sortBox.sort(fruits, (FruitSort.count, SortBoxOrder.ascending)); + final sorted = sortBox.sort(fruits, (property: FruitSort.count, order: SortBoxOrder.ascending)); final names = ['Apple', 'Banana', 'Coconut', 'Damson', 'Elderberry']; for (var i = 0; i < 5; i++) { @@ -152,7 +152,7 @@ void main() { const Fruit('Banana', 1, 3), const Fruit('Apple', 2, 3), ]; - final sorted = sortBox.sort(fruits, (FruitSort.price, SortBoxOrder.ascending)); + final sorted = sortBox.sort(fruits, (property: FruitSort.price, order: SortBoxOrder.ascending)); final price = [0, 2, 3, 3, 3]; for (var i = 0; i < 5; i++) {