iOS/Sources/Shared/Intents/OpenPageIntentHandler.swift
Bruno Pantaleão Gonçalves 63ae3a0506
Fetch panels instead of subscribe (#3275)
<!-- Thank you for submitting a Pull Request and helping to improve Home
Assistant. Please complete the following sections to help the processing
and review of your changes. Please do not delete anything from this
template. -->

## Summary
<!-- Provide a brief summary of the changes you have made and most
importantly what they aim to achieve -->

## Screenshots
<!-- If this is a user-facing change not in the frontend, please include
screenshots in light and dark mode. -->

## Link to pull request in Documentation repository
<!-- Pull requests that add, change or remove functionality must have a
corresponding pull request in the Companion App Documentation repository
(https://github.com/home-assistant/companion.home-assistant). Please add
the number of this pull request after the "#" -->
Documentation: home-assistant/companion.home-assistant#

## Any other notes
<!-- If there is any other information of note, like if this Pull
Request is part of a bigger change, please include it here. -->
2024-12-16 15:25:59 +01:00

83 lines
2.7 KiB
Swift

import Intents
import PromiseKit
public class OpenPageIntentHandler: NSObject, OpenPageIntentHandling, WidgetOpenPageIntentHandling {
public static func cacheKey(serverIdentifier: String) -> String {
"last-invalidated-widget-panels-\(serverIdentifier)"
}
public static func panels(completion: @escaping ([IntentPanel]) -> Void) {
var intentPanels: [IntentPanel] = []
do {
let panelsPerServer = try AppPanel.panelsPerServer()
for (server, panels) in panelsPerServer {
intentPanels.append(contentsOf: panels.map { appPanel in
IntentPanel(
panel: .init(
icon: appPanel.icon,
title: appPanel.title,
path: appPanel.path,
component: appPanel.component,
showInSidebar: appPanel.showInSidebar
),
server: server
)
})
}
completion(intentPanels)
} catch {
Current.Log.error("Widget error fetching panels: \(error)")
completion([])
}
}
private func panelsIntentCollection(completion: @escaping (INObjectCollection<IntentPanel>) -> Void) {
Self.panels { panels in
let sections: [INObjectSection<IntentPanel>] = Current.servers.all.map { server in
.init(
title: server.info.name,
items: panels.filter({ $0.serverIdentifier == server.identifier.rawValue })
)
}
completion(INObjectCollection<IntentPanel>.init(sections: sections))
}
}
public func providePagesOptionsCollection(
for intent: WidgetOpenPageIntent,
with completion: @escaping (INObjectCollection<IntentPanel>?, Error?) -> Void
) {
panelsIntentCollection { collection in
completion(collection, nil)
}
}
public func providePageOptionsCollection(
for intent: OpenPageIntent,
with completion: @escaping (INObjectCollection<IntentPanel>?, Error?) -> Void
) {
panelsIntentCollection { collection in
completion(collection, nil)
}
}
public func providePagesOptions(
for intent: WidgetOpenPageIntent,
with completion: @escaping ([IntentPanel]?, Error?) -> Void
) {
Self.panels { panels in
completion(panels, nil)
}
}
public func providePageOptions(
for intent: OpenPageIntent,
with completion: @escaping ([IntentPanel]?, Error?) -> Swift.Void
) {
Self.panels { panels in
completion(panels, nil)
}
}
}