2019-09-05 21:38:04 +00:00
|
|
|
//
|
|
|
|
// NotificationGroup.swift
|
|
|
|
// Pachyderm
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 9/5/19.
|
|
|
|
// Copyright © 2019 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
public class NotificationGroup {
|
2020-04-27 23:20:09 +00:00
|
|
|
public let notifications: [Notification]
|
2019-09-05 21:38:04 +00:00
|
|
|
public let id: String
|
|
|
|
public let kind: Notification.Kind
|
2019-11-28 23:36:58 +00:00
|
|
|
public let statusState: StatusState?
|
2020-04-27 23:20:09 +00:00
|
|
|
|
2019-09-05 21:38:04 +00:00
|
|
|
init?(notifications: [Notification]) {
|
|
|
|
guard !notifications.isEmpty else { return nil }
|
2020-04-27 23:20:09 +00:00
|
|
|
self.notifications = notifications
|
2019-09-05 21:38:04 +00:00
|
|
|
self.id = notifications.first!.id
|
|
|
|
self.kind = notifications.first!.kind
|
2019-11-28 23:36:58 +00:00
|
|
|
if kind == .mention {
|
|
|
|
self.statusState = .unknown
|
|
|
|
} else {
|
|
|
|
self.statusState = nil
|
|
|
|
}
|
2019-09-05 21:38:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static func createGroups(notifications: [Notification], only allowedTypes: [Notification.Kind]) -> [NotificationGroup] {
|
2020-02-29 00:21:39 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2019-09-05 21:38:04 +00:00
|
|
|
}
|
2020-02-29 00:21:39 +00:00
|
|
|
|
|
|
|
groups.append([notification])
|
|
|
|
}
|
|
|
|
return groups.map {
|
2019-09-05 21:38:04 +00:00
|
|
|
NotificationGroup(notifications: $0)!
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension NotificationGroup: Identifiable {}
|