calculateChatMessages function
Parses provided messages to chat messages (with headers and spacers) and returns them with a gallery
Implementation
List<Object> calculateChatMessages(
List<types.Message> messages,
types.User user, {
String Function(DateTime)? customDateHeaderText,
DateFormat? dateFormat,
required int dateHeaderThreshold,
String? dateLocale,
required int groupMessagesThreshold,
required bool showUserNames,
DateFormat? timeFormat,
}) {
final chatMessages = <Object>[];
final gallery = <PreviewImage>[];
var shouldShowName = false;
for (var i = messages.length - 1; i >= 0; i--) {
final isFirst = i == messages.length - 1;
final isLast = i == 0;
final message = messages[i];
final messageHasCreatedAt = message.createdAt != null;
final nextMessage = isLast ? null : messages[i - 1];
final nextMessageHasCreatedAt = nextMessage?.createdAt != null;
final nextMessageSameAuthor = message.author.id == nextMessage?.author.id;
final notMyMessage = message.author.id != user.id;
var nextMessageDateThreshold = false;
var nextMessageDifferentDay = false;
var nextMessageInGroup = false;
var showName = false;
if (showUserNames) {
final previousMessage = isFirst ? null : messages[i + 1];
final isFirstInGroup = notMyMessage &&
((message.author.id != previousMessage?.author.id) ||
(messageHasCreatedAt &&
previousMessage?.createdAt != null &&
message.createdAt! - previousMessage!.createdAt! >
groupMessagesThreshold));
if (isFirstInGroup) {
shouldShowName = false;
if (message.type == types.MessageType.text) {
showName = true;
} else {
shouldShowName = true;
}
}
if (message.type == types.MessageType.text && shouldShowName) {
showName = true;
shouldShowName = false;
}
}
if (messageHasCreatedAt && nextMessageHasCreatedAt) {
nextMessageDateThreshold =
nextMessage!.createdAt! - message.createdAt! >= dateHeaderThreshold;
nextMessageDifferentDay =
DateTime.fromMillisecondsSinceEpoch(message.createdAt!).day !=
DateTime.fromMillisecondsSinceEpoch(nextMessage.createdAt!).day;
nextMessageInGroup = nextMessageSameAuthor &&
nextMessage.createdAt! - message.createdAt! <= groupMessagesThreshold;
}
if (isFirst && messageHasCreatedAt) {
chatMessages.insert(
0,
DateHeader(
dateTime: DateTime.fromMillisecondsSinceEpoch(message.createdAt!),
text: customDateHeaderText != null
? customDateHeaderText(
DateTime.fromMillisecondsSinceEpoch(message.createdAt!),
)
: getVerboseDateTimeRepresentation(
DateTime.fromMillisecondsSinceEpoch(message.createdAt!),
dateFormat: dateFormat,
dateLocale: dateLocale,
timeFormat: timeFormat,
),
),
);
}
chatMessages.insert(0, {
'message': message,
'nextMessageInGroup': nextMessageInGroup,
'showName': notMyMessage && showUserNames && showName,
'showStatus': message.showStatus ?? true,
});
if (!nextMessageInGroup) {
chatMessages.insert(
0,
MessageSpacer(
height: 12,
id: message.id,
),
);
}
if (nextMessageDifferentDay || nextMessageDateThreshold) {
chatMessages.insert(
0,
DateHeader(
dateTime:
DateTime.fromMillisecondsSinceEpoch(nextMessage!.createdAt!),
text: customDateHeaderText != null
? customDateHeaderText(
DateTime.fromMillisecondsSinceEpoch(nextMessage.createdAt!),
)
: getVerboseDateTimeRepresentation(
DateTime.fromMillisecondsSinceEpoch(nextMessage.createdAt!),
dateFormat: dateFormat,
dateLocale: dateLocale,
timeFormat: timeFormat,
),
),
);
}
if (message is types.ImageMessage) {
if (kIsWeb) {
if (message.uri.startsWith('http') || message.uri.startsWith('blob')) {
gallery.add(PreviewImage(id: message.id, uri: message.uri));
}
} else {
gallery.add(PreviewImage(id: message.id, uri: message.uri));
}
}
}
return [chatMessages, gallery];
}