Files
iOS/Sources/App/Settings/AppleWatch/HomeCustomization/WatchConfigurationViewModel.swift
Bruno Pantaleão Gonçalves 9b1922fad6 Fix Assist pipeline picker for watch configuration (#3915)
<!-- 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. -->
2025-10-27 14:25:00 +01:00

133 lines
4.4 KiB
Swift

import Foundation
import GRDB
import PromiseKit
import Shared
final class WatchConfigurationViewModel: ObservableObject {
@Published var watchConfig = WatchConfig()
@Published var showAddItem = false
@Published var showError = false
@Published private(set) var errorMessage: String?
@Published var assistPipelines: [Pipeline] = []
@Published var servers: [Server] = []
private let magicItemProvider = Current.magicItemProvider()
@MainActor
func loadWatchConfig() {
servers = Current.servers.all
magicItemProvider.loadInformation { [weak self] _ in
guard let self else { return }
loadDatabase()
}
}
func magicItemInfo(for item: MagicItem) -> MagicItem.Info? {
magicItemProvider.getInfo(for: item)
}
func addItem(_ item: MagicItem) {
watchConfig.items.append(item)
}
func updateItem(_ item: MagicItem) {
if let indexToUpdate = watchConfig.items
.firstIndex(where: { $0.id == item.id && $0.serverId == item.serverId }) {
watchConfig.items.remove(at: indexToUpdate)
watchConfig.items.insert(item, at: indexToUpdate)
}
}
func deleteItem(at offsets: IndexSet) {
watchConfig.items.remove(atOffsets: offsets)
}
func moveItem(from source: IndexSet, to destination: Int) {
watchConfig.items.move(fromOffsets: source, toOffset: destination)
}
func deleteConfiguration(completion: (Bool) -> Void) {
do {
try Current.database().write { db in
try WatchConfig.deleteAll(db)
completion(true)
}
} catch {
showError(message: L10n.Watch.Debug.DeleteDb.Alert.Failed.message(error.localizedDescription))
}
}
@MainActor
func save(completion: (Bool) -> Void) {
do {
try Current.database().write { db in
if watchConfig.id != WatchConfig.watchConfigId {
// Previous config needs to be explicit deleted because when WatchConfig was released
// the ID wasn't static, so it was possible to have multiple rows in the table
try WatchConfig.deleteAll(db)
watchConfig.id = WatchConfig.watchConfigId
}
try watchConfig.insert(db, onConflict: .replace)
completion(true)
}
} catch {
Current.Log.error("Failed to save new Watch config, error: \(error.localizedDescription)")
showError(message: L10n.Grdb.Config.MigrationError.failedToSave(error.localizedDescription))
completion(false)
}
}
@MainActor
private func loadDatabase() {
do {
if let config = try WatchConfig.config() {
setConfig(config)
Current.Log.info("Watch configuration exists")
} else {
Current.Log.error("No watch config found")
convertLegacyActionsToWatchConfig()
}
} catch {
Current.Log.error("Failed to access database (GRDB), error: \(error.localizedDescription)")
showError(message: L10n.Grdb.Config.MigrationError.failedAccessGrdb(error.localizedDescription))
}
}
private func setConfig(_ config: WatchConfig) {
DispatchQueue.main.async { [weak self] in
self?.watchConfig = config
}
}
@MainActor
private func convertLegacyActionsToWatchConfig() {
var newWatchConfig = WatchConfig()
let actions = Current.realm().objects(Action.self).sorted(by: { $0.Position < $1.Position })
.filter(\.showInWatch)
guard !actions.isEmpty else { return }
let newWatchActionItems = actions.map { action in
MagicItem(id: action.ID, serverId: action.serverIdentifier, type: .action)
}
newWatchConfig.items = newWatchActionItems
DispatchQueue.main.async { [weak self] in
guard let self else { return }
watchConfig = newWatchConfig
save { success in
if !success {
Current.Log.error("Failed to migrate actions to watch config, failed to save config.")
}
}
}
}
private func showError(message: String) {
DispatchQueue.main.async { [weak self] in
self?.errorMessage = message
self?.showError = true
}
}
}