forked from shadowfacts/Tusker
49 lines
1.5 KiB
Swift
49 lines
1.5 KiB
Swift
//
|
|
// NotificationGroup.swift
|
|
// Pachyderm
|
|
//
|
|
// Created by Shadowfacts on 9/5/19.
|
|
// Copyright © 2019 Shadowfacts. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
public class NotificationGroup {
|
|
public let notificationIDs: [String]
|
|
public let id: String
|
|
public let kind: Notification.Kind
|
|
public let statusState: StatusState?
|
|
|
|
init?(notifications: [Notification]) {
|
|
guard !notifications.isEmpty else { return nil }
|
|
self.notificationIDs = notifications.map { $0.id }
|
|
self.id = notifications.first!.id
|
|
self.kind = notifications.first!.kind
|
|
if kind == .mention {
|
|
self.statusState = .unknown
|
|
} else {
|
|
self.statusState = nil
|
|
}
|
|
}
|
|
|
|
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])
|
|
}
|
|
}.map {
|
|
NotificationGroup(notifications: $0)!
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
extension NotificationGroup: Identifiable {}
|