Skip to content

Commit 593d487

Browse files
committed
feat(app): handle notification tap in app bloc
- Add event handler for AppNotificationTapped event - Implement logic to mark a specific notification as read when tapped - Add error handling and logging for the notification tap process
1 parent edfce76 commit 593d487

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

lib/app/bloc/app_bloc.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ class AppBloc extends Bloc<AppEvent, AppState> {
9494
_onAllInAppNotificationsMarkedAsRead,
9595
);
9696
on<AppInAppNotificationMarkedAsRead>(_onInAppNotificationMarkedAsRead);
97+
on<AppNotificationTapped>(_onAppNotificationTapped);
9798

9899
// Listen to token refresh events from the push notification service.
99100
// When a token is refreshed, dispatch an event to trigger device
@@ -681,6 +682,41 @@ class AppBloc extends Bloc<AppEvent, AppState> {
681682
}
682683
}
683684

685+
/// Handles marking a specific notification as read when it's tapped.
686+
Future<void> _onAppNotificationTapped(
687+
AppNotificationTapped event,
688+
Emitter<AppState> emit,
689+
) async {
690+
final userId = state.user?.id;
691+
if (userId == null) {
692+
_logger.warning(
693+
'[AppBloc] Cannot mark notification as read: user is not logged in.',
694+
);
695+
return;
696+
}
697+
698+
try {
699+
// First, read the existing notification to get the full object.
700+
final notification = await _inAppNotificationRepository.read(
701+
id: event.notificationId,
702+
userId: userId,
703+
);
704+
705+
// Then, update it with the 'readAt' timestamp.
706+
await _inAppNotificationRepository.update(
707+
id: notification.id,
708+
item: notification.copyWith(readAt: DateTime.now()),
709+
userId: userId,
710+
);
711+
712+
_logger.info(
713+
'[AppBloc] Marked notification ${event.notificationId} as read.',
714+
);
715+
} catch (e, s) {
716+
_logger.severe('Failed to mark notification as read.', e, s);
717+
}
718+
}
719+
684720
/// Handles the [AppPushNotificationTokenRefreshed] event.
685721
///
686722
/// This event is triggered when the underlying push notification provider

0 commit comments

Comments
 (0)