-
-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathEmbeddingRowInputView.swift
More file actions
205 lines (188 loc) · 7.27 KB
/
EmbeddingRowInputView.swift
File metadata and controls
205 lines (188 loc) · 7.27 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
// 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 SwiftUI
enum RowBackgroundKind: Equatable {
case frame
case regular
}
enum RowLayoutPolicy {
static let regularInsets = EdgeInsets(top: 4, leading: 16, bottom: 4, trailing: 16)
static func rowInsets(for rowInput: SitemapRowInput) -> EdgeInsets {
switch rowInput {
case let .frame(_, input):
return frameInsets(hasLabel: !input.displayState.labelText.isEmpty)
case let .linked(_, input):
if input.isFrame {
return frameInsets(hasLabel: !input.displayState.labelText.isEmpty)
}
return regularInsets
case .text,
.slider,
.selection,
.segmented,
.setpoint,
.rollershutter,
.toggle,
.input,
.colorPicker,
.media,
.colorTemperature,
.buttonGrid,
.generic:
return regularInsets
}
}
static func backgroundKind(for rowInput: SitemapRowInput) -> RowBackgroundKind {
switch rowInput {
case .frame:
.frame
case let .linked(_, input):
input.isFrame ? .frame : .regular
case .text,
.slider,
.selection,
.segmented,
.setpoint,
.rollershutter,
.toggle,
.input,
.colorPicker,
.media,
.colorTemperature,
.buttonGrid,
.generic:
.regular
}
}
private static func frameInsets(hasLabel: Bool) -> EdgeInsets {
hasLabel
? EdgeInsets(top: 6, leading: 16, bottom: 6, trailing: 16)
: EdgeInsets(top: 0, leading: 16, bottom: 0, trailing: 16)
}
}
private struct LinkedPageRowInputView: View {
let input: LinkedPageRowInput
var body: some View {
NavigationLink(value: input.destination) {
LinkedPageRowContent(input: input)
}
.buttonStyle(.plain)
}
}
private struct LinkedPageRowContent: View {
let input: LinkedPageRowInput
var body: some View {
let displayState = input.displayState
HStack {
IconInputView(input: input.icon, rowIdentity: input.destination.pageUrl, size: CGSize(width: 32, height: 32))
Text(displayState.labelText)
.ohTextToken(.rowLabel)
.foregroundStyle(input.labelColor.isEmpty ? .primary : Color(fromString: input.labelColor))
Spacer()
if let value = displayState.labelValue {
Text(value)
.ohTextToken(.rowValue)
.foregroundStyle(input.valueColor.isEmpty ? .secondary : Color(fromString: input.valueColor))
}
}
}
}
/// Transitional adapter: drives list from immutable row inputs while reusing existing widget-driven rows.
struct EmbeddingRowInputView: View {
let rowInput: SitemapRowInput
private var regularRowBackground: Color {
Color(UIColor.ohSecondarySystemGroupedBackground)
}
private var frameRowBackground: Color {
Color(UIColor.ohSystemGroupedBackground)
}
var body: some View {
switch rowInput {
case let .frame(_, input):
FrameRowView(input: input)
.contentShape(Rectangle())
.listRowInsets(RowLayoutPolicy.rowInsets(for: rowInput))
.listRowBackground(frameRowBackground)
case let .linked(_, input):
LinkedPageRowInputView(input: input)
.contentShape(Rectangle())
.listRowInsets(RowLayoutPolicy.rowInsets(for: rowInput))
.listRowBackground(RowLayoutPolicy.backgroundKind(for: rowInput) == .frame ? frameRowBackground : regularRowBackground)
case let .slider(_, input):
SliderRowView(input: input)
.contentShape(Rectangle())
.listRowInsets(RowLayoutPolicy.regularInsets)
.listRowBackground(regularRowBackground)
case let .selection(_, input):
SelectionRowView(input: input)
.contentShape(Rectangle())
.listRowInsets(RowLayoutPolicy.regularInsets)
.listRowBackground(regularRowBackground)
case let .segmented(_, input):
SegmentedRowView(input: input)
.contentShape(Rectangle())
.listRowInsets(RowLayoutPolicy.regularInsets)
.listRowBackground(regularRowBackground)
case let .setpoint(_, input):
SetpointRowView(input: input)
.contentShape(Rectangle())
.listRowInsets(RowLayoutPolicy.regularInsets)
.listRowBackground(regularRowBackground)
case let .text(_, input):
TextRowView(input: input)
.contentShape(Rectangle())
.listRowInsets(RowLayoutPolicy.regularInsets)
.listRowBackground(regularRowBackground)
case let .toggle(_, input):
SwitchRowView(input: input)
.contentShape(Rectangle())
.listRowInsets(RowLayoutPolicy.regularInsets)
.listRowBackground(regularRowBackground)
case let .rollershutter(_, input):
RollershutterRowView(input: input)
.contentShape(Rectangle())
.listRowInsets(RowLayoutPolicy.regularInsets)
.listRowBackground(regularRowBackground)
case let .input(_, input):
InputRowView(input: input)
.contentShape(Rectangle())
.listRowInsets(RowLayoutPolicy.regularInsets)
.listRowBackground(regularRowBackground)
case let .colorPicker(_, input):
ColorPickerRowView(input: input)
.contentShape(Rectangle())
.listRowInsets(RowLayoutPolicy.regularInsets)
.listRowBackground(regularRowBackground)
case let .media(_, input):
MediaRowView(input: input)
.contentShape(Rectangle())
.listRowInsets(RowLayoutPolicy.regularInsets)
.listRowBackground(regularRowBackground)
case let .colorTemperature(_, input):
ColorTemperaturePickerRowView(input: input)
.contentShape(Rectangle())
.listRowInsets(RowLayoutPolicy.regularInsets)
.listRowBackground(regularRowBackground)
case let .buttonGrid(_, input):
ButtonGridRowView(input: input)
.contentShape(Rectangle())
.listRowInsets(RowLayoutPolicy.regularInsets)
.listRowBackground(regularRowBackground)
case let .generic(_, input):
GenericRowView(input: input)
.contentShape(Rectangle())
.listRowInsets(RowLayoutPolicy.regularInsets)
.listRowBackground(regularRowBackground)
}
}
}