-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeatmapView.swift
More file actions
205 lines (184 loc) · 7.07 KB
/
Copy pathHeatmapView.swift
File metadata and controls
205 lines (184 loc) · 7.07 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
199
200
201
202
203
204
205
//
// HeatmapView.swift
// DevLogPresentation
//
// Created by 최윤진 on 3/2/26.
//
import SwiftUI
import DevLogCore
struct HeatmapView: View {
@State private var availableWidth = CGFloat.zero
let quarter: HeatmapQuarter
let selectedActivityKinds: Set<ActivityKind>
let selectedDay: HeatmapDay?
let onSelectDay: (HeatmapDay) -> Void
var body: some View {
let layout = HeatmapLayout(
availableWidth: availableWidth,
weekCounts: quarter.months.map(\.weeks.count)
)
ScrollView(.horizontal) {
LazyHStack(alignment: .top, spacing: layout.monthSpacing) {
ForEach(quarter.months) { month in
MonthCompactHeatmapView(
month: month,
maxCount: maxCount,
layout: layout,
selectedActivityKinds: selectedActivityKinds,
selectedDay: selectedDay,
onSelectDay: onSelectDay
)
}
}
.padding(.vertical, 2)
.frame(width: layout.contentWidth, alignment: .leading)
}
.scrollIndicators(.hidden)
.scrollDisabled(true)
.frame(maxWidth: .infinity, alignment: .leading)
.background {
GeometryReader { geometry in
Color.clear
.onAppear { updateAvailableWidth(geometry.size.width) }
.onChange(of: geometry.size.width) { updateAvailableWidth($1) }
}
}
}
private var maxCount: Int {
quarter.months
.flatMap(\.weeks)
.flatMap { $0 }
.filter(\.isVisible)
.map(dayCount(for:))
.max() ?? 0
}
private func dayCount(for day: HeatmapDay) -> Int {
var value = 0
if selectedActivityKinds.contains(.created) {
value += day.createdCount
}
if selectedActivityKinds.contains(.completed) {
value += day.completedCount
}
if selectedActivityKinds.contains(.deleted) {
value += day.deletedCount
}
return value
}
private func updateAvailableWidth(_ width: CGFloat) {
if availableWidth != width {
availableWidth = width
}
}
}
private struct HeatmapLayout {
private static let minimumCellSize: CGFloat = 8
private static let maximumCellSize: CGFloat = 22
let cellSize: CGFloat
let cellSpacing: CGFloat = 4
let monthSpacing: CGFloat = 12
let monthTitleSpacing: CGFloat = 6
let contentWidth: CGFloat
init(availableWidth: CGFloat, weekCounts: [Int]) {
let totalColumns = max(weekCounts.reduce(0, +), 1)
let totalColumnSpacings = weekCounts.reduce(0) { partialResult, count in
partialResult + max(count - 1, 0)
}
let fixedWidth = monthSpacing * CGFloat(max(weekCounts.count - 1, 0))
+ cellSpacing * CGFloat(totalColumnSpacings)
let fittingCellSize = max(0, availableWidth - fixedWidth) / CGFloat(totalColumns)
cellSize = min(max(fittingCellSize, Self.minimumCellSize), Self.maximumCellSize)
contentWidth = cellSize * CGFloat(totalColumns) + fixedWidth
}
var cellCornerRadius: CGFloat {
max(2, cellSize * 0.2)
}
var innerSelectionLineWidth: CGFloat {
max(1.2, cellSize * 0.12)
}
}
private struct MonthCompactHeatmapView: View {
@Environment(\.colorScheme) private var colorScheme
let month: HeatmapMonth
let maxCount: Int
let layout: HeatmapLayout
let selectedActivityKinds: Set<ActivityKind>
let selectedDay: HeatmapDay?
let onSelectDay: (HeatmapDay) -> Void
private let orderedWeekdays = Array(1...7)
var body: some View {
VStack(alignment: .leading, spacing: layout.monthTitleSpacing) {
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.indices, id: \.self) { weekIndex in
let day = month.weeks[weekIndex].first {
Calendar.current.component(.weekday, from: $0.date) == weekday
}
RoundedRectangle(cornerRadius: layout.cellCornerRadius)
.fill(fillColor(for: day, with: maxCount))
.stroke(
selectionInnerBorderColor(for: day),
lineWidth: layout.innerSelectionLineWidth
)
.overlay(
RoundedRectangle(cornerRadius: layout.cellCornerRadius + 1)
.stroke(selectionOuterBorderColor(for: day), lineWidth: 0.8)
.padding(-1)
)
.frame(width: layout.cellSize, height: layout.cellSize)
.onTapGesture {
if let day, day.isVisible {
onSelectDay(day)
}
}
}
}
}
}
}
}
private func isSelected(_ day: HeatmapDay?) -> Bool {
guard let day, let selectedDay, day.isVisible else { return false }
return Calendar.current.isDate(day.date, inSameDayAs: selectedDay.date)
}
private func selectionInnerBorderColor(for day: HeatmapDay?) -> Color {
isSelected(day) ? .white : .clear
}
private func selectionOuterBorderColor(for day: HeatmapDay?) -> Color {
if isSelected(day) && colorScheme == .light {
return Color.gray
}
return .clear
}
private func fillColor(for day: HeatmapDay?, with maxCount: Int) -> 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: HeatmapDay) -> Int {
var value = 0
if selectedActivityKinds.contains(.created) {
value += day.createdCount
}
if selectedActivityKinds.contains(.completed) {
value += day.completedCount
}
if selectedActivityKinds.contains(.deleted) {
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
}
}