|
| 1 | +import 'dart:math'; |
| 2 | + |
1 | 3 | import 'package:collection/collection.dart'; |
2 | 4 | import 'package:freezed_annotation/freezed_annotation.dart'; |
3 | 5 | import 'package:state_notifier/state_notifier.dart'; |
@@ -178,14 +180,19 @@ class FeedStateNotifier extends StateNotifier<FeedState> { |
178 | 180 |
|
179 | 181 | /// Handles updates to the feed state when an activity is marked read or seen. |
180 | 182 | void onActivityMarked(MarkActivityData markData) { |
181 | | - // TODO: Handle activity marking (read/seen/watched) operations |
182 | | - |
183 | | - // Note: The mark data contains information about: |
184 | | - // - markAllRead: Whether all activities should be marked as read |
185 | | - // - markAllSeen: Whether all activities should be marked as seen |
186 | | - // - markRead: List of specific activity IDs marked as read |
187 | | - // - markSeen: List of specific activity IDs marked as seen |
188 | | - // - markWatched: List of specific activity IDs marked as watched |
| 183 | + // Update the state based on the type of mark operation |
| 184 | + state = markData.handle( |
| 185 | + // If markAllRead is true, mark all activities as read |
| 186 | + markAllRead: () => _markAllRead(state), |
| 187 | + // If markAllSeen is true, mark all activities as seen |
| 188 | + markAllSeen: () => _markAllSeen(state), |
| 189 | + // If markRead contains specific IDs, mark those as read |
| 190 | + markRead: (read) => _markRead(read, state), |
| 191 | + // If markSeen contains specific IDs, mark those as seen |
| 192 | + markSeen: (seen) => _markSeen(seen, state), |
| 193 | + // For other cases, return the current state without changes |
| 194 | + orElse: (MarkActivityData data) => state, |
| 195 | + ); |
189 | 196 | } |
190 | 197 |
|
191 | 198 | /// Handles updates to the feed state when a bookmark is added or removed. |
@@ -377,6 +384,68 @@ class FeedStateNotifier extends StateNotifier<FeedState> { |
377 | 384 | return _addFollow(follow, removedFollowState); |
378 | 385 | } |
379 | 386 |
|
| 387 | + FeedState _markAllRead(FeedState state) { |
| 388 | + final aggregatedActivities = state.aggregatedActivities; |
| 389 | + final readActivities = aggregatedActivities.map((it) => it.group).toList(); |
| 390 | + |
| 391 | + // Set unread count to 0 and update read activities |
| 392 | + final updatedNotificationStatus = state.notificationStatus?.copyWith( |
| 393 | + unread: 0, |
| 394 | + readActivities: readActivities, |
| 395 | + lastReadAt: DateTime.timestamp(), |
| 396 | + ); |
| 397 | + |
| 398 | + return state.copyWith(notificationStatus: updatedNotificationStatus); |
| 399 | + } |
| 400 | + |
| 401 | + FeedState _markAllSeen(FeedState state) { |
| 402 | + final aggregatedActivities = state.aggregatedActivities; |
| 403 | + final seenActivities = aggregatedActivities.map((it) => it.group).toList(); |
| 404 | + |
| 405 | + // Set unseen count to 0 and update seen activities |
| 406 | + final updatedNotificationStatus = state.notificationStatus?.copyWith( |
| 407 | + unseen: 0, |
| 408 | + seenActivities: seenActivities, |
| 409 | + lastSeenAt: DateTime.timestamp(), |
| 410 | + ); |
| 411 | + |
| 412 | + return state.copyWith(notificationStatus: updatedNotificationStatus); |
| 413 | + } |
| 414 | + |
| 415 | + FeedState _markRead(Set<String> readIds, FeedState state) { |
| 416 | + final readActivities = state.notificationStatus?.readActivities?.toSet(); |
| 417 | + final updatedReadActivities = readActivities?.union(readIds).toList(); |
| 418 | + |
| 419 | + // Decrease unread count by the number of newly read activities |
| 420 | + final unreadCount = state.notificationStatus?.unread ?? 0; |
| 421 | + final updatedUnreadCount = max(unreadCount - readIds.length, 0); |
| 422 | + |
| 423 | + final updatedNotificationStatus = state.notificationStatus?.copyWith( |
| 424 | + unread: updatedUnreadCount, |
| 425 | + readActivities: updatedReadActivities, |
| 426 | + lastReadAt: DateTime.timestamp(), |
| 427 | + ); |
| 428 | + |
| 429 | + return state.copyWith(notificationStatus: updatedNotificationStatus); |
| 430 | + } |
| 431 | + |
| 432 | + FeedState _markSeen(Set<String> seenIds, FeedState state) { |
| 433 | + final seenActivities = state.notificationStatus?.seenActivities?.toSet(); |
| 434 | + final updatedSeenActivities = seenActivities?.union(seenIds).toList(); |
| 435 | + |
| 436 | + // Decrease unseen count by the number of newly seen activities |
| 437 | + final unseenCount = state.notificationStatus?.unseen ?? 0; |
| 438 | + final updatedUnseenCount = max(unseenCount - seenIds.length, 0); |
| 439 | + |
| 440 | + final updatedNotificationStatus = state.notificationStatus?.copyWith( |
| 441 | + unseen: updatedUnseenCount, |
| 442 | + seenActivities: updatedSeenActivities, |
| 443 | + lastSeenAt: DateTime.timestamp(), |
| 444 | + ); |
| 445 | + |
| 446 | + return state.copyWith(notificationStatus: updatedNotificationStatus); |
| 447 | + } |
| 448 | + |
380 | 449 | @override |
381 | 450 | void dispose() { |
382 | 451 | _removeMemberListListener?.call(); |
|
0 commit comments