From 6936c29c13215a708ef45ba03be5ebb90dbe0fcf Mon Sep 17 00:00:00 2001 From: pradeepkumar Date: Thu, 19 Mar 2026 03:32:35 +0530 Subject: [PATCH] feat: add token check to notification refresh and methods for controlling notification polling. --- .../providers/notification_provider.dart | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lib/features/notifications/presentation/providers/notification_provider.dart b/lib/features/notifications/presentation/providers/notification_provider.dart index 95cc590..bb59ce6 100644 --- a/lib/features/notifications/presentation/providers/notification_provider.dart +++ b/lib/features/notifications/presentation/providers/notification_provider.dart @@ -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 { } Future 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 { } } + /// 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();