From 70bca052c4c6b79dcbb45ca8d66984349c5346b9 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Fri, 28 Feb 2020 19:21:39 -0500 Subject: [PATCH] Tweak notification grouping Notifications that are of the same type but are separated by a groupable notification of a different type are now considered groupable. For example: favorite 1 (status 1) reblog 1 (status 1) favorite 2 (status 1) reblog 2 (status 1) mention 1 reblog 3 (status 1) will be grouped into: favorite 1, 2 (status 1) reblog 1, 2 (status 1) mention 1 reblog 3 (status 1) --- Pachyderm/Utilities/NotificationGroup.swift | 28 +++++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) 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)! } }