Skip to content

Commit

Permalink
Add (and improve) filter functions
Browse files Browse the repository at this point in the history
- Add filter function for databases.
- Improve code for filter function for blocks on pagination and children classes.
  • Loading branch information
jonathangomz committed Jun 22, 2021
1 parent 5ac3a17 commit abb1934
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
6 changes: 3 additions & 3 deletions lib/notion/general/lists/children.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ class Children {
if (exclude.isNotEmpty) {
filetered.removeWhere((block) => exclude.contains(block.type));
} else if (include.isNotEmpty) {
filetered.removeWhere((block) => !include.contains(block.type));
filetered.retainWhere((block) => include.contains(block.type));
} else if (onlyLeft != null) {
filetered.removeWhere((element) => element.type != onlyLeft);
filetered.retainWhere((element) => element.type == onlyLeft);
} else if (id != null) {
filetered.removeWhere((element) => element.id == id);
filetered.retainWhere((element) => element.id == id);
}

return filetered;
Expand Down
34 changes: 30 additions & 4 deletions lib/notion/general/lists/pagination.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,44 @@ class Pagination {
String? id,
}) {
List<Block> filetered = <Block>[];
if (isBlocksList) {
if (this.isBlocksList) {
filetered.addAll(_blocks!);
if (exclude.isNotEmpty) {
filetered.removeWhere((block) => exclude.contains(block.type));
} else if (include.isNotEmpty) {
filetered.removeWhere((block) => !include.contains(block.type));
filetered.retainWhere((block) => include.contains(block.type));
} else if (onlyLeft != null) {
filetered.removeWhere((element) => element.type != onlyLeft);
filetered.retainWhere((element) => element.type == onlyLeft);
} else if (id != null) {
filetered.removeWhere((element) => element.id == id);
filetered.retainWhere((element) => element.id == id);
}
}

return filetered;
}

/// Filter the list of databases children.
///
/// Can [exclude] or [include] databases with certain ids. Also can search search databases by [id].
///
/// If all fields are included then all filters are applied as a chain following the next order: [exclude], [include], and the [id].
List<Database> filterDatabases({
List<String> exclude: const [],
List<String> include: const [],
String? id,
}) {
List<Database> filetered = <Database>[];
if (this.isDatabasesList) {
filetered.addAll(_databases!);
if (exclude.isNotEmpty) {
filetered.removeWhere((database) => exclude.contains(database.id));
} else if (include.isNotEmpty) {
filetered.retainWhere((database) => include.contains(database.id));
} else if (id != null) {
filetered.retainWhere((element) => element.id == id);
}
}

return filetered;
}
}

0 comments on commit abb1934

Please sign in to comment.