diff --git a/Pachyderm/Utilities/NotificationGroup.swift b/Pachyderm/Utilities/NotificationGroup.swift index b53ea992..bb484556 100644 --- a/Pachyderm/Utilities/NotificationGroup.swift +++ b/Pachyderm/Utilities/NotificationGroup.swift @@ -27,18 +27,24 @@ public class NotificationGroup { } public static func createGroups(notifications: [Notification], only allowedTypes: [Notification.Kind]) -> [NotificationGroup] { - return notifications.reduce(into: [[Notification]]()) { (groups, notification) in - if allowedTypes.contains(notification.kind), - let lastGroup = groups.last, - let firstStatus = lastGroup.first, - firstStatus.kind == notification.kind, - firstStatus.status?.id == notification.status?.id { - - groups[groups.count - 1].append(notification) - } else { - groups.append([notification]) + var groups = [[Notification]]() + for notification in notifications { + if allowedTypes.contains(notification.kind) { + if let lastGroup = groups.last, let firstNotification = lastGroup.first, firstNotification.kind == notification.kind, firstNotification.status?.id == notification.status?.id { + groups[groups.count - 1].append(notification) + continue + } else if groups.count >= 2 { + let secondToLastGroup = groups[groups.count - 2] + if allowedTypes.contains(groups[groups.count - 1][0].kind), let firstNotification = secondToLastGroup.first, firstNotification.kind == notification.kind, firstNotification.status?.id == notification.status?.id { + groups[groups.count - 2].append(notification) + continue + } + } } - }.map { + + groups.append([notification]) + } + return groups.map { NotificationGroup(notifications: $0)! } }