sortMessages method

void sortMessages()

Sorts messages in this group by timestamp and failure state.

This method ensures proper message ordering:

  1. Successful messages are sorted chronologically
  2. Failed sender messages are moved to the end
  3. Maintains consistent display order for better UX

Implementation

void sortMessages() {
  messages.sort((a, b) {
    if (a is SenderMessage && b is SenderMessage) {
      if (a.isFailed && !b.isFailed) return 1;
      if (!a.isFailed && b.isFailed) return -1;
      if (a.isLoading && !b.isLoading) return 1;
      if (!a.isLoading && b.isLoading) return -1;
    }
    final aTimestamp = DateTime.parse(a.timestamp);
    final bTimestamp = DateTime.parse(b.timestamp);
    if (aTimestamp.isBefore(bTimestamp)) return -1;
    if (aTimestamp.isAfter(bTimestamp)) return 1;
    return 0;
  });
}