-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProfileCompletionQuarter.swift
More file actions
51 lines (45 loc) · 1.52 KB
/
Copy pathProfileCompletionQuarter.swift
File metadata and controls
51 lines (45 loc) · 1.52 KB
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
51
//
// ProfileCompletionQuarter.swift
// DevLog
//
// Created by opfic on 3/2/26.
//
import Foundation
struct ProfileCompletionQuarter: Identifiable, Hashable {
var id: Date { quarterStart }
let quarterStart: Date
let months: [ProfileCompletionMonth]
var weeklyTrendPoints: [ProfileWeeklyTrendPoint] {
Self.makeWeeklyTrendPoints(from: months, calendar: .current)
}
var maxCount: Int {
months
.flatMap { $0.weeks }
.flatMap { $0 }
.filter { $0.isVisible }
.map { $0.createdCount + $0.completedCount }
.max() ?? 0
}
static func makeWeeklyTrendPoints(
from months: [ProfileCompletionMonth],
calendar: Calendar
) -> [ProfileWeeklyTrendPoint] {
let days = months
.flatMap(\.weeks)
.flatMap { $0 }
.filter(\.isVisible)
let groupedByWeekStart = Dictionary(grouping: days) { day in
calendar.dateInterval(of: .weekOfYear, for: day.date)?.start
?? calendar.startOfDay(for: day.date)
}
return groupedByWeekStart.keys.sorted().enumerated().map { index, weekStart in
let weekDays = groupedByWeekStart[weekStart, default: []]
return ProfileWeeklyTrendPoint(
weekStart: weekStart,
weekIndex: index + 1,
createdCount: weekDays.reduce(0) { $0 + $1.createdCount },
completedCount: weekDays.reduce(0) { $0 + $1.completedCount }
)
}
}
}