-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix "View ID is not encodable" on WidgetBasicViewModel (#2738)
- Loading branch information
Showing
4 changed files
with
87 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
Sources/Shared/Extensions/MaterialDesignIcons+Encodable.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |