-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWidgetHeatmapGrid.swift
More file actions
198 lines (170 loc) · 6.61 KB
/
Copy pathWidgetHeatmapGrid.swift
File metadata and controls
198 lines (170 loc) · 6.61 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//
// WidgetHeatmapGrid.swift
// DevLogWidget
//
// Created by opfic on 4/28/26.
//
import SwiftUI
struct WidgetHeatmapGrid: View {
let months: [WidgetHeatmapMonthSnapshot]
let selectedActivityKindRawValues: [String]
let maxCount: Int
let showsMonthTitles: Bool
var body: some View {
GeometryReader { proxy in
let layout = WidgetHeatmapLayout(
availableWidth: proxy.size.width,
availableHeight: proxy.size.height,
weekCounts: months.map(\.weeks.count),
showsMonthTitles: showsMonthTitles
)
HStack(alignment: .top, spacing: layout.monthSpacing) {
ForEach(months, id: \.monthStart) { month in
WidgetHeatmapMonthGrid(
month: month,
layout: layout,
selectedActivityKindRawValues: selectedActivityKindRawValues,
maxCount: maxCount,
showsMonthTitle: showsMonthTitles
)
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
}
}
}
struct WidgetHeatmapPlaceholderGrid: View {
let months: [WidgetHeatmapPlaceholderMonthShape]
let showsMonthTitles: Bool
var body: some View {
let weekCounts = months.map(\.weeks.count)
GeometryReader { proxy in
let layout = WidgetHeatmapLayout(
availableWidth: proxy.size.width,
availableHeight: proxy.size.height,
weekCounts: weekCounts,
showsMonthTitles: showsMonthTitles
)
HStack(alignment: .top, spacing: layout.monthSpacing) {
ForEach(months) { month in
WidgetHeatmapPlaceholderMonthGrid(
month: month,
layout: layout,
showsMonthTitle: showsMonthTitles
)
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
}
}
}
private struct WidgetHeatmapMonthGrid: View {
let month: WidgetHeatmapMonthSnapshot
let layout: WidgetHeatmapLayout
let selectedActivityKindRawValues: [String]
let maxCount: Int
let showsMonthTitle: Bool
private let orderedWeekdays = Array(1...7)
var body: some View {
VStack(alignment: .leading, spacing: layout.monthTitleSpacing) {
if showsMonthTitle {
Text(month.monthStart.formatted(.dateTime.month(.abbreviated)))
.frame(height: layout.cellSize)
.font(.caption)
.foregroundStyle(.secondary)
}
VStack(alignment: .leading, spacing: layout.cellSpacing) {
ForEach(orderedWeekdays, id: \.self) { weekday in
HStack(spacing: layout.cellSpacing) {
ForEach(month.weeks, id: \.id) { week in
let day = week.days.first {
Calendar.current.component(.weekday, from: $0.date) == weekday
}
RoundedRectangle(cornerRadius: layout.cellCornerRadius)
.fill(fillColor(for: day))
.frame(width: layout.cellSize, height: layout.cellSize)
}
}
}
}
}
}
private func fillColor(for day: WidgetHeatmapDaySnapshot?) -> Color {
guard let day, day.isVisible else { return .clear }
let count = dayCount(for: day)
if count == 0 {
return Color(.systemGray5)
}
return Color.blue.opacity(opacity(for: count, max: maxCount))
}
private func dayCount(for day: WidgetHeatmapDaySnapshot) -> Int {
let selectedActivityKindRawValues = Set(selectedActivityKindRawValues)
var value = 0
if selectedActivityKindRawValues.contains(WidgetHeatmapActivityKind.created.rawValue) {
value += day.createdCount
}
if selectedActivityKindRawValues.contains(WidgetHeatmapActivityKind.completed.rawValue) {
value += day.completedCount
}
if selectedActivityKindRawValues.contains(WidgetHeatmapActivityKind.deleted.rawValue) {
value += day.deletedCount
}
return value
}
private func opacity(for count: Int, max: Int) -> Double {
guard 0 < count && 0 < max else { return 0 }
let ratio = Double(count) / Double(max)
return ceil(ratio * 10) / 10
}
}
private enum WidgetHeatmapActivityKind: String {
case created
case completed
case deleted
}
private struct WidgetHeatmapPlaceholderMonthGrid: View {
let month: WidgetHeatmapPlaceholderMonthShape
let layout: WidgetHeatmapLayout
let showsMonthTitle: Bool
private let orderedWeekdays = Array(1...7)
var body: some View {
VStack(alignment: .leading, spacing: layout.monthTitleSpacing) {
if showsMonthTitle {
RoundedRectangle(cornerRadius: 3)
.fill(Color.secondary.opacity(0.18))
.frame(width: layout.cellSize * 3, height: 8)
.frame(height: layout.cellSize, alignment: .leading)
}
VStack(alignment: .leading, spacing: layout.cellSpacing) {
ForEach(orderedWeekdays, id: \.self) { weekday in
HStack(spacing: layout.cellSpacing) {
ForEach(month.weeks) { week in
let day = week.days.first {
Calendar.current.component(.weekday, from: $0.date) == weekday
}
RoundedRectangle(cornerRadius: layout.cellCornerRadius)
.fill(fillColor(for: day))
.frame(width: layout.cellSize, height: layout.cellSize)
}
}
}
}
}
}
private func fillColor(for day: WidgetHeatmapPlaceholderDayShape?) -> Color {
guard let day, day.isVisible else { return .clear }
return Color.secondary.opacity(opacity(for: day))
}
private func opacity(for day: WidgetHeatmapPlaceholderDayShape) -> Double {
switch Calendar.current.component(.day, from: day.date) % 4 {
case 0:
return 1 / 8
case 1:
return 1 / 5
case 2:
return 1 / 4
default:
return 3 / 20
}
}
}