1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import Foundation
struct NotificationGroup: Identifiable, Equatable {
var notifications: [FeedNotification]
var id: String { notifications[0].id }
static func make(from notifications: [FeedNotification], pageStarts: Set<String> = []) -> [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
}
}