-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProfileHeatmapView.swift
More file actions
214 lines (192 loc) · 7.62 KB
/
Copy pathProfileHeatmapView.swift
File metadata and controls
214 lines (192 loc) · 7.62 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
206
207
208
209
210
211
212
213
214
//
// ProfileHeatmapView.swift
// DevLog
//
// Created by 최윤진 on 3/2/26.
//
import SwiftUI
struct ProfileHeatmapView: View {
@Environment(\.safeAreaInsets) private var safeAreaInsets
@Environment(\.sceneWidth) private var sceneWidth
let quarter: ProfileCompletionQuarter
let selectedActivityTypes: Set<ProfileActivityType>
let selectedDay: ProfileCompletionDay?
let onSelectDay: (ProfileCompletionDay) -> Void
var body: some View {
let layout = ProfileHeatmapLayout(
availableWidth: availableWidth,
weekCounts: quarter.months.map(\.weeks.count)
)
VStack(alignment: .leading, spacing: 10) {
Text("활동 히트맵")
.font(.subheadline)
.bold()
HStack(alignment: .top, spacing: 0) {
weekdayLabel(layout: layout)
HStack(alignment: .top, spacing: layout.monthSpacing) {
ForEach(quarter.months) { month in
MonthCompactHeatmapView(
month: month,
maxCount: quarter.maxCount,
layout: layout,
selectedActivityTypes: selectedActivityTypes,
selectedDay: selectedDay,
onSelectDay: onSelectDay
)
}
}
}
.padding(.vertical, 2)
}
}
@ViewBuilder
private func weekdayLabel(layout: ProfileHeatmapLayout) -> some View {
let labels: [Int: String] = [
2: "월",
4: "수",
6: "금"
]
let orderedWeekdays = Array(1...7)
VStack(alignment: .leading, spacing: layout.cellSpacing) {
ForEach(orderedWeekdays, id: \.self) { weekday in
Group {
if let label = labels[weekday] {
Text(label)
.font(.caption2)
.foregroundStyle(.secondary)
.frame(
width: layout.cellSize,
height: layout.cellSize,
alignment: .leading
)
} else {
Color.clear
.frame(
width: layout.cellSize,
height: layout.cellSize
)
}
}
}
}
.padding(.top, layout.weekdayTopPadding)
}
private var availableWidth: CGFloat {
// ProfileView의 바깥 가로 패딩(16)과 히트맵 카드 내부 패딩(12)을 합한 값
let horizontalPadding: CGFloat = 16 + 12
return max(
0,
sceneWidth
- safeAreaInsets.leading
- safeAreaInsets.trailing
- (horizontalPadding * 2)
)
}
}
private struct ProfileHeatmapLayout {
let cellSize: CGFloat
let cellSpacing: CGFloat = 4
let monthSpacing: CGFloat = 12
let monthTitleSpacing: CGFloat = 6
init(availableWidth: CGFloat, weekCounts: [Int]) {
let sanitizedWeekCounts = weekCounts.filter { 0 < $0 }
let totalColumns = max(sanitizedWeekCounts.reduce(0, +), 1)
let totalColumnSpacings = sanitizedWeekCounts.reduce(0) { partialResult, count in
partialResult + max(count - 1, 0)
}
let fixedWidth = monthSpacing * CGFloat(max(sanitizedWeekCounts.count - 1, 0))
+ cellSpacing * CGFloat(totalColumnSpacings)
cellSize = max(0, availableWidth - fixedWidth) / CGFloat(totalColumns + 1)
}
var weekdayTopPadding: CGFloat {
cellSize + monthTitleSpacing
}
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: ProfileCompletionMonth
let maxCount: Int
let layout: ProfileHeatmapLayout
let selectedActivityTypes: Set<ProfileActivityType>
let selectedDay: ProfileCompletionDay?
let onSelectDay: (ProfileCompletionDay) -> 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: ProfileCompletionDay?) -> Bool {
guard let day, let selectedDay else { return false }
return Calendar.current.isDate(day.date, inSameDayAs: selectedDay.date)
}
private func selectionInnerBorderColor(for day: ProfileCompletionDay?) -> Color {
isSelected(day) ? .white : .clear
}
private func selectionOuterBorderColor(for day: ProfileCompletionDay?) -> Color {
if isSelected(day) && colorScheme == .light {
return Color.gray
}
return .clear
}
private func fillColor(for day: ProfileCompletionDay?, 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: ProfileCompletionDay) -> Int {
var value = 0
if selectedActivityTypes.contains(.created) {
value += day.createdCount
}
if selectedActivityTypes.contains(.completed) {
value += day.completedCount
}
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
}
}