mirror of
https://github.com/Iconica-Development/flutter_chat.git
synced 2025-05-19 02:43:50 +02:00
feat: add minimum delay for fetching new and old messages that can be configured
This commit is contained in:
parent
b9d6e21863
commit
1dc1461a22
2 changed files with 24 additions and 2 deletions
|
@ -243,6 +243,8 @@ class ChatPaginationControls {
|
||||||
this.scrollOffset = 50.0,
|
this.scrollOffset = 50.0,
|
||||||
this.loadingIndicatorForNewMessages = true,
|
this.loadingIndicatorForNewMessages = true,
|
||||||
this.loadingIndicatorForOldMessages = 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
|
/// 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
|
/// Whether to show a loading indicator for old messages loading
|
||||||
final bool loadingIndicatorForOldMessages;
|
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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -260,7 +260,12 @@ class _ChatBody extends HookWidget {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
debugPrint("loading old messages from message: ${oldestMsg.id}");
|
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 {
|
} finally {
|
||||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
if (!scrollController.hasClients) {
|
if (!scrollController.hasClients) {
|
||||||
|
@ -285,7 +290,12 @@ class _ChatBody extends HookWidget {
|
||||||
var newestMsg = messages.last;
|
var newestMsg = messages.last;
|
||||||
try {
|
try {
|
||||||
debugPrint("loading new messages from message: ${newestMsg.id}");
|
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 {
|
} finally {
|
||||||
isLoadingNewer.value = false;
|
isLoadingNewer.value = false;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue