added depreciated coding keys

also reverted to non-hard typed LabelModel as done previous.

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2024-02-28 16:07:57 -06:00
parent 995bec6cb2
commit fb2a866f14

View File

@ -74,18 +74,31 @@ public class TitleLockupModel: MoleculeModelProtocol, ParentMoleculeModelProtoco
let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
id = try typeContainer.decodeIfPresent(String.self, forKey: .id) ?? UUID().uuidString
textAlignment = try typeContainer.decodeIfPresent(TitleLockup.TextAlignment.self, forKey: .textAlignment) ?? .left
title = try typeContainer.decode(LabelModel.self, forKey: .title)
title = try typeContainer.decodeMolecule(codingKey: .title)
eyebrow = try typeContainer.decodeIfPresent(LabelModel.self, forKey: .eyebrow)
subTitle = try typeContainer.decodeIfPresent(LabelModel.self, forKey: .subTitle)
subTitleColor = try typeContainer.decodeIfPresent(Use.self, forKey: .subTitleColor) ?? .primary
subTitle = try typeContainer.decodeMoleculeIfPresent(codingKey: .subTitle)
/// look for color hex code
if let color = try? typeContainer.decodeIfPresent(Color.self, forKey: .subTitleColor) {
self.subTitleColor = color.uiColor.isDark() ? .primary : .secondary
} else if let subTitleColor = try? typeContainer.decodeIfPresent(Use.self, forKey: .subTitleColor) {
self.subTitleColor = subTitleColor
} else {
subTitleColor = .primary
}
if let newAlignment = try typeContainer.decodeIfPresent(VDS.TitleLockup.TextAlignment.self, forKey: .alignment) {
alignment = newAlignment
}
if let invertedStatus = try typeContainer.decodeIfPresent(Bool.self, forKey: .inverted) {
inverted = invertedStatus
if let inverted = try typeContainer.decodeIfPresent(Bool.self, forKey: .inverted) {
self.inverted = inverted
} else {
try setInverted(deprecatedFrom: decoder)
}
}
public func encode(to encoder: Encoder) throws {
@ -156,6 +169,24 @@ public class TitleLockupModel: MoleculeModelProtocol, ParentMoleculeModelProtoco
return .init(text: subTitle.text, textColor: subTitleColor, textAttributes: attrs, numberOfLines: subTitle.numberOfLines ?? 0)
}
private enum DeprecatedCodingKeys: String, CodingKey {
case titleColor
case backgroundColor
}
private func setInverted(deprecatedFrom decoder: Decoder) throws {
let typeContainer = try decoder.container(keyedBy: DeprecatedCodingKeys.self)
if let titleColor = try typeContainer.decodeIfPresent(Color.self, forKey: .titleColor) {
inverted = !titleColor.uiColor.isDark()
}
if let backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor) {
inverted = !backgroundColor.uiColor.isDark()
}
}
}
extension TitleLockupModel {