add support for badges and notifications, adjust nav pages
All checks were successful
All checks were successful
This commit is contained in:
62
lib/services/data_service/data_service_notifications.dart
Normal file
62
lib/services/data_service/data_service_notifications.dart
Normal file
@@ -0,0 +1,62 @@
|
||||
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)
|
||||
.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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user