Merge pull request #1526 from GetStream/fix/qouted-message-delete

This commit is contained in:
Sahil Kumar
2023-05-11 13:30:59 +05:30
committed by GitHub
8 changed files with 110 additions and 63 deletions
+2
View File
@@ -4,6 +4,8 @@
- [[#1355]](https://github.com/GetStream/stream-chat-flutter/issues/1355) Fixed error while hiding channel and clearing
message history.
- [[#1525]](https://github.com/GetStream/stream-chat-flutter/issues/1525) Fixed removing message not removing quoted
message reference.
✅ Added
@@ -746,6 +746,7 @@ class Channel {
state!.deleteMessage(
message.copyWith(
type: 'deleted',
deletedAt: message.deletedAt ?? DateTime.now(),
status: MessageSendingStatus.sent,
),
hardDelete: hardDelete,
@@ -1928,11 +1929,9 @@ class ChannelClientState {
void _listenMessageDeleted() {
_subscriptions.add(_channel.on(EventType.messageDeleted).listen((event) {
final message = event.message!;
if (event.hardDelete == true) {
removeMessage(message);
} else {
updateMessage(message);
}
final hardDelete = event.hardDelete ?? false;
deleteMessage(message, hardDelete: hardDelete);
}));
}
@@ -1957,18 +1956,35 @@ class ChannelClientState {
/// Updates the [message] in the state if it exists. Adds it otherwise.
void updateMessage(Message message) {
// Regular messages, which are shown in channel.
if (message.parentId == null || message.showInChannel == true) {
final newMessages = [...messages];
var newMessages = [...messages];
final oldIndex = newMessages.indexWhere((m) => m.id == message.id);
if (oldIndex != -1) {
Message? m;
var updatedMessage = message;
// Add quoted message to the message if it is not present.
if (message.quotedMessageId != null && message.quotedMessage == null) {
final oldMessage = newMessages[oldIndex];
m = message.copyWith(
updatedMessage = updatedMessage.copyWith(
quotedMessage: oldMessage.quotedMessage,
);
}
newMessages[oldIndex] = m ?? message;
newMessages[oldIndex] = updatedMessage;
// Update quoted message reference for every message if available.
newMessages = [...newMessages].map((it) {
// Early return if the message doesn't have a quoted message.
if (it.quotedMessageId != message.id) return it;
// Setting it to null will remove the quoted message from the message
// So, we are setting the same message but with the deleted state.
return it.copyWith(
quotedMessage: updatedMessage.copyWith(
type: 'deleted',
deletedAt: updatedMessage.deletedAt ?? DateTime.now(),
),
);
}).toList();
} else {
newMessages.add(message);
}
@@ -1997,6 +2013,7 @@ class ChannelClientState {
);
}
// Thread messages, which are shown in thread page.
if (message.parentId != null) {
updateThreadInfo(message.parentId!, [message]);
}
@@ -2026,9 +2043,22 @@ class ChannelClientState {
}
// Remove regular message, thread message shown in channel
final allMessages = [...messages];
var updatedMessages = [...messages]..removeWhere((e) => e.id == message.id);
// Remove quoted message reference from every message if available.
updatedMessages = [...updatedMessages].map((it) {
// Early return if the message doesn't have a quoted message.
if (it.quotedMessageId != message.id) return it;
// Setting it to null will remove the quoted message from the message.
return it.copyWith(
quotedMessage: null,
quotedMessageId: null,
);
}).toList();
_channelState = _channelState.copyWith(
messages: allMessages..removeWhere((e) => e.id == message.id),
messages: updatedMessages,
);
}
@@ -13,6 +13,8 @@
used in message edit widget.
- [[#1523]](https://github.com/GetStream/stream-chat-flutter/issues/1523) Fixed `StreamMessageThemeData` not being
applied correctly.
- [[#1525]](https://github.com/GetStream/stream-chat-flutter/issues/1525) Fixed `StreamQuotedMessageWidget` message for
deleted messages not being shown correctly.
✅ Added
@@ -22,7 +22,6 @@ class StreamQuotedMessageWidget extends StatelessWidget {
this.padding = const EdgeInsets.all(8),
this.onTap,
this.onQuotedMessageClear,
this.composing = true,
});
/// The message
@@ -53,9 +52,6 @@ class StreamQuotedMessageWidget extends StatelessWidget {
/// Callback for clearing quoted messages.
final VoidCallback? onQuotedMessageClear;
/// True if the message is being composed
final bool composing;
@override
Widget build(BuildContext context) {
final children = [
@@ -63,11 +59,10 @@ class StreamQuotedMessageWidget extends StatelessWidget {
child: _QuotedMessage(
message: message,
textLimit: textLimit,
composing: composing,
onQuotedMessageClear: onQuotedMessageClear,
messageTheme: messageTheme,
showBorder: showBorder,
reverse: reverse,
onQuotedMessageClear: onQuotedMessageClear,
attachmentThumbnailBuilders: attachmentThumbnailBuilders,
),
),
@@ -104,17 +99,15 @@ class _QuotedMessage extends StatelessWidget {
const _QuotedMessage({
required this.message,
required this.textLimit,
required this.composing,
required this.onQuotedMessageClear,
required this.messageTheme,
required this.showBorder,
required this.reverse,
this.onQuotedMessageClear,
this.attachmentThumbnailBuilders,
});
final Message message;
final int textLimit;
final bool composing;
final VoidCallback? onQuotedMessageClear;
final StreamMessageThemeData messageTheme;
final bool showBorder;
@@ -134,6 +127,8 @@ class _QuotedMessage extends StatelessWidget {
bool get _isGiphy =>
message.attachments.any((element) => element.type == 'giphy');
bool get _isDeleted => message.isDeleted || message.deletedAt != null;
@override
Widget build(BuildContext context) {
final isOnlyEmoji = message.text!.isOnlyEmoji;
@@ -144,39 +139,54 @@ class _QuotedMessage extends StatelessWidget {
msg = msg.copyWith(text: '${msg.text!.substring(0, textLimit - 3)}...');
}
final children = [
if (composing)
PlatformWidgetBuilder(
web: (context, child) => child,
desktop: (context, child) => child,
child: ClearInputItemButton(
onTap: onQuotedMessageClear,
List<Widget> children;
if (_isDeleted) {
// Show deleted message text
children = [
Text(
context.translations.messageDeletedLabel,
style: messageTheme.messageTextStyle?.copyWith(
fontStyle: FontStyle.italic,
color: messageTheme.createdAtStyle?.color,
),
),
if (_hasAttachments)
_ParseAttachments(
message: message,
messageTheme: messageTheme,
attachmentThumbnailBuilders: attachmentThumbnailBuilders,
),
if (msg.text!.isNotEmpty && !_isGiphy)
Flexible(
child: StreamMessageText(
message: msg,
messageTheme: isOnlyEmoji && _containsText
? messageTheme.copyWith(
messageTextStyle: messageTheme.messageTextStyle?.copyWith(
fontSize: 32,
),
)
: messageTheme.copyWith(
messageTextStyle: messageTheme.messageTextStyle?.copyWith(
fontSize: 12,
),
),
];
} else {
// Show quoted message
children = [
if (onQuotedMessageClear != null)
PlatformWidgetBuilder(
web: (context, child) => child,
desktop: (context, child) => child,
child: ClearInputItemButton(
onTap: onQuotedMessageClear,
),
),
),
].insertBetween(const SizedBox(width: 8));
if (_hasAttachments)
_ParseAttachments(
message: message,
messageTheme: messageTheme,
attachmentThumbnailBuilders: attachmentThumbnailBuilders,
),
if (msg.text!.isNotEmpty && !_isGiphy)
Flexible(
child: StreamMessageText(
message: msg,
messageTheme: isOnlyEmoji && _containsText
? messageTheme.copyWith(
messageTextStyle: messageTheme.messageTextStyle?.copyWith(
fontSize: 32,
),
)
: messageTheme.copyWith(
messageTextStyle: messageTheme.messageTextStyle?.copyWith(
fontSize: 12,
),
),
),
),
].insertBetween(const SizedBox(width: 8));
}
return Container(
decoration: BoxDecoration(
@@ -204,7 +214,7 @@ class _QuotedMessage extends StatelessWidget {
}
Color? _getBackgroundColor(BuildContext context) {
if (_containsLinkAttachment) {
if (_containsLinkAttachment && !_isDeleted) {
return messageTheme.urlAttachmentBackgroundColor;
}
return messageTheme.messageBackgroundColor;
@@ -282,9 +282,14 @@ class StreamMessageListView extends StatefulWidget {
BuildContext context,
List<SpacingType> spacingTypes,
) {
if (!spacingTypes.contains(SpacingType.defaultSpacing)) {
if (spacingTypes.contains(SpacingType.otherUser)) {
return const SizedBox(height: 8);
} else if (spacingTypes.contains(SpacingType.thread)) {
return const SizedBox(height: 8);
} else if (spacingTypes.contains(SpacingType.timeDiff)) {
return const SizedBox(height: 8);
}
return const SizedBox(height: 2);
}
@@ -644,7 +649,8 @@ class _StreamMessageListViewState extends State<StreamMessageListView> {
Widget separator;
final isThread = message.replyCount! > 0;
final isPartOfThread = message.replyCount! > 0 ||
message.showInChannel == true;
if (!Jiffy(message.createdAt.toLocal()).isSame(
nextMessage.createdAt.toLocal(),
@@ -666,7 +672,7 @@ class _StreamMessageListViewState extends State<StreamMessageListView> {
final spacingRules = [
if (hasTimeDiff) SpacingType.timeDiff,
if (!isNextUserSame) SpacingType.otherUser,
if (isThread) SpacingType.thread,
if (isPartOfThread) SpacingType.thread,
if (isDeleted) SpacingType.deleted,
];
@@ -680,7 +686,7 @@ class _StreamMessageListViewState extends State<StreamMessageListView> {
);
}
if (!isThread &&
if (!isPartOfThread &&
unreadCount > 0 &&
_oldestUnreadMessage?.id == nextMessage.id) {
final unreadMessagesSeparator =
@@ -147,11 +147,10 @@ class BottomRow extends StatelessWidget {
@override
Widget build(BuildContext context) {
if (isDeleted) {
return deletedBottomRowBuilder?.call(
context,
message,
) ??
const Offstage();
final deletedBottomRowBuilder = this.deletedBottomRowBuilder;
if (deletedBottomRowBuilder != null) {
return deletedBottomRowBuilder(context, message);
}
}
final children = <WidgetSpan>[];
@@ -794,8 +794,7 @@ class _StreamMessageWidgetState extends State<StreamMessageWidget>
showUsername ||
showTimeStamp ||
showInChannel ||
showSendingIndicator ||
isDeleted;
showSendingIndicator;
/// {@template isPinned}
/// Whether [StreamMessageWidget.message] is pinned or not.
@@ -65,7 +65,6 @@ class _QuotedMessageState extends State<QuotedMessage> {
top: 8,
bottom: widget.hasNonUrlAttachments ? 8 : 0,
),
composing: false,
);
}
}