feat: add minimum delay for fetching new and old messages that can be configured

This commit is contained in:
Freek van de Ven 2025-02-17 09:40:01 +01:00 committed by Bart Ribbers
parent b9d6e21863
commit 1dc1461a22
2 changed files with 24 additions and 2 deletions

View file

@ -243,6 +243,8 @@ class ChatPaginationControls {
this.scrollOffset = 50.0,
this.loadingIndicatorForNewMessages = true,
this.loadingIndicatorForOldMessages = true,
this.loadingNewMessageMinDuration = Duration.zero,
this.loadingOldMessageMinDuration = Duration.zero,
});
/// The minimum scroll offset to trigger the pagination to call for more pages
@ -254,4 +256,14 @@ class ChatPaginationControls {
/// Whether to show a loading indicator for old messages loading
final bool loadingIndicatorForOldMessages;
/// The minimum duration for the loading indicator for new messages
/// to be shown. The loading indicator will wait for this duration and the
/// completion of [ChatService.loadNewMessagesAfter]
final Duration loadingNewMessageMinDuration;
/// The minimum duration for the loading indicator for old messages
/// to be shown. The loading indicator will wait for this duration and the
/// completion of [ChatService.loadOldMessagesBefore]
final Duration loadingOldMessageMinDuration;
}

View file

@ -260,7 +260,12 @@ class _ChatBody extends HookWidget {
try {
debugPrint("loading old messages from message: ${oldestMsg.id}");
await service.loadOldMessagesBefore(firstMessage: oldestMsg);
await Future.wait([
service.loadOldMessagesBefore(firstMessage: oldestMsg),
Future.delayed(
options.paginationControls.loadingOldMessageMinDuration,
),
]);
} finally {
WidgetsBinding.instance.addPostFrameCallback((_) {
if (!scrollController.hasClients) {
@ -285,7 +290,12 @@ class _ChatBody extends HookWidget {
var newestMsg = messages.last;
try {
debugPrint("loading new messages from message: ${newestMsg.id}");
await service.loadNewMessagesAfter(lastMessage: newestMsg);
await Future.wait([
service.loadNewMessagesAfter(lastMessage: newestMsg),
Future.delayed(
options.paginationControls.loadingNewMessageMinDuration,
),
]);
} finally {
isLoadingNewer.value = false;
}