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 6c256e78..29d7f58d 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 @@ -42,7 +42,7 @@ class SortBoxBuilder extends StatelessWidget { builder: (final context, final property, final _) => ValueListenableBuilder( valueListenable: sortBoxOrderOption, builder: (final context, final order, final _) { - final box = Box(property, order); + final box = (property, order); return builder(context, sortBox.sort(input, box)); }, diff --git a/packages/neon/neon_files/lib/sort/files.dart b/packages/neon/neon_files/lib/sort/files.dart index aba203b7..d0d7d96e 100644 --- a/packages/neon/neon_files/lib/sort/files.dart +++ b/packages/neon/neon_files/lib/sort/files.dart @@ -8,10 +8,10 @@ final filesSortBox = SortBox( }, { FilesSortProperty.modifiedDate: { - Box(FilesSortProperty.name, SortBoxOrder.ascending), + (FilesSortProperty.name, SortBoxOrder.ascending), }, FilesSortProperty.size: { - Box(FilesSortProperty.name, SortBoxOrder.ascending), + (FilesSortProperty.name, SortBoxOrder.ascending), }, }, ); diff --git a/packages/neon/neon_news/lib/sort/articles.dart b/packages/neon/neon_news/lib/sort/articles.dart index 18f69a15..4a082afb 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: { - Box(ArticlesSortProperty.publishDate, SortBoxOrder.descending), + (ArticlesSortProperty.publishDate, SortBoxOrder.descending), }, ArticlesSortProperty.byFeed: { - Box(ArticlesSortProperty.alphabetical, SortBoxOrder.ascending), + (ArticlesSortProperty.alphabetical, SortBoxOrder.ascending), }, }, ); diff --git a/packages/neon/neon_news/lib/sort/feeds.dart b/packages/neon/neon_news/lib/sort/feeds.dart index 5e9f37a5..c25d3596 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: { - Box(FeedsSortProperty.unreadCount, SortBoxOrder.descending), + (FeedsSortProperty.unreadCount, SortBoxOrder.descending), }, FeedsSortProperty.unreadCount: { - Box(FeedsSortProperty.alphabetical, SortBoxOrder.ascending), + (FeedsSortProperty.alphabetical, SortBoxOrder.ascending), }, }, ); diff --git a/packages/neon/neon_news/lib/sort/folders.dart b/packages/neon/neon_news/lib/sort/folders.dart index 39061dcc..cfc1bc9d 100644 --- a/packages/neon/neon_news/lib/sort/folders.dart +++ b/packages/neon/neon_news/lib/sort/folders.dart @@ -7,10 +7,10 @@ final foldersSortBox = SortBox( }, { FoldersSortProperty.alphabetical: { - Box(FoldersSortProperty.unreadCount, SortBoxOrder.descending), + (FoldersSortProperty.unreadCount, SortBoxOrder.descending), }, FoldersSortProperty.unreadCount: { - Box(FoldersSortProperty.alphabetical, SortBoxOrder.ascending), + (FoldersSortProperty.alphabetical, SortBoxOrder.ascending), }, }, ); diff --git a/packages/neon/neon_notes/lib/sort/categories.dart b/packages/neon/neon_notes/lib/sort/categories.dart index 53cbbc0e..32c16df8 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: { - Box(CategoriesSortProperty.alphabetical, SortBoxOrder.ascending), + (CategoriesSortProperty.alphabetical, SortBoxOrder.ascending), }, }, ); diff --git a/packages/neon/neon_notes/lib/sort/notes.dart b/packages/neon/neon_notes/lib/sort/notes.dart index 04606eb4..430bf0f9 100644 --- a/packages/neon/neon_notes/lib/sort/notes.dart +++ b/packages/neon/neon_notes/lib/sort/notes.dart @@ -7,7 +7,7 @@ final notesSortBox = SortBox( }, { NotesSortProperty.alphabetical: { - Box(NotesSortProperty.lastModified, SortBoxOrder.descending), + (NotesSortProperty.lastModified, SortBoxOrder.descending), }, }, ); diff --git a/packages/sort_box/lib/sort_box.dart b/packages/sort_box/lib/sort_box.dart index 7c922d2e..733238ac 100644 --- a/packages/sort_box/lib/sort_box.dart +++ b/packages/sort_box/lib/sort_box.dart @@ -9,15 +9,14 @@ class SortBox { ); final Map> _properties; - final Map>> _boxes; + final Map> _boxes; - List sort(final List input, final Box box) { + List sort(final List input, final (T property, SortBoxOrder order) box) { if (input.length <= 1) { return input; } - final sorted = input - ..sort((final item1, final item2) => _compare(item1, item2, box, _boxes[box.property]?.iterator)); + final sorted = input..sort((final item1, final item2) => _compare(item1, item2, box, _boxes[box.$1]?.iterator)); return sorted; } @@ -25,16 +24,19 @@ class SortBox { int _compare( final R item1, final R item2, - final Box box, - final Iterator>? iterator, + final (T property, SortBoxOrder order) box, + final Iterator<(T property, SortBoxOrder order)>? iterator, ) { - final comparableGetter = _properties[box.property]!; + final (property, sortBoxOrder) = box; + final comparableGetter = _properties[property]!; final comparable1 = comparableGetter(item1); final comparable2 = comparableGetter(item2); - final order = - box.order == SortBoxOrder.ascending ? comparable1.compareTo(comparable2) : comparable2.compareTo(comparable1); + final order = switch (sortBoxOrder) { + SortBoxOrder.ascending => comparable1.compareTo(comparable2), + SortBoxOrder.descending => comparable2.compareTo(comparable1), + }; if (order == 0 && iterator != null && iterator.moveNext()) { return _compare(item1, item2, iterator.current, iterator); @@ -48,13 +50,3 @@ enum SortBoxOrder { ascending, descending, } - -class Box { - Box( - this.property, - this.order, - ); - - final T property; - final SortBoxOrder order; -} diff --git a/packages/sort_box/test/sort_box_test.dart b/packages/sort_box/test/sort_box_test.dart index 02032676..01fa842b 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: { - Box(FruitSort.count, SortBoxOrder.ascending), + (FruitSort.count, SortBoxOrder.ascending), }, FruitSort.count: { - Box(FruitSort.alphabetical, SortBoxOrder.ascending), + (FruitSort.alphabetical, SortBoxOrder.ascending), }, FruitSort.price: { - Box(FruitSort.alphabetical, SortBoxOrder.descending), - Box(FruitSort.count, SortBoxOrder.ascending), + (FruitSort.alphabetical, SortBoxOrder.descending), + (FruitSort.count, SortBoxOrder.ascending), }, }, ); @@ -52,7 +52,7 @@ void main() { const Fruit('Banana', 4), const Fruit('Apple', 5), ]; - final sorted = sortBox.sort(fruits, Box(FruitSort.alphabetical, SortBoxOrder.ascending)); + final sorted = sortBox.sort(fruits, (FruitSort.alphabetical, 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, Box(FruitSort.count, SortBoxOrder.ascending)); + final sorted = sortBox.sort(fruits, (FruitSort.count, 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, Box(FruitSort.count, SortBoxOrder.ascending)); + final sorted = sortBox.sort(fruits, (FruitSort.count, 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, Box(FruitSort.alphabetical, SortBoxOrder.ascending)); + final sorted = sortBox.sort(fruits, (FruitSort.alphabetical, 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, Box(FruitSort.count, SortBoxOrder.ascending)); + final sorted = sortBox.sort(fruits, (FruitSort.count, 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, Box(FruitSort.price, SortBoxOrder.ascending)); + final sorted = sortBox.sort(fruits, (FruitSort.price, SortBoxOrder.ascending)); final price = [0, 2, 3, 3, 3]; for (var i = 0; i < 5; i++) {