Merge pull request #231 from GetStream/add-merge-function

[Message, ChannelModel] Add merge helper functions for easier data manipulation
This commit is contained in:
Salvatore Giordano
2021-01-22 12:34:24 +01:00
committed by GitHub
3 changed files with 55 additions and 38 deletions
+2 -38
View File
@@ -1108,30 +1108,7 @@ class ChannelClientState {
final oldIndex = newMessages.indexWhere((m) => m.id == message.id);
if (oldIndex != -1) {
newMessages[oldIndex] = newMessages[oldIndex].copyWith(
text: message.text,
type: message.type,
attachments: message.attachments,
mentionedUsers: message.mentionedUsers,
reactionCounts: message.reactionCounts,
reactionScores: message.reactionScores,
latestReactions: message.latestReactions,
ownReactions: message.ownReactions,
parentId: message.parentId,
quotedMessageId: message.quotedMessageId,
quotedMessage: message.quotedMessage,
replyCount: message.replyCount,
showInChannel: message.showInChannel,
command: message.command,
silent: message.silent,
createdAt: message.createdAt,
updatedAt: message.updatedAt,
deletedAt: message.deletedAt,
user: message.user,
status: message.status,
threadParticipants: message.threadParticipants,
extraData: message.extraData,
);
newMessages[oldIndex] = newMessages[oldIndex].merge(message);
} else {
newMessages.add(message);
}
@@ -1395,20 +1372,7 @@ class ChannelClientState {
_channelState = _channelState.copyWith(
messages: newMessages,
channel: _channelState.channel?.copyWith(
lastMessageAt: updatedState.channel?.lastMessageAt,
createdAt: updatedState.channel?.createdAt,
type: updatedState.channel?.type,
extraData: updatedState.channel?.extraData,
updatedAt: updatedState.channel?.updatedAt,
id: updatedState.channel?.id,
createdBy: updatedState.channel?.createdBy,
config: updatedState.channel?.config,
deletedAt: updatedState.channel?.deletedAt,
cid: updatedState.channel?.cid,
frozen: updatedState.channel?.frozen,
memberCount: updatedState.channel?.memberCount,
),
channel: _channelState.channel?.merge(updatedState.channel),
watchers: newWatchers,
watcherCount: updatedState.watcherCount,
members: newMembers,
@@ -142,4 +142,25 @@ class ChannelModel {
extraData: extraData ?? this.extraData,
team: team ?? this.team,
);
/// Returns a new [ChannelModel] that is a combination of this channelModel and the given
/// [other] channelModel.
ChannelModel merge(ChannelModel other) {
if (other == null) return this;
return copyWith(
id: other.id,
type: other.type,
cid: other.cid,
config: other.config,
createdBy: other.createdBy,
frozen: other.frozen,
lastMessageAt: other.lastMessageAt,
createdAt: other.createdAt,
updatedAt: other.updatedAt,
deletedAt: other.deletedAt,
memberCount: other.memberCount,
extraData: other.extraData,
team: other.team,
);
}
}
@@ -250,6 +250,38 @@ class Message {
deletedAt: deletedAt ?? this.deletedAt,
status: status ?? this.status,
);
/// Returns a new [Message] that is a combination of this message and the given
/// [other] message.
Message merge(Message other) {
if (other == null) return this;
return copyWith(
id: other.id,
text: other.text,
type: other.type,
attachments: other.attachments,
mentionedUsers: other.mentionedUsers,
reactionCounts: other.reactionCounts,
reactionScores: other.reactionScores,
latestReactions: other.latestReactions,
ownReactions: other.ownReactions,
parentId: other.parentId,
quotedMessage: other.quotedMessage,
quotedMessageId: other.quotedMessageId,
replyCount: other.replyCount,
threadParticipants: other.threadParticipants,
showInChannel: other.showInChannel,
command: other.command,
createdAt: other.createdAt,
silent: other.silent,
extraData: other.extraData,
user: other.user,
shadowed: other.shadowed,
updatedAt: other.updatedAt,
deletedAt: other.deletedAt,
status: other.status,
);
}
}
/// A translated message