refresh method
Fetch first page of messages + unread count in parallel.
Implementation
Future<void> refresh() async {
if (!_initialized) return;
_state = _state.copyWith(isLoading: true);
notifyListeners();
try {
final results = await Future.wait([
_client!.inboxFetchMessages(limit: 20),
_client!.inboxFetchUnreadCount(),
]);
final messagesResult = results[0] as Map<String, dynamic>;
final unreadCount = results[1] as int;
final messages = (messagesResult['messages'] as List<dynamic>)
.map((m) => InboxMessage.fromMap(m as Map<String, dynamic>))
.toList();
final nextCursor = messagesResult['nextCursor'] as String?;
_state = _state.copyWith(
messages: messages,
unreadCount: unreadCount,
nextCursor: nextCursor,
isLoading: false,
hasMore: nextCursor != null,
lastFetchTime: DateTime.now(),
);
notifyListeners();
await _persistState();
} catch (e) {
_state = _state.copyWith(isLoading: false);
notifyListeners();
debugPrint('Error refreshing inbox: $e');
}
}