-
Notifications
You must be signed in to change notification settings - Fork 433
Expand file tree
/
Copy pathEntityAddToHandler.swift
More file actions
294 lines (256 loc) · 11 KB
/
EntityAddToHandler.swift
File metadata and controls
294 lines (256 loc) · 11 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
import Foundation
import PromiseKit
@preconcurrency import Shared
import SwiftUI
/// Handles the "Add To" functionality for Home Assistant entities, allowing users to add entities
/// to various iOS platform features and connected devices.
///
/// This class provides two main capabilities:
/// 1. Determining which actions are available for a given entity based on its type and domain
/// 2. Executing the selected action to add the entity to the chosen platform feature
final class EntityAddToHandler {
weak var webViewController: WebViewControllerProtocol?
init(webViewController: WebViewControllerProtocol? = nil) {
self.webViewController = webViewController
}
/// Returns the list of available actions for the specified entity.
///
/// The available actions depend on the entity's domain and current system state.
///
/// - Parameter entityId: The entity ID to get available actions for (e.g., "light.living_room")
/// - Returns: Promise that resolves to a list of actions that can be performed for this entity
func actionsForEntity(entityId: String) -> Promise<[any EntityAddToAction]> {
Promise { seal in
DispatchQueue.global(qos: .userInitiated).async {
var actions: [any EntityAddToAction] = []
// Extract the domain from the entity ID
let domain = Domain(entityId: entityId)
// CarPlay is available on iPhone only (not iPad) for supported domains
#if !targetEnvironment(macCatalyst)
if !Current.isCatalyst, UIDevice.current.userInterfaceIdiom == .phone {
let isCarPlaySupported = domain.map { CarPlaySupportedDomains.all.contains($0) } ?? false
if isCarPlaySupported {
actions.append(CarPlayQuickAccessAction())
}
}
#endif
// Watch is available on iPhone for supported domains
#if os(iOS)
if !Current.isCatalyst {
let isWatchSupported = domain.map { WatchSupportedDomains.all.contains($0) } ?? false
if isWatchSupported {
actions.append(WatchItemAction())
}
}
#endif
// Widgets are available on all platforms
if let domain = Domain(entityId: entityId), HAAppUsedContent.domains.contains(domain) {
actions.append(CustomWidgetAction())
}
seal.fulfill(actions)
}
}
}
/// Executes the specified action to add the entity to the chosen platform feature.
///
/// This function performs the appropriate operation based on the action type.
///
/// - Parameters:
/// - action: The action to execute
/// - entityId: The entity ID to add (e.g., "light.living_room")
/// - Returns: Promise that resolves when the action is executed
func execute(action: any EntityAddToAction, entityId: String) -> Promise<Void> {
Promise { seal in
DispatchQueue.main.async { [weak self] in
guard let self else {
seal.reject(EntityAddToError.handlerDeallocated)
return
}
guard let webViewController else {
seal.reject(EntityAddToError.webViewControllerUnavailable)
return
}
let actionType = EntityAddToActionType(rawValue: action.actionType)
switch actionType {
case .carPlayQuickAccess:
addToCarPlayQuickAccess(entityId: entityId, webViewController: webViewController)
seal.fulfill(())
case .watchItem:
addToWatchItems(entityId: entityId, webViewController: webViewController)
seal.fulfill(())
case .customWidget:
openWidgetBuilder(
actionType: actionType,
entityId: entityId,
webViewController: webViewController
)
seal.fulfill(())
case .none:
seal.reject(EntityAddToError.unknownActionType)
}
}
}
}
// MARK: - Private Methods
private func addToCarPlayQuickAccess(entityId: String, webViewController: WebViewControllerProtocol) {
// Navigate to CarPlay configuration screen
Current.Log.info("Adding entity \(entityId) to CarPlay quick access")
let viewModel = CarPlayConfigurationViewModel(prefilledItem: .init(
id: entityId,
serverId: webViewController.server.identifier.rawValue,
type: .entity
))
let carPlaySettingsView = CarPlayConfigurationView(viewModel: viewModel)
webViewController.presentOverlayController(
controller: carPlaySettingsView.embeddedInHostingController(),
animated: true
)
}
private func addToWatchItems(entityId: String, webViewController: WebViewControllerProtocol) {
// Navigate to Watch configuration screen
Current.Log.info("Adding entity \(entityId) to Watch")
let viewModel = WatchConfigurationViewModel(prefilledItem: .init(
id: entityId,
serverId: webViewController.server.identifier.rawValue,
type: .entity
))
let watchSettingsView = WatchConfigurationView(needsNavigationController: true, viewModel: viewModel)
.preferredColorScheme(.dark)
let viewController = watchSettingsView.embeddedInHostingController()
viewController.overrideUserInterfaceStyle = .dark
webViewController.presentOverlayController(controller: viewController, animated: true)
}
private func openWidgetBuilder(
actionType: EntityAddToActionType?,
entityId: String,
webViewController: WebViewControllerProtocol
) {
Current.Log.info("Opening widget selection for entity \(entityId)")
let serverId = webViewController.server.identifier.rawValue
let selectionView = WidgetSelectionView(
entityId: entityId,
serverId: serverId
) { [weak self] selectedWidget in
self?.handleWidgetSelection(
widget: selectedWidget,
entityId: entityId,
serverId: serverId,
webViewController: webViewController
)
}
.modify { view in
if Current.isCatalyst {
view.toolbar(content: {
ToolbarItem(placement: .topBarLeading) {
CloseButton {
webViewController.dismissOverlayController(animated: true, completion: nil)
}
}
})
} else {
view
}
}
let hostingController = selectionView.embeddedInHostingController()
if Current.isCatalyst {
let navigationController = UINavigationController(rootViewController: hostingController)
webViewController.presentOverlayController(controller: navigationController, animated: true)
} else {
// Present as a bottom sheet
if let sheet = hostingController.sheetPresentationController {
sheet.detents = [.medium(), .large()]
sheet.prefersGrabberVisible = true
sheet.prefersScrollingExpandsWhenScrolledToEdge = false
}
webViewController.presentOverlayController(controller: hostingController, animated: true)
}
}
private func handleWidgetSelection(
widget: CustomWidget?,
entityId: String,
serverId: String,
webViewController: WebViewControllerProtocol
) {
// Small delay to allow the selection sheet to dismiss
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
if let widget {
// Add entity to existing widget
self.addEntityToWidget(
widget: widget,
entityId: entityId,
serverId: serverId,
webViewController: webViewController
)
} else {
// Create new widget with the entity pre-filled
self.createNewWidgetWithEntity(
entityId: entityId,
serverId: serverId,
webViewController: webViewController
)
}
}
}
private func addEntityToWidget(
widget: CustomWidget,
entityId: String,
serverId: String,
webViewController: WebViewControllerProtocol
) {
Current.Log.info("Adding entity \(entityId) to widget '\(widget.name)'")
// Create a new MagicItem for the entity
let newItem = MagicItem(
id: entityId,
serverId: serverId,
type: .entity
)
// Create updated widget with the new item
var updatedWidget = widget
updatedWidget.items.append(newItem)
// Save to database
do {
try Current.database().write { db in
try updatedWidget.update(db)
}
// Open the widget creation view to let user see and further customize
let widgetCreationView = WidgetCreationView(widget: updatedWidget) {
// Reload widgets after changes
}
let hostingController = widgetCreationView
.embeddedInHostingController()
webViewController.presentOverlayController(controller: hostingController, animated: true)
} catch {
Current.Log.error("Failed to add entity to widget: \(error.localizedDescription)")
}
}
private func createNewWidgetWithEntity(
entityId: String,
serverId: String,
webViewController: WebViewControllerProtocol
) {
Current.Log.info("Creating new widget with entity \(entityId)")
// Create a new widget with the entity pre-filled
let newItem = MagicItem(
id: entityId,
serverId: serverId,
type: .entity
)
let newWidget = CustomWidget(
id: UUID().uuidString,
name: "",
items: [newItem]
)
let widgetCreationView = WidgetCreationView(widget: newWidget) {
// Reload widgets after changes
}
let hostingController = widgetCreationView
.embeddedInHostingController()
webViewController.presentOverlayController(controller: hostingController, animated: true)
}
}
// MARK: - Error Types
extension EntityAddToError {
static let handlerDeallocated = EntityAddToError.decodingFailed
static let webViewControllerUnavailable = EntityAddToError.decodingFailed
static let unknownActionType = EntityAddToError.invalidPayload
}