From 44bf613f8d8a97a33d5b8b306be837ba9c96eb53 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Wed, 24 Nov 2021 12:41:25 +0100 Subject: [PATCH 1/2] fix(ui): listen for read events in the message widget --- .../stream_chat/lib/src/core/models/read.dart | 10 ++- .../lib/src/channel_preview.dart | 30 ++++++--- .../lib/src/message_widget.dart | 65 ++++++++++++------- 3 files changed, 70 insertions(+), 35 deletions(-) diff --git a/packages/stream_chat/lib/src/core/models/read.dart b/packages/stream_chat/lib/src/core/models/read.dart index 812fc6f7..0ad1164c 100644 --- a/packages/stream_chat/lib/src/core/models/read.dart +++ b/packages/stream_chat/lib/src/core/models/read.dart @@ -1,3 +1,4 @@ +import 'package:equatable/equatable.dart'; import 'package:json_annotation/json_annotation.dart'; import 'package:stream_chat/src/core/models/user.dart'; @@ -5,7 +6,7 @@ part 'read.g.dart'; /// The class that defines a read event @JsonSerializable() -class Read { +class Read extends Equatable { /// Constructor used for json serialization Read({ required this.lastRead, @@ -39,4 +40,11 @@ class Read { user: user ?? this.user, unreadMessages: unreadMessages ?? this.unreadMessages, ); + + @override + List get props => [ + lastRead, + user, + unreadMessages, + ]; } diff --git a/packages/stream_chat_flutter/lib/src/channel_preview.dart b/packages/stream_chat_flutter/lib/src/channel_preview.dart index 6e3e5363..de038c82 100644 --- a/packages/stream_chat_flutter/lib/src/channel_preview.dart +++ b/packages/stream_chat_flutter/lib/src/channel_preview.dart @@ -126,16 +126,26 @@ class ChannelPreview extends StatelessWidget { streamChatState.currentUser?.id) { return Padding( padding: const EdgeInsets.only(right: 4), - child: SendingIndicator( - message: lastMessage!, - size: channelPreviewTheme.indicatorIconSize, - isMessageRead: channel.state!.read - .where((element) => - element.user.id != - channel.client.state.currentUser!.id) - .where((element) => element.lastRead - .isAfter(lastMessage.createdAt)) - .isNotEmpty, + child: BetterStreamBuilder>( + stream: channel.state?.readStream, + initialData: channel.state?.read, + builder: (context, data) { + final readList = data.where((it) => + it.user.id != + channel.client.state.currentUser?.id && + (it.lastRead + .isAfter(lastMessage!.createdAt) || + it.lastRead.isAtSameMomentAs( + lastMessage.createdAt, + ))); + final isMessageRead = readList.length >= + (channel.memberCount ?? 0) - 1; + return SendingIndicator( + message: lastMessage!, + size: channelPreviewTheme.indicatorIconSize, + isMessageRead: isMessageRead, + ); + }, ), ); } diff --git a/packages/stream_chat_flutter/lib/src/message_widget.dart b/packages/stream_chat_flutter/lib/src/message_widget.dart index cd2488c3..b82d4c31 100644 --- a/packages/stream_chat_flutter/lib/src/message_widget.dart +++ b/packages/stream_chat_flutter/lib/src/message_widget.dart @@ -97,14 +97,20 @@ class MessageWidget extends StatefulWidget { this.deletedBottomRowBuilder, this.onReturnAction, this.customAttachmentBuilders, - this.readList, this.padding, this.textPadding = const EdgeInsets.symmetric( horizontal: 16, vertical: 8, ), this.attachmentPadding = EdgeInsets.zero, - this.allRead = false, + @Deprecated(''' + allRead is now deprecated and it will be removed in future releases. + The MessageWidget now listens for read events on its own. + ''') this.allRead = false, + @Deprecated(''' + readList is now deprecated and it will be removed in future releases. + The MessageWidget now listens for read events on its own. + ''') this.readList, this.onQuotedMessageTap, this.customActions = const [], this.onAttachmentTap, @@ -558,8 +564,6 @@ class _MessageWidgetState extends State bool get showTimeStamp => widget.showTimestamp; - bool get isMessageRead => widget.readList?.isNotEmpty == true; - bool get showInChannel => widget.showInChannelIndicator; bool get hasQuotedMessage => widget.message.quotedMessage != null; @@ -1253,27 +1257,40 @@ class _MessageWidgetState extends State ); } - Widget child = SendingIndicator( - message: message, - isMessageRead: isMessageRead, - size: style!.fontSize, + final channel = StreamChannel.of(context).channel; + + return BetterStreamBuilder>( + stream: channel.state?.readStream, + initialData: channel.state?.read, + builder: (context, data) { + final readList = data.where((it) => + it.user.id != _streamChat.currentUser?.id && + (it.lastRead.isAfter(message.createdAt) || + it.lastRead.isAtSameMomentAs(message.createdAt))); + final isMessageRead = readList.length >= (channel.memberCount ?? 0) - 1; + Widget child = SendingIndicator( + message: message, + isMessageRead: isMessageRead, + size: style!.fontSize, + ); + if (isMessageRead) { + child = Row( + children: [ + if (memberCount > 2) + Text( + readList.length.toString(), + style: style.copyWith( + color: _streamChatTheme.colorTheme.accentPrimary, + ), + ), + const SizedBox(width: 2), + child, + ], + ); + } + return child; + }, ); - if (isMessageRead) { - child = Row( - children: [ - if (memberCount > 2) - Text( - widget.readList!.length.toString(), - style: style.copyWith( - color: _streamChatTheme.colorTheme.accentPrimary, - ), - ), - const SizedBox(width: 2), - child, - ], - ); - } - return child; } Widget _buildUserAvatar() => Transform.translate( From dfc2e480d224fdad85809be2cb77471a54a47180 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Wed, 24 Nov 2021 12:42:06 +0100 Subject: [PATCH 2/2] chore(ui): update changelog --- packages/stream_chat_flutter/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/stream_chat_flutter/CHANGELOG.md b/packages/stream_chat_flutter/CHANGELOG.md index a94eaaea..f2ac6518 100644 --- a/packages/stream_chat_flutter/CHANGELOG.md +++ b/packages/stream_chat_flutter/CHANGELOG.md @@ -12,6 +12,7 @@ - Fixed `MessageWidget` null errors associated with `channel.memberCount`. - Fixed adding attachments on web. - [[#767]](https://github.com/GetStream/stream-chat-flutter/issues/767): Fix `MessageInput` focus behaviour when sending messages. +- Fixed read indicator not updating correctly in specific situations. ## 3.2.0