part of 'data_service.dart'; extension DataServiceNotifications on DataService { Future fetchNotifications() async { _isNotificationsLoading = true; try { final json = await api.get('/notifications'); List? list; if (json is List) { list = json; } else if (json is Map) { for (final key in ['notifications', 'data', 'items']) { final value = json[key]; if (value is List) { list = value; break; } } } final parsed = list ?.whereType>() .map(UserNotification.fromJson) .where((n) => !n.dismissed && n.channel.toLowerCase() != 'web') .toList(); if (parsed != null) { parsed.sort((a, b) { final aTs = a.createdAt?.millisecondsSinceEpoch ?? 0; final bTs = b.createdAt?.millisecondsSinceEpoch ?? 0; return bTs.compareTo(aTs); }); _notifications = parsed; } else { _notifications = []; } } catch (e) { debugPrint('Failed to fetch notifications: $e'); _notifications = []; } finally { _isNotificationsLoading = false; _notifyAsync(); } } Future dismissNotifications(List notificationIds) async { if (notificationIds.isEmpty) return; try { await api.put('/notifications/dismiss', { "notification_ids": notificationIds, "payload": {"dismissed": true}, }); _notifications = _notifications .where((n) => !notificationIds.contains(n.id)) .toList(); } catch (e) { debugPrint('Failed to dismiss notifications: $e'); rethrow; } finally { _notifyAsync(); } } }