This commit is contained in:
pradeepkumar
2026-03-28 02:21:36 +05:30
parent 40410c9972
commit 5909b7bf97
3 changed files with 70 additions and 52 deletions

View File

@@ -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 (_) {}
}
}
}