@@ -162,8 +162,7 @@ class BottomRow extends StatelessWidget {
|
|||||||
msg = context.translations.threadReplyCountText(replyCount);
|
msg = context.translations.threadReplyCountText(replyCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ignore: prefer_function_declarations_over_variables
|
Future<void> _onThreadTap() async {
|
||||||
final _onThreadTap = () async {
|
|
||||||
try {
|
try {
|
||||||
var message = this.message;
|
var message = this.message;
|
||||||
if (showInChannel) {
|
if (showInChannel) {
|
||||||
@@ -172,12 +171,9 @@ class BottomRow extends StatelessWidget {
|
|||||||
}
|
}
|
||||||
return onThreadTap!(message);
|
return onThreadTap!(message);
|
||||||
} catch (e, stk) {
|
} catch (e, stk) {
|
||||||
print(e);
|
debugPrint('Error while fetching message: $e, $stk');
|
||||||
print(stk);
|
|
||||||
// ignore: avoid_returning_null_for_void
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
const usernameKey = Key('username');
|
const usernameKey = Key('username');
|
||||||
|
|
||||||
|
|||||||
@@ -255,6 +255,15 @@ class StreamChatPersistenceClient extends ChatPersistenceClient {
|
|||||||
PaginationParams? paginationParams,
|
PaginationParams? paginationParams,
|
||||||
}) async {
|
}) async {
|
||||||
assert(_debugIsConnected, '');
|
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');
|
_logger.info('getChannelStates');
|
||||||
|
|
||||||
final channels = await db!.channelQueryDao.getChannels(filter: filter);
|
final channels = await db!.channelQueryDao.getChannels(filter: filter);
|
||||||
@@ -263,6 +272,18 @@ class StreamChatPersistenceClient extends ChatPersistenceClient {
|
|||||||
channels.map((e) => getChannelStateByCid(e.cid)),
|
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;
|
final offset = paginationParams?.offset;
|
||||||
if (offset != null && offset > 0 && channelStates.isNotEmpty) {
|
if (offset != null && offset > 0 && channelStates.isNotEmpty) {
|
||||||
channelStates.removeRange(0, offset);
|
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<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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user