Skip to content

Commit 2cd4487

Browse files
committed
autocomplete: Handle general chat for topics
Signed-off-by: Zixuan James Li <[email protected]>
1 parent 3705a5c commit 2cd4487

File tree

5 files changed

+67
-6
lines changed

5 files changed

+67
-6
lines changed

lib/model/autocomplete.dart

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

846846
TopicAutocompleteResult? _testTopic(TopicAutocompleteQuery query, TopicName topic) {
847-
if (query.testTopic(topic)) {
847+
if (query.testTopic(store, topic)) {
848848
return TopicAutocompleteResult(topic: topic);
849849
}
850850
return null;
@@ -862,10 +862,17 @@ class TopicAutocompleteView extends AutocompleteView<TopicAutocompleteQuery, Top
862862
class TopicAutocompleteQuery extends AutocompleteQuery {
863863
TopicAutocompleteQuery(super.raw);
864864

865-
bool testTopic(TopicName topic) {
865+
bool testTopic(PerAccountStore store, TopicName topic) {
866866
// TODO(#881): Sort by match relevance, like web does.
867+
868+
// ignore: unnecessary_null_comparison // null topic names soon to be enabled
869+
if (topic.displayName == null) {
870+
return store.realmEmptyTopicDisplayName.toLowerCase()
871+
.contains(raw.toLowerCase());
872+
}
867873
return topic.displayName != raw
868-
&& topic.displayName.toLowerCase().contains(raw.toLowerCase());
874+
// ignore: unnecessary_non_null_assertion // null topic names soon to be enabled
875+
&& topic.displayName!.toLowerCase().contains(raw.toLowerCase());
869876
}
870877

871878
@override

lib/widgets/autocomplete.dart

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

329329
@override
330330
Widget buildItem(BuildContext context, int index, TopicAutocompleteResult option) {
331+
final Widget child;
332+
// ignore: unnecessary_null_comparison // null topic names soon to be enabled
333+
if (option.topic.displayName == null) {
334+
final store = PerAccountStoreWidget.of(context);
335+
child = Text(store.realmEmptyTopicDisplayName,
336+
style: const TextStyle(fontStyle: FontStyle.italic));
337+
} else {
338+
// ignore: unnecessary_non_null_assertion // null topic names soon to be enabled
339+
child = Text(option.topic.displayName!);
340+
}
341+
331342
return InkWell(
332343
onTap: () {
333344
_onTapOption(context, option);
334345
},
335346
child: Padding(
336347
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
337-
child: Text(option.topic.displayName)));
348+
child: child));
338349
}
339350
}

lib/widgets/compose_box.dart

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

124124
void setTopic(TopicName newTopic) {
125-
value = TextEditingValue(text: newTopic.displayName);
125+
// ignore: dead_null_aware_expression // null topic names soon to be enabled
126+
value = TextEditingValue(text: newTopic.displayName ?? '');
126127
}
127128
}
128129

test/model/autocomplete_test.dart

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

903903
group('TopicAutocompleteQuery.testTopic', () {
904+
final store = eg.store();
904905
void doCheck(String rawQuery, String topic, bool expected) {
905-
final result = TopicAutocompleteQuery(rawQuery).testTopic(eg.t(topic));
906+
final result = TopicAutocompleteQuery(rawQuery).testTopic(store, eg.t(topic));
906907
expected ? check(result).isTrue() : check(result).isFalse();
907908
}
908909

test/widgets/autocomplete_test.dart

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,5 +343,46 @@ void main() {
343343

344344
await tester.pump(Duration.zero);
345345
});
346+
347+
testWidgets('display general chat for empty topic', (tester) async {
348+
final topic = eg.getStreamTopicsEntry(name: '');
349+
final topicInputFinder = await setupToTopicInput(tester, topics: [topic]);
350+
351+
// TODO(#226): Remove this extra edit when this bug is fixed.
352+
await tester.enterText(topicInputFinder, 'a');
353+
await tester.enterText(topicInputFinder, '');
354+
await tester.pumpAndSettle();
355+
356+
check(find.text(eg.defaultRealmEmptyTopicDisplayName)).findsOne();
357+
}, skip: true); // null topic names soon to be enabled
358+
359+
testWidgets('match general chat in autocomplete', (tester) async {
360+
final topic = eg.getStreamTopicsEntry(name: '');
361+
final topicInputFinder = await setupToTopicInput(tester, topics: [topic]);
362+
363+
// TODO(#226): Remove this extra edit when this bug is fixed.
364+
await tester.enterText(topicInputFinder, 'general ch');
365+
await tester.enterText(topicInputFinder, 'general cha');
366+
await tester.pumpAndSettle();
367+
368+
check(find.text(eg.defaultRealmEmptyTopicDisplayName)).findsOne();
369+
}, skip: true); // null topic names soon to be enabled
370+
371+
testWidgets('autocomplete to general chat sets topic to empty string', (tester) async {
372+
final topic = eg.getStreamTopicsEntry(name: '');
373+
final topicInputFinder = await setupToTopicInput(tester, topics: [topic]);
374+
final controller = tester.widget<TextField>(topicInputFinder).controller!;
375+
376+
// TODO(#226): Remove this extra edit when this bug is fixed.
377+
await tester.enterText(topicInputFinder, 'general ch');
378+
await tester.enterText(topicInputFinder, 'general cha');
379+
await tester.pumpAndSettle();
380+
381+
await tester.tap(find.text(eg.defaultRealmEmptyTopicDisplayName));
382+
await tester.pump();
383+
check(controller.value).text.equals('');
384+
385+
await tester.pump(Duration.zero);
386+
}, skip: true); // null topic names soon to be enabled
346387
});
347388
}

0 commit comments

Comments
 (0)