fix
This commit is contained in:
@@ -42,6 +42,16 @@ class UnreadCountNotifier extends StateNotifier<int> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Set count to zero immediately (optimistic update).
|
||||
void setZero() {
|
||||
if (mounted) state = 0;
|
||||
}
|
||||
|
||||
/// Decrement count by 1 (optimistic update for single mark-as-read).
|
||||
void decrement() {
|
||||
if (mounted && state > 0) state = state - 1;
|
||||
}
|
||||
|
||||
/// Stop polling (call on logout).
|
||||
void stopPolling() {
|
||||
_timer?.cancel();
|
||||
@@ -164,18 +174,24 @@ class NotificationListNotifier extends StateNotifier<NotificationListState> {
|
||||
return n;
|
||||
}).toList(),
|
||||
);
|
||||
_ref.read(unreadCountProvider.notifier).refresh();
|
||||
// Decrement count immediately, then sync with server
|
||||
final countNotifier = _ref.read(unreadCountProvider.notifier);
|
||||
countNotifier.decrement();
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
Future<void> markAllAsRead() async {
|
||||
try {
|
||||
// Set count to 0 immediately for instant UI feedback
|
||||
_ref.read(unreadCountProvider.notifier).setZero();
|
||||
await _repo.markAllAsRead();
|
||||
state = state.copyWith(
|
||||
notifications:
|
||||
state.notifications.map((n) => n.copyWith(read: true)).toList(),
|
||||
);
|
||||
} catch (_) {
|
||||
// If API fails, refresh from server to get correct count
|
||||
_ref.read(unreadCountProvider.notifier).refresh();
|
||||
} catch (_) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user