-
Notifications
You must be signed in to change notification settings - Fork 4
/
ProjectsGroup.swift
50 lines (44 loc) · 1.14 KB
/
ProjectsGroup.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import Foundation
import IdentifiedCollections
public struct ProjectsGroup: Equatable, Sendable {
public init(
date: Date,
projects: IdentifiedArrayOf<Project>
) {
self.date = date
self.projects = projects
}
public var date: Date
public var projects: IdentifiedArrayOf<Project>
}
extension ProjectsGroup: Identifiable {
public struct ID: Hashable, Sendable {
public init(date: Date) {
self.date = date
}
public var date: Date
}
public var id: ID { ID(date: date) }
}
extension IdentifiedArrayOf<ProjectsGroup> {
public init(
groupingByYear projects: IdentifiedArrayOf<Project>,
calendar: Calendar = .current
) {
self = .init(
uniqueElements: Dictionary<Int, [Project]>(
grouping: projects,
by: { calendar.component(.year, from: $0.date) }
)
.map { (year: Int, projects: [Project]) -> ProjectsGroup in
ProjectsGroup(
date: calendar.date(from: DateComponents(year: year))!,
projects: .init(
uniqueElements: projects.sorted { $0.date > $1.date }
)
)
}
.sorted { $0.date > $1.date }
)
}
}