Files
iOS/Sources/Shared/Watch/WatchConfig.swift
Bruno Pantaleão Gonçalves 59758943e0 Simplify watch and carplay database insert (#3348)
<!-- 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-01-21 21:01:35 +01:00

62 lines
1.8 KiB
Swift

import Foundation
import GRDB
public struct WatchConfig: WatchCodable, FetchableRecord, PersistableRecord {
public static var watchConfigId: String { "watch-config" }
public var id = WatchConfig.watchConfigId
public var assist: Assist = .init(showAssist: true)
public var items: [MagicItem] = []
public init(
id: String = UUID().uuidString,
assist: Assist = Assist(showAssist: true),
items: [MagicItem] = []
) {
self.id = id
self.assist = assist
self.items = items
}
public struct Assist: Codable, Equatable {
public var showAssist: Bool
public var serverId: String
public var pipelineId: String
public init(showAssist: Bool, serverId: String = "", pipelineId: String = "") {
self.showAssist = showAssist
self.serverId = serverId
self.pipelineId = pipelineId
}
}
public static func config() throws -> WatchConfig? {
try Current.database.read({ db in
try WatchConfig.fetchOne(db)
})
}
}
public protocol WatchCodable: Codable {
func encodeForWatch() -> Data
static func decodeForWatch(_ data: Data) -> Self?
}
public extension WatchCodable {
func encodeForWatch() -> Data {
do {
return try PropertyListEncoder().encode(self)
} catch {
fatalError("Faield to encode watch config for watch transfer, error: \(error.localizedDescription)")
}
}
static func decodeForWatch(_ data: Data) -> Self? {
do {
return try PropertyListDecoder().decode(Self.self, from: data)
} catch {
Current.Log.error("Failed to decode watch config for watch, error: \(error.localizedDescription)")
return nil
}
}
}