From 2a22e8d7df6b6ab28dd16c0c99d6c0e534222f4b Mon Sep 17 00:00:00 2001 From: Freek van de Ven Date: Mon, 17 Feb 2025 09:40:01 +0100 Subject: [PATCH] feat: add minimum delay for fetching new and old messages that can be configured --- .../flutter_chat/lib/src/config/chat_options.dart | 12 ++++++++++++ .../screens/chat_detail/chat_detail_screen.dart | 14 ++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/packages/flutter_chat/lib/src/config/chat_options.dart b/packages/flutter_chat/lib/src/config/chat_options.dart index 8de71a9..e34ecd3 100644 --- a/packages/flutter_chat/lib/src/config/chat_options.dart +++ b/packages/flutter_chat/lib/src/config/chat_options.dart @@ -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; } diff --git a/packages/flutter_chat/lib/src/screens/chat_detail/chat_detail_screen.dart b/packages/flutter_chat/lib/src/screens/chat_detail/chat_detail_screen.dart index a6ec95e..8313e4b 100644 --- a/packages/flutter_chat/lib/src/screens/chat_detail/chat_detail_screen.dart +++ b/packages/flutter_chat/lib/src/screens/chat_detail/chat_detail_screen.dart @@ -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; }