1
1
import 'package:dartx/dartx.dart' ;
2
2
import 'package:freezed_annotation/freezed_annotation.dart' ;
3
3
4
+ import 'artefact.dart' ;
4
5
import 'filter.dart' ;
6
+ import 'test_execution.dart' ;
5
7
6
8
part 'filters.freezed.dart' ;
7
9
@@ -22,7 +24,15 @@ class Filters<T> with _$Filters<T> {
22
24
filters: [
23
25
for (final filter in filters)
24
26
if (filter.name == filterName)
25
- filter.copyWithOptionValue (optionName, optionValue)
27
+ if (optionValue)
28
+ filter.copyWith (
29
+ selectedOptions: filter.selectedOptions.union ({optionName}),
30
+ )
31
+ else
32
+ filter.copyWith (
33
+ selectedOptions:
34
+ filter.selectedOptions.difference ({optionName}),
35
+ )
26
36
else
27
37
filter,
28
38
],
@@ -31,4 +41,69 @@ class Filters<T> with _$Filters<T> {
31
41
32
42
bool doesObjectPassFilters (T object) =>
33
43
filters.all ((filter) => filter.doesObjectPassFilter (object));
44
+
45
+ Filters <T > copyWithQueryParams (Map <String , List <String >> queryParams) {
46
+ final newFilters = filters.map ((filter) {
47
+ final values = queryParams[filter.name]? .toSet ();
48
+ if (values == null || values.isEmpty) return filter;
49
+ return filter.copyWith (
50
+ selectedOptions: values.toSet (),
51
+ detectedOptions: filter.detectedOptions.toSet ().union (values).toList ()
52
+ ..sort (),
53
+ );
54
+ });
55
+
56
+ return copyWith (filters: newFilters.toList ());
57
+ }
58
+
59
+ Map <String , List <String >> toQueryParams () {
60
+ final queryParams = < String , List <String >> {};
61
+ for (final filter in filters) {
62
+ if (filter.selectedOptions.isNotEmpty) {
63
+ queryParams[filter.name] = filter.selectedOptions.toList ();
64
+ }
65
+ }
66
+ return queryParams;
67
+ }
68
+
69
+ Filters <T > copyWithOptionsExtracted (List <T > objects) {
70
+ final newFilters = < Filter <T >> [];
71
+ for (final filter in filters) {
72
+ final options = < String > {};
73
+ for (final object in objects) {
74
+ final option = filter.extractOption (object);
75
+ if (option != null ) options.add (option);
76
+ }
77
+ newFilters
78
+ .add (filter.copyWith (detectedOptions: options.toList ()..sort ()));
79
+ }
80
+ return copyWith (filters: newFilters);
81
+ }
34
82
}
83
+
84
+ final emptyArtefactFilters = Filters <Artefact >(
85
+ filters: [
86
+ Filter <Artefact >(
87
+ name: 'Assignee' ,
88
+ extractOption: (artefact) => artefact.assignee? .name,
89
+ ),
90
+ Filter <Artefact >(
91
+ name: 'Status' ,
92
+ extractOption: (artefact) => artefact.status.name,
93
+ ),
94
+ ],
95
+ );
96
+
97
+ final emptyTestExecutionFilters = Filters <TestExecution >(
98
+ filters: [
99
+ Filter <TestExecution >(
100
+ name: 'Review status' ,
101
+ extractOption: (te) =>
102
+ te.reviewDecision.isEmpty ? 'Undecided' : 'Reviewed' ,
103
+ ),
104
+ Filter <TestExecution >(
105
+ name: 'Execution status' ,
106
+ extractOption: (te) => te.status.name,
107
+ ),
108
+ ],
109
+ );
0 commit comments