From 3df5db9fc227458c7af5eb18612010b456e2634f Mon Sep 17 00:00:00 2001 From: Sahil Kumar Date: Thu, 13 Jul 2023 14:45:06 +0530 Subject: [PATCH] chore: some fixes. Signed-off-by: xsahil03x --- .../lib/src/message_widget/bottom_row.dart | 10 ++-- .../src/stream_chat_persistence_client.dart | 53 +++++++++++++++++++ 2 files changed, 56 insertions(+), 7 deletions(-) diff --git a/packages/stream_chat_flutter/lib/src/message_widget/bottom_row.dart b/packages/stream_chat_flutter/lib/src/message_widget/bottom_row.dart index 7092dadf5..83a40a4d5 100644 --- a/packages/stream_chat_flutter/lib/src/message_widget/bottom_row.dart +++ b/packages/stream_chat_flutter/lib/src/message_widget/bottom_row.dart @@ -162,8 +162,7 @@ class BottomRow extends StatelessWidget { msg = context.translations.threadReplyCountText(replyCount); } - // ignore: prefer_function_declarations_over_variables - final _onThreadTap = () async { + Future _onThreadTap() async { try { var message = this.message; if (showInChannel) { @@ -172,12 +171,9 @@ class BottomRow extends StatelessWidget { } return onThreadTap!(message); } catch (e, stk) { - print(e); - print(stk); - // ignore: avoid_returning_null_for_void - return null; + debugPrint('Error while fetching message: $e, $stk'); } - }; + } const usernameKey = Key('username'); diff --git a/packages/stream_chat_persistence/lib/src/stream_chat_persistence_client.dart b/packages/stream_chat_persistence/lib/src/stream_chat_persistence_client.dart index fa748477f..d29819e47 100644 --- a/packages/stream_chat_persistence/lib/src/stream_chat_persistence_client.dart +++ b/packages/stream_chat_persistence/lib/src/stream_chat_persistence_client.dart @@ -255,6 +255,15 @@ class StreamChatPersistenceClient extends ChatPersistenceClient { PaginationParams? paginationParams, }) async { assert(_debugIsConnected, ''); + assert(() { + if (channelStateSort?.any((it) => it.comparator == null) ?? false) { + throw ArgumentError( + 'SortOption requires a comparator in order to sort', + ); + } + return true; + }(), ''); + _logger.info('getChannelStates'); final channels = await db!.channelQueryDao.getChannels(filter: filter); @@ -263,6 +272,18 @@ class StreamChatPersistenceClient extends ChatPersistenceClient { channels.map((e) => getChannelStateByCid(e.cid)), ); + // Only sort the channel states if the channels are not already sorted. + if (channelStateSort == null) { + var comparator = _defaultChannelStateComparator; + if (channelStateSort != null && channelStateSort.isNotEmpty) { + comparator = _combineComparators( + channelStateSort.map((it) => it.comparator).withNullifyer, + ); + } + + channelStates.sort(comparator); + } + final offset = paginationParams?.offset; if (offset != null && offset > 0 && channelStates.isNotEmpty) { channelStates.removeRange(0, offset); @@ -400,3 +421,35 @@ class StreamChatPersistenceClient extends ChatPersistenceClient { } } } + +// Creates a new combined [Comparator] which sorts items +// by the given [comparators]. +Comparator _combineComparators(Iterable> comparators) { + return (T a, T b) { + for (final comparator in comparators) { + try { + final result = comparator(a, b); + if (result != 0) return result; + } catch (e) { + // If the comparator throws an exception, we ignore it and + // continue with the next comparator. + continue; + } + } + return 0; + }; +} + +// The default [Comparator] used to sort [ChannelState]s. +int _defaultChannelStateComparator(ChannelState a, ChannelState b) { + final dateA = a.channel?.lastMessageAt ?? a.channel?.createdAt; + final dateB = b.channel?.lastMessageAt ?? b.channel?.createdAt; + + if (dateA == null && dateB == null) return 0; + if (dateA == null) return 1; + if (dateB == null) { + return -1; + } else { + return dateB.compareTo(dateA); + } +}