Changes in CornerLabelModel to dynamically pick molecule's model

This commit is contained in:
Sumanth Nadigadda 2021-07-16 02:14:56 +05:30
parent edbe68429b
commit 3af89dfdc6

View File

@ -8,7 +8,7 @@
import UIKit import UIKit
public class CornerLabelsModel: MoleculeModelProtocol { public class CornerLabelsModel: ParentMoleculeModelProtocol {
public static var identifier: String = "cornerLabels" public static var identifier: String = "cornerLabels"
public var backgroundColor: Color? public var backgroundColor: Color?
public var topLeftLabel: LabelModel? public var topLeftLabel: LabelModel?
@ -16,6 +16,9 @@ public class CornerLabelsModel: MoleculeModelProtocol {
public var bottomLeftLabel: LabelModel? public var bottomLeftLabel: LabelModel?
public var bottomRightLabel: LabelModel? public var bottomRightLabel: LabelModel?
public var molecule: MoleculeModelProtocol? public var molecule: MoleculeModelProtocol?
public var children: [MoleculeModelProtocol] {
return [topLeftLabel, topRightLabel, bottomLeftLabel, bottomRightLabel].compactMap { (molecule: MoleculeModelProtocol?) in molecule }
}
init(with molecule: MoleculeModelProtocol?) { init(with molecule: MoleculeModelProtocol?) {
self.molecule = molecule self.molecule = molecule
@ -35,20 +38,20 @@ public class CornerLabelsModel: MoleculeModelProtocol {
let typeContainer = try decoder.container(keyedBy: CodingKeys.self) let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor) backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
molecule = try typeContainer.decodeModelIfPresent(codingKey: .molecule) molecule = try typeContainer.decodeModelIfPresent(codingKey: .molecule)
topLeftLabel = try typeContainer.decodeIfPresent(LabelModel.self, forKey: .topLeftLabel) topLeftLabel = try typeContainer.decodeMoleculeIfPresent(codingKey: .topLeftLabel)
topRightLabel = try typeContainer.decodeIfPresent(LabelModel.self, forKey: .topRightLabel) topRightLabel = try typeContainer.decodeMoleculeIfPresent(codingKey: .topRightLabel)
bottomLeftLabel = try typeContainer.decodeIfPresent(LabelModel.self, forKey: .bottomLeftLabel) bottomLeftLabel = try typeContainer.decodeMoleculeIfPresent(codingKey: .bottomLeftLabel)
bottomRightLabel = try typeContainer.decodeIfPresent(LabelModel.self, forKey: .bottomRightLabel) bottomRightLabel = try typeContainer.decodeMoleculeIfPresent(codingKey: .bottomRightLabel)
} }
public func encode(to encoder: Encoder) throws { public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self) var container = encoder.container(keyedBy: CodingKeys.self)
try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor) try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
try container.encodeModelIfPresent(molecule, forKey: .molecule) try container.encodeModelIfPresent(molecule, forKey: .molecule)
try container.encodeIfPresent(topLeftLabel, forKey: .topLeftLabel) try container.encodeModelIfPresent(topLeftLabel, forKey: .topLeftLabel)
try container.encodeIfPresent(topRightLabel, forKey: .topRightLabel) try container.encodeModelIfPresent(topRightLabel, forKey: .topRightLabel)
try container.encodeIfPresent(bottomLeftLabel, forKey: .bottomLeftLabel) try container.encodeModelIfPresent(bottomLeftLabel, forKey: .bottomLeftLabel)
try container.encodeIfPresent(bottomRightLabel, forKey: .bottomRightLabel) try container.encodeModelIfPresent(bottomRightLabel, forKey: .bottomRightLabel)
try container.encode(moleculeName, forKey: .moleculeName) try container.encode(moleculeName, forKey: .moleculeName)
} }
} }