Files
mileograph_flutter/lib/services/data_service/data_service_notifications.dart
Pete Gregory 2fa66ff9c6
All checks were successful
Release / meta (push) Successful in 8s
Release / linux-build (push) Successful in 6m49s
Release / web-build (push) Successful in 6m23s
Release / android-build (push) Successful in 16m56s
Release / release-master (push) Successful in 30s
Release / release-dev (push) Successful in 32s
add admin page, fix no legs infinite load on dashboard
2026-01-02 18:49:14 +00:00

63 lines
1.7 KiB
Dart

part of 'data_service.dart';
extension DataServiceNotifications on DataService {
Future<void> fetchNotifications() async {
_isNotificationsLoading = true;
try {
final json = await api.get('/notifications');
List<dynamic>? 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<String, dynamic>>()
.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<void> dismissNotifications(List<int> 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();
}
}
}