forked from home-assistant/iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHADynamicIslandView.swift
More file actions
165 lines (147 loc) · 5.51 KB
/
HADynamicIslandView.swift
File metadata and controls
165 lines (147 loc) · 5.51 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
#if os(iOS) && !targetEnvironment(macCatalyst)
import ActivityKit
import Shared
import SwiftUI
import WidgetKit
// MARK: - DynamicIsland builder
/// Builds the `DynamicIsland` for a Home Assistant Live Activity.
/// Used in `HALiveActivityConfiguration`'s `dynamicIsland:` closure.
@available(iOS 17.2, *)
func makeHADynamicIsland(
attributes: HALiveActivityAttributes,
state: HALiveActivityAttributes.ContentState
) -> DynamicIsland {
DynamicIsland {
DynamicIslandExpandedRegion(.leading) {
HADynamicIslandIconView(slug: state.icon, color: state.color, size: 24)
.padding(.leading, DesignSystem.Spaces.half)
}
DynamicIslandExpandedRegion(.center) {
Text(attributes.title)
.font(.caption.weight(.semibold))
.foregroundStyle(.white)
.lineLimit(1)
}
DynamicIslandExpandedRegion(.trailing) {
HAExpandedTrailingView(state: state)
.padding(.trailing, DesignSystem.Spaces.half)
}
DynamicIslandExpandedRegion(.bottom) {
HAExpandedBottomView(state: state)
.padding(.horizontal, DesignSystem.Spaces.one)
.padding(.bottom, DesignSystem.Spaces.half)
}
} compactLeading: {
HADynamicIslandIconView(slug: state.icon, color: state.color, size: 16)
.padding(.leading, DesignSystem.Spaces.half)
} compactTrailing: {
HACompactTrailingView(state: state)
.padding(.trailing, DesignSystem.Spaces.half)
} minimal: {
HADynamicIslandIconView(slug: state.icon, color: state.color, size: 14)
}
}
// MARK: - Icon view
@available(iOS 17.2, *)
struct HADynamicIslandIconView: View {
let slug: String?
let color: String?
let size: CGFloat
/// Hex string for Home Assistant brand blue — used for UIColor(hex:) fallback.
private static let haBlueHex = "#03A9F4"
var body: some View {
if let slug {
// UIColor(hex:) from Shared handles nil/CSS names/3-6-8 digit hex; non-failable.
let uiColor = UIColor(hex: color ?? Self.haBlueHex)
let mdiIcon = MaterialDesignIcons(serversideValueNamed: slug)
Image(uiImage: mdiIcon.image(
ofSize: .init(width: size, height: size),
color: uiColor
))
.resizable()
.frame(width: size, height: size)
}
}
}
// MARK: - Compact trailing
@available(iOS 17.2, *)
struct HACompactTrailingView: View {
let state: HALiveActivityAttributes.ContentState
/// Fixed width for the countdown timer text in compact trailing.
/// 44 pt fits "M:SS" at caption2 size and prevents the Dynamic Island from
/// squeezing the slot narrower than the text needs.
private static let compactTrailingTimerWidth: CGFloat = 44
/// Maximum width for non-timer compact trailing content (criticalText, progress %).
private static let compactTrailingMaxWidth: CGFloat = 50
var body: some View {
if state.chronometer == true, let end = state.countdownEnd {
Text(timerInterval: Date.now ... end, countsDown: true)
.font(.caption2)
.foregroundStyle(.white)
.monospacedDigit()
.contentTransition(.numericText(countsDown: true))
.frame(width: Self.compactTrailingTimerWidth)
} else if let critical = state.criticalText {
Text(critical)
.font(.caption2)
.foregroundStyle(.white)
.lineLimit(1)
.frame(maxWidth: Self.compactTrailingMaxWidth)
} else if let fraction = state.progressFraction {
Text("\(Int(fraction * 100))%")
.font(.caption2)
.foregroundStyle(.white)
.monospacedDigit()
}
}
}
// MARK: - Expanded trailing
@available(iOS 17.2, *)
struct HAExpandedTrailingView: View {
let state: HALiveActivityAttributes.ContentState
var body: some View {
if let fraction = state.progressFraction {
Text("\(Int(fraction * 100))%")
.font(.caption2)
.foregroundStyle(.white)
.monospacedDigit()
} else if let critical = state.criticalText {
Text(critical)
.font(.caption2)
.foregroundStyle(.white)
.lineLimit(1)
}
}
}
// MARK: - Expanded bottom
@available(iOS 17.2, *)
struct HAExpandedBottomView: View {
let state: HALiveActivityAttributes.ContentState
var body: some View {
VStack(spacing: DesignSystem.Spaces.half) {
if state.chronometer == true, let end = state.countdownEnd {
Text(timerInterval: Date.now ... end, countsDown: true)
.font(.body.monospacedDigit())
.foregroundStyle(.white)
.contentTransition(.numericText(countsDown: true))
} else {
Text(state.message)
.font(.subheadline)
.foregroundStyle(.white.opacity(0.85))
.lineLimit(2)
}
if let fraction = state.progressFraction {
ProgressView(value: fraction)
.tint(accentColor)
}
}
}
/// Accent color from ContentState, fallback to Home Assistant primary blue.
private var accentColor: Color {
if let hex = state.color {
return Color(hex: hex)
}
return .haPrimary
}
}
#endif