feat: add token check to notification refresh and methods for controlling notification polling.

This commit is contained in:
pradeepkumar
2026-03-19 03:32:35 +05:30
parent 3ec7fc01e1
commit 6936c29c13

View File

@@ -1,6 +1,7 @@
import 'dart:async';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:real_estate_mobile/core/storage/secure_storage.dart';
import 'package:real_estate_mobile/features/notifications/data/models/notification_model.dart';
import 'package:real_estate_mobile/features/notifications/data/notification_repository.dart';
@@ -20,6 +21,12 @@ class UnreadCountNotifier extends StateNotifier<int> {
}
Future<void> refresh() async {
// Skip if no token (logged out) — prevents 401 errors
final token = await SecureStorage.getAccessToken();
if (token == null) {
if (mounted) state = 0;
return;
}
try {
final count = await _repo.getUnreadCount();
if (mounted) state = count;
@@ -28,6 +35,20 @@ class UnreadCountNotifier extends StateNotifier<int> {
}
}
/// Stop polling (call on logout).
void stopPolling() {
_timer?.cancel();
_timer = null;
if (mounted) state = 0;
}
/// Restart polling (call on login).
void startPolling() {
_timer?.cancel();
refresh();
_timer = Timer.periodic(const Duration(seconds: 30), (_) => refresh());
}
@override
void dispose() {
_timer?.cancel();