Skip to content

Commit

Permalink
Fix "View ID is not encodable" on WidgetBasicViewModel (#2738)
Browse files Browse the repository at this point in the history
  • Loading branch information
bgoncal committed Apr 25, 2024
1 parent 995df18 commit a3e10a5
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 3 deletions.
10 changes: 10 additions & 0 deletions HomeAssistant.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,9 @@
4296C37A2B9205450051B63C /* WidgetActionsAppIntent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4296C3792B9205450051B63C /* WidgetActionsAppIntent.swift */; };
4296C37B2B92054C0051B63C /* WidgetActionsAppIntent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4296C3792B9205450051B63C /* WidgetActionsAppIntent.swift */; };
429C72202B28D0EC00BCD558 /* Haptics.swift in Sources */ = {isa = PBXBuildFile; fileRef = 429C721F2B28D0EC00BCD558 /* Haptics.swift */; };
42A3B63B2BD91854007BC0F3 /* Color+Codable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42A3B63A2BD91854007BC0F3 /* Color+Codable.swift */; };
42A3B63C2BD91891007BC0F3 /* Color+Codable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42A3B63A2BD91854007BC0F3 /* Color+Codable.swift */; };
42A3B63E2BD918D6007BC0F3 /* MaterialDesignIcons+Encodable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42A3B63D2BD918D6007BC0F3 /* MaterialDesignIcons+Encodable.swift */; };
42A818E02BBEA8150083D045 /* AssistViewModel.test.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42A818DF2BBEA8150083D045 /* AssistViewModel.test.swift */; };
42A818E32BBEA9780083D045 /* MockAudioRecorder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42A818E22BBEA9780083D045 /* MockAudioRecorder.swift */; };
42A818E52BBEAA3A0083D045 /* MockAudioPlayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42A818E42BBEAA3A0083D045 /* MockAudioPlayer.swift */; };
Expand Down Expand Up @@ -1737,6 +1740,8 @@
4296C3742B91F0860051B63C /* WidgetActionsAppIntentTimelineProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WidgetActionsAppIntentTimelineProvider.swift; sourceTree = "<group>"; };
4296C3792B9205450051B63C /* WidgetActionsAppIntent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = WidgetActionsAppIntent.swift; path = Sources/Extensions/AppIntents/WidgetActionsAppIntent.swift; sourceTree = SOURCE_ROOT; };
429C721F2B28D0EC00BCD558 /* Haptics.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Haptics.swift; sourceTree = "<group>"; };
42A3B63A2BD91854007BC0F3 /* Color+Codable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Color+Codable.swift"; sourceTree = "<group>"; };
42A3B63D2BD918D6007BC0F3 /* MaterialDesignIcons+Encodable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MaterialDesignIcons+Encodable.swift"; sourceTree = "<group>"; };
42A818DF2BBEA8150083D045 /* AssistViewModel.test.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AssistViewModel.test.swift; sourceTree = "<group>"; };
42A818E22BBEA9780083D045 /* MockAudioRecorder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockAudioRecorder.swift; sourceTree = "<group>"; };
42A818E42BBEAA3A0083D045 /* MockAudioPlayer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockAudioPlayer.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3246,6 +3251,8 @@
42C3737E2BC415AC00898990 /* UIViewController+Extensions.swift */,
42FCCFA72B9A05400057783F /* View+RoundedCorner.swift */,
421B1C1B2BD65BFA001ED18C /* View+ConditionalModifier.swift */,
42A3B63A2BD91854007BC0F3 /* Color+Codable.swift */,
42A3B63D2BD918D6007BC0F3 /* MaterialDesignIcons+Encodable.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -6210,6 +6217,7 @@
11C9E43C2505B04E00492A88 /* HACoreAudioObjectSystem.swift in Sources */,
11F2F1ED2586ED6100F61F7C /* NotificationAttachmentManager.swift in Sources */,
3997926F2B7F907B00231B54 /* MobileAppConfigPush.swift in Sources */,
42A3B63C2BD91891007BC0F3 /* Color+Codable.swift in Sources */,
117675F0252D5CA80047B1D3 /* WebhookResponseUpdateComplications.swift in Sources */,
B67CE8AE22200F220034C1D0 /* TokenManager.swift in Sources */,
11BA5ECA2759AC0300FC40E8 /* XCGLogger+Export.swift in Sources */,
Expand Down Expand Up @@ -6470,6 +6478,7 @@
11AF4D1C249C8AA0006C74C0 /* BatterySensor.swift in Sources */,
D014EEA92128E192008EA6F5 /* ConnectionInfo.swift in Sources */,
11169BC8262BE460005EF90A /* UNNotificationContent+Additions.swift in Sources */,
42A3B63B2BD91854007BC0F3 /* Color+Codable.swift in Sources */,
11AF4D14249C7E09006C74C0 /* ActivitySensor.swift in Sources */,
429C72202B28D0EC00BCD558 /* Haptics.swift in Sources */,
11B38EE9275C54A200205C7B /* GetCameraImageIntentHandler.swift in Sources */,
Expand Down Expand Up @@ -6552,6 +6561,7 @@
B6C091232151F90300A326DC /* LocationHistory.swift in Sources */,
D0BE440A2104224600C74314 /* TokenInfo.swift in Sources */,
119EC3C724D5119300617D51 /* MobileAppConfigAction.swift in Sources */,
42A3B63E2BD918D6007BC0F3 /* MaterialDesignIcons+Encodable.swift in Sources */,
B658AA7E2250B2A000C9BFE3 /* MobileAppUpdateRegistrationRequest.swift in Sources */,
42F5CAE52B10CDC600409816 /* HACornerRadius.swift in Sources */,
1182620724F9C492000795C6 /* HACoreMediaObjectCamera.swift in Sources */,
Expand Down
6 changes: 3 additions & 3 deletions Sources/Extensions/Widgets/Common/WidgetBasicView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Foundation
import Shared
import SwiftUI

struct WidgetBasicViewModel: Identifiable, Hashable {
struct WidgetBasicViewModel: Identifiable, Hashable, Encodable {
init(
id: String,
title: String,
Expand Down Expand Up @@ -39,12 +39,12 @@ struct WidgetBasicViewModel: Identifiable, Hashable {
var textColor: Color
var iconColor: Color

enum InteractionType: Hashable {
enum InteractionType: Hashable, Encodable {
case widgetURL(URL)
case appIntent(WidgetIntentType)
}

enum WidgetIntentType: Hashable {
enum WidgetIntentType: Hashable, Encodable {
case action(id: String, name: String)
}
}
Expand Down
60 changes: 60 additions & 0 deletions Sources/Shared/Extensions/Color+Codable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#if os(iOS)
import UIKit
#elseif os(watchOS)
import WatchKit
#elseif os(macOS)
import AppKit
#endif
import SwiftUI

private extension Color {
#if os(macOS)
typealias SystemColor = NSColor
#else
typealias SystemColor = UIColor
#endif

var colorComponents: (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)? {
var r: CGFloat = 0
var g: CGFloat = 0
var b: CGFloat = 0
var a: CGFloat = 0

#if os(macOS)
SystemColor(self).getRed(&r, green: &g, blue: &b, alpha: &a)
#else
guard SystemColor(self).getRed(&r, green: &g, blue: &b, alpha: &a) else {
return nil
}
#endif

return (r, g, b, a)
}
}

extension Color: Codable {
enum CodingKeys: String, CodingKey {
case red, green, blue
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let r = try container.decode(Double.self, forKey: .red)
let g = try container.decode(Double.self, forKey: .green)
let b = try container.decode(Double.self, forKey: .blue)

self.init(red: r, green: g, blue: b)
}

public func encode(to encoder: Encoder) throws {
guard let colorComponents else {
return
}

var container = encoder.container(keyedBy: CodingKeys.self)

try container.encode(colorComponents.red, forKey: .red)
try container.encode(colorComponents.green, forKey: .green)
try container.encode(colorComponents.blue, forKey: .blue)
}
}
14 changes: 14 additions & 0 deletions Sources/Shared/Extensions/MaterialDesignIcons+Encodable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Foundation

extension MaterialDesignIcons: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(name, forKey: .name)
try container.encode(unicode, forKey: .unicode)
}

private enum CodingKeys: String, CodingKey {
case name
case unicode
}
}

0 comments on commit a3e10a5

Please sign in to comment.