import Foundation struct NotificationGroup: Identifiable, Equatable { var notifications: [FeedNotification] var id: String { notifications[0].id } static func make(from notifications: [FeedNotification], pageStarts: Set = []) -> [NotificationGroup] { struct Key: Hashable { let reason: FeedNotification.Reason let postID: String } var groups: [NotificationGroup] = [] var groupIndexes: [Key: Int] = [:] for notification in notifications { if pageStarts.contains(notification.id) { groupIndexes.removeAll() } guard notification.reason == .like || notification.reason == .repost, let postID = notification.postID else { groups.append(NotificationGroup(notifications: [notification])) continue } let key = Key(reason: notification.reason, postID: postID) if let index = groupIndexes[key] { groups[index].notifications.append(notification) } else { groupIndexes[key] = groups.count groups.append(NotificationGroup(notifications: [notification])) } } return groups } }