Skip to content

Commit a7e39fc

Browse files
apoorvapendsePIG208
andcommitted
autocomplete: Put best matches near input field.
This commit reverses the list that was originally presented to the user while showing the typeahead menu. This makes sense since on mobile its easier to click on options closer to the input box, i.e. where your fingers are currently present, instead of pressing arrow keys on a keyboard which is true on a desktop setup. Hence we place the best matching options not at the top of the typeahead menu, but instead put them at the bottom for better reachability and convenience of the user. Also mentions dependencies on #226 where required. Co-authored-by: Zixuan James Li <[email protected]> Fixes #1121.
1 parent 0bfa79d commit a7e39fc

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

lib/widgets/autocomplete.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ class _AutocompleteFieldState<QueryT extends AutocompleteQuery, ResultT extends
134134
constraints: const BoxConstraints(maxHeight: 300), // TODO not hard-coded
135135
child: ListView.builder(
136136
padding: EdgeInsets.zero,
137+
reverse: true,
137138
shrinkWrap: true,
138139
itemCount: _resultsToDisplay.length,
139140
itemBuilder: _buildItem))));

test/widgets/autocomplete_test.dart

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,38 @@ void main() {
177177

178178
debugNetworkImageHttpClientProvider = null;
179179
});
180+
181+
testWidgets('options are shown in reversed order', (tester) async {
182+
final users = List.generate(8, (i) => eg.user(fullName: 'A$i', avatarUrl: 'user$i.png'));
183+
final composeInputFinder = await setupToComposeInput(tester, users: users);
184+
final store = await testBinding.globalStore.perAccount(eg.selfAccount.id);
185+
186+
// TODO(#226): Remove this extra edit when this bug is fixed.
187+
await tester.enterText(composeInputFinder, 'hello @');
188+
await tester.enterText(composeInputFinder, 'hello @A');
189+
await tester.pump();
190+
// Add an extra pump to account for any potential frame delays introduced
191+
// by the post frame callback in RawAutocomplete's implementation.
192+
await tester.pump();
193+
final initialPosition = tester.getTopLeft(find.text(users.first.fullName)).dy;
194+
// Initially, all but the last autocomplete options are visible.
195+
checkUserShown(users.last, store, expected: false);
196+
users.take(7).forEach((user) => checkUserShown(user, store, expected: true));
197+
198+
// Can't scroll down because the options grow from the bottom.
199+
await tester.drag(find.byType(ListView), const Offset(0, -50));
200+
await tester.pump();
201+
check(tester.getTopLeft(find.text(users.first.fullName)).dy)
202+
.equals(initialPosition);
203+
204+
// The last autocomplete option becomes visible after scrolling up.
205+
await tester.drag(find.byType(ListView), const Offset(0, 200));
206+
await tester.pump();
207+
users.skip(1).forEach((user) => checkUserShown(user, store, expected: true));
208+
checkUserShown(users.first, store, expected: false);
209+
210+
debugNetworkImageHttpClientProvider = null;
211+
});
180212
});
181213

182214
group('emoji', () {
@@ -250,6 +282,66 @@ void main() {
250282
debugNetworkImageHttpClientProvider = null;
251283
});
252284

285+
testWidgets('emoji options appear in the reverse order and do not scroll down', (tester) async {
286+
final composeInputFinder = await setupToComposeInput(tester);
287+
final store = await testBinding.globalStore.perAccount(eg.selfAccount.id);
288+
289+
store.setServerEmojiData(
290+
ServerEmojiData(
291+
codeToNames: {
292+
'1f4a4': ['zzz'], // Unicode emoji for "zzz"
293+
'1f52a': ['biohazard'],
294+
'1f92a': ['zany_face'],
295+
'1f993': ['zebra'],
296+
'0030-fe0f-20e3': ['zero'],
297+
'1f9d0': ['zombie'],
298+
}));
299+
300+
await store.handleEvent(
301+
RealmEmojiUpdateEvent(
302+
id: 1,
303+
realmEmoji: {
304+
'1': eg.realmEmojiItem(emojiCode: '1', emojiName: 'buzzing')}));
305+
306+
final emojiNames = ['zulip', 'zany_face', 'zebra', '💤', 'zombie', 'zero', 'buzzing', 'zzz', 'biohazard'];
307+
308+
// Enter a query; options appear, of all three emoji types.
309+
// TODO(#226): Remove this extra edit when this bug is fixed.
310+
await tester.enterText(composeInputFinder, 'hi :');
311+
await tester.enterText(composeInputFinder, 'hi :z');
312+
await tester.pump();
313+
// Add an extra pump to account for any potential frame delays introduced
314+
// by the post frame callback in RawAutocomplete's implementation.
315+
await tester.pump();
316+
317+
final firstEmojiInitialPosition = tester.getTopLeft(find.text(emojiNames[0])).dy;
318+
final listViewFinder = find.byType(ListView);
319+
320+
emojiNames.take(8).forEach((emojiName) => check(find.text(emojiName)).findsOne());
321+
check(find.text(emojiNames.last)).findsNothing();
322+
323+
await tester.drag(listViewFinder, const Offset(0, -50));
324+
await tester.pump();
325+
final firstEmojiPositionAfterScrollDown = tester.getTopLeft(find.text(emojiNames[0])).dy;
326+
check(
327+
because: "ListView options should not scroll down further than initial position",
328+
firstEmojiInitialPosition,
329+
).equals(firstEmojiPositionAfterScrollDown);
330+
331+
check(
332+
because: "The last emoji should not be visible before scrolling up",
333+
find.text(emojiNames.last),
334+
).findsNothing();
335+
336+
await tester.drag(listViewFinder, const Offset(0, 50));
337+
await tester.pump();
338+
339+
check(because: "The last emoji should be visible after scrolling up",
340+
find.text(emojiNames.last)).findsOne();
341+
342+
debugNetworkImageHttpClientProvider = null;
343+
});
344+
253345
testWidgets('text emoji means just show text', (tester) async {
254346
final composeInputFinder = await setupToComposeInput(tester);
255347
final store = await testBinding.globalStore.perAccount(eg.selfAccount.id);

0 commit comments

Comments
 (0)