-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeatmapWidgetEntryView.swift
More file actions
98 lines (89 loc) · 3.06 KB
/
Copy pathHeatmapWidgetEntryView.swift
File metadata and controls
98 lines (89 loc) · 3.06 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
//
// HeatmapWidgetEntryView.swift
// DevLogWidget
//
// Created by opfic on 4/15/26.
//
import SwiftUI
import WidgetKit
struct HeatmapWidgetEntryView: View {
let entry: HeatmapWidgetEntry
@Environment(\.widgetFamily) private var widgetFamily
var body: some View {
Group {
if let snapshot = entry.snapshot {
content(snapshot)
} else {
emptyState
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
}
@ViewBuilder
private func content(_ snapshot: HeatmapWidgetSnapshot) -> some View {
switch widgetFamily {
case .systemSmall:
VStack(alignment: .leading, spacing: 4) {
header(title: "widget_heatmap_current_month_title")
WidgetHeatmapGrid(
months: currentMonths(from: snapshot),
selectedActivityKindRawValues: snapshot.selectedActivityKindRawValues,
maxCount: snapshot.maxCount,
showsMonthTitles: false
)
}
case .systemMedium:
VStack(alignment: .leading, spacing: 8) {
header(title: "widget_heatmap_current_quarter_title")
WidgetHeatmapGrid(
months: snapshot.months,
selectedActivityKindRawValues: snapshot.selectedActivityKindRawValues,
maxCount: snapshot.maxCount,
showsMonthTitles: true
)
}
default:
EmptyView()
}
}
@ViewBuilder
private var emptyState: some View {
let shape = WidgetHeatmapPlaceholderShape(date: entry.date)
switch widgetFamily {
case .systemSmall:
VStack(alignment: .leading, spacing: 8) {
header(title: "widget_heatmap_current_month_title")
WidgetHeatmapPlaceholderGrid(
months: shape.currentMonths,
showsMonthTitles: false
)
}
case .systemMedium:
VStack(alignment: .leading, spacing: 8) {
header(title: "widget_heatmap_current_quarter_title")
WidgetHeatmapPlaceholderGrid(
months: shape.quarterMonths,
showsMonthTitles: true
)
}
default:
EmptyView()
}
}
private func header(title: LocalizedStringKey) -> some View {
HStack(alignment: .firstTextBaseline, spacing: 6) {
Text(title)
.font(.headline)
.lineLimit(1)
Spacer(minLength: 0)
}
}
private func currentMonths(from snapshot: HeatmapWidgetSnapshot) -> [WidgetHeatmapMonthSnapshot] {
if let currentMonth = snapshot.months.first(where: {
Calendar.current.isDate($0.monthStart, equalTo: snapshot.generatedAt, toGranularity: .month)
}) {
return [currentMonth]
}
return Array(snapshot.months.prefix(1))
}
}