-
-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathColorTemperaturePickerRowView.swift
More file actions
329 lines (288 loc) · 12.7 KB
/
ColorTemperaturePickerRowView.swift
File metadata and controls
329 lines (288 loc) · 12.7 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// Copyright (c) 2010-2026 Contributors to the openHAB project
//
// See the NOTICE file(s) distributed with this work for additional
// information.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0
//
// SPDX-License-Identifier: EPL-2.0
import CommonUI
import OpenHABCore
import os.log
import SFSafeSymbols
import SwiftUI
enum ColorTemperatureRowMath {
static let minimumKelvin = 1_000.0
static let maximumKelvin = 10_000.0
static let warmWhiteKelvin = 2_700.0
static let minimumGradientSteps = 1
static let minimumSliderStep = 1.0
static let minimumValidSpan = 0.0
static let minimumConvertibleKelvin = 0.0
static let fractionLowerBound = 0.0
static let fractionUpperBound = 1.0
static let fallbackRange: ClosedRange<Double> = minimumKelvin ... maximumKelvin
static let fallbackTemperature = warmWhiteKelvin
static func normalizedRange(minValue: Double, maxValue: Double) -> ClosedRange<Double> {
let lowerCandidate = sanitizeTemperatureValue(minValue) ?? fallbackRange.lowerBound
let upperCandidate = sanitizeTemperatureValue(maxValue) ?? fallbackRange.upperBound
let lower = min(lowerCandidate, upperCandidate).clamped(to: fallbackRange)
let upper = max(lowerCandidate, upperCandidate).clamped(to: fallbackRange)
guard lower.isFinite, upper.isFinite, lower < upper else {
return fallbackRange
}
return lower ... upper
}
static func normalizedTemperature(state: String?, serverValue: Double?, range: ClosedRange<Double>) -> Double {
let resolvedValue = state.flatMap { parseTemperature(state: $0) }
?? sanitizeTemperatureValue(serverValue)
?? fallbackTemperature
return resolvedValue.clamped(to: range)
}
static func gradientTemperatures(for range: ClosedRange<Double>, steps: Int) -> [Double] {
let safeSteps = max(steps, minimumGradientSteps)
let span = range.upperBound - range.lowerBound
return (0 ... safeSteps).map { index in
range.lowerBound + (span * Double(index) / Double(safeSteps))
}
}
static func sliderFraction(value: Double, range: ClosedRange<Double>) -> Double {
let span = range.upperBound - range.lowerBound
guard span.isFinite, span > minimumValidSpan else { return fractionLowerBound }
let fraction = (value - range.lowerBound) / span
return fraction.clamped(to: fractionLowerBound ... fractionUpperBound)
}
static func sanitizeTemperatureValue(_ value: Double?) -> Double? {
guard let value, value.isFinite, value > minimumConvertibleKelvin else { return nil }
let convertedValue = value < fallbackRange.lowerBound ? value.asColorTemperatureInKelvin : value
return convertedValue.isFinite ? convertedValue : nil
}
private static func parseTemperature(state: String) -> Double? {
guard !state.isEmpty else { return nil }
return sanitizeTemperatureValue(state.parseAsNumber().value)
}
}
private struct ColorTemperatureRowConfig {
let input: ColorTemperatureRowInput
let viewModel: SitemapPageViewModel
}
@MainActor
private func makeColorTemperatureRowContent(_ config: ColorTemperatureRowConfig) -> ColorTemperaturePickerRowContent {
ColorTemperaturePickerRowContent(
input: config.input,
iconInput: config.input.icon
) { command, key in
guard let itemName = config.input.itemName else { return }
config.viewModel.sendCommand(
command,
for: itemName,
policy: WidgetCommandDefaults.slider,
key: key
)
} onCancelPending: { key in
guard let itemName = config.input.itemName else { return }
config.viewModel.cancelPendingCommand(for: itemName, key: key)
}
}
struct CustomSliderView: View {
@Binding var value: Double
let range: ClosedRange<Double>
let step: Double
let onEditingChanged: () -> Void
let onDragStateChanged: (Bool) -> Void
@State private var lastSendTime: Date = .distantPast
@State private var isDragging = false
var body: some View {
GeometryReader { geometry in
let width = geometry.size.width
let height = geometry.size.height
let normalized = CGFloat(ColorTemperatureRowMath.sliderFraction(value: value, range: range))
let xPos = normalized * width
ZStack(alignment: .leading) {
Color.clear
Circle()
.frame(width: 20, height: 20)
.foregroundStyle(.white)
.shadow(radius: 1)
.overlay(Circle().stroke(Color.gray.opacity(0.6), lineWidth: 1))
.position(x: xPos, y: height / 2)
.gesture(
DragGesture()
.onChanged { gesture in
if !isDragging {
isDragging = true
onDragStateChanged(true)
}
guard width > ColorTemperatureRowMath.minimumValidSpan else { return }
let location = gesture.location.x.clamped(to: 0 ... width)
let raw = Double(location / width) * (range.upperBound - range.lowerBound) + range.lowerBound
let safeStep = max(step, ColorTemperatureRowMath.minimumSliderStep)
let stepped = (raw / safeStep).rounded() * safeStep
value = stepped.clamped(to: range)
let now = Date()
if now.timeIntervalSince(lastSendTime) > 0.2 {
lastSendTime = now
onEditingChanged()
}
}
.onEnded { _ in
isDragging = false
onDragStateChanged(false)
onEditingChanged()
}
)
}
}
}
}
private struct ColorTemperaturePickerRowContent: View {
let input: ColorTemperatureRowInput
let iconInput: RowIconInput
let onSendCommand: (String, String) -> Void
let onCancelPending: (String) -> Void
@State private var selectedTemperature: Double = 2700 // Default warm white
@State private var isDraggingSlider = false
@State private var suppressNextServerSync = false
private let logger = Logger(subsystem: "org.openhab", category: "ColorTemperaturePickerRowView")
private var temperatureRange: ClosedRange<Double> {
ColorTemperatureRowMath.normalizedRange(minValue: input.minValue, maxValue: input.maxValue)
}
var body: some View {
let displayState = input.displayState
RowViewWithIcon(input: input, alignment: .top) {
VStack(spacing: 6) {
HStack(alignment: .firstTextBaseline, spacing: 0) {
if !displayState.labelText.isEmpty {
Text(displayState.labelText)
.ohTextToken(.rowLabel)
.foregroundStyle(input.labelColor.isEmpty ? .primary : Color(fromString: input.labelColor))
}
Spacer(minLength: 0)
// Temperature value display
HStack(spacing: 0) {
Text("\(Int(selectedTemperature))K")
.ohTextToken(.rowValueCompact)
.foregroundStyle(.secondary)
.monospacedDigit()
Text(" - ")
.ohTextToken(.rowValueCompact)
.foregroundStyle(.secondary)
Text(temperatureDescription)
.ohTextToken(.secondary)
.foregroundStyle(.secondary)
}
}
.padding(.top, 6)
// Color temperature slider with gradient background
HStack {
// Warm indicator
Image(systemSymbol: .sunMinFill)
.foregroundStyle(.orange)
.ohTextToken(.secondary)
// Slider with custom gradient track
ZStack(alignment: .leading) {
// Gradient background representing color temperature spectrum
// Using realistic color temperature colors like Android app
LinearGradient(
colors: colorTemperatureGradient(),
startPoint: .leading,
endPoint: .trailing
)
.frame(height: 10)
.clipShape(.rect(cornerRadius: 3))
// Actual slider
CustomSliderView(
value: $selectedTemperature,
range: temperatureRange,
step: 100,
onEditingChanged: {
sendTemperatureCommand()
},
onDragStateChanged: { isDragging in
isDraggingSlider = isDragging
}
)
.frame(height: 28)
.disabled(input.readOnly)
}
// Cool indicator
Image(systemSymbol: .snowflake)
.foregroundStyle(.blue)
.ohTextToken(.secondary)
}
}
}
.onAppear {
selectedTemperature = loadCurrentTemperature(state: displayState.effectiveState)
}
.onChange(of: displayState.effectiveState) { newState in
guard !isDraggingSlider else { return }
if suppressNextServerSync {
suppressNextServerSync = false
return
}
selectedTemperature = loadCurrentTemperature(state: newState)
}
.onDisappear {
onCancelPending(input.colorTemperatureCommandKey)
}
}
private var temperatureDescription: String {
switch selectedTemperature {
case 1000 ..< 2000: String(localized: "Candlelight")
case 2000 ..< 2700: String(localized: "Very Warm")
case 2700 ..< 3000: String(localized: "Warm White")
case 3000 ..< 4000: String(localized: "Soft White")
case 4000 ..< 5000: String(localized: "Cool White")
case 5000 ..< 6500: String(localized: "Daylight")
case 6500 ..< 8000: String(localized: "Cool Daylight")
default: String(localized: "Very Cool")
}
}
// Generate gradient colors similar to Android implementation
private func colorTemperatureGradient(steps: Int = 20) -> [Color] {
ColorTemperatureRowMath.gradientTemperatures(for: temperatureRange, steps: steps).map { Color(temperature: $0) }
}
private func loadCurrentTemperature(state: String?) -> Double {
ColorTemperatureRowMath.normalizedTemperature(
state: state,
serverValue: input.serverValue,
range: temperatureRange
)
}
private func sendTemperatureCommand() {
// Send temperature directly as Kelvin value (like Android app)
let clampedRoundedTemperature = Int(selectedTemperature.rounded()).clamped(
to: Int(temperatureRange.lowerBound) ... Int(temperatureRange.upperBound)
)
let command = "\(clampedRoundedTemperature)"
logger.info("Sending color temperature command: \(command)K")
suppressNextServerSync = true
onSendCommand(command, input.colorTemperatureCommandKey)
}
}
struct ColorTemperaturePickerRowView: View {
let input: ColorTemperatureRowInput
@EnvironmentObject var viewModel: SitemapPageViewModel
var body: some View {
makeColorTemperatureRowContent(
ColorTemperatureRowConfig(
input: input,
viewModel: viewModel
)
)
}
}
#if DEBUG
#Preview {
let widget = PreviewConstants.openHABSitemapPage!.widgets[13]
VStack {
ColorTemperaturePickerRowView(input: ColorTemperatureRowInput.from(widget: widget))
.padding()
Spacer()
}
.environmentObject(SitemapPageViewModel())
}
#endif