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

View File

@@ -318,7 +318,7 @@ class _NotificationsScreenState extends ConsumerState<NotificationsScreen> {
),
const SizedBox(height: 2),
Text(
notification.description,
_formatDescription(notification.description),
style: TextStyle(
fontFamily: 'SourceSerif4',
fontSize: 14,
@@ -425,6 +425,18 @@ class _NotificationsScreenState extends ConsumerState<NotificationsScreen> {
}
}
String _formatDescription(String description) {
final trimmed = description.trim();
// Detect raw URLs (image/gif links) and show friendly text instead
if (trimmed.startsWith('http://') || trimmed.startsWith('https://')) {
final lower = trimmed.toLowerCase();
if (lower.contains('.gif')) return 'Sent a GIF';
if (lower.contains('.png') || lower.contains('.jpg') || lower.contains('.jpeg') || lower.contains('.webp')) return 'Sent an image';
return 'Sent a link';
}
return description;
}
String _timeAgo(DateTime dateTime) {
final now = DateTime.now();
final diff = now.difference(dateTime);