refactored out base enums into base model

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2024-04-16 15:15:02 -05:00
parent 08a2079654
commit 0ddf76b576
2 changed files with 36 additions and 58 deletions

View File

@ -9,7 +9,7 @@
import Foundation
import VDS
open class TileContainerModel: TileContainerBaseModel, MoleculeModelProtocol {
open class TileContainerModel: TileContainerBaseModel<TileContainer.Padding, TileContainer>, MoleculeModelProtocol {
//--------------------------------------------------
// MARK: - Properties
@ -17,30 +17,18 @@ open class TileContainerModel: TileContainerBaseModel, MoleculeModelProtocol {
public static var identifier: String = "tileContainer"
public var id: String = UUID().uuidString
public var backgroundColor: Color?
public var molecule: MoleculeModelProtocol?
public var padding: TileContainer.Padding = .padding4X
public var aspectRatio: TileContainer.AspectRatio = .ratio1x1
public var color: TileContainer.BackgroundColor = .secondary
public var backgroundEffect: TileContainer.BackgroundEffect {
guard let effect = backgroundEffectContainer else { return .none }
return .convert(from: effect)
}
private enum CodingKeys: String, CodingKey {
case id
case moleculeName
case molecule
case padding
case aspectRatio
case color
}
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
molecule = try container.decodeModelIfPresent(codingKey: .molecule)
padding = try container.decodeIfPresent(TileContainer.Padding.self, forKey: .padding) ?? .padding4X
color = try container.decodeIfPresent(TileContainer.BackgroundColor.self, forKey: .color) ?? .secondary
aspectRatio = try container.decodeIfPresent(TileContainer.AspectRatio.self, forKey: .aspectRatio) ?? .none
try super.init(from: decoder)
}
@ -49,15 +37,12 @@ open class TileContainerModel: TileContainerBaseModel, MoleculeModelProtocol {
try container.encode(id, forKey: .id)
try container.encode(moleculeName, forKey: .moleculeName)
try container.encodeModelIfPresent(molecule, forKey: .molecule)
try container.encodeIfPresent(padding, forKey: .padding)
try container.encodeIfPresent(color, forKey: .color)
try container.encodeIfPresent(aspectRatio, forKey: .aspectRatio)
try super.encode(to: encoder)
}
}
open class TileContainerBaseModel: Codable {
open class TileContainerBaseModel<PaddingType: DefaultValuing & Codable, TileContainerType:TileContainerBase<PaddingType>> : Codable{
//--------------------------------------------------
// MARK: - Properties
@ -69,12 +54,32 @@ open class TileContainerBaseModel: Codable {
public var height: CGFloat?
public var showBorder: Bool = false
public var showDropShadwows: Bool = false
internal var backgroundEffectContainer: BackgroundEffectContainer?
public var padding = PaddingType.defaultValue
public var color: TileContainerType.BackgroundColor = .black
public var aspectRatio: TileContainerType.AspectRatio = .ratio1x1
internal var backgroundEffectCodable: BackgroundEffectCodable?
public var backgroundEffect: TileContainerType.BackgroundEffect {
guard let effect = backgroundEffectCodable else { return .none }
switch effect.type {
case .transparency:
return .transparency
case .none:
return .none
case .gradient:
guard let value = effect.value else {
fatalError("Gradient must have a value")
}
return .gradient(value.firstColor, value.secondColor)
}
}
private enum CodingKeys: String, CodingKey {
case backgroundImage
case action
case padding
case color
case aspectRatio
case imageFallbackColor
case width
case height
@ -88,7 +93,11 @@ open class TileContainerBaseModel: Codable {
width = try container.decodeIfPresent(CGFloat.self, forKey: .width)
height = try container.decodeIfPresent(CGFloat.self, forKey: .height)
action = try container.decodeModelIfPresent(codingKey: .action)
backgroundEffectContainer = try container.decodeIfPresent(BackgroundEffectContainer.self, forKey: .backgroundEffect)
padding = try container.decodeIfPresent(PaddingType.self, forKey: .padding) ?? PaddingType.defaultValue
color = try container.decodeIfPresent(TileContainerType.BackgroundColor.self, forKey: .color) ?? .black
aspectRatio = try container.decodeIfPresent(TileContainerType.AspectRatio.self, forKey: .aspectRatio) ?? .ratio1x1
backgroundEffectCodable = try container.decodeIfPresent(BackgroundEffectCodable.self, forKey: .backgroundEffect)
}
public func encode(to encoder: Encoder) throws {
@ -96,12 +105,15 @@ open class TileContainerBaseModel: Codable {
try container.encodeIfPresent(backgroundImage, forKey: .backgroundImage)
try container.encodeIfPresent(width, forKey: .width)
try container.encodeIfPresent(height, forKey: .height)
try container.encodeIfPresent(backgroundEffectContainer, forKey: .backgroundEffect)
try container.encodeIfPresent(backgroundEffectCodable, forKey: .backgroundEffect)
try container.encodeIfPresent(padding, forKey: .padding)
try container.encodeIfPresent(color, forKey: .color)
try container.encodeIfPresent(aspectRatio, forKey: .aspectRatio)
try container.encodeModelIfPresent(action, forKey: .action)
}
}
internal struct BackgroundEffectContainer: Codable {
internal struct BackgroundEffectCodable: Codable {
public enum BackgroundEffectType: String, Codable {
case transparency
case none
@ -142,19 +154,3 @@ internal struct BackgroundEffectContainer: Codable {
}
}
}
extension TileContainerBase.BackgroundEffect {
internal static func convert(from effect: BackgroundEffectContainer) -> Self {
switch effect.type {
case .transparency:
return .transparency
case .none:
return .none
case .gradient:
guard let value = effect.value else {
fatalError("Gradient must have a value")
}
return .gradient(value.firstColor, value.secondColor)
}
}
}

View File

@ -9,7 +9,7 @@
import Foundation
import VDS
open class TileletModel: TileContainerBaseModel, MoleculeModelProtocol {
open class TileletModel: TileContainerBaseModel<Tilelet.Padding, Tilelet>, MoleculeModelProtocol {
//--------------------------------------------------
// MARK: - Properties
@ -17,13 +17,7 @@ open class TileletModel: TileContainerBaseModel, MoleculeModelProtocol {
public static var identifier: String = "tilelet"
public var id: String = UUID().uuidString
public var backgroundColor: Color?
public var color: Tilelet.BackgroundColor = .black
public var padding: Tilelet.Padding = .large
public var aspectRatio: Tilelet.AspectRatio = .ratio1x1
public var backgroundEffect: Tilelet.BackgroundEffect {
guard let effect = backgroundEffectContainer else { return .none }
return .convert(from: effect)
}
public var badge: Tilelet.BadgeModel?
public var title: LabelModel?
public var subTitle: LabelModel?
@ -35,10 +29,6 @@ open class TileletModel: TileContainerBaseModel, MoleculeModelProtocol {
private enum CodingKeys: String, CodingKey {
case id
case moleculeName
case backgroundColor
case color
case padding
case aspectRatio
case badge
case title
case subTitle
@ -50,10 +40,6 @@ open class TileletModel: TileContainerBaseModel, MoleculeModelProtocol {
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
backgroundColor = try container.decodeIfPresent(Color.self, forKey: .backgroundColor)
color = try container.decodeIfPresent(Tilelet.BackgroundColor.self, forKey: .color) ?? Tilelet.BackgroundColor.black
padding = try container.decodeIfPresent(Tilelet.Padding.self, forKey: .padding) ?? Tilelet.Padding.small
aspectRatio = try container.decodeIfPresent(Tilelet.AspectRatio.self, forKey: .aspectRatio) ?? Tilelet.AspectRatio.none
badge = try container.decodeIfPresent(Tilelet.BadgeModel.self, forKey: .badge)
title = try container.decodeIfPresent(LabelModel.self, forKey: .title)
subTitle = try container.decodeIfPresent(LabelModel.self, forKey: .subTitle)
@ -102,10 +88,6 @@ open class TileletModel: TileContainerBaseModel, MoleculeModelProtocol {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(id, forKey: .id)
try container.encode(moleculeName, forKey: .moleculeName)
try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
try container.encodeIfPresent(color, forKey: .color)
try container.encodeIfPresent(padding, forKey: .padding)
try container.encodeIfPresent(aspectRatio, forKey: .aspectRatio)
try container.encodeIfPresent(badge, forKey: .badge)
try container.encodeIfPresent(title, forKey: .title)
try container.encodeIfPresent(subTitle, forKey: .subTitle)