import 'package:flutter_riverpod/flutter_riverpod.dart'; import '../core/api/client.dart'; import '../core/api/endpoints.dart'; import '../core/api/exceptions.dart'; import '../models/collaboration.dart'; class NotificationState { final List notifications; final int unreadCount; final bool isLoading; final String? error; NotificationState({ this.notifications = const [], this.unreadCount = 0, this.isLoading = false, this.error, }); NotificationState copyWith({ List? notifications, int? unreadCount, bool? isLoading, String? error, }) => NotificationState( notifications: notifications ?? this.notifications, unreadCount: unreadCount ?? this.unreadCount, isLoading: isLoading ?? this.isLoading, error: error, ); } class NotificationNotifier extends StateNotifier { final ApiClient _api; NotificationNotifier(this._api) : super(NotificationState()); Future loadNotifications() async { state = state.copyWith(isLoading: true); try { final response = await _api.get(Endpoints.notifications); final list = (response.data as List) .map((e) => AppNotification.fromJson(e as Map)) .toList(); state = state.copyWith(notifications: list, isLoading: false); _updateUnreadCount(); } on ApiException catch (e) { state = state.copyWith(isLoading: false, error: e.message); } } Future loadUnreadCount() async { try { final response = await _api.get(Endpoints.notificationsUnreadCount); state = state.copyWith( unreadCount: response.data['count'] as int? ?? 0); } catch (_) {} } Future markAsRead(String id) async { try { await _api.patch(Endpoints.notification(id)); state = state.copyWith( notifications: state.notifications.map((n) { if (n.id == id) { return AppNotification( id: n.id, userId: n.userId, title: n.title, body: n.body, isRead: true, ideaId: n.ideaId, createdAt: n.createdAt, ); } return n; }).toList(), ); _updateUnreadCount(); } catch (_) {} } Future markAllAsRead() async { try { await _api.post(Endpoints.notificationsReadAll); state = state.copyWith( notifications: state.notifications .map((n) => AppNotification( id: n.id, userId: n.userId, title: n.title, body: n.body, isRead: true, ideaId: n.ideaId, createdAt: n.createdAt, )) .toList(), unreadCount: 0, ); } catch (_) {} } void _updateUnreadCount() { state = state.copyWith( unreadCount: state.notifications.where((n) => !n.isRead).length, ); } } final notificationProvider = StateNotifierProvider((ref) { final api = ref.read(apiClientProvider); return NotificationNotifier(api); }); final unreadCountProvider = Provider((ref) { return ref.watch(notificationProvider).unreadCount; });