fix(llc, ui): message search pagination

Signed-off-by: xsahil03x <[email protected]>
This commit is contained in:
Sahil Kumar
2021-09-06 19:35:04 +05:30
committed by xsahil03x
parent 8585456edd
commit 4ddb0703b3
11 changed files with 127 additions and 29 deletions
@@ -43,6 +43,9 @@ class MessageSearchBlocState extends State<MessageSearchBloc>
with AutomaticKeepAliveClientMixin {
late StreamChatCoreState _streamChatCoreState;
String? nextId;
String? previousId;
/// The current messages list
List<GetMessageResponse>? get messageResponses =>
_messageResponses.valueOrNull;
@@ -76,11 +79,17 @@ class MessageSearchBlocState extends State<MessageSearchBloc>
_queryMessagesLoadingController.add(true);
}
try {
final clear = pagination == null || pagination.offset == 0;
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 messages = await client.search(
final response = await client.search(
filter,
sort: sort,
query: query,
@@ -88,10 +97,20 @@ class MessageSearchBlocState extends State<MessageSearchBloc>
messageFilters: messageFilter,
);
final next = response.next;
final previous = response.previous;
nextId = next != null && next.isNotEmpty
? next
: /*reset nextId if we get nothing*/ null;
previousId = previous != null && previous.isNotEmpty
? previous
: /*reset previousId if we get nothing*/ null;
if (clear) {
_messageResponses.add(messages.results);
_messageResponses.add(response.results);
} else {
final temp = oldMessages + messages.results;
final temp = oldMessages + response.results;
_messageResponses.add(temp);
}
if (_messageResponses.hasValue && _queryMessagesLoadingController.value) {
@@ -37,7 +37,7 @@ class MessageSearchListCore extends StatefulWidget {
/// * [errorBuilder]
/// * [loadingBuilder]
/// * [childBuilder]
const MessageSearchListCore({
MessageSearchListCore({
Key? key,
required this.emptyBuilder,
required this.errorBuilder,
@@ -49,7 +49,21 @@ class MessageSearchListCore extends StatefulWidget {
this.paginationParams,
this.messageFilters,
this.messageSearchListController,
}) : super(key: key);
}) : assert(
messageQuery != null || messageFilters != null,
'Provide at least `query` or `messageFilters`',
),
assert(
messageQuery == null || messageFilters == null,
"Can't provide both `query` and `messageFilters` at the same time",
),
assert(
paginationParams?.offset == null ||
paginationParams?.offset == 0 ||
sortOptions == null,
'Cannot specify `offset` with `sortOptions` parameter',
),
super(key: key);
/// A [MessageSearchListController] allows reloading and pagination.
/// Use [MessageSearchListController.loadData] and
@@ -73,9 +87,8 @@ class MessageSearchListCore extends StatefulWidget {
final List<SortOption>? sortOptions;
/// Pagination parameters
/// limit: the number of users to return (max is 30)
/// limit: the number of messages to return (max is 30)
/// offset: the offset (max is 1000)
/// message_limit: how many messages should be included to each channel
final PaginationParams? paginationParams;
/// The message query filters to use.
@@ -159,15 +172,25 @@ class MessageSearchListCoreState extends State<MessageSearchListCore> {
);
/// Fetches more messages with updated pagination and updates the widget
Future<void> paginateData() => _messageSearchBloc!.search(
filter: widget.filters,
sort: widget.sortOptions,
pagination: widget.paginationParams!.copyWith(
offset: _messageSearchBloc!.messageResponses?.length ?? 0,
),
query: widget.messageQuery,
messageFilter: widget.messageFilters,
Future<void> paginateData() {
PaginationParams? pagination;
if (widget.sortOptions != null) {
pagination = widget.paginationParams?.copyWith(
next: _messageSearchBloc?.nextId,
);
} else {
pagination = widget.paginationParams?.copyWith(
offset: _messageSearchBloc?.messageResponses?.length,
);
}
return _messageSearchBloc!.search(
filter: widget.filters,
sort: widget.sortOptions,
pagination: pagination,
query: widget.messageQuery,
messageFilter: widget.messageFilters,
);
}
@override
void didUpdateWidget(MessageSearchListCore oldWidget) {