chore: improve comparator.
Signed-off-by: xsahil03x <[email protected]>
This commit is contained in:
@@ -268,38 +268,14 @@ class StreamChatPersistenceClient extends ChatPersistenceClient {
|
|||||||
|
|
||||||
// Only sort the channel states if the channels are not already sorted.
|
// Only sort the channel states if the channels are not already sorted.
|
||||||
if (sort == null) {
|
if (sort == null) {
|
||||||
var chainedComparator = (ChannelState a, ChannelState b) {
|
var comparator = _defaultChannelStateComparator;
|
||||||
final dateA = a.channel?.lastMessageAt ?? a.channel?.createdAt;
|
|
||||||
final dateB = b.channel?.lastMessageAt ?? b.channel?.createdAt;
|
|
||||||
|
|
||||||
if (dateA == null && dateB == null) {
|
|
||||||
return 0;
|
|
||||||
} else if (dateA == null) {
|
|
||||||
return 1;
|
|
||||||
} else if (dateB == null) {
|
|
||||||
return -1;
|
|
||||||
} else {
|
|
||||||
return dateB.compareTo(dateA);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if (channelStateSort != null && channelStateSort.isNotEmpty) {
|
if (channelStateSort != null && channelStateSort.isNotEmpty) {
|
||||||
chainedComparator = (a, b) {
|
comparator = _combineComparators(
|
||||||
int result;
|
channelStateSort.map((it) => it.comparator).withNullifyer,
|
||||||
for (final comparator
|
);
|
||||||
in channelStateSort.map((it) => it.comparator).withNullifyer) {
|
|
||||||
try {
|
|
||||||
result = comparator(a, b);
|
|
||||||
} catch (e) {
|
|
||||||
result = 0;
|
|
||||||
}
|
|
||||||
if (result != 0) return result;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
channelStates.sort(chainedComparator);
|
channelStates.sort(comparator);
|
||||||
}
|
}
|
||||||
|
|
||||||
final offset = paginationParams?.offset;
|
final offset = paginationParams?.offset;
|
||||||
@@ -439,3 +415,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