refactored badge text/fill color
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
f66dc66818
commit
4487f9b03b
@ -31,10 +31,10 @@ open class Badge: VDS.Badge, VDSMoleculeViewProtocol {
|
|||||||
self.accessibilityIdentifier = accessibilityIdentifier
|
self.accessibilityIdentifier = accessibilityIdentifier
|
||||||
}
|
}
|
||||||
text = viewModel.text
|
text = viewModel.text
|
||||||
textColor = viewModel.textColor
|
textColor = viewModel.textColorStyle
|
||||||
maxWidth = viewModel.maxWidth
|
maxWidth = viewModel.maxWidth
|
||||||
numberOfLines = viewModel.numberOfLines
|
numberOfLines = viewModel.numberOfLines
|
||||||
fillColor = viewModel.fillColor
|
fillColor = viewModel.fillColorStyle
|
||||||
surface = viewModel.surface
|
surface = viewModel.surface
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -22,15 +22,16 @@ open class BadgeModel: MoleculeModelProtocol {
|
|||||||
// MARK: - VDS Properties
|
// MARK: - VDS Properties
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
public var text: String = ""
|
public var text: String = ""
|
||||||
public var textColor: Badge.TextColor? = nil
|
public var textColorStyle: Badge.TextColor? = nil
|
||||||
public var accessibilityText: String?
|
public var accessibilityText: String?
|
||||||
public var maxWidth: CGFloat?
|
public var maxWidth: CGFloat?
|
||||||
public var numberOfLines: Int = 1
|
public var numberOfLines: Int = 1
|
||||||
public var fillColor = Badge.FillColor.red
|
public var fillColorStyle = Badge.FillColor.red
|
||||||
|
public var fillColor: Color?
|
||||||
public var surface: Surface = .light
|
public var surface: Surface = .light
|
||||||
|
|
||||||
private enum CodingKeys: String, CodingKey {
|
private enum CodingKeys: String, CodingKey {
|
||||||
case id, accessibilityIdentifier, text, textColor, accessibilityText, fillColor, surface, numberOfLines, maxWidth
|
case id, accessibilityIdentifier, text, textColor, textColorStyle, accessibilityText, fillColor, fillColorStyle, surface, numberOfLines, maxWidth
|
||||||
}
|
}
|
||||||
|
|
||||||
required public convenience init(from decoder: Decoder) throws {
|
required public convenience init(from decoder: Decoder) throws {
|
||||||
@ -39,11 +40,20 @@ open class BadgeModel: MoleculeModelProtocol {
|
|||||||
id = try container.decodeIfPresent(String.self, forKey: .id) ?? UUID().uuidString
|
id = try container.decodeIfPresent(String.self, forKey: .id) ?? UUID().uuidString
|
||||||
accessibilityIdentifier = try container.decodeIfPresent(String.self, forKey: .accessibilityIdentifier)
|
accessibilityIdentifier = try container.decodeIfPresent(String.self, forKey: .accessibilityIdentifier)
|
||||||
text = try container.decode(String.self, forKey: .text)
|
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)
|
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: .fillColor) ?? .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
|
surface = try container.decodeIfPresent(Surface.self, forKey: .surface) ?? .light
|
||||||
numberOfLines = try container.decodeIfPresent(Int.self, forKey: .numberOfLines) ?? 1
|
numberOfLines = try container.decodeIfPresent(Int.self, forKey: .numberOfLines) ?? 1
|
||||||
maxWidth = try container.decodeIfPresent(CGFloat.self, forKey: .maxWidth)
|
maxWidth = try container.decodeIfPresent(CGFloat.self, forKey: .maxWidth)
|
||||||
@ -59,7 +69,7 @@ open class BadgeModel: MoleculeModelProtocol {
|
|||||||
try container.encode(surface, forKey: .surface)
|
try container.encode(surface, forKey: .surface)
|
||||||
try container.encode(numberOfLines, forKey: .numberOfLines)
|
try container.encode(numberOfLines, forKey: .numberOfLines)
|
||||||
try container.encodeIfPresent(maxWidth, forKey: .maxWidth)
|
try container.encodeIfPresent(maxWidth, forKey: .maxWidth)
|
||||||
switch textColor {
|
switch textColorStyle {
|
||||||
case .custom(let color):
|
case .custom(let color):
|
||||||
try container.encode(Color(uiColor: color), forKey: .textColor)
|
try container.encode(Color(uiColor: color), forKey: .textColor)
|
||||||
default:
|
default:
|
||||||
|
|||||||
@ -94,8 +94,8 @@ open class TileletModel: TileContainerBaseModel<Tilelet.Padding, Tilelet>, Molec
|
|||||||
public func badgeModel() -> Tilelet.BadgeModel? {
|
public func badgeModel() -> Tilelet.BadgeModel? {
|
||||||
guard let badge else { return nil }
|
guard let badge else { return nil }
|
||||||
return .init(text: badge.text,
|
return .init(text: badge.text,
|
||||||
textColor: badge.textColor,
|
textColor: badge.textColorStyle,
|
||||||
fillColor: badge.fillColor,
|
fillColor: badge.fillColorStyle,
|
||||||
surface: badge.surface,
|
surface: badge.surface,
|
||||||
numberOfLines: badge.numberOfLines,
|
numberOfLines: badge.numberOfLines,
|
||||||
maxWidth: badge.maxWidth
|
maxWidth: badge.maxWidth
|
||||||
|
|||||||
@ -305,30 +305,64 @@ extension VDS.Badge.FillColor: Codable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public init(from decoder: Decoder) throws {
|
public init(from decoder: Decoder) throws {
|
||||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
//try legacy ones that map to real fillColor cases
|
||||||
let type = try container.decode(CustomColorType.self, forKey: .type)
|
if let legacy = try? LegacyFillColor(from: decoder) {
|
||||||
|
self = legacy.fillColor
|
||||||
switch type {
|
|
||||||
case .red:
|
} //try to convert to a color
|
||||||
self = .red
|
else if let color = try? Color(from: decoder) {
|
||||||
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)
|
self = .custom(color.uiColor)
|
||||||
|
|
||||||
|
} //try to use new format
|
||||||
|
else {
|
||||||
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private enum LegacyFillColor: String, Decodable {
|
||||||
|
case red, yellow, green, orange, blue, black, white
|
||||||
|
|
||||||
|
var fillColor: Badge.FillColor {
|
||||||
|
switch self {
|
||||||
|
case .red:
|
||||||
|
return .red
|
||||||
|
case .yellow:
|
||||||
|
return .yellow
|
||||||
|
case .green:
|
||||||
|
return .green
|
||||||
|
case .orange:
|
||||||
|
return .orange
|
||||||
|
case .blue:
|
||||||
|
return .blue
|
||||||
|
case .black:
|
||||||
|
return .black
|
||||||
|
case .white:
|
||||||
|
return .white
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public func encode(to encoder: Encoder) throws {
|
public func encode(to encoder: Encoder) throws {
|
||||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||||
switch self {
|
switch self {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user