IceCubesApp/Packages/Notifications/Sources/Notifications/Notification+Consolidated.swift

37 lines
1.3 KiB
Swift
Raw Normal View History

//
// Notification+Consolidated.swift
//
//
// Created by Jérôme Danthinne on 31/01/2023.
//
import Foundation
2023-02-18 06:26:48 +00:00
import Models
extension Array where Element == Models.Notification {
func consolidated(selectedType: Models.Notification.NotificationType?) async -> [ConsolidatedNotification] {
2023-02-18 06:26:48 +00:00
await withCheckedContinuation { result in
DispatchQueue.global().async {
let notifications: [ConsolidatedNotification] =
2023-02-18 06:26:48 +00:00
Dictionary(grouping: self) { $0.consolidationId(selectedType: selectedType) }
.values
.compactMap { notifications in
guard let notification = notifications.first,
let supportedType = notification.supportedType
else { return nil }
2023-02-18 06:26:48 +00:00
return ConsolidatedNotification(notifications: notifications,
type: supportedType,
createdAt: notification.createdAt,
accounts: notifications.map(\.account),
status: notification.status)
}
.sorted {
$0.createdAt.asDate > $1.createdAt.asDate
}
result.resume(returning: notifications)
}
2023-02-18 06:26:48 +00:00
}
}
}