|
| 1 | +import SwiftUI |
| 2 | + |
| 3 | +public final class ImageProps: ObservableObject, Decodable { |
| 4 | + @Published var name: String = "" |
| 5 | + @Published var isSystemImage: Bool = false |
| 6 | + @Published var resizeMode: ImageResizeMode |
| 7 | + @Published var tintColor: String? |
| 8 | + @Published var style: StyleProps? |
| 9 | + |
| 10 | + enum ImageResizeMode: String, CaseIterable { |
| 11 | + case `default` |
| 12 | + case cover |
| 13 | + case contain |
| 14 | + case stretch |
| 15 | + case center |
| 16 | + |
| 17 | + @ViewBuilder |
| 18 | + func applyResizeMode(_ image: Image) -> some View { |
| 19 | + switch self { |
| 20 | + case .cover: |
| 21 | + image.resizable().scaledToFill() |
| 22 | + case .contain: |
| 23 | + image.resizable().scaledToFit() |
| 24 | + case .stretch: |
| 25 | + image.resizable() |
| 26 | + case .center: |
| 27 | + image |
| 28 | + case .default: |
| 29 | + image |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + enum CodingKeys: String, CodingKey { |
| 35 | + case name, isSystemImage, resizeMode, tintColor, style |
| 36 | + } |
| 37 | + |
| 38 | + public required init(from decoder: Decoder) throws { |
| 39 | + let container = try decoder.container(keyedBy: CodingKeys.self) |
| 40 | + name = try container.decode(String.self, forKey: .name) |
| 41 | + isSystemImage = try container.decodeIfPresent(Bool.self, forKey: .isSystemImage) ?? false |
| 42 | + resizeMode = try ImageResizeMode(rawValue: container.decodeIfPresent(String.self, forKey: .resizeMode) ?? "") ?? .default |
| 43 | + tintColor = try container.decodeIfPresent(String.self, forKey: .tintColor) |
| 44 | + style = try container.decodeIfPresent(StyleProps.self, forKey: .style) |
| 45 | + } |
| 46 | + |
| 47 | + public func merge(from other: ImageProps) { |
| 48 | + name = other.name |
| 49 | + isSystemImage = other.isSystemImage |
| 50 | + resizeMode = other.resizeMode |
| 51 | + tintColor = other.tintColor |
| 52 | + style = other.style |
| 53 | + } |
| 54 | +} |
0 commit comments