Dash line color require for model, optional for server

This commit is contained in:
Pfeil, Scott Robert 2020-01-09 11:00:00 -05:00
parent f40335ce19
commit d651fb5783
2 changed files with 32 additions and 18 deletions

View File

@ -15,8 +15,11 @@ open class DashLine: View {
// MARK: - Properties
//------------------------------------------------------
@objc public var dashColor: UIColor?
@objc public var dashLayer: CAShapeLayer?
var dashModel: DashLineModel? {
get { return model as? DashLineModel }
}
@objc private var dashLayer: CAShapeLayer?
//------------------------------------------------------
// MARK: - Lifecycle
@ -53,7 +56,7 @@ open class DashLine: View {
dashLayer.lineCap = .round
dashLayer.lineDashPattern = [NSNumber(value: 2), NSNumber(value: 2)]
dashLayer.path = path.cgPath
dashLayer.strokeColor = dashColor?.cgColor ?? UIColor.mfLighterGray().cgColor
dashLayer.strokeColor = dashModel?.dashColor.uiColor.cgColor ?? UIColor.mfLighterGray().cgColor
dashLayer.fillColor = UIColor.clear.cgColor
dashLayer.backgroundColor = backgroundColor?.cgColor ?? UIColor.white.cgColor
self.dashLayer = dashLayer
@ -70,27 +73,16 @@ open class DashLine: View {
}
open override func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) {
super.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData)
// Configure class properties with JSON values
guard let jsonDictionary = json else { return }
if let isHiddenValue = jsonDictionary[KeyIsHidden] as? Bool {
isHidden = isHiddenValue
}
if let dashColorHex = jsonDictionary["dashColor"] as? String {
dashColor = UIColor.mfGet(forHex: dashColorHex)
}
guard let json = json, let model = try? Self.decodeJSONToModel(json: json, type: DashLineModel.self) else { return }
setWithModel(model, delegateObject, additionalData as? [String : AnyHashable])
}
//MARK: - MVMCoreMoleculeViewProtocol
public override func setWithModel(_ model: MoleculeProtocol?, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [String : AnyHashable]?) {
super.setWithModel(model, delegateObject, additionalData)
guard let dashLineModel = model as? DashLineModel else {
guard let dashLineModel = dashModel else {
return
}
dashColor = dashLineModel.dashColor.uiColor
if let isHiddenValue = dashLineModel.isHidden {
isHidden = isHiddenValue
}

View File

@ -12,10 +12,32 @@ import Foundation
public static var identifier: String = "dashLine"
public var backgroundColor: Color?
public var dashColor: Color
public var dashColor: Color = Color(uiColor: .mfLighterGray())
public var isHidden: Bool?
public init(dashColor: Color) {
self.dashColor = dashColor
}
enum CodingKeys: String, CodingKey {
case backgroundColor
case dashColor
case isHidden
}
required public init(from decoder: Decoder) throws {
let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
if let dashColor = try typeContainer.decodeIfPresent(Color.self, forKey: .dashColor) {
self.dashColor = dashColor
}
backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
isHidden = try typeContainer.decodeIfPresent(Bool.self, forKey: .isHidden)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(dashColor, forKey: .dashColor)
try container.encodeIfPresent(isHidden, forKey: .isHidden)
try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
}
}