Skip to content

Commit 908d4cf

Browse files
committed
autocomplete [nfc]: Handle autocompleting to empty topic
While this appears to be a user facing change, it's not visible yet, not until TopicName.displayName becomes nullable. This is a part of a series of changes to handle empty topics. Signed-off-by: Zixuan James Li <[email protected]>
1 parent 755df57 commit 908d4cf

File tree

5 files changed

+71
-7
lines changed

5 files changed

+71
-7
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(topic, store)) {
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(TopicName topic, PerAccountStore store) {
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
@@ -367,12 +367,23 @@ class TopicAutocomplete extends AutocompleteField<TopicAutocompleteQuery, TopicA
367367

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

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(eg.t(topic), store);
10321033
expected ? check(result).isTrue() : check(result).isFalse();
10331034
}
10341035

test/widgets/autocomplete_test.dart

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,11 @@ Future<Finder> setupToComposeInput(WidgetTester tester, {
9999
/// Returns a [Finder] for the topic input's [TextField].
100100
Future<Finder> setupToTopicInput(WidgetTester tester, {
101101
required List<GetStreamTopicsEntry> topics,
102+
String? realmEmptyTopicDisplayName,
102103
}) async {
103104
addTearDown(testBinding.reset);
104-
await testBinding.globalStore.add(eg.selfAccount, eg.initialSnapshot());
105+
await testBinding.globalStore.add(eg.selfAccount, eg.initialSnapshot(
106+
realmEmptyTopicDisplayName: realmEmptyTopicDisplayName));
105107
final store = await testBinding.globalStore.perAccount(eg.selfAccount.id);
106108
await store.addUser(eg.selfUser);
107109
final connection = store.connection as FakeApiConnection;
@@ -401,5 +403,47 @@ void main() {
401403

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

0 commit comments

Comments
 (0)