fix(persistence,llc,core,ui): deprecated sort, add channelStateSort s… (#1366)

* fix(persistence,llc,core,ui): deprecated sort, add channelStateSort sorting the channels offline using the state

* update changelogs

* update tests

* remove deprecated test

* change threashold

* minor fixes

* remove tests

* fix(persistence): minor changes.

Signed-off-by: xsahil03x <[email protected]>

Signed-off-by: xsahil03x <[email protected]>
Co-authored-by: Sahil Kumar <[email protected]>
This commit is contained in:
Salvatore Giordano
2022-10-25 17:07:02 +02:00
committed by GitHub
co-authored by Sahil Kumar
parent 5784c53e6b
commit d09d3817b5
19 changed files with 131 additions and 55 deletions
@@ -1,6 +1,8 @@
## Upcoming
- Reintroduce support for experimental indexedDB on Web.
- Deprecated the `sort` parameter in the getChannelStates method in favor of `channelStateSort`.
- Use the comparator function to sort the channel states and not the channel models.
🐞 Fixed
@@ -263,19 +263,76 @@ class StreamChatPersistenceClient extends ChatPersistenceClient {
@override
Future<List<ChannelState>> getChannelStates({
Filter? filter,
List<SortOption<ChannelModel>>? sort,
@Deprecated('''
sort has been deprecated.
Please use channelStateSort instead.''') List<SortOption<ChannelModel>>? sort,
List<SortOption<ChannelState>>? channelStateSort,
PaginationParams? paginationParams,
}) {
assert(_debugIsConnected, '');
assert(
sort == null || channelStateSort == null,
'sort and channelStateSort cannot be used together',
);
_logger.info('getChannelStates');
return _readProtected(
() async {
final channels = await db!.channelQueryDao.getChannels(
filter: filter,
sort: sort,
paginationParams: paginationParams,
);
return Future.wait(channels.map((e) => getChannelStateByCid(e.cid)));
final channelStates = await Future.wait(
channels.map((e) => getChannelStateByCid(e.cid)),
);
// Only sort the channel states if the channels are not already sorted.
if (sort == null) {
var chainedComparator = (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;
} else if (dateA == null) {
return 1;
} else if (dateB == null) {
return -1;
} else {
return dateB.compareTo(dateA);
}
};
if (channelStateSort != null && channelStateSort.isNotEmpty) {
chainedComparator = (a, b) {
int result;
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);
}
final offset = paginationParams?.offset;
if (offset != null && offset > 0 && channelStates.isNotEmpty) {
channelStates.removeRange(0, offset);
}
if (paginationParams?.limit != null) {
return channelStates.take(paginationParams!.limit).toList();
}
return channelStates;
},
);
}
@@ -137,27 +137,6 @@ void main() {
}
});
test(
'should return all the inserted channels along with pagination applied',
() async {
const offset = 5;
const limit = 15;
const pagination = PaginationParams(offset: offset, limit: limit);
// Inserting test data for get channels
await _insertTestDataForGetChannel(filter, count: 30);
// Should match with the inserted channels
final updatedChannels = await channelQueryDao.getChannels(
filter: filter,
paginationParams: pagination,
);
expect(updatedChannels.length, limit);
expect(updatedChannels.first.id, 'testId24');
expect(updatedChannels.first.cid, 'testCid24');
},
);
test('should return sorted channels using member count', () async {
int sortComparator(ChannelModel a, ChannelModel b) =>
b.memberCount.compareTo(a.memberCount);
@@ -169,6 +148,7 @@ void main() {
// Should match with the inserted channels
final updatedChannels = await channelQueryDao.getChannels(
filter: filter,
// ignore: deprecated_member_use_from_same_package
sort: [
SortOption(
'member_count',
@@ -202,15 +182,6 @@ void main() {
}
});
test('should throw if comparator is not provided in sort list', () {
expect(
() => channelQueryDao.getChannels(
sort: [const SortOption('test_custom_field')],
),
throwsArgumentError,
);
});
test('should return sorted channels using custom field', () async {
int sortComparator(ChannelModel a, ChannelModel b) {
final aData = int.parse(a.extraData['test_custom_field'].toString());
@@ -225,6 +196,7 @@ void main() {
// Should match with the inserted channels
final updatedChannels = await channelQueryDao.getChannels(
filter: filter,
// ignore: deprecated_member_use_from_same_package
sort: [SortOption('test_custom_field', comparator: sortComparator)],
);