diff --git a/MVMCoreUI/Atomic/Atoms/Views/Badge.swift b/MVMCoreUI/Atomic/Atoms/Views/Badge.swift index 9957101f..218cc6fc 100644 --- a/MVMCoreUI/Atomic/Atoms/Views/Badge.swift +++ b/MVMCoreUI/Atomic/Atoms/Views/Badge.swift @@ -31,10 +31,10 @@ open class Badge: VDS.Badge, VDSMoleculeViewProtocol { self.accessibilityIdentifier = accessibilityIdentifier } text = viewModel.text - textColor = viewModel.textColor + textColor = viewModel.textColorStyle maxWidth = viewModel.maxWidth numberOfLines = viewModel.numberOfLines - fillColor = viewModel.fillColor + fillColor = viewModel.fillColorStyle surface = viewModel.surface } diff --git a/MVMCoreUI/Atomic/Atoms/Views/BadgeModel.swift b/MVMCoreUI/Atomic/Atoms/Views/BadgeModel.swift index 52e43e9a..de5d0dd4 100644 --- a/MVMCoreUI/Atomic/Atoms/Views/BadgeModel.swift +++ b/MVMCoreUI/Atomic/Atoms/Views/BadgeModel.swift @@ -22,15 +22,18 @@ open class BadgeModel: MoleculeModelProtocol { // MARK: - VDS Properties //-------------------------------------------------- public var text: String = "" - public var textColor: Badge.TextColor? = nil + public var textColorStyle: Badge.TextColor? = nil public var accessibilityText: String? public var maxWidth: CGFloat? public var numberOfLines: Int = 1 - public var fillColor = Badge.FillColor.red + public var fillColorStyle = Badge.FillColor.red public var surface: Surface = .light private enum CodingKeys: String, CodingKey { - case id, accessibilityIdentifier, text, textColor, accessibilityText, fillColor, surface, numberOfLines, maxWidth + case id, accessibilityIdentifier, accessibilityText + case surface, numberOfLines, maxWidth + case text, textColor + case fillColor, fillColorStyle } required public convenience init(from decoder: Decoder) throws { @@ -39,11 +42,20 @@ open class BadgeModel: MoleculeModelProtocol { id = try container.decodeIfPresent(String.self, forKey: .id) ?? UUID().uuidString accessibilityIdentifier = try container.decodeIfPresent(String.self, forKey: .accessibilityIdentifier) text = try container.decode(String.self, forKey: .text) - if let foundTextColor = try container.decodeIfPresent(Color.self, forKey: .textColor) { - textColor = .custom(foundTextColor.uiColor) - } accessibilityText = try container.decodeIfPresent(String.self, forKey: .accessibilityText) - fillColor = try container.decodeIfPresent(Badge.FillColor.self, forKey: .fillColor) ?? .red + + //look for a textColor + if let textColor = try container.decodeIfPresent(Color.self, forKey: .textColor) { + textColorStyle = .custom(textColor.uiColor) + } + + //look for a style + fillColorStyle = try container.decodeIfPresent(Badge.FillColor.self, forKey: .fillColorStyle) ?? .red + + //look for a color and set the style + if let fillColor = try container.decodeIfPresent(Color.self, forKey: .fillColor) { + fillColorStyle = .custom(fillColor.uiColor) + } surface = try container.decodeIfPresent(Surface.self, forKey: .surface) ?? .light numberOfLines = try container.decodeIfPresent(Int.self, forKey: .numberOfLines) ?? 1 maxWidth = try container.decodeIfPresent(CGFloat.self, forKey: .maxWidth) @@ -55,11 +67,11 @@ open class BadgeModel: MoleculeModelProtocol { try container.encode(text, forKey: .text) try container.encode(accessibilityText, forKey: .accessibilityText) try container.encodeIfPresent(accessibilityIdentifier, forKey: .accessibilityIdentifier) - try container.encode(fillColor, forKey: .fillColor) try container.encode(surface, forKey: .surface) try container.encode(numberOfLines, forKey: .numberOfLines) try container.encodeIfPresent(maxWidth, forKey: .maxWidth) - switch textColor { + try container.encode(fillColorStyle, forKey: .fillColorStyle) + switch textColorStyle { case .custom(let color): try container.encode(Color(uiColor: color), forKey: .textColor) default: @@ -70,7 +82,8 @@ open class BadgeModel: MoleculeModelProtocol { public func isEqual(to model: any ModelComparisonProtocol) -> Bool { guard let model = model as? BadgeModel else { return false } return self.backgroundColor == model.backgroundColor - && self.fillColor == model.fillColor + && self.fillColorStyle == model.fillColorStyle + && self.textColorStyle == model.textColorStyle && self.numberOfLines == model.numberOfLines && self.text == model.text && self.surface == model.surface diff --git a/MVMCoreUI/Atomic/Atoms/Views/TileletModel.swift b/MVMCoreUI/Atomic/Atoms/Views/TileletModel.swift index 6802e5a7..33575f7c 100644 --- a/MVMCoreUI/Atomic/Atoms/Views/TileletModel.swift +++ b/MVMCoreUI/Atomic/Atoms/Views/TileletModel.swift @@ -94,8 +94,8 @@ open class TileletModel: TileContainerBaseModel, Molec public func badgeModel() -> Tilelet.BadgeModel? { guard let badge else { return nil } return .init(text: badge.text, - textColor: badge.textColor, - fillColor: badge.fillColor, + textColor: badge.textColorStyle, + fillColor: badge.fillColorStyle, surface: badge.surface, numberOfLines: badge.numberOfLines, maxWidth: badge.maxWidth diff --git a/MVMCoreUI/Atomic/Extensions/VDS-Enums+Codable.swift b/MVMCoreUI/Atomic/Extensions/VDS-Enums+Codable.swift index ab286f35..1109cece 100644 --- a/MVMCoreUI/Atomic/Extensions/VDS-Enums+Codable.swift +++ b/MVMCoreUI/Atomic/Extensions/VDS-Enums+Codable.swift @@ -294,49 +294,40 @@ extension VDS.TitleLockup.TextColor: Codable { } extension VDS.Badge.FillColor: Codable { - enum CodingKeys: String, CodingKey { - case type - case color - } - - enum CustomColorType: String, Codable { - case red, yellow, green, orange, blue, black, white - case custom - } - public init(from decoder: Decoder) throws { - let container = try decoder.container(keyedBy: CodingKeys.self) - let type = try container.decode(CustomColorType.self, forKey: .type) - + let container = try decoder.singleValueContainer() + let type = try container.decode(String.self) switch type { - case .red: + case "red": self = .red - case .yellow: + case "yellow": self = .yellow - case .green: + case "green": self = .green - case .orange: + case "orange": self = .orange - case .blue: + case "blue": self = .blue - case .black: + case "black": self = .black - case .white: + case "white": self = .white - case .custom: - let color = try container.decode(Color.self, forKey: .color) - self = .custom(color.uiColor) + default: + if let color = try? Color(from: decoder) { + self = .custom(color.uiColor) + } else { + self = .custom(UIColor(hexString: type)) + } } } - + public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) + var container = encoder.singleValueContainer() switch self { - case .custom(let color): - try container.encode(CustomColorType.custom.rawValue, forKey: .type) - try container.encode(Color(uiColor: color), forKey: .color) + case .custom(let value): + try container.encode(Color(uiColor: value)) default: - try container.encode("\(self)", forKey: .type) + try container.encode(String(reflecting: self)) } } }