refdactored to use better codable for the BackgroundEffect Enum
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
0ddf76b576
commit
c87bbb3a78
@ -57,22 +57,7 @@ open class TileContainerBaseModel<PaddingType: DefaultValuing & Codable, TileCon
|
||||
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)
|
||||
}
|
||||
}
|
||||
public var backgroundEffect: TileContainerType.BackgroundEffect = .none
|
||||
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case backgroundImage
|
||||
@ -96,7 +81,7 @@ open class TileContainerBaseModel<PaddingType: DefaultValuing & Codable, TileCon
|
||||
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)
|
||||
backgroundEffect = try container.decodeIfPresent(TileContainerType.BackgroundEffect.self, forKey: .backgroundEffect) ?? .none
|
||||
|
||||
}
|
||||
|
||||
@ -105,52 +90,10 @@ open class TileContainerBaseModel<PaddingType: DefaultValuing & Codable, TileCon
|
||||
try container.encodeIfPresent(backgroundImage, forKey: .backgroundImage)
|
||||
try container.encodeIfPresent(width, forKey: .width)
|
||||
try container.encodeIfPresent(height, forKey: .height)
|
||||
try container.encodeIfPresent(backgroundEffectCodable, forKey: .backgroundEffect)
|
||||
try container.encodeIfPresent(backgroundEffect, 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 BackgroundEffectCodable: Codable {
|
||||
public enum BackgroundEffectType: String, Codable {
|
||||
case transparency
|
||||
case none
|
||||
case gradient
|
||||
}
|
||||
|
||||
public struct GradientValue: Codable {
|
||||
public let firstColor: String
|
||||
public let secondColor: String
|
||||
}
|
||||
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case type
|
||||
case value
|
||||
}
|
||||
|
||||
public let type: BackgroundEffectType
|
||||
public let value: GradientValue?
|
||||
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
type = try container.decode(BackgroundEffectType.self, forKey: .type)
|
||||
if type == .gradient {
|
||||
value = try container.decode(GradientValue.self, forKey: .value)
|
||||
} else {
|
||||
value = nil
|
||||
}
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
try container.encode(type, forKey: .type)
|
||||
switch type {
|
||||
case .transparency, .none:
|
||||
break // No value to encode
|
||||
case .gradient:
|
||||
try container.encode(value, forKey: .value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -100,6 +100,53 @@ extension VDS.TileContainerBase.BackgroundColor: Codable {
|
||||
}
|
||||
}
|
||||
|
||||
extension VDS.TileContainerBase.BackgroundEffect: Codable {
|
||||
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case type
|
||||
case firstColor
|
||||
case secondColor
|
||||
}
|
||||
|
||||
enum BackgroundEffectType: String, Codable {
|
||||
case transparency
|
||||
case none
|
||||
case gradient
|
||||
}
|
||||
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
let type = try container.decode(BackgroundEffectType.self, forKey: .type)
|
||||
|
||||
switch type {
|
||||
case .transparency:
|
||||
self = .transparency
|
||||
case .none:
|
||||
self = .none
|
||||
case .gradient:
|
||||
let firstColor = try container.decode(String.self, forKey: .firstColor)
|
||||
let secondColor = try container.decode(String.self, forKey: .secondColor)
|
||||
self = .gradient(firstColor, secondColor)
|
||||
}
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
switch self {
|
||||
case .transparency:
|
||||
try container.encode(BackgroundEffectType.transparency.rawValue, forKey: .type)
|
||||
case .none:
|
||||
try container.encode(BackgroundEffectType.none.rawValue, forKey: .type)
|
||||
case .gradient(let firstColor, let secondColor):
|
||||
try container.encode(BackgroundEffectType.gradient.rawValue, forKey: .type)
|
||||
try container.encode(firstColor, forKey: .firstColor)
|
||||
try container.encode(secondColor, forKey: .secondColor)
|
||||
@unknown default:
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension VDS.TileContainer.Padding: Codable {
|
||||
enum PaddingError: Error {
|
||||
case valueNotFound(type: String)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user