Skip to content

Commit 5411896

Browse files
committed
feat(account): add support for marking a single notification as read from a deep-link
- Implement new event InAppNotificationCenterMarkOneAsRead - Add new handler _onMarkOneAsRead in InAppNotificationCenterBloc - Extract shared logic to _markOneAsRead helper method - Update imports to follow package order
1 parent 35e5a8e commit 5411896

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

lib/account/bloc/in_app_notification_center_bloc.dart

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import 'dart:async';
22

33
import 'package:bloc/bloc.dart';
4-
import 'package:core/core.dart';
54
import 'package:collection/collection.dart';
5+
import 'package:core/core.dart';
66
import 'package:data_repository/data_repository.dart';
77
import 'package:equatable/equatable.dart';
88
import 'package:flutter_news_app_mobile_client_full_source_code/app/bloc/app_bloc.dart';
@@ -34,6 +34,7 @@ class InAppNotificationCenterBloc
3434
on<InAppNotificationCenterMarkedAsRead>(_onMarkedAsRead);
3535
on<InAppNotificationCenterMarkAllAsRead>(_onMarkAllAsRead);
3636
on<InAppNotificationCenterTabChanged>(_onTabChanged);
37+
on<InAppNotificationCenterMarkOneAsRead>(_onMarkOneAsRead);
3738
}
3839

3940
final DataRepository<InAppNotification> _inAppNotificationRepository;
@@ -128,6 +129,18 @@ class InAppNotificationCenterBloc
128129
(n) => n.id == event.notificationId,
129130
);
130131

132+
await _markOneAsRead(notification, emit);
133+
}
134+
135+
/// Handles marking a single notification as read from a deep-link.
136+
Future<void> _onMarkOneAsRead(
137+
InAppNotificationCenterMarkOneAsRead event,
138+
Emitter<InAppNotificationCenterState> emit,
139+
) async {
140+
final notification = state.notifications.firstWhereOrNull(
141+
(n) => n.id == event.notificationId,
142+
);
143+
131144
if (notification == null) {
132145
_logger.warning(
133146
'Attempted to mark a notification as read that does not exist in the '
@@ -139,6 +152,18 @@ class InAppNotificationCenterBloc
139152
// If already read, do nothing.
140153
if (notification.isRead) return;
141154

155+
await _markOneAsRead(notification, emit);
156+
}
157+
158+
/// A shared helper method to mark a single notification as read.
159+
///
160+
/// This is used by both [_onMarkedAsRead] (from the notification center UI)
161+
/// and [_onMarkOneAsRead] (from a deep-link).
162+
Future<void> _markOneAsRead(
163+
InAppNotification? notification,
164+
Emitter<InAppNotificationCenterState> emit,
165+
) async {
166+
if (notification == null) return;
142167
final updatedNotification = notification.copyWith(readAt: DateTime.now());
143168

144169
try {

lib/account/bloc/in_app_notification_center_event.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,16 @@ class InAppNotificationCenterTabChanged extends InAppNotificationCenterEvent {
4242
@override
4343
List<Object> get props => [tabIndex];
4444
}
45+
46+
/// Dispatched when a single in-app notification is marked as read by its ID,
47+
/// typically from a deep-link without navigating from the notification center.
48+
class InAppNotificationCenterMarkOneAsRead
49+
extends InAppNotificationCenterEvent {
50+
const InAppNotificationCenterMarkOneAsRead(this.notificationId);
51+
52+
/// The ID of the notification to be marked as read.
53+
final String notificationId;
54+
55+
@override
56+
List<Object> get props => [notificationId];
57+
}

0 commit comments

Comments
 (0)