diff --git a/MVMCoreUI/Atomic/Atoms/Views/Badge.swift b/MVMCoreUI/Atomic/Atoms/Views/Badge.swift index 81203d47..9957101f 100644 --- a/MVMCoreUI/Atomic/Atoms/Views/Badge.swift +++ b/MVMCoreUI/Atomic/Atoms/Views/Badge.swift @@ -31,6 +31,7 @@ open class Badge: VDS.Badge, VDSMoleculeViewProtocol { self.accessibilityIdentifier = accessibilityIdentifier } text = viewModel.text + textColor = viewModel.textColor maxWidth = viewModel.maxWidth numberOfLines = viewModel.numberOfLines fillColor = viewModel.fillColor diff --git a/MVMCoreUI/Atomic/Atoms/Views/BadgeModel.swift b/MVMCoreUI/Atomic/Atoms/Views/BadgeModel.swift index 34ed444c..52e43e9a 100644 --- a/MVMCoreUI/Atomic/Atoms/Views/BadgeModel.swift +++ b/MVMCoreUI/Atomic/Atoms/Views/BadgeModel.swift @@ -22,6 +22,7 @@ open class BadgeModel: MoleculeModelProtocol { // MARK: - VDS Properties //-------------------------------------------------- public var text: String = "" + public var textColor: Badge.TextColor? = nil public var accessibilityText: String? public var maxWidth: CGFloat? public var numberOfLines: Int = 1 @@ -29,7 +30,7 @@ open class BadgeModel: MoleculeModelProtocol { public var surface: Surface = .light private enum CodingKeys: String, CodingKey { - case id, accessibilityIdentifier, text, accessibilityText, fillColor, surface, numberOfLines, maxWidth + case id, accessibilityIdentifier, text, textColor, accessibilityText, fillColor, surface, numberOfLines, maxWidth } required public convenience init(from decoder: Decoder) throws { @@ -38,6 +39,9 @@ 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 surface = try container.decodeIfPresent(Surface.self, forKey: .surface) ?? .light @@ -55,6 +59,12 @@ open class BadgeModel: MoleculeModelProtocol { try container.encode(surface, forKey: .surface) try container.encode(numberOfLines, forKey: .numberOfLines) try container.encodeIfPresent(maxWidth, forKey: .maxWidth) + switch textColor { + case .custom(let color): + try container.encode(Color(uiColor: color), forKey: .textColor) + default: + break + } } public func isEqual(to model: any ModelComparisonProtocol) -> Bool { diff --git a/MVMCoreUI/Atomic/Atoms/Views/Tilelet.swift b/MVMCoreUI/Atomic/Atoms/Views/Tilelet.swift index 151d8567..439f295e 100644 --- a/MVMCoreUI/Atomic/Atoms/Views/Tilelet.swift +++ b/MVMCoreUI/Atomic/Atoms/Views/Tilelet.swift @@ -51,7 +51,7 @@ open class Tilelet: VDS.Tilelet, VDSMoleculeViewProtocol{ eyebrowModel = viewModel.eyebrowModel(delegateObject: delegateObject, additionalData: additionalData) titleModel = viewModel.titleModel(delegateObject: delegateObject, additionalData: additionalData) subTitleModel = viewModel.subTitleModel(delegateObject: delegateObject, additionalData: additionalData) - badgeModel = viewModel.badge + badgeModel = viewModel.badgeModel() descriptiveIconModel = viewModel.descriptiveIcon directionalIconModel = viewModel.directionalIcon //setup action diff --git a/MVMCoreUI/Atomic/Atoms/Views/TileletModel.swift b/MVMCoreUI/Atomic/Atoms/Views/TileletModel.swift index f7b69c47..6802e5a7 100644 --- a/MVMCoreUI/Atomic/Atoms/Views/TileletModel.swift +++ b/MVMCoreUI/Atomic/Atoms/Views/TileletModel.swift @@ -18,7 +18,7 @@ open class TileletModel: TileContainerBaseModel, Molec public var id: String = UUID().uuidString public var backgroundColor: Color? - public var badge: Tilelet.BadgeModel? + public var badge: BadgeModel? public var eyebrow: LabelModel? public var eyebrowColor: TitleLockup.TextColor = .primary public var title: LabelModel? @@ -49,7 +49,7 @@ open class TileletModel: TileContainerBaseModel, Molec required public init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) id = try container.decodeIfPresent(String.self, forKey: .id) ?? UUID().uuidString - badge = try container.decodeIfPresent(Tilelet.BadgeModel.self, forKey: .badge) + badge = try container.decodeIfPresent(BadgeModel.self, forKey: .badge) eyebrow = try container.decodeIfPresent(LabelModel.self, forKey: .eyebrow) title = try container.decodeIfPresent(LabelModel.self, forKey: .title) subTitle = try container.decodeIfPresent(LabelModel.self, forKey: .subTitle) @@ -91,6 +91,17 @@ open class TileletModel: TileContainerBaseModel, Molec try super.init(from: decoder) } + public func badgeModel() -> Tilelet.BadgeModel? { + guard let badge else { return nil } + return .init(text: badge.text, + textColor: badge.textColor, + fillColor: badge.fillColor, + surface: badge.surface, + numberOfLines: badge.numberOfLines, + maxWidth: badge.maxWidth + ) + } + public func eyebrowModel(delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) -> Tilelet.EyebrowModel? { guard let eyebrow else { return nil } let attrs = eyebrow.attributes?.toVDSLabelAttributeModel(delegateObject: delegateObject, additionalData: additionalData) diff --git a/MVMCoreUI/Atomic/Extensions/VDS-Enums+Codable.swift b/MVMCoreUI/Atomic/Extensions/VDS-Enums+Codable.swift index 6c6ae71f..ab286f35 100644 --- a/MVMCoreUI/Atomic/Extensions/VDS-Enums+Codable.swift +++ b/MVMCoreUI/Atomic/Extensions/VDS-Enums+Codable.swift @@ -16,7 +16,6 @@ import MVMCore //-------------------------------------------------- extension VDS.Surface: Codable {} -extension VDS.Badge.FillColor: Codable {} extension VDS.BadgeIndicator.FillColor: Codable {} extension VDS.BadgeIndicator.Kind: Codable {} extension VDS.BadgeIndicator.MaximumDigits: Codable {} @@ -294,6 +293,54 @@ 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) + + switch type { + case .red: + self = .red + case .yellow: + self = .yellow + case .green: + self = .green + case .orange: + self = .orange + case .blue: + self = .blue + case .black: + self = .black + case .white: + self = .white + case .custom: + let color = try container.decode(Color.self, forKey: .color) + self = .custom(color.uiColor) + } + } + + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + switch self { + case .custom(let color): + try container.encode(CustomColorType.custom.rawValue, forKey: .type) + try container.encode(Color(uiColor: color), forKey: .color) + default: + try container.encode("\(self)", forKey: .type) + } + } +} + extension VDS.TitleLockup.TitleTextColor: Codable { enum CodingKeys: String, CodingKey {