-
Notifications
You must be signed in to change notification settings - Fork 424
Expand file tree
/
Copy pathEntityTileView.swift
More file actions
183 lines (167 loc) · 5.4 KB
/
EntityTileView.swift
File metadata and controls
183 lines (167 loc) · 5.4 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
import HAKit
import Shared
import SwiftUI
/// Pure UI component for displaying an entity tile
/// This view is designed to be reusable across different contexts in the app
///
/// ## Usage Example
/// ```swift
/// // Simple usage with static data
/// EntityTileView(
/// entityName: "Living Room Light",
/// entityState: "On",
/// icon: .lightbulbIcon,
/// iconColor: .yellow,
/// isUnavailable: false,
/// onIconTap: {
/// // Handle icon tap action
/// },
/// onTileTap: {
/// // Handle tile tap action
/// }
/// )
///
/// // Usage in a custom view
/// struct CustomEntityList: View {
/// let entities: [MyCustomEntity]
///
/// var body: some View {
/// ForEach(entities) { entity in
/// EntityTileView(
/// entityName: entity.name,
/// entityState: entity.status,
/// icon: entity.icon,
/// iconColor: entity.color,
/// onIconTap: { performAction(on: entity) }
/// )
/// }
/// }
/// }
/// ```
///
/// For Home Assistant entity integration, use `HomeEntityTileView` instead,
/// which handles all the business logic like device class lookup, icon color
/// computation, and AppIntents integration.
@available(iOS 26.0, *)
struct EntityTileView: View {
enum Constants {
static let tileHeight: CGFloat = 65
static let cornerRadius: CGFloat = 16
static let iconSize: CGFloat = 38
static let iconFontSize: CGFloat = 20
static let iconOpacity: CGFloat = 0.3
static let borderLineWidth: CGFloat = 1
static let textVStackSpacing: CGFloat = 2
}
// MARK: - Display Data
let entityName: String
let entityState: String
let icon: MaterialDesignIcons
let iconColor: Color
let isUnavailable: Bool
let onIconTap: (() -> Void)?
let onTileTap: (() -> Void)?
// MARK: - State
@State private var triggerHaptic = 0
// MARK: - Initializer
init(
entityName: String,
entityState: String,
icon: MaterialDesignIcons,
iconColor: Color,
isUnavailable: Bool = false,
onIconTap: (() -> Void)? = nil,
onTileTap: (() -> Void)? = nil
) {
self.entityName = entityName
self.entityState = entityState
self.icon = icon
self.iconColor = iconColor
self.isUnavailable = isUnavailable
self.onIconTap = onIconTap
self.onTileTap = onTileTap
}
// MARK: - Body
var body: some View {
tileContent
.frame(height: Constants.tileHeight)
.frame(maxWidth: .infinity)
.background(.tileBackground)
.contentShape(RoundedRectangle(cornerRadius: Constants.cornerRadius))
.clipShape(RoundedRectangle(cornerRadius: Constants.cornerRadius))
.overlay(
RoundedRectangle(cornerRadius: Constants.cornerRadius)
.stroke(
isUnavailable ? .gray : .tileBorder,
style: isUnavailable ?
StrokeStyle(lineWidth: Constants.borderLineWidth, dash: [5, 3]) :
StrokeStyle(lineWidth: Constants.borderLineWidth)
)
)
.opacity(isUnavailable ? 0.5 : 1.0)
.onTapGesture {
onTileTap?()
}
}
// MARK: - View Components
private var tileContent: some View {
VStack(alignment: .leading, spacing: DesignSystem.Spaces.one) {
contentRow
.padding([.leading, .trailing], DesignSystem.Spaces.oneAndHalf)
}
}
private var contentRow: some View {
HStack(alignment: .center, spacing: DesignSystem.Spaces.oneAndHalf) {
iconView
entityInfoStack
.frame(maxWidth: .infinity, alignment: .leading)
}
}
private var entityInfoStack: some View {
VStack(alignment: .leading, spacing: Constants.textVStackSpacing) {
entityNameText
entityStateText
}
}
private var entityNameText: some View {
Text(entityName)
.font(.footnote)
.fontWeight(.semibold)
#if os(iOS)
.foregroundColor(Color(uiColor: .label))
#else
.foregroundColor(.primary)
#endif
.lineLimit(2)
.multilineTextAlignment(.leading)
}
private var entityStateText: some View {
Text(entityState)
.font(.caption)
#if os(iOS)
.foregroundColor(Color(uiColor: .secondaryLabel))
#else
.foregroundColor(.secondary)
#endif
.lineLimit(1)
.truncationMode(.tail)
}
private var iconView: some View {
Button {
onIconTap?()
triggerHaptic += 1
} label: {
VStack {
Text(verbatim: icon.unicode)
.font(.custom(MaterialDesignIcons.familyName, size: Constants.iconFontSize))
.foregroundColor(iconColor)
.fixedSize(horizontal: false, vertical: false)
}
.frame(width: Constants.iconSize, height: Constants.iconSize)
.background(iconColor.opacity(Constants.iconOpacity))
.clipShape(Circle())
}
.buttonStyle(.plain)
.sensoryFeedback(.impact, trigger: triggerHaptic)
}
}