loadMore method
Loads more messages using the provided callback.
Returns early if already loading or no more messages. The callback should return a list of messages to add.
Implementation
Future<void> loadMore(Future<List<ChatMessage>> Function()? loadCallback) async {
// if (_isLoadingMore || !_hasMoreMessages || !paginationConfig.enabled) {
// return;
// }
try {
_isLoadingMore = true;
notifyListeners();
// Simulate network delay if specified
if (paginationConfig.loadingDelay.inMilliseconds > 0) {
await Future<void>.delayed(paginationConfig.loadingDelay);
}
// Get more messages from the callback or use the backward compatibility one
final moreMessages = loadCallback != null
? await loadCallback()
: _onLoadMoreMessagesCallback != null
? await _onLoadMoreMessagesCallback!(_messages.isNotEmpty ? _messages.last : null)
: <ChatMessage>[];
if (moreMessages.isEmpty) {
_hasMoreMessages = false;
} else {
// Add the messages
addMessages(moreMessages);
_currentPage++;
}
} catch (e) {
_hasMoreMessages = true; // Allow retry on error
rethrow;
} finally {
_isLoadingMore = false;
notifyListeners();
}
}