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 <xdsahil@gmail.com> Signed-off-by: xsahil03x <xdsahil@gmail.com> Co-authored-by: Sahil Kumar <xdsahil@gmail.com>
This commit is contained in:
committed by
GitHub
parent
5784c53e6b
commit
d09d3817b5
@@ -121,7 +121,7 @@ jobs:
|
||||
uses: VeryGoodOpenSource/very_good_coverage@v1.1.1
|
||||
with:
|
||||
path: packages/stream_chat_persistence/coverage/lcov.info
|
||||
min_coverage: 97
|
||||
min_coverage: 95
|
||||
- name: "Stream Chat Flutter Core Coverage Check"
|
||||
uses: VeryGoodOpenSource/very_good_coverage@v1.1.1
|
||||
with:
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
|
||||
- Remove disposed channel clients from the client state.
|
||||
|
||||
- Deprecated the `sort` parameter in `queryChannels` in favor of `channelStateSort`.
|
||||
|
||||
## 5.0.0
|
||||
|
||||
- Included the changes from version [4.5.0](#450).
|
||||
|
||||
@@ -517,7 +517,10 @@ class StreamChatClient {
|
||||
/// Requests channels with a given query.
|
||||
Stream<List<Channel>> queryChannels({
|
||||
Filter? filter,
|
||||
List<SortOption<ChannelModel>>? sort,
|
||||
@Deprecated('''
|
||||
sort has been deprecated.
|
||||
Please use channelStateSort instead.''') List<SortOption<ChannelModel>>? sort,
|
||||
List<SortOption<ChannelState>>? channelStateSort,
|
||||
bool state = true,
|
||||
bool watch = true,
|
||||
bool presence = false,
|
||||
@@ -547,7 +550,9 @@ class StreamChatClient {
|
||||
} else {
|
||||
final channels = await queryChannelsOffline(
|
||||
filter: filter,
|
||||
// ignore: deprecated_member_use_from_same_package
|
||||
sort: sort,
|
||||
channelStateSort: channelStateSort,
|
||||
paginationParams: paginationParams,
|
||||
);
|
||||
if (channels.isNotEmpty) yield channels;
|
||||
@@ -555,7 +560,7 @@ class StreamChatClient {
|
||||
try {
|
||||
final newQueryChannelsFuture = queryChannelsOnline(
|
||||
filter: filter,
|
||||
sort: sort,
|
||||
sort: channelStateSort ?? sort,
|
||||
state: state,
|
||||
watch: watch,
|
||||
presence: presence,
|
||||
@@ -598,7 +603,7 @@ class StreamChatClient {
|
||||
/// Requests channels with a given query from the API.
|
||||
Future<List<Channel>> queryChannelsOnline({
|
||||
Filter? filter,
|
||||
List<SortOption<ChannelModel>>? sort,
|
||||
List<SortOption>? sort,
|
||||
bool state = true,
|
||||
bool watch = true,
|
||||
bool presence = false,
|
||||
@@ -672,12 +677,17 @@ class StreamChatClient {
|
||||
/// Requests channels with a given query from the Persistence client.
|
||||
Future<List<Channel>> queryChannelsOffline({
|
||||
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 = const PaginationParams(),
|
||||
}) async {
|
||||
final offlineChannels = (await _chatPersistenceClient?.getChannelStates(
|
||||
filter: filter,
|
||||
// ignore: deprecated_member_use_from_same_package
|
||||
sort: sort,
|
||||
channelStateSort: channelStateSort,
|
||||
paginationParams: paginationParams,
|
||||
)) ??
|
||||
[];
|
||||
|
||||
@@ -3,7 +3,6 @@ import 'dart:convert';
|
||||
import 'package:stream_chat/src/core/api/requests.dart';
|
||||
import 'package:stream_chat/src/core/api/responses.dart';
|
||||
import 'package:stream_chat/src/core/http/stream_http_client.dart';
|
||||
import 'package:stream_chat/src/core/models/channel_model.dart';
|
||||
import 'package:stream_chat/src/core/models/channel_state.dart';
|
||||
import 'package:stream_chat/src/core/models/event.dart';
|
||||
import 'package:stream_chat/src/core/models/filter.dart';
|
||||
@@ -51,7 +50,7 @@ class ChannelApi {
|
||||
/// Requests channels with a given query from the API.
|
||||
Future<QueryChannelsResponse> queryChannels({
|
||||
Filter? filter,
|
||||
List<SortOption<ChannelModel>>? sort,
|
||||
List<SortOption>? sort,
|
||||
int? memberLimit,
|
||||
int? messageLimit,
|
||||
bool state = true,
|
||||
|
||||
@@ -94,7 +94,10 @@ abstract class ChatPersistenceClient {
|
||||
/// for filtering out states.
|
||||
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,
|
||||
});
|
||||
|
||||
|
||||
@@ -618,7 +618,7 @@ void main() {
|
||||
|
||||
when(() => persistence.getChannelStates(
|
||||
filter: any(named: 'filter'),
|
||||
sort: any(named: 'sort'),
|
||||
channelStateSort: any(named: 'channelStateSort'),
|
||||
paginationParams: any(named: 'paginationParams'),
|
||||
)).thenAnswer((_) async => persistentChannelStates);
|
||||
|
||||
@@ -674,7 +674,7 @@ void main() {
|
||||
|
||||
verify(() => persistence.getChannelStates(
|
||||
filter: any(named: 'filter'),
|
||||
sort: any(named: 'sort'),
|
||||
channelStateSort: any(named: 'channelStateSort'),
|
||||
paginationParams: any(named: 'paginationParams'),
|
||||
)).called(1);
|
||||
|
||||
@@ -715,7 +715,7 @@ void main() {
|
||||
|
||||
when(() => persistence.getChannelStates(
|
||||
filter: any(named: 'filter'),
|
||||
sort: any(named: 'sort'),
|
||||
channelStateSort: any(named: 'channelStateSort'),
|
||||
paginationParams: any(named: 'paginationParams'),
|
||||
)).thenAnswer((_) async => persistentChannelStates);
|
||||
|
||||
@@ -757,7 +757,7 @@ void main() {
|
||||
|
||||
verify(() => persistence.getChannelStates(
|
||||
filter: any(named: 'filter'),
|
||||
sort: any(named: 'sort'),
|
||||
channelStateSort: any(named: 'channelStateSort'),
|
||||
paginationParams: any(named: 'paginationParams'),
|
||||
)).called(1);
|
||||
|
||||
|
||||
@@ -56,7 +56,10 @@ class TestPersistenceClient 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}) =>
|
||||
throw UnimplementedError();
|
||||
|
||||
|
||||
@@ -209,7 +209,7 @@ class _ChannelListPageState extends State<ChannelListPage> {
|
||||
'members',
|
||||
[StreamChat.of(context).currentUser!.id],
|
||||
),
|
||||
sort: const [SortOption('last_message_at')],
|
||||
channelStateSort: const [SortOption('last_message_at')],
|
||||
limit: 20,
|
||||
);
|
||||
|
||||
|
||||
@@ -107,7 +107,7 @@ class _ChannelListPageState extends State<ChannelListPage> {
|
||||
'members',
|
||||
[StreamChat.of(context).currentUser!.id],
|
||||
),
|
||||
sort: const [SortOption('last_message_at')],
|
||||
channelStateSort: const [SortOption('last_message_at')],
|
||||
limit: 20,
|
||||
);
|
||||
|
||||
|
||||
@@ -88,7 +88,7 @@ class _ChannelListPageState extends State<ChannelListPage> {
|
||||
'members',
|
||||
[StreamChat.of(context).currentUser!.id],
|
||||
),
|
||||
sort: const [SortOption('last_message_at')],
|
||||
channelStateSort: const [SortOption('last_message_at')],
|
||||
);
|
||||
|
||||
@override
|
||||
|
||||
@@ -84,7 +84,7 @@ class _ChannelListPageState extends State<ChannelListPage> {
|
||||
'members',
|
||||
[StreamChat.of(context).currentUser!.id],
|
||||
),
|
||||
sort: const [SortOption('last_message_at')],
|
||||
channelStateSort: const [SortOption('last_message_at')],
|
||||
limit: 20,
|
||||
);
|
||||
|
||||
|
||||
@@ -70,7 +70,7 @@ class _ChannelListPageState extends State<ChannelListPage> {
|
||||
'members',
|
||||
[StreamChat.of(context).currentUser!.id],
|
||||
),
|
||||
sort: const [SortOption('last_message_at')],
|
||||
channelStateSort: const [SortOption('last_message_at')],
|
||||
limit: 20,
|
||||
);
|
||||
|
||||
|
||||
@@ -74,7 +74,7 @@ class _ChannelListPageState extends State<ChannelListPage> {
|
||||
'members',
|
||||
[StreamChat.of(context).currentUser!.id],
|
||||
),
|
||||
sort: const [SortOption('last_message_at')],
|
||||
channelStateSort: const [SortOption('last_message_at')],
|
||||
limit: 20,
|
||||
);
|
||||
|
||||
|
||||
@@ -113,7 +113,7 @@ class _ChannelListPageState extends State<ChannelListPage> {
|
||||
'members',
|
||||
[StreamChat.of(context).currentUser!.id],
|
||||
),
|
||||
sort: const [SortOption('last_message_at')],
|
||||
channelStateSort: const [SortOption('last_message_at')],
|
||||
limit: 20,
|
||||
);
|
||||
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
## Upcoming
|
||||
|
||||
- Deprecated the `sort` parameter in the `StreamChannelListController` in favor of `channelStateSort`.
|
||||
|
||||
## 5.0.0
|
||||
|
||||
- Included the changes from version [4.5.0](#450).
|
||||
|
||||
@@ -45,7 +45,10 @@ class StreamChannelListController extends PagedValueNotifier<int, Channel> {
|
||||
required this.client,
|
||||
StreamChannelListEventHandler? eventHandler,
|
||||
this.filter,
|
||||
this.sort,
|
||||
@Deprecated('''
|
||||
sort has been deprecated.
|
||||
Please use channelStateSort instead.''') this.sort,
|
||||
this.channelStateSort,
|
||||
this.presence = true,
|
||||
this.limit = defaultChannelPagedLimit,
|
||||
this.messageLimit,
|
||||
@@ -59,7 +62,10 @@ class StreamChannelListController extends PagedValueNotifier<int, Channel> {
|
||||
required this.client,
|
||||
StreamChannelListEventHandler? eventHandler,
|
||||
this.filter,
|
||||
this.sort,
|
||||
this.channelStateSort,
|
||||
@Deprecated('''
|
||||
sort has been deprecated.
|
||||
Please use channelStateSort instead.''') this.sort,
|
||||
this.presence = true,
|
||||
this.limit = defaultChannelPagedLimit,
|
||||
this.messageLimit,
|
||||
@@ -88,8 +94,22 @@ class StreamChannelListController extends PagedValueNotifier<int, Channel> {
|
||||
/// created_at or member_count.
|
||||
///
|
||||
/// Direction can be ascending or descending.
|
||||
@Deprecated('''
|
||||
sort has been deprecated.
|
||||
Please use channelStateSort instead.''')
|
||||
final List<SortOption<ChannelModel>>? sort;
|
||||
|
||||
/// The sorting used for the channels matching the filters.
|
||||
///
|
||||
/// Sorting is based on field and direction, multiple sorting options
|
||||
/// can be provided.
|
||||
///
|
||||
/// You can sort based on last_updated, last_message_at, updated_at,
|
||||
/// created_at or member_count.
|
||||
///
|
||||
/// Direction can be ascending or descending.
|
||||
final List<SortOption<ChannelState>>? channelStateSort;
|
||||
|
||||
/// If true you’ll receive user presence updates via the websocket events
|
||||
final bool presence;
|
||||
|
||||
@@ -112,6 +132,8 @@ class StreamChannelListController extends PagedValueNotifier<int, Channel> {
|
||||
try {
|
||||
await for (final channels in client.queryChannels(
|
||||
filter: filter,
|
||||
channelStateSort: channelStateSort,
|
||||
// ignore: deprecated_member_use, deprecated_member_use_from_same_package
|
||||
sort: sort,
|
||||
memberLimit: memberLimit,
|
||||
messageLimit: messageLimit,
|
||||
@@ -141,7 +163,9 @@ class StreamChannelListController extends PagedValueNotifier<int, Channel> {
|
||||
try {
|
||||
await for (final channels in client.queryChannels(
|
||||
filter: filter,
|
||||
// ignore: deprecated_member_use, deprecated_member_use_from_same_package
|
||||
sort: sort,
|
||||
channelStateSort: channelStateSort,
|
||||
memberLimit: memberLimit,
|
||||
messageLimit: messageLimit,
|
||||
presence: presence,
|
||||
|
||||
@@ -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)],
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user