Skip to content

Commit 2795e98

Browse files
committed
autocomplete: Handle autocompleting to empty topic
Signed-off-by: Zixuan James Li <[email protected]>
1 parent e4528fd commit 2795e98

File tree

5 files changed

+65
-6
lines changed

5 files changed

+65
-6
lines changed

lib/model/autocomplete.dart

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,7 @@ class TopicAutocompleteView extends AutocompleteView<TopicAutocompleteQuery, Top
921921
}
922922

923923
TopicAutocompleteResult? _testTopic(TopicAutocompleteQuery query, TopicName topic) {
924-
if (query.testTopic(topic)) {
924+
if (query.testTopic(store, topic)) {
925925
return TopicAutocompleteResult(topic: topic);
926926
}
927927
return null;
@@ -939,10 +939,17 @@ class TopicAutocompleteView extends AutocompleteView<TopicAutocompleteQuery, Top
939939
class TopicAutocompleteQuery extends AutocompleteQuery {
940940
TopicAutocompleteQuery(super.raw);
941941

942-
bool testTopic(TopicName topic) {
942+
bool testTopic(PerAccountStore store, TopicName topic) {
943943
// TODO(#881): Sort by match relevance, like web does.
944+
945+
// ignore: unnecessary_null_comparison // null topic names soon to be enabled
946+
if (topic.displayName == null) {
947+
return store.realmEmptyTopicDisplayName.toLowerCase()
948+
.contains(raw.toLowerCase());
949+
}
944950
return topic.displayName != raw
945-
&& topic.displayName.toLowerCase().contains(raw.toLowerCase());
951+
// ignore: unnecessary_non_null_assertion // null topic names soon to be enabled
952+
&& topic.displayName!.toLowerCase().contains(raw.toLowerCase());
946953
}
947954

948955
@override

lib/widgets/autocomplete.dart

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,12 +365,23 @@ class TopicAutocomplete extends AutocompleteField<TopicAutocompleteQuery, TopicA
365365

366366
@override
367367
Widget buildItem(BuildContext context, int index, TopicAutocompleteResult option) {
368+
final Widget child;
369+
// ignore: unnecessary_null_comparison // null topic names soon to be enabled
370+
if (option.topic.displayName == null) {
371+
final store = PerAccountStoreWidget.of(context);
372+
child = Text(store.realmEmptyTopicDisplayName,
373+
style: const TextStyle(fontStyle: FontStyle.italic));
374+
} else {
375+
// ignore: unnecessary_non_null_assertion // null topic names soon to be enabled
376+
child = Text(option.topic.displayName!);
377+
}
378+
368379
return InkWell(
369380
onTap: () {
370381
_onTapOption(context, option);
371382
},
372383
child: Padding(
373384
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
374-
child: Text(option.topic.displayName)));
385+
child: child));
375386
}
376387
}

lib/widgets/compose_box.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ class ComposeTopicController extends ComposeController<TopicValidationError> {
182182
}
183183

184184
void setTopic(TopicName newTopic) {
185-
value = TextEditingValue(text: newTopic.displayName);
185+
// ignore: dead_null_aware_expression // null topic names soon to be enabled
186+
value = TextEditingValue(text: newTopic.displayName ?? '');
186187
}
187188
}
188189

test/model/autocomplete_test.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1027,8 +1027,9 @@ void main() {
10271027
});
10281028

10291029
group('TopicAutocompleteQuery.testTopic', () {
1030+
final store = eg.store();
10301031
void doCheck(String rawQuery, String topic, bool expected) {
1031-
final result = TopicAutocompleteQuery(rawQuery).testTopic(eg.t(topic));
1032+
final result = TopicAutocompleteQuery(rawQuery).testTopic(store, eg.t(topic));
10321033
expected ? check(result).isTrue() : check(result).isFalse();
10331034
}
10341035

test/widgets/autocomplete_test.dart

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,5 +401,44 @@ void main() {
401401

402402
await tester.pump(Duration.zero);
403403
});
404+
405+
testWidgets('display general chat for empty topic', (tester) async {
406+
final topic = eg.getStreamTopicsEntry(name: '');
407+
final topicInputFinder = await setupToTopicInput(tester, topics: [topic]);
408+
409+
// TODO(#226): Remove this extra edit when this bug is fixed.
410+
await tester.enterText(topicInputFinder, 'a');
411+
await tester.enterText(topicInputFinder, '');
412+
await tester.pumpAndSettle();
413+
414+
check(find.text(eg.defaultRealmEmptyTopicDisplayName)).findsOne();
415+
}, skip: true); // null topic names soon to be enabled
416+
417+
testWidgets('match general chat in autocomplete', (tester) async {
418+
final topic = eg.getStreamTopicsEntry(name: '');
419+
final topicInputFinder = await setupToTopicInput(tester, topics: [topic]);
420+
421+
// TODO(#226): Remove this extra edit when this bug is fixed.
422+
await tester.enterText(topicInputFinder, 'general ch');
423+
await tester.enterText(topicInputFinder, 'general cha');
424+
await tester.pumpAndSettle();
425+
426+
check(find.text(eg.defaultRealmEmptyTopicDisplayName)).findsOne();
427+
}, skip: true); // null topic names soon to be enabled
428+
429+
testWidgets('autocomplete to general chat sets topic to empty string', (tester) async {
430+
final topic = eg.getStreamTopicsEntry(name: '');
431+
final topicInputFinder = await setupToTopicInput(tester, topics: [topic]);
432+
final controller = tester.widget<TextField>(topicInputFinder).controller!;
433+
434+
// TODO(#226): Remove this extra edit when this bug is fixed.
435+
await tester.enterText(topicInputFinder, 'general ch');
436+
await tester.enterText(topicInputFinder, 'general cha');
437+
await tester.pumpAndSettle();
438+
439+
await tester.tap(find.text(eg.defaultRealmEmptyTopicDisplayName));
440+
await tester.pump(Duration.zero);
441+
check(controller.value).text.equals('');
442+
}, skip: true); // null topic names soon to be enabled
404443
});
405444
}

0 commit comments

Comments
 (0)