Skip to content

Commit

Permalink
chore: some fixes.
Browse files Browse the repository at this point in the history
Signed-off-by: xsahil03x <[email protected]>
  • Loading branch information
xsahil03x committed Jul 13, 2023
1 parent d23e621 commit 3df5db9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,7 @@ class BottomRow extends StatelessWidget {
msg = context.translations.threadReplyCountText(replyCount);
}

// ignore: prefer_function_declarations_over_variables
final _onThreadTap = () async {
Future<void> _onThreadTap() async {
try {
var message = this.message;
if (showInChannel) {
Expand All @@ -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');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -400,3 +421,35 @@ class StreamChatPersistenceClient extends ChatPersistenceClient {
}
}
}

// Creates a new combined [Comparator] which sorts items
// by the given [comparators].
Comparator<T> _combineComparators<T>(Iterable<Comparator<T>> 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);
}
}

0 comments on commit 3df5db9

Please sign in to comment.