Skip to content

Commit da83d0e

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 1ffe2b8 commit da83d0e

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
@@ -415,12 +415,23 @@ class TopicAutocomplete extends AutocompleteField<TopicAutocompleteQuery, TopicA
415415

416416
@override
417417
Widget buildItem(BuildContext context, int index, TopicAutocompleteResult option) {
418+
final Widget child;
419+
// ignore: unnecessary_null_comparison // null topic names soon to be enabled
420+
if (option.topic.displayName == null) {
421+
final store = PerAccountStoreWidget.of(context);
422+
child = Text(store.realmEmptyTopicDisplayName,
423+
style: const TextStyle(fontStyle: FontStyle.italic));
424+
} else {
425+
// ignore: unnecessary_non_null_assertion // null topic names soon to be enabled
426+
child = Text(option.topic.displayName!);
427+
}
428+
418429
return InkWell(
419430
onTap: () {
420431
_onTapOption(context, option);
421432
},
422433
child: Padding(
423434
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
424-
child: Text(option.topic.displayName)));
435+
child: child));
425436
}
426437
}

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;
@@ -459,5 +461,47 @@ void main() {
459461

460462
await tester.pump(Duration.zero);
461463
});
464+
465+
testWidgets('display realmEmptyTopicDisplayName for empty topic', (tester) async {
466+
final topic = eg.getStreamTopicsEntry(name: '');
467+
final topicInputFinder = await setupToTopicInput(tester, topics: [topic],
468+
realmEmptyTopicDisplayName: 'some display name');
469+
470+
// TODO(#226): Remove this extra edit when this bug is fixed.
471+
await tester.enterText(topicInputFinder, ' ');
472+
await tester.enterText(topicInputFinder, '');
473+
await tester.pumpAndSettle();
474+
475+
check(find.text('some display name')).findsOne();
476+
}, skip: true); // null topic names soon to be enabled
477+
478+
testWidgets('match realmEmptyTopicDisplayName in autocomplete', (tester) async {
479+
final topic = eg.getStreamTopicsEntry(name: '');
480+
final topicInputFinder = await setupToTopicInput(tester, topics: [topic],
481+
realmEmptyTopicDisplayName: 'general chat');
482+
483+
// TODO(#226): Remove this extra edit when this bug is fixed.
484+
await tester.enterText(topicInputFinder, 'general ch');
485+
await tester.enterText(topicInputFinder, 'general cha');
486+
await tester.pumpAndSettle();
487+
488+
check(find.text('general chat')).findsOne();
489+
}, skip: true); // null topic names soon to be enabled
490+
491+
testWidgets('autocomplete to realmEmptyTopicDisplayName sets topic to empty string', (tester) async {
492+
final topic = eg.getStreamTopicsEntry(name: '');
493+
final topicInputFinder = await setupToTopicInput(tester, topics: [topic],
494+
realmEmptyTopicDisplayName: 'general chat');
495+
final controller = tester.widget<TextField>(topicInputFinder).controller!;
496+
497+
// TODO(#226): Remove this extra edit when this bug is fixed.
498+
await tester.enterText(topicInputFinder, 'general ch');
499+
await tester.enterText(topicInputFinder, 'general cha');
500+
await tester.pumpAndSettle();
501+
502+
await tester.tap(find.text('general chat'));
503+
await tester.pump(Duration.zero);
504+
check(controller.value).text.equals('');
505+
}, skip: true); // null topic names soon to be enabled
462506
});
463507
}

0 commit comments

Comments
 (0)