-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenuBarView.swift
More file actions
265 lines (238 loc) · 7.97 KB
/
MenuBarView.swift
File metadata and controls
265 lines (238 loc) · 7.97 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import SwiftUI
struct MenuBarView: View {
@ObservedObject var bluetoothManager: BluetoothManager
var onSettingsClicked: (() -> Void)?
var body: some View {
VStack(alignment: .leading, spacing: 12) {
// Header
HStack {
Text("Aranet4 Air Quality")
.font(.headline)
Spacer()
ConnectionStatusView(status: bluetoothManager.connectionStatus)
Button(action: {
openSettings()
}) {
Image(systemName: "gear")
.foregroundColor(.secondary)
.imageScale(.medium)
}
.buttonStyle(.plain)
.help("Settings")
}
.padding(.bottom, 4)
Divider()
// Readings
if let reading = bluetoothManager.currentReading {
ReadingsView(reading: reading)
} else {
Text("Device not in range")
.foregroundColor(.secondary)
.frame(maxWidth: .infinity, alignment: .center)
.padding()
}
// Error message
if let error = bluetoothManager.errorMessage {
HStack {
Image(systemName: "exclamationmark.triangle")
.foregroundColor(.orange)
Text(error)
.font(.caption)
.foregroundColor(.secondary)
}
.padding(8)
.background(Color.orange.opacity(0.15))
.cornerRadius(6)
}
Divider()
// Last updated
if let lastUpdated = bluetoothManager.lastUpdated {
HStack {
Image(systemName: "clock")
.foregroundColor(.secondary)
Text("Updated \(timeAgo(lastUpdated))")
.font(.caption)
.foregroundColor(.secondary)
}
}
// Actions
HStack(spacing: 12) {
Button(action: {
if bluetoothManager.connectionStatus == .connected {
bluetoothManager.refreshReadings()
} else {
bluetoothManager.startScanning()
}
}) {
HStack {
Image(systemName: bluetoothManager.connectionStatus == .connected ? "arrow.clockwise" : "magnifyingglass")
Text(bluetoothManager.connectionStatus == .connected ? "Refresh" : "Scan")
}
.frame(maxWidth: .infinity)
}
.buttonStyle(.bordered)
.disabled(bluetoothManager.connectionStatus == .scanning || bluetoothManager.connectionStatus == .connecting)
Button(action: {
bluetoothManager.sendTestNotification()
}) {
HStack {
Image(systemName: "bell")
Text("Test Alert")
}
.frame(maxWidth: .infinity)
}
.buttonStyle(.bordered)
}
Button(action: {
NSApplication.shared.terminate(nil)
}) {
HStack {
Image(systemName: "xmark.circle")
Text("Quit")
}
.frame(maxWidth: .infinity)
}
.buttonStyle(.bordered)
}
.padding()
.frame(width: 300)
}
private func timeAgo(_ date: Date) -> String {
let seconds = Int(Date().timeIntervalSince(date))
if seconds < 60 {
return "just now"
} else if seconds < 3600 {
let minutes = seconds / 60
return "\(minutes) minute\(minutes == 1 ? "" : "s") ago"
} else {
let hours = seconds / 3600
return "\(hours) hour\(hours == 1 ? "" : "s") ago"
}
}
private func openSettings() {
onSettingsClicked?()
}
}
struct ReadingsView: View {
let reading: Aranet4Reading
var body: some View {
VStack(spacing: 12) {
// CO2 - most important
HStack {
VStack(alignment: .leading, spacing: 4) {
Text("CO2")
.font(.caption)
.foregroundColor(.secondary)
HStack(alignment: .firstTextBaseline, spacing: 4) {
Text("\(reading.co2)")
.font(.system(size: 32, weight: .bold))
Text("ppm")
.font(.caption)
.foregroundColor(.secondary)
Text(reading.co2Status.color)
.font(.title2)
}
}
Spacer()
}
.padding()
.background(co2Background(reading.co2Status))
.cornerRadius(8)
// Other readings
LazyVGrid(columns: [GridItem(.flexible()), GridItem(.flexible())], spacing: 12) {
ReadingCard(
icon: "thermometer",
label: "Temperature",
value: String(format: "%.1f°C", reading.temperature)
)
ReadingCard(
icon: "humidity",
label: "Humidity",
value: "\(reading.humidity)%"
)
ReadingCard(
icon: "gauge",
label: "Pressure",
value: String(format: "%.1f hPa", reading.pressure)
)
ReadingCard(
icon: "battery.100",
label: "Battery",
value: "\(reading.battery)%"
)
}
}
}
private func co2Background(_ status: CO2Level) -> Color {
switch status {
case .good:
return Color.green.opacity(0.12)
case .moderate:
return Color.yellow.opacity(0.12)
case .poor:
return Color.red.opacity(0.12)
}
}
}
struct ReadingCard: View {
let icon: String
let label: String
let value: String
var body: some View {
VStack(alignment: .leading, spacing: 4) {
HStack {
Image(systemName: icon)
.foregroundColor(.secondary)
.font(.caption)
Text(label)
.font(.caption)
.foregroundColor(.secondary)
}
Text(value)
.font(.system(size: 18, weight: .semibold))
}
.frame(maxWidth: .infinity, alignment: .leading)
.padding(10)
.background(Color.secondary.opacity(0.08))
.cornerRadius(6)
}
}
struct ConnectionStatusView: View {
let status: ConnectionStatus
var body: some View {
HStack(spacing: 4) {
Circle()
.fill(statusColor)
.frame(width: 8, height: 8)
Text(statusText)
.font(.caption)
.foregroundColor(.secondary)
}
}
private var statusColor: Color {
switch status {
case .disconnected:
return .red
case .scanning, .connecting:
return .orange
case .connected:
return .green
case .notFound:
return .gray
}
}
private var statusText: String {
switch status {
case .disconnected:
return "Disconnected"
case .scanning:
return "Scanning"
case .connecting:
return "Connecting"
case .connected:
return "Connected"
case .notFound:
return "Not in Range"
}
}
}