@@ -340,51 +340,66 @@ class InAppNotificationCenterBloc
340340 InAppNotificationCenterReadItemsDeleted event,
341341 Emitter <InAppNotificationCenterState > emit,
342342 ) async {
343- final userId = _appBloc.state.user? .id;
344- final isBreakingNewsTab = state.currentTabIndex == 0 ;
345- final notificationsForTab = isBreakingNewsTab
346- ? state.breakingNewsNotifications
347- : state.digestNotifications;
343+ final userId = _appBloc.state.user! .id;
344+ try {
345+ emit (state.copyWith (status: InAppNotificationCenterStatus .deleting));
348346
349- final readNotifications = notificationsForTab
350- .where ((n) => n.isRead)
351- .toList ();
347+ final isBreakingNewsTab = state.currentTabIndex == 0 ;
348+ final notificationsForTab = isBreakingNewsTab
349+ ? state.breakingNewsNotifications
350+ : state.digestNotifications;
352351
353- if (readNotifications.isEmpty) {
354- _logger.info ('No read notifications to delete in the current tab.' );
355- return ;
356- }
352+ final readNotifications = notificationsForTab
353+ .where ((n) => n.isRead)
354+ .toList ();
357355
358- final idsToDelete = readNotifications.map ((n) => n.id).toList ();
356+ if (readNotifications.isEmpty) {
357+ _logger.info ('No read notifications to delete in the current tab.' );
358+ emit (state.copyWith (status: InAppNotificationCenterStatus .success));
359+ return ;
360+ }
359361
360- // Optimistic UI update: remove the read items from the state immediately.
361- if (isBreakingNewsTab) {
362- final updatedList = state.breakingNewsNotifications
363- .where ((n) => ! n.isRead)
364- .toList ();
365- emit (state.copyWith (breakingNewsNotifications: updatedList));
366- } else {
367- final updatedList = state.digestNotifications
368- .where ((n) => ! n.isRead)
369- .toList ();
370- emit (state.copyWith (digestNotifications: updatedList));
371- }
362+ final idsToDelete = readNotifications.map ((n) => n.id).toList ();
372363
373- _logger.info (
374- 'Optimistically removed ${idsToDelete .length } read notifications from UI. '
375- 'Deleting from repository in the background.' ,
376- );
364+ _logger.info ('Deleting ${idsToDelete .length } read notifications...' );
377365
378- // Perform the actual deletion in the background.
379- try {
380366 await Future .wait (
381367 idsToDelete.map (
382368 (id) => _inAppNotificationRepository.delete (id: id, userId: userId),
383369 ),
384370 );
385- } catch (e, s) {
386- _logger.severe ('Failed to delete one or more notifications.' , e, s);
387- // Do not revert state to avoid UI flicker. The error is logged.
371+
372+ _logger.info ('Deletion successful. Refreshing notification list.' );
373+
374+ // After deletion, re-fetch the current tab's data to ensure consistency.
375+ final filter = isBreakingNewsTab ? _breakingNewsFilter : _digestFilter;
376+ final response = await _fetchNotifications (
377+ userId: userId,
378+ filter: filter,
379+ );
380+
381+ // Update the state with the refreshed list.
382+ if (isBreakingNewsTab) {
383+ emit (
384+ state.copyWith (
385+ breakingNewsNotifications: response.items,
386+ breakingNewsHasMore: response.hasMore,
387+ breakingNewsCursor: response.cursor,
388+ ),
389+ );
390+ } else {
391+ emit (
392+ state.copyWith (
393+ digestNotifications: response.items,
394+ digestHasMore: response.hasMore,
395+ digestCursor: response.cursor,
396+ ),
397+ );
398+ }
399+
400+ emit (state.copyWith (status: InAppNotificationCenterStatus .success));
401+ } catch (error, stackTrace) {
402+ _handleFetchError (emit, error, stackTrace);
388403 }
389404 }
390405
0 commit comments