updated models

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2023-01-25 12:19:50 -06:00
parent 5a10d3fc29
commit bfcd559ac4
2 changed files with 107 additions and 6 deletions

View File

@ -37,8 +37,8 @@ open class Tilelet: VDS.Tilelet, VDSMoleculeViewProtocol {
width = viewModel.width
textWidth = viewModel.textWidth
textPercentage = viewModel.textPercentage
titleModel = viewModel.title
subTitleModel = viewModel.subTitle
titleModel = viewModel.titleModel
subTitleModel = viewModel.subTitleModel
badgeModel = viewModel.badge
descriptiveIconModel = viewModel.descriptiveIcon
directionalIconModel = viewModel.directionalIcon

View File

@ -20,8 +20,8 @@ open class TileletModel: MoleculeModelProtocol {
public var padding: TileContainer.Padding
public var aspectRatio: TileContainer.AspectRatio
public var badge: Tilelet.BadgeModel?
public var title: Tilelet.TitleModel?
public var subTitle: Tilelet.SubTitleModel?
public var title: LabelModel?
public var subTitle: LabelModel?
public var descriptiveIcon: Tilelet.DescriptiveIcon?
public var directionalIcon: Tilelet.DirectionalIcon?
public var width: CGFloat?
@ -35,12 +35,113 @@ open class TileletModel: MoleculeModelProtocol {
self.padding = try container.decodeIfPresent(TileContainer.Padding.self, forKey: .padding) ?? TileContainer.Padding.padding4X
self.aspectRatio = try container.decodeIfPresent(TileContainer.AspectRatio.self, forKey: .aspectRatio) ?? TileContainer.AspectRatio.none
self.badge = try container.decodeIfPresent(Tilelet.BadgeModel.self, forKey: .badge)
self.title = try container.decodeIfPresent(Tilelet.TitleModel.self, forKey: .title)
self.subTitle = try container.decodeIfPresent(Tilelet.SubTitleModel.self, forKey: .subTitle)
self.title = try container.decodeIfPresent(LabelModel.self, forKey: .title)
self.subTitle = try container.decodeIfPresent(LabelModel.self, forKey: .subTitle)
self.descriptiveIcon = try container.decodeIfPresent(Tilelet.DescriptiveIcon.self, forKey: .descriptiveIcon)
self.directionalIcon = try container.decodeIfPresent(Tilelet.DirectionalIcon.self, forKey: .directionalIcon)
self.width = try container.decodeIfPresent(CGFloat.self, forKey: .width)
self.textWidth = try container.decodeIfPresent(CGFloat.self, forKey: .textWidth)
self.textPercentage = try container.decodeIfPresent(CGFloat.self, forKey: .textPercentage)
}
public var titleModel: Tilelet.TitleModel? {
guard let title else { return nil }
let style = title.fontStyle?.vdsTextStyle
if let style, let found = Tilelet.TitleModel.TextStyle(rawValue: style.rawValue) {
return .init(text: title.text, textStyle: found)
} else {
return .init(text: title.text)
}
}
public var subTitleModel: Tilelet.SubTitleModel? {
guard let subTitle else { return nil }
let style = subTitle.fontStyle?.vdsTextStyle
if let style, let found = Tilelet.SubTitleModel.TextStyle(rawValue: style.rawValue) {
return .init(text: subTitle.text, textStyle: found)
} else {
return .init(text: subTitle.text)
}
}
}
extension Styler.Font {
public var vdsTextStyle: VDS.TextStyle? {
let raw = "\(self)"
let newRaw = raw.prefix(1).lowercased() + raw.dropFirst()
guard let style = VDS.TextStyle(rawValue: newRaw) else { return nil }
return style
}
}
extension TileContainer.BackgroundColor: Codable {}
extension TileContainer.Padding: Codable {}
extension TileContainer.AspectRatio: Codable {}
extension Surface: Codable {}
extension Badge.FillColor: Codable {}
extension Icon.Name: Codable {}
extension Icon.Size: Codable {}
extension Tilelet.BadgeModel: Codable {
private enum CodingKeys: String, CodingKey {
case text, fillColor, surface, numberOfLines, maxWidth
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let text = try container.decode(String.self, forKey: .text)
let fillColor = try container.decodeIfPresent(Badge.FillColor.self, forKey: .fillColor) ?? .red
let surface = try container.decodeIfPresent(Surface.self, forKey: .surface) ?? .light
let numberOfLines = try container.decodeIfPresent(Int.self, forKey: .numberOfLines) ?? 0
let maxWidth = try container.decodeIfPresent(CGFloat.self, forKey: .maxWidth)
self.init(text: text, fillColor: fillColor, surface: surface, numberOfLines: numberOfLines, maxWidth: maxWidth)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(fillColor, forKey: .fillColor)
try container.encode(surface, forKey: .surface)
try container.encode(numberOfLines, forKey: .numberOfLines)
try container.encodeIfPresent(maxWidth, forKey: .maxWidth)
}
}
extension Tilelet.DescriptiveIcon: Codable {
private enum CodingKeys: String, CodingKey {
case name, size, surface
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let name = try container.decode(Icon.Name.self, forKey: .name)
let size = try container.decodeIfPresent(Icon.Size.self, forKey: .size) ?? .medium
let surface = try container.decodeIfPresent(Surface.self, forKey: .surface) ?? .dark
self.init(name: name, size: size, surface: surface)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(name, forKey: .name)
try container.encode(size, forKey: .size)
try container.encode(surface, forKey: .surface)
}
}
extension Tilelet.DirectionalIcon: Codable {
private enum CodingKeys: String, CodingKey {
case size, surface
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let size = try container.decodeIfPresent(Icon.Size.self, forKey: .size) ?? .medium
let surface = try container.decodeIfPresent(Surface.self, forKey: .surface) ?? .dark
self.init(size: size, surface: surface)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(size, forKey: .size)
try container.encode(surface, forKey: .surface)
}
}