Skip to content

Commit 77a30cc

Browse files
committed
fix: report-builder chips could stack the same filter token repeatedly
Tapping +ACTIVE twice produced "+ACTIVE +ACTIVE"; tapping status: twice produced "status: status:". Filter tokens AND together, so a duplicate is never meaningful — a repeated virtual tag is pure noise, and a second attr: token can only contradict the first (status:pending status:completed matches nothing). A chip whose token the expression already contains now inserts nothing and renders disabled instead of looking tappable-but-inert. Presence is judged against the live field text, so it tracks hand-typed tokens and the filter of a report opened for editing, not just prior chip taps. The comparison is case-insensitive because the engine is (+active and +ACTIVE are the same filter), and an attribute chip counts as present once any token starts with "attr:" — a completed status:pending keeps the status: chip disabled for the same AND-semantics reason. Tested: four new widget tests (426 Dart tests pass) — double-tap inserts one token, attribute prefix disables on any attr: token, hand-typed lowercase tokens disable their chips, and an edited report opens with its tokens already disabled. Verified on device in both create and edit flows.
1 parent 5b3f8a6 commit 77a30cc

2 files changed

Lines changed: 157 additions & 4 deletions

File tree

‎lib/app/modules/report_engine/views/report_builder_sheet.dart‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,22 @@ class _ReportBuilderSheetState extends State<ReportBuilderSheet> {
105105
}
106106
}
107107

108+
/// Whether the expression already carries [token]. Tokens AND together, so
109+
/// a repeated virtual tag is pure noise and a second `attr:` token can only
110+
/// contradict the first (`status:pending status:completed` matches nothing) —
111+
/// either way there is nothing for a second tap to add. Case-insensitive
112+
/// because the engine is: `+active` and `+ACTIVE` are the same filter.
113+
bool _tokenPresent(String token) {
114+
final String want = token.toLowerCase();
115+
final bool isAttributePrefix = token.endsWith(':');
116+
return _filter.text
117+
.split(RegExp(r'\s+'))
118+
.map((t) => t.toLowerCase())
119+
.any((t) => isAttributePrefix ? t.startsWith(want) : t == want);
120+
}
121+
108122
void _insertToken(String token) {
123+
if (_tokenPresent(token)) return;
109124
final String current = _filter.text.trimRight();
110125
final String next = current.isEmpty ? token : '$current $token';
111126
_filter.text = next;
@@ -196,10 +211,15 @@ class _ReportBuilderSheetState extends State<ReportBuilderSheet> {
196211
spacing: 6,
197212
runSpacing: 4,
198213
children: [
214+
// A chip whose token is already in the expression is disabled
215+
// rather than left looking tappable-but-inert; the chip row
216+
// rebuilds on every keystroke, so this tracks hand-edits too.
199217
for (final String t in VirtualFilterEngine.virtualTags)
200-
_chip(c, '+$t', () => _insertToken('+$t')),
218+
_chip(c, '+$t',
219+
_tokenPresent('+$t') ? null : () => _insertToken('+$t')),
201220
for (final String a in VirtualFilterEngine.attributes)
202-
_chip(c, '$a:', () => _insertToken('$a:')),
221+
_chip(c, '$a:',
222+
_tokenPresent('$a:') ? null : () => _insertToken('$a:')),
203223
],
204224
),
205225

@@ -314,12 +334,17 @@ class _ReportBuilderSheetState extends State<ReportBuilderSheet> {
314334
),
315335
);
316336

317-
Widget _chip(TaskwarriorColorTheme c, String label, VoidCallback onTap) =>
337+
Widget _chip(TaskwarriorColorTheme c, String label, VoidCallback? onTap) =>
318338
ActionChip(
319339
label: Text(label,
320340
style: GoogleFonts.poppins(
321-
fontSize: 11, color: c.primaryTextColor)),
341+
fontSize: 11,
342+
color: onTap == null
343+
? c.secondaryTextColor
344+
: c.primaryTextColor)),
322345
backgroundColor: c.secondaryBackgroundColor,
346+
// Null disables the chip — Flutter renders it non-interactive, which
347+
// is the honest state for a token the expression already contains.
323348
onPressed: onTap,
324349
visualDensity: VisualDensity.compact,
325350
);
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
import 'package:get/get.dart';
4+
import 'package:taskwarrior/app/models/report.dart';
5+
import 'package:taskwarrior/app/modules/report_engine/controllers/report_engine_controller.dart';
6+
import 'package:taskwarrior/app/modules/report_engine/views/report_builder_sheet.dart';
7+
import 'package:taskwarrior/app/utils/themes/dark_theme.dart';
8+
9+
/// The filter chips insert tokens into the expression. Tokens AND together,
10+
/// so a duplicate is never meaningful: a repeated virtual tag is noise and a
11+
/// second `attr:` token can only contradict the first. These pin the rule that
12+
/// a chip whose token is already present is disabled and inserts nothing —
13+
/// whether the token came from a chip tap, a hand-edit, or the report being
14+
/// edited.
15+
void main() {
16+
TestWidgetsFlutterBinding.ensureInitialized();
17+
18+
setUp(() {
19+
Get.testMode = true;
20+
Get.put(ReportEngineController());
21+
});
22+
23+
tearDown(Get.reset);
24+
25+
Future<void> pump(WidgetTester tester, {ReportDefinition? existing}) async {
26+
await tester.pumpWidget(
27+
GetMaterialApp(
28+
theme: darkTheme,
29+
home: Scaffold(
30+
body: SingleChildScrollView(
31+
child: ReportBuilderSheet(
32+
controller: Get.find<ReportEngineController>(),
33+
existing: existing,
34+
),
35+
),
36+
),
37+
),
38+
);
39+
await tester.pumpAndSettle();
40+
}
41+
42+
String filterText(WidgetTester tester) {
43+
final EditableText field = tester.widget<EditableText>(
44+
find.descendant(
45+
of: find.byType(TextField),
46+
matching: find.byType(EditableText),
47+
).at(2), // name, description, filter
48+
);
49+
return field.controller.text;
50+
}
51+
52+
// Scoped to chips: once a token is inserted, the filter field renders the
53+
// same string, so a bare find.text() would match both.
54+
Finder chipFinder(String label) => find.descendant(
55+
of: find.byType(ActionChip),
56+
matching: find.text(label),
57+
);
58+
59+
ActionChip chip(WidgetTester tester, String label) =>
60+
tester.widget<ActionChip>(find.ancestor(
61+
of: chipFinder(label),
62+
matching: find.byType(ActionChip),
63+
));
64+
65+
testWidgets('tapping the same virtual-tag chip twice inserts one token',
66+
(tester) async {
67+
await pump(tester);
68+
69+
await tester.tap(chipFinder('+ACTIVE'));
70+
await tester.pumpAndSettle();
71+
expect(filterText(tester), '+ACTIVE');
72+
// The chip is disabled now, but tap anyway (warnIfMissed off) to prove a
73+
// second activation could not double the token even if it got through.
74+
await tester.tap(chipFinder('+ACTIVE'), warnIfMissed: false);
75+
await tester.pumpAndSettle();
76+
77+
expect(filterText(tester), '+ACTIVE');
78+
expect(chip(tester, '+ACTIVE').onPressed, isNull,
79+
reason: 'a chip whose token is present must be disabled');
80+
expect(chip(tester, '+READY').onPressed, isNotNull,
81+
reason: 'other chips stay tappable');
82+
});
83+
84+
testWidgets('an attribute chip is disabled once any attr: token exists',
85+
(tester) async {
86+
await pump(tester);
87+
88+
await tester.tap(chipFinder('status:'));
89+
await tester.pumpAndSettle();
90+
expect(filterText(tester), 'status:');
91+
// A second status: filter can only contradict the first.
92+
expect(chip(tester, 'status:').onPressed, isNull);
93+
expect(chip(tester, 'project:').onPressed, isNotNull);
94+
});
95+
96+
testWidgets('a hand-typed token disables its chip, case-insensitively',
97+
(tester) async {
98+
await pump(tester);
99+
100+
await tester.enterText(
101+
find.byType(TextField).at(2), 'status:pending +active');
102+
await tester.pumpAndSettle();
103+
104+
expect(chip(tester, '+ACTIVE').onPressed, isNull,
105+
reason: 'the engine treats +active and +ACTIVE as the same filter');
106+
expect(chip(tester, 'status:').onPressed, isNull);
107+
expect(chip(tester, '+READY').onPressed, isNotNull);
108+
});
109+
110+
testWidgets('editing an existing report opens with its tokens disabled',
111+
(tester) async {
112+
await pump(
113+
tester,
114+
existing: ReportDefinition(
115+
name: 'my-overdue',
116+
description: 'my-overdue',
117+
filterExpression: '+OVERDUE',
118+
sortCriteria: SortCriterion.parseList('due+'),
119+
columns: ColumnSpec.parseList('id,description'),
120+
isCustom: true,
121+
),
122+
);
123+
124+
expect(filterText(tester), '+OVERDUE');
125+
expect(chip(tester, '+OVERDUE').onPressed, isNull);
126+
expect(chip(tester, '+ACTIVE').onPressed, isNotNull);
127+
});
128+
}

0 commit comments

Comments
 (0)