loadMore method

Future<void> loadMore()

Fetch next page using nextCursor and append to list.

Implementation

Future<void> loadMore() async {
  if (!_initialized || _state.isLoading || !_state.hasMore) return;

  _state = _state.copyWith(isLoading: true);
  notifyListeners();

  try {
    final result = await _client!.inboxFetchMessages(
      limit: 20,
      cursor: _state.nextCursor,
    );

    final newMessages = (result['messages'] as List<dynamic>)
        .map((m) => InboxMessage.fromMap(m as Map<String, dynamic>))
        .toList();
    final nextCursor = result['nextCursor'] as String?;

    _state = _state.copyWith(
      messages: [..._state.messages, ...newMessages],
      nextCursor: nextCursor,
      isLoading: false,
      hasMore: nextCursor != null,
    );
    notifyListeners();

    await _persistState();
  } catch (e) {
    _state = _state.copyWith(isLoading: false);
    notifyListeners();
    debugPrint('Error loading more inbox messages: $e');
  }
}