From 35107b698ae756f84baccf4af9eb3ab8dbb9b85d Mon Sep 17 00:00:00 2001 From: Dmytro Popov Date: Tue, 9 Jan 2024 19:13:58 +0000 Subject: [PATCH] 1.0.2: Fixed the inability to remove default sorts --- CHANGELOG.md | 5 ++++ example/pubspec.lock | 2 +- .../application/pagination_controller.dart | 23 +++++++++++++------ pubspec.yaml | 2 +- test/pagination_controller_test.dart | 21 +++++++++++++++++ 5 files changed, 44 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c7040e..4fa84e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/example/pubspec.lock b/example/pubspec.lock index 86e9c40..9f782d8 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -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 diff --git a/lib/src/application/pagination_controller.dart b/lib/src/application/pagination_controller.dart index b7512b6..091d7fb 100644 --- a/lib/src/application/pagination_controller.dart +++ b/lib/src/application/pagination_controller.dart @@ -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. /// @@ -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 diff --git a/pubspec.yaml b/pubspec.yaml index 52a2670..4e301b4 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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 diff --git a/test/pagination_controller_test.dart b/test/pagination_controller_test.dart index c9feb54..11fe1e0 100644 --- a/test/pagination_controller_test.dart +++ b/test/pagination_controller_test.dart @@ -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