Skip to content

Commit 1a417cc

Browse files
committed
msglist: Display typing indicators on typing.
Because we don't have a Figma design yet, this revision supports a basic design similar to the web app when there are people typing. Fixes #665. Signed-off-by: Zixuan James Li <[email protected]>
1 parent cc95666 commit 1a417cc

File tree

4 files changed

+152
-5
lines changed

4 files changed

+152
-5
lines changed

assets/l10n/app_en.arb

+19
Original file line numberDiff line numberDiff line change
@@ -483,5 +483,24 @@
483483
"notifSelfUser": "You",
484484
"@notifSelfUser": {
485485
"description": "Display name for the user themself, to show after replying in an Android notification"
486+
},
487+
"onePersonTyping": "{typist} is typing…",
488+
"@onePersonTyping": {
489+
"description": "Text to display when there is one user typing.",
490+
"placeholders": {
491+
"typist": {"type": "String", "example": "Alice"}
492+
}
493+
},
494+
"twoPeopleTyping": "{typist} and {otherTypist} are typing…",
495+
"@twoPeopleTyping": {
496+
"description": "Text to display when there is one user typing.",
497+
"placeholders": {
498+
"typist": {"type": "String", "example": "Alice"},
499+
"otherTypist": {"type": "String", "example": "Bob"}
500+
}
501+
},
502+
"manyPeopleTyping": "Several people are typing…",
503+
"@manyPeopleTyping": {
504+
"description": "Text to display when there is one user typing."
486505
}
487506
}

lib/model/typing_status.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class TypingStatus extends ChangeNotifier {
2020
Iterable<SendableNarrow> get debugActiveNarrows => _timerMapsByNarrow.keys;
2121

2222
Iterable<int> typistIdsInNarrow(SendableNarrow narrow) =>
23-
_timerMapsByNarrow[narrow]?.keys ?? [];
23+
_timerMapsByNarrow[narrow]?.keys.where((id) => id != selfUserId) ?? [];
2424

2525
// Using SendableNarrow as the key covers the narrows
2626
// where typing notifications are supported (topics and DMs).
@@ -64,7 +64,7 @@ class TypingStatus extends ChangeNotifier {
6464
void handleTypingEvent(TypingEvent event) {
6565
SendableNarrow narrow = switch (event.messageType) {
6666
MessageType.direct => DmNarrow(
67-
allRecipientIds: event.recipientIds!..sort(), selfUserId: selfUserId),
67+
allRecipientIds: event.recipientIds!.toList()..sort(), selfUserId: selfUserId),
6868
MessageType.stream => TopicNarrow(event.streamId!, event.topic!),
6969
};
7070

lib/widgets/message_list.dart

+68-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ import 'dart:math';
22

33
import 'package:collection/collection.dart';
44
import 'package:flutter/material.dart';
5+
import 'package:flutter_color_models/flutter_color_models.dart';
56
import 'package:flutter_gen/gen_l10n/zulip_localizations.dart';
67
import 'package:intl/intl.dart';
78

89
import '../api/model/model.dart';
910
import '../model/message_list.dart';
1011
import '../model/narrow.dart';
1112
import '../model/store.dart';
13+
import '../model/typing_status.dart';
1214
import 'action_sheet.dart';
1315
import 'actions.dart';
1416
import 'compose_box.dart';
@@ -496,17 +498,19 @@ class _MessageListState extends State<MessageList> with PerAccountStoreAwareStat
496498
final valueKey = key as ValueKey<int>;
497499
final index = model!.findItemWithMessageId(valueKey.value);
498500
if (index == -1) return null;
499-
return length - 1 - (index - 2);
501+
return length - 1 - (index - 3);
500502
},
501-
childCount: length + 2,
503+
childCount: length + 3,
502504
(context, i) {
503505
// To reinforce that the end of the feed has been reached:
504506
// https://chat.zulip.org/#narrow/stream/243-mobile-team/topic/flutter.3A.20Mark-as-read/near/1680603
505507
if (i == 0) return const SizedBox(height: 36);
506508

507509
if (i == 1) return MarkAsReadWidget(narrow: widget.narrow);
508510

509-
final data = model!.items[length - 1 - (i - 2)];
511+
if (i == 2) return TypingStatusWidget(narrow: widget.narrow);
512+
513+
final data = model!.items[length - 1 - (i - 3)];
510514
return _buildItem(data, i);
511515
}));
512516

@@ -609,6 +613,67 @@ class ScrollToBottomButton extends StatelessWidget {
609613
}
610614
}
611615

616+
class TypingStatusWidget extends StatefulWidget {
617+
const TypingStatusWidget({super.key, required this.narrow});
618+
619+
final Narrow narrow;
620+
621+
@override
622+
State<StatefulWidget> createState() => _TypingStatusWidgetState();
623+
}
624+
625+
class _TypingStatusWidgetState extends State<TypingStatusWidget> with PerAccountStoreAwareStateMixin<TypingStatusWidget> {
626+
TypingStatus? model;
627+
628+
@override
629+
void onNewStore() {
630+
model?.removeListener(_modelChanged);
631+
model = PerAccountStoreWidget.of(context).typingStatus
632+
..addListener(_modelChanged);
633+
}
634+
635+
@override
636+
void dispose() {
637+
model?.removeListener(_modelChanged);
638+
super.dispose();
639+
}
640+
641+
void _modelChanged() {
642+
setState(() {
643+
// The actual state lives in [model].
644+
// This method was called because that just changed.
645+
});
646+
}
647+
648+
@override
649+
Widget build(BuildContext context) {
650+
final store = PerAccountStoreWidget.of(context);
651+
final narrow = widget.narrow;
652+
if (narrow is! SendableNarrow) return const SizedBox();
653+
654+
final localizations = ZulipLocalizations.of(context);
655+
final typistIds = model!.typistIdsInNarrow(narrow).toList();
656+
if (typistIds.isEmpty) return const SizedBox();
657+
final String text = switch (typistIds) {
658+
[final firstTypist] =>
659+
localizations.onePersonTyping(
660+
store.users[firstTypist]?.fullName ?? localizations.unknownUserName),
661+
[final firstTypist, final secondTypist] =>
662+
localizations.twoPeopleTyping(
663+
store.users[firstTypist]?.fullName ?? localizations.unknownUserName,
664+
store.users[secondTypist]?.fullName ?? localizations.unknownUserName,
665+
),
666+
_ => localizations.manyPeopleTyping,
667+
};
668+
669+
return Padding(
670+
padding: const EdgeInsetsDirectional.only(start: 16, top: 2),
671+
child: Text(text,
672+
style: const TextStyle(
673+
color: HslColor(0, 0, 53), fontStyle: FontStyle.italic)));
674+
}
675+
}
676+
612677
class MarkAsReadWidget extends StatefulWidget {
613678
const MarkAsReadWidget({super.key, required this.narrow});
614679

test/widgets/message_list_test.dart

+63
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,69 @@ void main() {
320320
});
321321
});
322322

323+
group('TypingStatusWidget', () {
324+
final users = [eg.selfUser, eg.otherUser, eg.thirdUser, eg.fourthUser];
325+
final finder = find.descendant(
326+
of: find.byType(TypingStatusWidget),
327+
matching: find.byType(Text)
328+
);
329+
330+
Future<void> checkTyping(WidgetTester tester, TypingEvent event, {required String expected}) async {
331+
await store.handleEvent(event);
332+
await tester.pump();
333+
check(tester.widget<Text>(finder)).data.equals(expected);
334+
}
335+
336+
final dmMessage = eg.dmMessage(from: eg.selfUser, to: [eg.otherUser]);
337+
final dmNarrow = DmNarrow.ofMessage(dmMessage, selfUserId: eg.selfUser.userId);
338+
339+
final streamMessage = eg.streamMessage();
340+
final topicNarrow = TopicNarrow.ofMessage(streamMessage);
341+
342+
for (final (description, message, narrow) in [
343+
('typing in dm', dmMessage, dmNarrow),
344+
('typing in topic', streamMessage, topicNarrow),
345+
]) {
346+
testWidgets(description, (tester) async {
347+
await setupMessageListPage(tester,
348+
narrow: narrow, users: users, messages: [message]);
349+
await tester.pump();
350+
check(finder.evaluate()).isEmpty();
351+
await checkTyping(tester,
352+
eg.typingEvent(narrow, TypingOp.start, eg.otherUser.userId),
353+
expected: 'Other User is typing…');
354+
await checkTyping(tester,
355+
eg.typingEvent(narrow, TypingOp.start, eg.selfUser.userId),
356+
expected: 'Other User is typing…');
357+
await checkTyping(tester,
358+
eg.typingEvent(narrow, TypingOp.start, eg.thirdUser.userId),
359+
expected: 'Other User and Third User are typing…');
360+
await checkTyping(tester,
361+
eg.typingEvent(narrow, TypingOp.start, eg.fourthUser.userId),
362+
expected: 'Several people are typing…');
363+
await checkTyping(tester,
364+
eg.typingEvent(narrow, TypingOp.stop, eg.otherUser.userId),
365+
expected: 'Third User and Fourth User are typing…');
366+
// Verify that typing indicators expire after a set duration.
367+
await tester.pump(const Duration(seconds: 15));
368+
check(finder.evaluate()).isEmpty();
369+
});
370+
}
371+
372+
testWidgets('unknown user typing', (tester) async {
373+
final streamMessage = eg.streamMessage();
374+
final narrow = TopicNarrow.ofMessage(streamMessage);
375+
await setupMessageListPage(tester,
376+
narrow: narrow, users: [], messages: [streamMessage]);
377+
await checkTyping(tester,
378+
eg.typingEvent(narrow, TypingOp.start, 1000),
379+
expected: '(unknown user) is typing…',
380+
);
381+
// Wait for the pending timers to end.
382+
await tester.pump(const Duration(seconds: 15));
383+
});
384+
});
385+
323386
group('MarkAsReadWidget', () {
324387
bool isMarkAsReadButtonVisible(WidgetTester tester) {
325388
final zulipLocalizations = GlobalLocalizations.zulipLocalizations;

0 commit comments

Comments
 (0)