refactored out base enums into base model
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
08a2079654
commit
0ddf76b576
@ -9,7 +9,7 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
import VDS
|
import VDS
|
||||||
|
|
||||||
open class TileContainerModel: TileContainerBaseModel, MoleculeModelProtocol {
|
open class TileContainerModel: TileContainerBaseModel<TileContainer.Padding, TileContainer>, MoleculeModelProtocol {
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Properties
|
// MARK: - Properties
|
||||||
@ -17,30 +17,18 @@ open class TileContainerModel: TileContainerBaseModel, MoleculeModelProtocol {
|
|||||||
public static var identifier: String = "tileContainer"
|
public static var identifier: String = "tileContainer"
|
||||||
public var id: String = UUID().uuidString
|
public var id: String = UUID().uuidString
|
||||||
public var backgroundColor: Color?
|
public var backgroundColor: Color?
|
||||||
|
|
||||||
public var molecule: MoleculeModelProtocol?
|
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 {
|
private enum CodingKeys: String, CodingKey {
|
||||||
case id
|
case id
|
||||||
case moleculeName
|
case moleculeName
|
||||||
case molecule
|
case molecule
|
||||||
case padding
|
|
||||||
case aspectRatio
|
|
||||||
case color
|
|
||||||
}
|
}
|
||||||
required public init(from decoder: Decoder) throws {
|
required public init(from decoder: Decoder) throws {
|
||||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||||
id = try container.decodeIfPresent(String.self, forKey: .id) ?? UUID().uuidString
|
id = try container.decodeIfPresent(String.self, forKey: .id) ?? UUID().uuidString
|
||||||
molecule = try container.decodeModelIfPresent(codingKey: .molecule)
|
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)
|
try super.init(from: decoder)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,15 +37,12 @@ open class TileContainerModel: TileContainerBaseModel, MoleculeModelProtocol {
|
|||||||
try container.encode(id, forKey: .id)
|
try container.encode(id, forKey: .id)
|
||||||
try container.encode(moleculeName, forKey: .moleculeName)
|
try container.encode(moleculeName, forKey: .moleculeName)
|
||||||
try container.encodeModelIfPresent(molecule, forKey: .molecule)
|
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)
|
try super.encode(to: encoder)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
open class TileContainerBaseModel: Codable {
|
open class TileContainerBaseModel<PaddingType: DefaultValuing & Codable, TileContainerType:TileContainerBase<PaddingType>> : Codable{
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Properties
|
// MARK: - Properties
|
||||||
@ -69,12 +54,32 @@ open class TileContainerBaseModel: Codable {
|
|||||||
public var height: CGFloat?
|
public var height: CGFloat?
|
||||||
public var showBorder: Bool = false
|
public var showBorder: Bool = false
|
||||||
public var showDropShadwows: 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 {
|
private enum CodingKeys: String, CodingKey {
|
||||||
case backgroundImage
|
case backgroundImage
|
||||||
case action
|
case action
|
||||||
case padding
|
case padding
|
||||||
|
case color
|
||||||
|
case aspectRatio
|
||||||
case imageFallbackColor
|
case imageFallbackColor
|
||||||
case width
|
case width
|
||||||
case height
|
case height
|
||||||
@ -88,7 +93,11 @@ open class TileContainerBaseModel: Codable {
|
|||||||
width = try container.decodeIfPresent(CGFloat.self, forKey: .width)
|
width = try container.decodeIfPresent(CGFloat.self, forKey: .width)
|
||||||
height = try container.decodeIfPresent(CGFloat.self, forKey: .height)
|
height = try container.decodeIfPresent(CGFloat.self, forKey: .height)
|
||||||
action = try container.decodeModelIfPresent(codingKey: .action)
|
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 {
|
public func encode(to encoder: Encoder) throws {
|
||||||
@ -96,12 +105,15 @@ open class TileContainerBaseModel: Codable {
|
|||||||
try container.encodeIfPresent(backgroundImage, forKey: .backgroundImage)
|
try container.encodeIfPresent(backgroundImage, forKey: .backgroundImage)
|
||||||
try container.encodeIfPresent(width, forKey: .width)
|
try container.encodeIfPresent(width, forKey: .width)
|
||||||
try container.encodeIfPresent(height, forKey: .height)
|
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)
|
try container.encodeModelIfPresent(action, forKey: .action)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal struct BackgroundEffectContainer: Codable {
|
internal struct BackgroundEffectCodable: Codable {
|
||||||
public enum BackgroundEffectType: String, Codable {
|
public enum BackgroundEffectType: String, Codable {
|
||||||
case transparency
|
case transparency
|
||||||
case none
|
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@ -9,7 +9,7 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
import VDS
|
import VDS
|
||||||
|
|
||||||
open class TileletModel: TileContainerBaseModel, MoleculeModelProtocol {
|
open class TileletModel: TileContainerBaseModel<Tilelet.Padding, Tilelet>, MoleculeModelProtocol {
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Properties
|
// MARK: - Properties
|
||||||
@ -17,13 +17,7 @@ open class TileletModel: TileContainerBaseModel, MoleculeModelProtocol {
|
|||||||
public static var identifier: String = "tilelet"
|
public static var identifier: String = "tilelet"
|
||||||
public var id: String = UUID().uuidString
|
public var id: String = UUID().uuidString
|
||||||
public var backgroundColor: Color?
|
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 badge: Tilelet.BadgeModel?
|
||||||
public var title: LabelModel?
|
public var title: LabelModel?
|
||||||
public var subTitle: LabelModel?
|
public var subTitle: LabelModel?
|
||||||
@ -35,10 +29,6 @@ open class TileletModel: TileContainerBaseModel, MoleculeModelProtocol {
|
|||||||
private enum CodingKeys: String, CodingKey {
|
private enum CodingKeys: String, CodingKey {
|
||||||
case id
|
case id
|
||||||
case moleculeName
|
case moleculeName
|
||||||
case backgroundColor
|
|
||||||
case color
|
|
||||||
case padding
|
|
||||||
case aspectRatio
|
|
||||||
case badge
|
case badge
|
||||||
case title
|
case title
|
||||||
case subTitle
|
case subTitle
|
||||||
@ -50,10 +40,6 @@ open class TileletModel: TileContainerBaseModel, MoleculeModelProtocol {
|
|||||||
required public init(from decoder: Decoder) throws {
|
required public init(from decoder: Decoder) throws {
|
||||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||||
id = try container.decodeIfPresent(String.self, forKey: .id) ?? UUID().uuidString
|
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)
|
badge = try container.decodeIfPresent(Tilelet.BadgeModel.self, forKey: .badge)
|
||||||
title = try container.decodeIfPresent(LabelModel.self, forKey: .title)
|
title = try container.decodeIfPresent(LabelModel.self, forKey: .title)
|
||||||
subTitle = try container.decodeIfPresent(LabelModel.self, forKey: .subTitle)
|
subTitle = try container.decodeIfPresent(LabelModel.self, forKey: .subTitle)
|
||||||
@ -102,10 +88,6 @@ open class TileletModel: TileContainerBaseModel, MoleculeModelProtocol {
|
|||||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||||
try container.encode(id, forKey: .id)
|
try container.encode(id, forKey: .id)
|
||||||
try container.encode(moleculeName, forKey: .moleculeName)
|
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(badge, forKey: .badge)
|
||||||
try container.encodeIfPresent(title, forKey: .title)
|
try container.encodeIfPresent(title, forKey: .title)
|
||||||
try container.encodeIfPresent(subTitle, forKey: .subTitle)
|
try container.encodeIfPresent(subTitle, forKey: .subTitle)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user