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)
This commit is contained in:
Shadowfacts 2020-02-28 19:21:39 -05:00
parent d9bae42f81
commit 70bca052c4
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 17 additions and 11 deletions

View File

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