mirror of
https://github.com/home-assistant/iOS.git
synced 2026-06-19 07:24:05 -05:00
<!-- 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. --> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
88 lines
2.6 KiB
Swift
88 lines
2.6 KiB
Swift
import Combine
|
|
import Foundation
|
|
import GRDB
|
|
import HAKit
|
|
import PromiseKit
|
|
import Shared
|
|
|
|
enum MagicItemAddType {
|
|
case scripts
|
|
case actions
|
|
case scenes
|
|
case entities
|
|
}
|
|
|
|
final class MagicItemAddViewModel: ObservableObject {
|
|
@Published var selectedItemType = MagicItemAddType.scripts
|
|
@Published var scripts: [Server: [HAAppEntity]] = [:]
|
|
@Published var scenes: [Server: [HAAppEntity]] = [:]
|
|
@Published var entities: [Server: [HAAppEntity]] = [:]
|
|
@Published var actions: [Action] = []
|
|
@Published var searchText: String = ""
|
|
@Published var selectedServerId: String?
|
|
|
|
private var entitiesSubscription: AnyCancellable?
|
|
|
|
init() {
|
|
self.entitiesSubscription = $entities.sink { entities in
|
|
var scripts = entities
|
|
for (key, value) in scripts {
|
|
scripts[key] = value.filter({ entity in
|
|
entity.domain == Domain.script.rawValue
|
|
})
|
|
}
|
|
var scenes = entities
|
|
for (key, value) in scenes {
|
|
scenes[key] = value.filter({ entity in
|
|
entity.domain == Domain.scene.rawValue
|
|
})
|
|
}
|
|
DispatchQueue.main.async {
|
|
self.scripts = scripts
|
|
self.scenes = scenes
|
|
}
|
|
}
|
|
}
|
|
|
|
deinit {
|
|
entitiesSubscription?.cancel()
|
|
}
|
|
|
|
@MainActor
|
|
func loadContent() {
|
|
loadAppEntities()
|
|
loadActions()
|
|
}
|
|
|
|
func subtitleForEntity(entity: HAAppEntity, serverId: String) -> String {
|
|
// Fetch area from database based on entity
|
|
do {
|
|
let areas = try AppArea.fetchAreas(containingEntity: entity.entityId, serverId: serverId)
|
|
if let area = areas.first {
|
|
return area.name
|
|
}
|
|
} catch {
|
|
Current.Log.error("Failed to fetch area for entity from database: \(error.localizedDescription)")
|
|
}
|
|
return entity.entityId
|
|
}
|
|
|
|
@MainActor
|
|
private func loadAppEntities() {
|
|
Current.magicItemProvider().loadInformation { [weak self] entities in
|
|
guard let self else { return }
|
|
entities.forEach { key, value in
|
|
guard let server = Current.servers.all.first(where: { $0.identifier.rawValue == key }) else { return }
|
|
self.entities[server] = value
|
|
}
|
|
}
|
|
}
|
|
|
|
@MainActor
|
|
private func loadActions() {
|
|
actions = Current.realm().objects(Action.self)
|
|
.filter({ $0.Scene == nil })
|
|
.sorted(by: { $0.Position < $1.Position })
|
|
}
|
|
}
|