Merge remote-tracking branch 'origin/fix/message-search-pagination' into fix/message-search-pagination
This commit is contained in:
@@ -65,6 +65,8 @@ class MessageSearchBlocState extends State<MessageSearchBloc>
|
||||
Stream<bool> get queryMessagesLoading =>
|
||||
_queryMessagesLoadingController.stream;
|
||||
|
||||
bool _paginationEnded = false;
|
||||
|
||||
/// Calls [StreamChatClient.search] updating
|
||||
/// [messagesStream] and [queryMessagesLoading] stream
|
||||
Future<void> search({
|
||||
@@ -72,24 +74,31 @@ class MessageSearchBlocState extends State<MessageSearchBloc>
|
||||
Filter? messageFilter,
|
||||
List<SortOption>? sort,
|
||||
String? query,
|
||||
PaginationParams? pagination,
|
||||
PaginationParams pagination = const PaginationParams(limit: 30),
|
||||
}) async {
|
||||
final client = _streamChatCoreState.client;
|
||||
|
||||
if (_queryMessagesLoadingController.value == true) return;
|
||||
var clear = false;
|
||||
if (sort != null) {
|
||||
clear |= pagination.next == null;
|
||||
} else {
|
||||
final offset = pagination.offset;
|
||||
clear |= offset == null || offset == 0;
|
||||
}
|
||||
|
||||
if (clear && _paginationEnded) {
|
||||
_paginationEnded = false;
|
||||
}
|
||||
|
||||
if ((!clear && _paginationEnded) ||
|
||||
_queryMessagesLoadingController.value == true) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (_messageResponses.hasValue) {
|
||||
_queryMessagesLoadingController.add(true);
|
||||
}
|
||||
try {
|
||||
var clear = pagination == null;
|
||||
if (sort != null) {
|
||||
clear |= pagination?.next == null;
|
||||
} else {
|
||||
final offset = pagination?.offset;
|
||||
clear |= offset == null || offset == 0;
|
||||
}
|
||||
|
||||
final oldMessages = List<GetMessageResponse>.from(messageResponses ?? []);
|
||||
|
||||
final response = await client.search(
|
||||
@@ -110,15 +119,19 @@ class MessageSearchBlocState extends State<MessageSearchBloc>
|
||||
? previous
|
||||
: /*reset previousId if we get nothing*/ null;
|
||||
|
||||
final newMessages = response.results;
|
||||
if (clear) {
|
||||
_messageResponses.add(response.results);
|
||||
_messageResponses.add(newMessages);
|
||||
} else {
|
||||
final temp = oldMessages + response.results;
|
||||
final temp = oldMessages + newMessages;
|
||||
_messageResponses.add(temp);
|
||||
}
|
||||
if (_messageResponses.hasValue && _queryMessagesLoadingController.value) {
|
||||
_queryMessagesLoadingController.add(false);
|
||||
}
|
||||
if (newMessages.isEmpty || newMessages.length < pagination.limit) {
|
||||
_paginationEnded = true;
|
||||
}
|
||||
} catch (e, stk) {
|
||||
// reset loading controller
|
||||
_queryMessagesLoadingController.add(false);
|
||||
|
||||
@@ -47,7 +47,7 @@ class MessageSearchListCore extends StatefulWidget {
|
||||
required this.filters,
|
||||
this.messageQuery,
|
||||
this.sortOptions,
|
||||
this.paginationParams,
|
||||
this.paginationParams = const PaginationParams(limit: 30),
|
||||
this.messageFilters,
|
||||
this.messageSearchListController,
|
||||
}) : assert(
|
||||
@@ -84,7 +84,7 @@ class MessageSearchListCore extends StatefulWidget {
|
||||
/// Pagination parameters
|
||||
/// limit: the number of messages to return (max is 30)
|
||||
/// offset: the offset (max is 1000)
|
||||
final PaginationParams? paginationParams;
|
||||
final PaginationParams paginationParams;
|
||||
|
||||
/// The message query filters to use.
|
||||
/// You can query on any of the custom fields you've defined on the [Channel].
|
||||
@@ -163,13 +163,13 @@ class MessageSearchListCoreState extends State<MessageSearchListCore> {
|
||||
|
||||
/// Fetches more messages with updated pagination and updates the widget
|
||||
Future<void> paginateData() {
|
||||
PaginationParams? pagination;
|
||||
PaginationParams pagination;
|
||||
if (widget.sortOptions != null) {
|
||||
pagination = widget.paginationParams?.copyWith(
|
||||
pagination = widget.paginationParams.copyWith(
|
||||
next: _messageSearchBloc?.nextId,
|
||||
);
|
||||
} else {
|
||||
pagination = widget.paginationParams?.copyWith(
|
||||
pagination = widget.paginationParams.copyWith(
|
||||
offset: _messageSearchBloc?.messageResponses?.length,
|
||||
);
|
||||
}
|
||||
@@ -190,8 +190,8 @@ class MessageSearchListCoreState extends State<MessageSearchListCore> {
|
||||
widget.messageQuery?.toString() != oldWidget.messageQuery?.toString() ||
|
||||
widget.messageFilters?.toString() !=
|
||||
oldWidget.messageFilters?.toString() ||
|
||||
widget.paginationParams?.toJson().toString() !=
|
||||
oldWidget.paginationParams?.toJson().toString()) {
|
||||
widget.paginationParams.toJson().toString() !=
|
||||
oldWidget.paginationParams.toJson().toString()) {
|
||||
loadData();
|
||||
}
|
||||
|
||||
|
||||
@@ -66,7 +66,7 @@ class UserListCore extends StatefulWidget {
|
||||
this.filter,
|
||||
this.sort,
|
||||
this.presence,
|
||||
this.pagination,
|
||||
this.pagination = const PaginationParams(limit: 30),
|
||||
this.groupAlphabetically = false,
|
||||
this.userListController,
|
||||
}) : super(key: key);
|
||||
@@ -106,7 +106,7 @@ class UserListCore extends StatefulWidget {
|
||||
/// limit: the number of users to return (max is 30)
|
||||
/// offset: the offset (max is 1000)
|
||||
/// message_limit: how many messages should be included to each channel
|
||||
final PaginationParams? pagination;
|
||||
final PaginationParams pagination;
|
||||
|
||||
/// Set it to true to group users by their first character
|
||||
///
|
||||
@@ -201,7 +201,7 @@ class UserListCoreState extends State<UserListCore>
|
||||
filter: widget.filter,
|
||||
sort: widget.sort,
|
||||
presence: widget.presence,
|
||||
pagination: widget.pagination!.copyWith(
|
||||
pagination: widget.pagination.copyWith(
|
||||
offset: _usersBloc!.users?.length ?? 0,
|
||||
),
|
||||
);
|
||||
@@ -212,8 +212,8 @@ class UserListCoreState extends State<UserListCore>
|
||||
if (widget.filter?.toString() != oldWidget.filter?.toString() ||
|
||||
jsonEncode(widget.sort) != jsonEncode(oldWidget.sort) ||
|
||||
widget.presence != oldWidget.presence ||
|
||||
widget.pagination?.toJson().toString() !=
|
||||
oldWidget.pagination?.toJson().toString()) {
|
||||
widget.pagination.toJson().toString() !=
|
||||
oldWidget.pagination.toJson().toString()) {
|
||||
loadData();
|
||||
}
|
||||
|
||||
|
||||
@@ -57,6 +57,8 @@ class UsersBlocState extends State<UsersBloc>
|
||||
|
||||
late StreamChatCoreState _streamChatCore;
|
||||
|
||||
bool _paginationEnded = false;
|
||||
|
||||
/// The Query Users method allows you to search for users and see if they are
|
||||
/// online/offline.
|
||||
/// [API Reference](https://getstream.io/chat/docs/flutter-dart/query_users/?language=dart)
|
||||
@@ -64,21 +66,27 @@ class UsersBlocState extends State<UsersBloc>
|
||||
Filter? filter,
|
||||
List<SortOption>? sort,
|
||||
bool? presence,
|
||||
PaginationParams? pagination,
|
||||
PaginationParams pagination = const PaginationParams(limit: 30),
|
||||
}) async {
|
||||
final client = _streamChatCore.client;
|
||||
|
||||
if (_queryUsersLoadingController.value == true) return;
|
||||
final offset = pagination.offset;
|
||||
final clear = offset == null || offset == 0;
|
||||
|
||||
if (clear && _paginationEnded) {
|
||||
_paginationEnded = false;
|
||||
}
|
||||
|
||||
if ((!clear && _paginationEnded) ||
|
||||
_queryUsersLoadingController.value == true) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (_usersController.hasValue) {
|
||||
_queryUsersLoadingController.add(true);
|
||||
}
|
||||
|
||||
try {
|
||||
final clear = pagination == null ||
|
||||
pagination.offset == null ||
|
||||
pagination.offset == 0;
|
||||
|
||||
final oldUsers = List<User>.from(users ?? []);
|
||||
|
||||
final usersResponse = await client.queryUsers(
|
||||
@@ -88,6 +96,7 @@ class UsersBlocState extends State<UsersBloc>
|
||||
pagination: pagination,
|
||||
);
|
||||
|
||||
final newUsers = usersResponse.users;
|
||||
if (clear) {
|
||||
_usersController.add(usersResponse.users);
|
||||
} else {
|
||||
@@ -97,6 +106,9 @@ class UsersBlocState extends State<UsersBloc>
|
||||
if (_usersController.hasValue && _queryUsersLoadingController.value) {
|
||||
_queryUsersLoadingController.add(false);
|
||||
}
|
||||
if (newUsers.isEmpty || newUsers.length < pagination.limit) {
|
||||
_paginationEnded = true;
|
||||
}
|
||||
} catch (e, stk) {
|
||||
// reset loading controller
|
||||
_queryUsersLoadingController.add(false);
|
||||
|
||||
Reference in New Issue
Block a user