~ knotless Search/
↑↓ move↵ open⇧↵ all results

geesawra.industries/old-fella

Old people's social media app

0
1
main
31 lines · 1.2 KB Raw History
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
    }
}