Skip to content

Commit

Permalink
1.0.2: Fixed the inability to remove default sorts
Browse files Browse the repository at this point in the history
  • Loading branch information
mitryp committed Jan 9, 2024
1 parent fe462fd commit 35107b6
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.0.2

* Fixed the inability to remove the default sorts.
* Added a test to verify the default sorts behavior.

## 1.0.1

* Fixed formatting issues
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.0.0"
version: "1.0.2"
flutter_test:
dependency: "direct dev"
description: flutter
Expand Down
23 changes: 16 additions & 7 deletions lib/src/application/pagination_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ abstract interface class PaginationController with ChangeNotifier {
/// If no config is present, an empty map will be used instead.
///
/// If the sort set is not empty, the listeners are notified.
void clearSorts();
///
/// If the [resetDefaults] is set to true, the contents of the respective
/// [PaginateConfig.defaultSortBy] will be added to the sort list.
void clearSorts({bool resetDefaults = false});

/// Adds a filter by the given [field] with the given [operator] to the query.
///
Expand Down Expand Up @@ -230,14 +233,20 @@ class PaginationControllerImpl
}

@override
void clearSorts() {
if (_sorts.isEmpty) return;
void clearSorts({bool resetDefaults = false}) {
bool wasChanged = false;

_sorts
..clear()
..addAll(paginateConfig.defaultSortBy);
if (_sorts.isNotEmpty) {
_sorts.clear();
wasChanged = true;
}

notifyListeners();
if (resetDefaults) {
_sorts.addAll(paginateConfig.defaultSortBy);
wasChanged |= paginateConfig.defaultSortBy.isNotEmpty;
}

if (wasChanged) notifyListeners();
}

@override
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_nestjs_paginate
description: "A library providing a reactive paginated view builder compatible with `nestjs-paginate`"
version: 1.0.1
version: 1.0.2
homepage: https://github.com/mitryp/flutter_nestjs_paginate
issue_tracker: https://github.com/mitryp/flutter_nestjs_paginate/issues

Expand Down
21 changes: 21 additions & 0 deletions test/pagination_controller_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,27 @@ void main() {
expect(controller.sorts, isEmpty);
});

test('Sorts are cleared correctly with a non-empty defaultSortBy', () {
const defaultSortBy = {testSortName1: testSort};
final controller = PaginationController(
paginateConfig: const PaginateConfig(
sortableColumns: {testSortName1},
defaultSortBy: defaultSortBy,
),
);

expect(controller.sorts, hasLength(1));

controller.clearSorts();
expect(controller.sorts, isEmpty);

controller.clearSorts(resetDefaults: true);
expect(controller.sorts, equals(defaultSortBy));

controller.removeSort(testSortName1);
expect(controller.sorts, isEmpty);
});

test('silently works correctly', () {
// without silently controller should notify 4 times here
controller
Expand Down

0 comments on commit 35107b6

Please sign in to comment.