Browse Source

refactor(sort_box,neon,neon_files,neon_news): use Dart3 records and switch

Signed-off-by: Nikolas Rimikis <rimikis.nikolas@gmail.com>
pull/591/head
Nikolas Rimikis 1 year ago
parent
commit
4d96f08186
No known key found for this signature in database
GPG Key ID: 85ED1DE9786A4FF2
  1. 2
      packages/neon/neon/lib/src/sort_box/sort_box_builder.dart
  2. 4
      packages/neon/neon_files/lib/sort/files.dart
  3. 4
      packages/neon/neon_news/lib/sort/articles.dart
  4. 4
      packages/neon/neon_news/lib/sort/feeds.dart
  5. 4
      packages/neon/neon_news/lib/sort/folders.dart
  6. 2
      packages/neon/neon_notes/lib/sort/categories.dart
  7. 2
      packages/neon/neon_notes/lib/sort/notes.dart
  8. 30
      packages/sort_box/lib/sort_box.dart
  9. 20
      packages/sort_box/test/sort_box_test.dart

2
packages/neon/neon/lib/src/sort_box/sort_box_builder.dart

@ -42,7 +42,7 @@ class SortBoxBuilder<T extends Enum, R> extends StatelessWidget {
builder: (final context, final property, final _) => ValueListenableBuilder<SortBoxOrder>( builder: (final context, final property, final _) => ValueListenableBuilder<SortBoxOrder>(
valueListenable: sortBoxOrderOption, valueListenable: sortBoxOrderOption,
builder: (final context, final order, final _) { builder: (final context, final order, final _) {
final box = Box(property, order); final box = (property, order);
return builder(context, sortBox.sort(input, box)); return builder(context, sortBox.sort(input, box));
}, },

4
packages/neon/neon_files/lib/sort/files.dart

@ -8,10 +8,10 @@ final filesSortBox = SortBox<FilesSortProperty, WebDavFile>(
}, },
{ {
FilesSortProperty.modifiedDate: { FilesSortProperty.modifiedDate: {
Box(FilesSortProperty.name, SortBoxOrder.ascending), (FilesSortProperty.name, SortBoxOrder.ascending),
}, },
FilesSortProperty.size: { FilesSortProperty.size: {
Box(FilesSortProperty.name, SortBoxOrder.ascending), (FilesSortProperty.name, SortBoxOrder.ascending),
}, },
}, },
); );

4
packages/neon/neon_news/lib/sort/articles.dart

@ -8,10 +8,10 @@ final articlesSortBox = SortBox<ArticlesSortProperty, NewsArticle>(
}, },
{ {
ArticlesSortProperty.alphabetical: { ArticlesSortProperty.alphabetical: {
Box(ArticlesSortProperty.publishDate, SortBoxOrder.descending), (ArticlesSortProperty.publishDate, SortBoxOrder.descending),
}, },
ArticlesSortProperty.byFeed: { ArticlesSortProperty.byFeed: {
Box(ArticlesSortProperty.alphabetical, SortBoxOrder.ascending), (ArticlesSortProperty.alphabetical, SortBoxOrder.ascending),
}, },
}, },
); );

4
packages/neon/neon_news/lib/sort/feeds.dart

@ -7,10 +7,10 @@ final feedsSortBox = SortBox<FeedsSortProperty, NewsFeed>(
}, },
{ {
FeedsSortProperty.alphabetical: { FeedsSortProperty.alphabetical: {
Box(FeedsSortProperty.unreadCount, SortBoxOrder.descending), (FeedsSortProperty.unreadCount, SortBoxOrder.descending),
}, },
FeedsSortProperty.unreadCount: { FeedsSortProperty.unreadCount: {
Box(FeedsSortProperty.alphabetical, SortBoxOrder.ascending), (FeedsSortProperty.alphabetical, SortBoxOrder.ascending),
}, },
}, },
); );

4
packages/neon/neon_news/lib/sort/folders.dart

@ -7,10 +7,10 @@ final foldersSortBox = SortBox<FoldersSortProperty, FolderFeedsWrapper>(
}, },
{ {
FoldersSortProperty.alphabetical: { FoldersSortProperty.alphabetical: {
Box(FoldersSortProperty.unreadCount, SortBoxOrder.descending), (FoldersSortProperty.unreadCount, SortBoxOrder.descending),
}, },
FoldersSortProperty.unreadCount: { FoldersSortProperty.unreadCount: {
Box(FoldersSortProperty.alphabetical, SortBoxOrder.ascending), (FoldersSortProperty.alphabetical, SortBoxOrder.ascending),
}, },
}, },
); );

2
packages/neon/neon_notes/lib/sort/categories.dart

@ -7,7 +7,7 @@ final categoriesSortBox = SortBox<CategoriesSortProperty, NoteCategory>(
}, },
{ {
CategoriesSortProperty.notesCount: { CategoriesSortProperty.notesCount: {
Box(CategoriesSortProperty.alphabetical, SortBoxOrder.ascending), (CategoriesSortProperty.alphabetical, SortBoxOrder.ascending),
}, },
}, },
); );

2
packages/neon/neon_notes/lib/sort/notes.dart

@ -7,7 +7,7 @@ final notesSortBox = SortBox<NotesSortProperty, NotesNote>(
}, },
{ {
NotesSortProperty.alphabetical: { NotesSortProperty.alphabetical: {
Box(NotesSortProperty.lastModified, SortBoxOrder.descending), (NotesSortProperty.lastModified, SortBoxOrder.descending),
}, },
}, },
); );

30
packages/sort_box/lib/sort_box.dart

@ -9,15 +9,14 @@ class SortBox<T extends Enum, R> {
); );
final Map<T, ComparableGetter<R>> _properties; final Map<T, ComparableGetter<R>> _properties;
final Map<T, Set<Box<T>>> _boxes; final Map<T, Set<(T property, SortBoxOrder order)>> _boxes;
List<R> sort(final List<R> input, final Box<T> box) { List<R> sort(final List<R> input, final (T property, SortBoxOrder order) box) {
if (input.length <= 1) { if (input.length <= 1) {
return input; return input;
} }
final sorted = input final sorted = input..sort((final item1, final item2) => _compare(item1, item2, box, _boxes[box.$1]?.iterator));
..sort((final item1, final item2) => _compare(item1, item2, box, _boxes[box.property]?.iterator));
return sorted; return sorted;
} }
@ -25,16 +24,19 @@ class SortBox<T extends Enum, R> {
int _compare( int _compare(
final R item1, final R item1,
final R item2, final R item2,
final Box<T> box, final (T property, SortBoxOrder order) box,
final Iterator<Box<T>>? iterator, 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 comparable1 = comparableGetter(item1);
final comparable2 = comparableGetter(item2); final comparable2 = comparableGetter(item2);
final order = final order = switch (sortBoxOrder) {
box.order == SortBoxOrder.ascending ? comparable1.compareTo(comparable2) : comparable2.compareTo(comparable1); SortBoxOrder.ascending => comparable1.compareTo(comparable2),
SortBoxOrder.descending => comparable2.compareTo(comparable1),
};
if (order == 0 && iterator != null && iterator.moveNext()) { if (order == 0 && iterator != null && iterator.moveNext()) {
return _compare(item1, item2, iterator.current, iterator); return _compare(item1, item2, iterator.current, iterator);
@ -48,13 +50,3 @@ enum SortBoxOrder {
ascending, ascending,
descending, descending,
} }
class Box<T extends Enum> {
Box(
this.property,
this.order,
);
final T property;
final SortBoxOrder order;
}

20
packages/sort_box/test/sort_box_test.dart

@ -31,14 +31,14 @@ void main() {
}, },
{ {
FruitSort.alphabetical: { FruitSort.alphabetical: {
Box(FruitSort.count, SortBoxOrder.ascending), (FruitSort.count, SortBoxOrder.ascending),
}, },
FruitSort.count: { FruitSort.count: {
Box(FruitSort.alphabetical, SortBoxOrder.ascending), (FruitSort.alphabetical, SortBoxOrder.ascending),
}, },
FruitSort.price: { FruitSort.price: {
Box(FruitSort.alphabetical, SortBoxOrder.descending), (FruitSort.alphabetical, SortBoxOrder.descending),
Box(FruitSort.count, SortBoxOrder.ascending), (FruitSort.count, SortBoxOrder.ascending),
}, },
}, },
); );
@ -52,7 +52,7 @@ void main() {
const Fruit('Banana', 4), const Fruit('Banana', 4),
const Fruit('Apple', 5), 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++) { for (var i = 0; i < 3; i++) {
expect(sorted[i].name, 'Apple'); expect(sorted[i].name, 'Apple');
@ -70,7 +70,7 @@ void main() {
const Fruit('Banana', 2), const Fruit('Banana', 2),
const Fruit('Apple', 3), 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']; final names = ['Apple', 'Banana', 'Apple', 'Apple', 'Banana'];
for (var i = 0; i < 5; i++) { for (var i = 0; i < 5; i++) {
@ -91,7 +91,7 @@ void main() {
const Fruit('Banana', 1), const Fruit('Banana', 1),
const Fruit('Apple', 2), 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']; final names = ['Apple', 'Banana', 'Apple', 'Apple', 'Banana'];
for (var i = 0; i < 5; i++) { for (var i = 0; i < 5; i++) {
@ -112,7 +112,7 @@ void main() {
const Fruit('Banana', 2), const Fruit('Banana', 2),
const Fruit('Apple', 5), 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++) { for (var i = 0; i < 3; i++) {
expect(sorted[i].name, 'Apple'); expect(sorted[i].name, 'Apple');
@ -134,7 +134,7 @@ void main() {
const Fruit('Elderberry', 1), const Fruit('Elderberry', 1),
const Fruit('Damson', 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']; final names = ['Apple', 'Banana', 'Coconut', 'Damson', 'Elderberry'];
for (var i = 0; i < 5; i++) { for (var i = 0; i < 5; i++) {
@ -152,7 +152,7 @@ void main() {
const Fruit('Banana', 1, 3), const Fruit('Banana', 1, 3),
const Fruit('Apple', 2, 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]; final price = [0, 2, 3, 3, 3];
for (var i = 0; i < 5; i++) { for (var i = 0; i < 5; i++) {

Loading…
Cancel
Save